Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wip mem 8 attachm meta #5

Open
wants to merge 3 commits into
base: 8-attachm-meta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions attachment_metadata/models/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
# Copyright (C) 2014 initOS GmbH & Co. KG (<http://www.initos.com>).
# @author: Joel Grand-Guillaume @ Camptocamp SA
# @ 2015 Valentin CHEMIERE @ Akretion
# © 2016 @author Mourad EL HADJ MIMOUNE <[email protected]>
# 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'
Expand All @@ -31,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."))
Expand All @@ -49,3 +55,4 @@ def _get_file_type(self):
The file is just added as an attachement
"""
return [('basic_import', 'Basic import')]

1 change: 1 addition & 0 deletions attachment_metadata/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_attachment
36 changes: 36 additions & 0 deletions attachment_metadata/tests/test_attachment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# coding: utf-8
# ©2016 @author Mourad EL HADJ MIMOUNE <[email protected]>
# 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)