Browse Source

Merge pull request #21 from etobella/11.0-add-barcodes_action

[ADD] barcode_action
pull/20/head
Jordi Ballester Alomar 6 years ago
committed by GitHub
parent
commit
ee2eceb453
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 74
      barcode_action/README.rst
  2. 2
      barcode_action/__init__.py
  3. 25
      barcode_action/__manifest__.py
  4. 17
      barcode_action/demo/barcode_action_demo.xml
  5. 1
      barcode_action/models/__init__.py
  6. 31
      barcode_action/models/res_partner.py
  7. 3
      barcode_action/readme/CONTRIBUTORS.rst
  8. 3
      barcode_action/readme/DESCRIPTION.rst
  9. 59
      barcode_action/readme/USAGE.rst
  10. BIN
      barcode_action/static/description/icon.png
  11. 23
      barcode_action/static/src/js/action_barcode_form.js
  12. 24
      barcode_action/static/src/js/action_barcode_widget.js
  13. 1
      barcode_action/tests/__init__.py
  14. 24
      barcode_action/tests/test_partner_find.py
  15. 12
      barcode_action/views/barcode_templates.xml
  16. 1
      barcode_action/wizard/__init__.py
  17. 22
      barcode_action/wizard/barcode_action.py
  18. 50
      barcode_action/wizard/barcode_action_view.xml

74
barcode_action/README.rst

@ -0,0 +1,74 @@
=======================
Barcode action launcher
=======================
This module allows to use barcodes as launchers of actions.
The action will launch a function that uses the barcode in order to return an action.
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)
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.
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::
<act_window id="res_partner_find"
name="Find Partner"
res_model="barcode.action"
view_mode="form"
view_type="form"
context="{'default_model': 'res.partner', 'default_method': 'find_res_partner_by_ref_using_barcode'}"
target="new"/>
<menuitem id="menu_orders_customers" name="Find partners"
action="res_partner_find"
parent="base.menu_administration"/>
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 <etobella@creublanca.es>
* Jordi Ballester <jordi.ballester@eficent.com>

2
barcode_action/__init__.py

@ -0,0 +1,2 @@
from . import wizard
from . import models

25
barcode_action/__manifest__.py

@ -0,0 +1,25 @@
# 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).
{
'name': 'Barcode action launcher',
'version': '11.0.1.0.0',
'category': 'Extra Tools',
'website': 'https://github.com/OCA/server-ux',
'author': 'Creu Blanca, Eficent, Odoo Community Association (OCA)',
'license': 'AGPL-3',
'installable': True,
'application': False,
'summary': 'Allows to use barcodes as a launcher',
'depends': [
'barcodes',
],
'data': [
'views/barcode_templates.xml',
'wizard/barcode_action_view.xml',
],
'demo': [
'demo/barcode_action_demo.xml'
],
}

17
barcode_action/demo/barcode_action_demo.xml

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<act_window id="res_partner_find"
name="Find Partner"
res_model="barcode.action"
view_mode="form"
view_type="form"
context="{'default_model': 'res.partner', 'default_method': 'find_res_partner_by_ref_using_barcode'}"
target="new"/>
<menuitem id="menu_orders_customers" name="Find partners"
action="res_partner_find"
parent="base.menu_administration"/>
</data>
</odoo>

1
barcode_action/models/__init__.py

@ -0,0 +1 @@
from . import res_partner

31
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('barcode_action.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

3
barcode_action/readme/CONTRIBUTORS.rst

@ -0,0 +1,3 @@
* Enric Tobella <etobella@creublanca.es>
* Jordi Ballester <jordi.ballester@eficent.com>

3
barcode_action/readme/DESCRIPTION.rst

@ -0,0 +1,3 @@
This module allows to use barcodes as launchers of actions.
The action will launch a function that uses the barcode in order to return an action.

59
barcode_action/readme/USAGE.rst

@ -0,0 +1,59 @@
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)
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.
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::
<act_window id="res_partner_find"
name="Find Partner"
res_model="barcode.action"
view_mode="form"
view_type="form"
context="{'default_model': 'res.partner', 'default_method': 'find_res_partner_by_ref_using_barcode'}"
target="new"/>
<menuitem id="menu_orders_customers" name="Find partners"
action="res_partner_find"
parent="base.menu_administration"/>
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

