From 9366ebf79840ecb0c46c68415dd608557a3ddfc9 Mon Sep 17 00:00:00 2001 From: EL HADJI DEM Date: Wed, 11 Mar 2015 11:31:46 -0400 Subject: [PATCH 1/2] [UPD]Change the filter and add the context Add a context to keep the model and model_id. Change the domain filter with the new field [ADD] Add a new field function This new function allows to get the related documents with the model and the model_id. We add this new field because the domain in the view or in the js doesn't match the wanted result [UPD] Add missing comma [ADD] Add unittests --- document_multiple_records/document.py | 26 ++++ .../static/src/js/document.js | 9 +- document_multiple_records/tests/__init__.py | 29 +++++ .../tests/test_ir_attachment.py | 116 ++++++++++++++++++ 4 files changed, 178 insertions(+), 2 deletions(-) create mode 100644 document_multiple_records/tests/__init__.py create mode 100644 document_multiple_records/tests/test_ir_attachment.py diff --git a/document_multiple_records/document.py b/document_multiple_records/document.py index 0c7ebd88..269188fa 100644 --- a/document_multiple_records/document.py +++ b/document_multiple_records/document.py @@ -51,6 +51,29 @@ class document_file(orm.Model): data[attachment.id] = False return data + def _get_related_model_documents( + self, cr, uid, ids, field_name, args, context=None): + """ This function allows to get only related documents with a specific + model and model_id. + """ + context = context or {} + ir_attachment_doc_obj = self.pool.get('ir.attachment.document') + res = [('id', 'in', [])] + # Format query with the res_model and res_id to search + # in ir.attachment.document model. + query = [ + ('res_model', '=', context.get('model')), + ('res_id', '=', context.get('model_id'))] + ir_attachment_doc_ids = ir_attachment_doc_obj.search( + cr, uid, query, context=context) + + ir_attachment_ids = self.search( + cr, uid, [ + ('attachment_document_ids.id', 'in', ir_attachment_doc_ids)], + context=context) + res = [('id', 'in', ir_attachment_ids)] + return res + _columns = { 'attachment_document_ids': fields.one2many('ir.attachment.document', 'attachment_id', @@ -58,6 +81,9 @@ class document_file(orm.Model): 'res_name': fields.function(_name_get_resname, type='char', size=128, string='Resource Name', store=True), + 'related_document': fields.function( + lambda self, *a, **kw: True, type='boolean', + fnct_search=_get_related_model_documents), } def create(self, cr, uid, data, context=None): diff --git a/document_multiple_records/static/src/js/document.js b/document_multiple_records/static/src/js/document.js index e8808e81..20e95cf7 100644 --- a/document_multiple_records/static/src/js/document.js +++ b/document_multiple_records/static/src/js/document.js @@ -63,6 +63,11 @@ var _t = instance.web._t, var self = this; this.dataset = dataset; this.model_id = model_id; + // Add the model and the model_id in the context + var context = { + 'model': dataset.model, + 'model_id': model_id + }; if (args && args[0].error) { this.do_warn(_t('Uploading Error'), args[0].error); } @@ -70,8 +75,8 @@ var _t = instance.web._t, this.on_attachments_loaded([]); } else { - var dom = [ ['attachment_document_ids.res_model', '=', dataset.model], ['attachment_document_ids.res_id', '=', model_id], ['type', 'in', ['binary', 'url']] ]; - var ds = new instance.web.DataSetSearch(this, 'ir.attachment', dataset.get_context(), dom); + var dom = [ ['related_document', '=', true] ]; + var ds = new instance.web.DataSetSearch(this, 'ir.attachment', context, dom); ds.read_slice(['name', 'url', 'type', 'create_uid', 'create_date', 'write_uid', 'write_date'], {}).done(this.on_attachments_loaded); } } diff --git a/document_multiple_records/tests/__init__.py b/document_multiple_records/tests/__init__.py new file mode 100644 index 00000000..24cebe79 --- /dev/null +++ b/document_multiple_records/tests/__init__.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- + +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2015 Savoir-faire Linux (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +from . import ( + test_ir_attachment, +) + +checks = [ + test_ir_attachment, +] diff --git a/document_multiple_records/tests/test_ir_attachment.py b/document_multiple_records/tests/test_ir_attachment.py new file mode 100644 index 00000000..728393b6 --- /dev/null +++ b/document_multiple_records/tests/test_ir_attachment.py @@ -0,0 +1,116 @@ +# -*- encoding: utf-8 -*- +############################################################################### +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2015 Savoir-faire Linux () +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################### + +from openerp.tests.common import TransactionCase + + +class test_ir_attachment(TransactionCase): + + def setUp(self): + super(test_ir_attachment, self).setUp() + + # Get registries + self.ira_model = self.registry('ir.attachment') + self.user_model = self.registry("res.users") + self.partner_model = self.registry("res.partner") + self.context = self.user_model.context_get(self.cr, self.uid) + + # Create first partner + self.partner_id_1 = self.partner_model.create(self.cr, self.uid, { + 'name': 'Test first partner' + }, context=self.context) + + # Create second partner + self.partner_id_2 = self.partner_model.create(self.cr, self.uid, { + 'name': 'Test second partner' + }, context=self.context) + + blob1 = 'blob1' + blob1_b64 = blob1.encode('base64') + + self.vals = { + 'name': 'Test documemt', + 'datas': blob1_b64, + 'res_model': 'res.partner', + 'res_id': self.partner_id_1, + 'res_name': 'Test first partner', + 'attachment_document_ids': [(0, 0, { + 'res_model': 'res.partner', + 'res_id': self.partner_id_1, + 'res_name': 'Test first partner', + }), + (0, 0, { + 'res_model': 'res.partner', + 'res_id': self.partner_id_2, + 'res_name': 'Test second partner', + }) + ] + } + + def test_create_document(self): + """Test create document and check each value in vals variable """ + cr, uid, vals, context = self.cr, self.uid, self.vals, self.context + ira_id = self.ira_model.create(cr, uid, vals, context=context) + ira_obj = self.ira_model.browse(cr, uid, ira_id, context=context) + self.assertEqual(ira_obj.name, vals['name']) + self.assertEqual(ira_obj.res_id, vals['res_id']) + self.assertEqual(ira_obj.res_name, vals['res_name']) + self.assertEqual(ira_obj.res_model, vals['res_model']) + self.assertEqual( + ira_obj.attachment_document_ids[0].res_id, self.partner_id_1) + self.assertEqual( + ira_obj.attachment_document_ids[0].res_name, 'Test first partner') + self.assertEqual( + ira_obj.attachment_document_ids[0].res_model, 'res.partner') + + def test_unlink_document(self): + """Test normal unlink document """ + cr, uid, vals = self.cr, self.uid, self.vals.copy() + context = self.context + ira_id = self.ira_model.create(cr, uid, vals, context=context) + self.ira_model.unlink(cr, uid, ira_id, context=context) + + def test_unlink_document_drop_down(self): + """Test unlink from drop-down list in the form view """ + cr, uid, vals = self.cr, self.uid, self.vals.copy() + context = self.context + context['multiple_records_res_model'] = 'res.partner' + context['multiple_records_res_id'] = self.partner_id_1 + ira_id = self.ira_model.create(cr, uid, vals, context=context) + self.ira_model.unlink(cr, uid, [ira_id], context=context) + + def test_name_get_resname(self): + """Test _name_get_resname function """ + cr, uid, vals, context = self.cr, self.uid, self.vals, self.context + ira_id = self.ira_model.create(cr, uid, vals, context=context) + self.assertEquals(self.ira_model._name_get_resname( + cr, uid, ira_id, obj=None, method=None, + context=context)[ira_id], vals['res_name']) + + def test_get_related_model_documents(self): + cr, uid, vals = self.cr, self.uid, self.vals.copy() + context = self.context + context['model'] = 'res.partner' + context['model_id'] = self.partner_id_1 + ira_id = self.ira_model.create(cr, uid, vals, context=context) + res = self.ira_model._get_related_model_documents( + cr, uid, ira_id, field_name=None, args=None, context=context) + self.assertEqual(res[0], ('id', 'in', [ira_id])) From f06d0194df6592b661216e488998a7e9b39b903b Mon Sep 17 00:00:00 2001 From: Maxime Chambreuil Date: Sat, 14 Nov 2015 12:33:50 -0500 Subject: [PATCH 2/2] [FIX] Pylint errors --- document_multiple_records/__init__.py | 4 +- document_multiple_records/__openerp__.py | 52 +++++++++---------- document_multiple_records/document.py | 2 +- .../tests/test_ir_attachment.py | 2 +- .../{ => views}/document_view.xml | 0 document_multiple_records/wizard/__init__.py | 4 +- .../wizard/document_wizard.py | 2 +- 7 files changed, 31 insertions(+), 35 deletions(-) rename document_multiple_records/{ => views}/document_view.xml (100%) diff --git a/document_multiple_records/__init__.py b/document_multiple_records/__init__.py index 579abcbc..8631f443 100644 --- a/document_multiple_records/__init__.py +++ b/document_multiple_records/__init__.py @@ -1,4 +1,4 @@ -# -*- encoding: utf-8 -*- +# -*- coding: utf-8 -*- ############################################################################## # # OpenERP, Open Source Management Solution @@ -22,5 +22,3 @@ from . import document from . import wizard - -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/document_multiple_records/__openerp__.py b/document_multiple_records/__openerp__.py index b7e55f58..1eb91cd7 100644 --- a/document_multiple_records/__openerp__.py +++ b/document_multiple_records/__openerp__.py @@ -1,4 +1,4 @@ -# -*- encoding: utf-8 -*- +# -*- coding: utf-8 -*- ############################################################################## # # OpenERP, Open Source Management Solution @@ -21,40 +21,40 @@ ############################################################################## { - 'name': 'Document Management System for Multiple Records', - 'version': '0.1', - 'category': 'Knowledge Management', - 'summary': 'Document Management System for Multiple Records', - 'description': """ + "name": "Document Management System for Multiple Records", + "version": "7.0.1.0.0", + "category": "Knowledge Management", + "summary": "Document Management System for Multiple Records", + "description": """ +=============================================== Document Management System for Multiple Records -============================================== +=============================================== Contributors ------------ * El Hadji Dem (elhadji.dem@savoirfairelinux.com) +* Maxime Chambreuil (maxime.chambreuil@savoirfairelinux.com) """, - 'author': "Savoir-faire Linux,Odoo Community Association (OCA)", - 'website': 'www.savoirfairelinux.com', - 'license': 'AGPL-3', - 'depends': [ - 'document', + "author": "Savoir-faire Linux, Odoo Community Association (OCA)", + "website": "www.savoirfairelinux.com", + "license": "AGPL-3", + "depends": [ + "document", ], - 'data': [ - 'document_view.xml', - 'security/ir.model.access.csv', - 'wizard/document_wizard_view.xml', + "data": [ + "views/document_view.xml", + "security/ir.model.access.csv", + "wizard/document_wizard_view.xml", ], - 'js': [ - 'static/src/js/document.js' + "js": [ + "static/src/js/document.js" ], - 'qweb': [ - 'static/src/xml/document.xml' + "qweb": [ + "static/src/xml/document.xml" ], - 'test': [], - 'demo': [ + "test": [], + "demo": [ ], - 'installable': True, - 'auto_install': False, + "installable": True, + "auto_install": False, } - -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/document_multiple_records/document.py b/document_multiple_records/document.py index 269188fa..ea3d5aeb 100644 --- a/document_multiple_records/document.py +++ b/document_multiple_records/document.py @@ -1,4 +1,4 @@ -# -*- encoding: utf-8 -*- +# -*- coding: utf-8 -*- ############################################################################## # # OpenERP, Open Source Management Solution diff --git a/document_multiple_records/tests/test_ir_attachment.py b/document_multiple_records/tests/test_ir_attachment.py index 728393b6..585ac01b 100644 --- a/document_multiple_records/tests/test_ir_attachment.py +++ b/document_multiple_records/tests/test_ir_attachment.py @@ -1,4 +1,4 @@ -# -*- encoding: utf-8 -*- +# -*- coding: utf-8 -*- ############################################################################### # # OpenERP, Open Source Management Solution diff --git a/document_multiple_records/document_view.xml b/document_multiple_records/views/document_view.xml similarity index 100% rename from document_multiple_records/document_view.xml rename to document_multiple_records/views/document_view.xml diff --git a/document_multiple_records/wizard/__init__.py b/document_multiple_records/wizard/__init__.py index 108a1a20..e311da4c 100644 --- a/document_multiple_records/wizard/__init__.py +++ b/document_multiple_records/wizard/__init__.py @@ -1,4 +1,4 @@ -# -*- encoding: utf-8 -*- +# -*- coding: utf-8 -*- ############################################################################## # # OpenERP, Open Source Management Solution @@ -21,5 +21,3 @@ ############################################################################## from . import document_wizard - -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/document_multiple_records/wizard/document_wizard.py b/document_multiple_records/wizard/document_wizard.py index 44b44b66..f6f9cc08 100644 --- a/document_multiple_records/wizard/document_wizard.py +++ b/document_multiple_records/wizard/document_wizard.py @@ -1,4 +1,4 @@ -# -*- encoding: utf-8 -*- +# -*- coding: utf-8 -*- ############################################################################## # # OpenERP, Open Source Management Solution