From e8838ce41f2ef23b5fcdc6110f1cbd432b7f0020 Mon Sep 17 00:00:00 2001 From: Mourad Elhadj Mimoune Date: Thu, 2 Jun 2016 10:48:35 +0200 Subject: [PATCH 1/3] [FIX] Beautify code --- attachment_metadata/models/attachment.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/attachment_metadata/models/attachment.py b/attachment_metadata/models/attachment.py index 6fd9d52b0b5..f45724d0133 100644 --- a/attachment_metadata/models/attachment.py +++ b/attachment_metadata/models/attachment.py @@ -2,13 +2,15 @@ # Copyright (C) 2014 initOS GmbH & Co. KG (). # @author: Joel Grand-Guillaume @ Camptocamp SA # @ 2015 Valentin CHEMIERE @ Akretion +# © 2016 @author Mourad EL HADJ MIMOUNE # 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 +from openerp import models, fields, api, _ +from openerp.exceptions import Warning as UserError + class IrAttachmentMetadata(models.Model): _name = 'ir.attachment.metadata' From 5033f8566deb2cd005d8a5026e5dd57b396e1560 Mon Sep 17 00:00:00 2001 From: Mourad Elhadj Mimoune Date: Thu, 2 Jun 2016 10:51:46 +0200 Subject: [PATCH 2/3] [FIX] separating constraint from internal_hash compute method --- attachment_metadata/models/attachment.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/attachment_metadata/models/attachment.py b/attachment_metadata/models/attachment.py index f45724d0133..f385810c39a 100644 --- a/attachment_metadata/models/attachment.py +++ b/attachment_metadata/models/attachment.py @@ -33,14 +33,18 @@ class IrAttachmentMetadata(models.Model): help="The file type determines an import method to be used " "to parse and transform data before their import in ERP") - @api.depends('datas', 'external_hash') + @api.depends('datas') def _compute_hash(self): for attachment in self: if attachment.datas: attachment.internal_hash = hashlib.md5( b64decode(attachment.datas)).hexdigest() + + @api.constrains('internal_hash', 'external_hash') + def _check_external_hash(self): + for attachment in self: if attachment.external_hash and\ - attachment.internal_hash != attachment.external_hash: + attachment.internal_hash != attachment.external_hash: raise UserError( _("File corrupted: Something was wrong with " "the retrieved file, please relaunch the task.")) @@ -51,3 +55,4 @@ def _get_file_type(self): The file is just added as an attachement """ return [('basic_import', 'Basic import')] + From 1aa5e528084aca5479fc348f50e1254232bc6540 Mon Sep 17 00:00:00 2001 From: Mourad Elhadj Mimoune Date: Thu, 2 Jun 2016 10:55:31 +0200 Subject: [PATCH 3/3] [IMP] add test --- attachment_metadata/tests/__init__.py | 1 + attachment_metadata/tests/test_attachment.py | 36 ++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 attachment_metadata/tests/__init__.py create mode 100644 attachment_metadata/tests/test_attachment.py diff --git a/attachment_metadata/tests/__init__.py b/attachment_metadata/tests/__init__.py new file mode 100644 index 00000000000..42c4530cf78 --- /dev/null +++ b/attachment_metadata/tests/__init__.py @@ -0,0 +1 @@ +from . import test_attachment diff --git a/attachment_metadata/tests/test_attachment.py b/attachment_metadata/tests/test_attachment.py new file mode 100644 index 00000000000..7a49fe5473d --- /dev/null +++ b/attachment_metadata/tests/test_attachment.py @@ -0,0 +1,36 @@ +# coding: utf-8 +# ©2016 @author Mourad EL HADJ MIMOUNE +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from base64 import b64encode +import hashlib + +import openerp.tests.common as common + + +class TestNewSource(common.TransactionCase): + def setUp(self): + super(TestNewSource, self).setUp() + self.model_attachment = self.env['ir.attachment.metadata'] + + def test_attachement_hash(self): + ir_attachment_id1 = self.model_attachment.create({ + 'name': 'filename1', + 'datas': b64encode("Test import1"), + 'datas_fname': 'filename1', + 'external_hash': hashlib.md5("Test import1").hexdigest(), + }) + self.assertEqual(ir_attachment_id1.internal_hash, + ir_attachment_id1.external_hash) + ir_attachment_id2 = self.model_attachment.create({ + 'name': 'filename2', + 'datas': b64encode('Test import2'), + 'datas_fname': 'filename2', + }) + ir_attachment_ids = ir_attachment_id1 | ir_attachment_id2 + + ir_attachment_ids.write( + { + 'datas': b64encode('Test import1'), + }) + self.assertEqual(ir_attachment_ids[0].internal_hash, + ir_attachment_ids[0].internal_hash)