BIN
barcode_action/static/description/icon.png

After

Width: 128  |  Height: 128  |  Size: 9.2 KiB

23
barcode_action/static/src/js/action_barcode_form.js

@ -0,0 +1,23 @@
odoo.define('barcode_action.form', function (require) {
"use strict";
var FormController = require('web.FormController');
FormController.include({
_barcodeHandleAction: function (barcode, activeBarcode) {
var record = this.model.get(this.handle);
var self = this;
return self._rpc({
model: record.data.model,
method: record.data.method,
args: [[record.data.res_id], barcode],
}).done(function (action) {
if (action){
self._barcodeStopListening();
self.do_action(action);
}
});
},
});
});

24
barcode_action/static/src/js/action_barcode_widget.js

@ -0,0 +1,24 @@
odoo.define('barcode_action.field', function (require) {
"use strict";
var AbstractField = require('web.AbstractField');
var field_registry = require('web.field_registry');
var ActionBarcodeField = AbstractField.extend({
init: function() {
this._super.apply(this, arguments);
this.trigger_up('activeBarcode', {
name: this.name,
commands: {
barcode: '_barcodeHandleAction',
}
});
},
});
field_registry.add('action_barcode_handler', ActionBarcodeField);
return {
ActionBarcodeField:ActionBarcodeField,
};
});

1
barcode_action/tests/__init__.py

@ -0,0 +1 @@
from . import test_partner_find

24
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))

12
barcode_action/views/barcode_templates.xml

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<template id="assets_backend" name="pos_validation_barcode assets"
inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript"
src="/barcode_action/static/src/js/action_barcode_form.js"/>
<script type="text/javascript"
src="/barcode_action/static/src/js/action_barcode_widget.js"/>
</xpath>
</template>
</odoo>

1
barcode_action/wizard/__init__.py

@ -0,0 +1 @@
from . import barcode_action

22
barcode_action/wizard/barcode_action.py

@ -0,0 +1,22 @@
from odoo import fields, models
class BarcodeAction(models.TransientModel):
_name = 'barcode.action'
_inherit = "barcodes.barcode_events_mixin"
model = fields.Char(
required=True, readonly=True
)
res_id = fields.Integer()
method = fields.Char(
required=True, readonly=True
)
state = fields.Selection([
('waiting', 'Waiting'),
('warning', 'Warning')
], default='waiting', readonly=True, )
status = fields.Text(
readonly=True,
default="Start scanning",
)

50
barcode_action/wizard/barcode_action_view.xml

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2017 Eficent Business and IT Consulting Services, S.L.
Copyright 2017 Creu Blanca
License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
-->
<odoo>
<record id="barcode_action_form" model="ir.ui.view">
<field name="name">barcode.action.form</field>
<field name="model">barcode.action</field>
<field name="arch" type="xml">
<form string="Find">
<div class="alert alert-success text-center o_form_header"
role="alert" style="margin-bottom:0px;"
attrs="{'invisible':[('state', '=', 'warning')]}">
<bold><field name="status"/></bold>
</div>
<div class="alert alert-danger alert-dismissable text-center"
role="alert" style="margin-bottom:0px;"
attrs="{'invisible':[('state', '=', 'waiting')]}">
<bold><field name="status"/></bold>
</div>
<field name="state" invisible="1"/>
<field name="res_id" invisible="1"/>
<field name="model" invisible="1"/>
<field name="method" invisible="1"/>
<field name="_barcode_scanned" widget="action_barcode_handler"/>
<footer>
<button
name="action_cancel"
string="Close"
class="oe_link"
special="cancel"
/>
</footer>
</form>
</field>
</record>
<act_window id="barcode_action_action"
name="Barcode Launcher"
res_model="barcode.action"
view_mode="form"
view_type="form"
target="new"/>
</odoo>
Loading…
Cancel
Save