From 07a3317fceb4a1b70ad88d212cac82675b8b1da1 Mon Sep 17 00:00:00 2001 From: Enric Tobella Date: Wed, 4 Jul 2018 15:02:07 +0200 Subject: [PATCH] [IMP] barcode_action: Add tests --- barcode_action/README.rst | 85 +++++++++++++++---- barcode_action/__manifest__.py | 2 +- barcode_action/models/res_partner.py | 2 +- barcode_action/readme/CONTRIBUTORS.rst | 1 + .../static/src/js/action_barcode_form.js | 7 +- barcode_action/tests/__init__.py | 1 + barcode_action/tests/test_partner_find.py | 24 ++++++ barcode_action/wizard/barcode_action_view.xml | 2 +- 8 files changed, 99 insertions(+), 25 deletions(-) create mode 100644 barcode_action/tests/__init__.py create mode 100644 barcode_action/tests/test_partner_find.py diff --git a/barcode_action/README.rst b/barcode_action/README.rst index 21cd785..05452d4 100644 --- a/barcode_action/README.rst +++ b/barcode_action/README.rst @@ -1,21 +1,74 @@ -**This file is going to be generated by oca-gen-addon-readme.** +======================= +Barcode action launcher +======================= -*Manual changes will be overwritten.* +This module allows to use barcodes as launchers of actions. -Please provide content in the ``readme`` directory: +The action will launch a function that uses the barcode in order to return an action. -* **DESCRIPTION.rst** (required) -* INSTALL.rst (optional) -* CONFIGURE.rst (optional) -* **USAGE.rst** (optional, highly recommended) -* DEVELOP.rst (optional) -* ROADMAP.rst (optional) -* HISTORY.rst (optional, recommended) -* **CONTRIBUTORS.rst** (optional, highly recommended) -* CREDITS.rst (optional) +Usage +===== +Actions must be configured with the following data in the context: +* model: Model where we can find the method (required) +* method: Method to execute (required) +* res_id: Id as base (optional) -Content of this README will also be drawn from the addon manifest, -from keys such as name, authors, maintainers, development_status, -and license. +The method must return an action. Installing this module with demo data will +install a demo application that allows the system administrator to find a +partner by the external reference encoded in a barcode. -A good, one sentence summary in the manifest is also highly recommended. +Go to *Settings / Find partners* and scan a barcode that contains the +internal reference of an existing partner. As soon as you read the barcode +the system will redirect you to that partner's form view. + +Technical implementation of this example: + +Action:: + + + + + +Python code:: + + import json + from odoo import api, models, _ + from odoo.tools.safe_eval import safe_eval + + + class ResPartner(models.Model): + _inherit = 'res.partner' + + @api.multi + def find_res_partner_by_ref_using_barcode(self, barcode): + partner = self.search([('ref', '=', barcode)], limit=1) + if not partner: + action = self.env.ref('res_partner_find') + result = action.read()[0] + context = safe_eval(result['context']) + context.update({ + 'default_state': 'warning', + 'default_status': _('Partner with Internal Reference ' + '%s cannot be found') % barcode + }) + result['context'] = json.dumps(context) + return result + action = self.env.ref('base.action_partner_form') + result = action.read()[0] + res = self.env.ref('base.view_partner_form', False) + result['views'] = [(res and res.id or False, 'form')] + result['res_id'] = partner.id + return result + +Contributors +============ +* Enric Tobella +* Jordi Ballester diff --git a/barcode_action/__manifest__.py b/barcode_action/__manifest__.py index aecef5e..1396c77 100644 --- a/barcode_action/__manifest__.py +++ b/barcode_action/__manifest__.py @@ -5,7 +5,7 @@ { 'name': 'Barcode action launcher', 'version': '11.0.1.0.0', - 'category': 'Reporting', + 'category': 'Extra Tools', 'website': 'https://github.com/OCA/server-ux', 'author': 'Creu Blanca, Eficent, Odoo Community Association (OCA)', 'license': 'AGPL-3', diff --git a/barcode_action/models/res_partner.py b/barcode_action/models/res_partner.py index 03190bf..8a8a563 100644 --- a/barcode_action/models/res_partner.py +++ b/barcode_action/models/res_partner.py @@ -13,7 +13,7 @@ class ResPartner(models.Model): def find_res_partner_by_ref_using_barcode(self, barcode): partner = self.search([('ref', '=', barcode)], limit=1) if not partner: - action = self.env.ref('res_partner_find') + action = self.env.ref('barcode_action.res_partner_find') result = action.read()[0] context = safe_eval(result['context']) context.update({ diff --git a/barcode_action/readme/CONTRIBUTORS.rst b/barcode_action/readme/CONTRIBUTORS.rst index 02397ae..6296b24 100644 --- a/barcode_action/readme/CONTRIBUTORS.rst +++ b/barcode_action/readme/CONTRIBUTORS.rst @@ -1,2 +1,3 @@ * Enric Tobella +* Jordi Ballester diff --git a/barcode_action/static/src/js/action_barcode_form.js b/barcode_action/static/src/js/action_barcode_form.js index dad2cea..e53c381 100644 --- a/barcode_action/static/src/js/action_barcode_form.js +++ b/barcode_action/static/src/js/action_barcode_form.js @@ -5,11 +5,6 @@ var FormController = require('web.FormController'); FormController.include({ _barcodeHandleAction: function (barcode, activeBarcode) { - if (this.mode === 'readonly') { - this.do_warn(_t('Error : Document not editable'), - _t('To modify this document, please first start edition.')); - return new $.Deferred().reject(); - } var record = this.model.get(this.handle); var self = this; return self._rpc({ @@ -17,7 +12,7 @@ FormController.include({ method: record.data.method, args: [[record.data.res_id], barcode], }).done(function (action) { - if (action !== undefined){ + if (action){ self._barcodeStopListening(); self.do_action(action); } diff --git a/barcode_action/tests/__init__.py b/barcode_action/tests/__init__.py new file mode 100644 index 0000000..1050b9e --- /dev/null +++ b/barcode_action/tests/__init__.py @@ -0,0 +1 @@ +from . import test_partner_find diff --git a/barcode_action/tests/test_partner_find.py b/barcode_action/tests/test_partner_find.py new file mode 100644 index 0000000..ad57923 --- /dev/null +++ b/barcode_action/tests/test_partner_find.py @@ -0,0 +1,24 @@ +# Copyright 2018 Creu Blanca +# Copyright 2018 Eficent Business and IT Consulting Services, S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). +from odoo.tests.common import TransactionCase + + +class TestPartnerFind(TransactionCase): + + def test_partner(self): + partner_obj = self.env['res.partner'] + ref = 'testing_partner_internal_reference' + partner = partner_obj.create({ + 'name': 'Testing partner', + 'ref': ref, + }) + # We should find the partner when the ref is found + self.assertEqual( + partner.id, + partner_obj.find_res_partner_by_ref_using_barcode(ref).get( + 'res_id', False)) + # No partner is found, then there is no res_id on the result + self.assertFalse( + partner_obj.find_res_partner_by_ref_using_barcode( + '%s-%s' % (ref, ref)).get('res_id', False)) diff --git a/barcode_action/wizard/barcode_action_view.xml b/barcode_action/wizard/barcode_action_view.xml index 4271207..9abe0bb 100644 --- a/barcode_action/wizard/barcode_action_view.xml +++ b/barcode_action/wizard/barcode_action_view.xml @@ -1,4 +1,4 @@ - +