# coding: utf-8 # Copyright (C) 2014 initOS GmbH & Co. KG (). # @author: Joel Grand-Guillaume @ Camptocamp SA # @ 2015 Valentin CHEMIERE @ Akretion # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from openerp import models, fields, api, _ from openerp.exceptions import Warning as UserError import hashlib from base64 import b64decode class IrAttachmentMetadata(models.Model): _name = 'ir.attachment.metadata' _inherits = {'ir.attachment': 'attachment_id'} internal_hash = fields.Char( store=True, compute='_compute_hash', help="File hash computed with file data to be compared " "to external hash when provided.") external_hash = fields.Char( help="File hash comes from the external owner of the file.\n" "If provided allow to check than downloaded file " "is the exact copy of the original file.") attachment_id = fields.Many2one( 'ir.attachment', required=True, ondelete='cascade', help="Link to ir.attachment model ") @api.depends('datas', 'external_hash') def _compute_hash(self): if self.datas: self.internal_hash = hashlib.md5(b64decode(self.datas)).hexdigest() if self.external_hash and self.internal_hash != self.external_hash: raise UserError( _("File corrupted: Something was wrong with " "the retrieved file, please relaunch the task."))