From d509a2da21d2db014fbd817a15b214bcccf16bd3 Mon Sep 17 00:00:00 2001 From: Jordi Ballester Alomar Date: Wed, 4 Jul 2018 13:54:03 +0200 Subject: [PATCH] implement an actual running example --- barcode_action/__init__.py | 5 +- barcode_action/__manifest__.py | 5 +- barcode_action/demo/barcode_action_demo.xml | 17 +++++ barcode_action/models/__init__.py | 1 + barcode_action/models/res_partner.py | 31 +++++++++ barcode_action/readme/USAGE.rst | 75 +++++++++++++-------- 6 files changed, 102 insertions(+), 32 deletions(-) create mode 100644 barcode_action/demo/barcode_action_demo.xml create mode 100644 barcode_action/models/__init__.py create mode 100644 barcode_action/models/res_partner.py diff --git a/barcode_action/__init__.py b/barcode_action/__init__.py index 3efbf5f..134df27 100644 --- a/barcode_action/__init__.py +++ b/barcode_action/__init__.py @@ -1,5 +1,2 @@ -# Copyright 2017 Creu Blanca -# Copyright 2017 Eficent Business and IT Consulting Services, S.L. -# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). - from . import wizard +from . import models diff --git a/barcode_action/__manifest__.py b/barcode_action/__manifest__.py index 2de79ab..aecef5e 100644 --- a/barcode_action/__manifest__.py +++ b/barcode_action/__manifest__.py @@ -18,5 +18,8 @@ 'data': [ 'views/barcode_templates.xml', 'wizard/barcode_action_view.xml', - ] + ], + 'demo': [ + 'demo/barcode_action_demo.xml' + ], } diff --git a/barcode_action/demo/barcode_action_demo.xml b/barcode_action/demo/barcode_action_demo.xml new file mode 100644 index 0000000..c976611 --- /dev/null +++ b/barcode_action/demo/barcode_action_demo.xml @@ -0,0 +1,17 @@ + + + + + + + + + diff --git a/barcode_action/models/__init__.py b/barcode_action/models/__init__.py new file mode 100644 index 0000000..91fed54 --- /dev/null +++ b/barcode_action/models/__init__.py @@ -0,0 +1 @@ +from . import res_partner diff --git a/barcode_action/models/res_partner.py b/barcode_action/models/res_partner.py new file mode 100644 index 0000000..03190bf --- /dev/null +++ b/barcode_action/models/res_partner.py @@ -0,0 +1,31 @@ +# 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). +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 diff --git a/barcode_action/readme/USAGE.rst b/barcode_action/readme/USAGE.rst index c9c1f52..d43eb9d 100644 --- a/barcode_action/readme/USAGE.rst +++ b/barcode_action/readme/USAGE.rst @@ -3,36 +3,57 @@ Actions must be configured with the following data in the context: * method: Method to execute (required) * res_id: Id as base (optional) -The method must return an action. For example +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. -Action example:: +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: -Python example:: +Action:: - @api.multi - def find_sale_order_by_barcode(self, barcode): - sale_order = self.search([('name', '=', barcode)]) - if not sale_order: - action = self.env.ref('sale_order_find') + + + + +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] - context = safe_eval(result['context']) - context.update({ - 'default_state': 'warning', - 'default_status': _('Sale Order %s cannot be found') % barcode - }) - result['context'] = json.dumps(context) + 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 - action = self.env.ref('sale.action_quotations') - result = action.read()[0] - res = self.env.ref('sale.view_order_form', False) - result['views'] = [(res and res.id or False, 'form')] - result['res_id'] = sale_order.id - return result