summaryrefslogtreecommitdiff
path: root/addons/barcodes/models
diff options
context:
space:
mode:
authorArthur Maniet <arthurmaniet@me.com>2015-07-08 08:08:25 +0200
committerArthur Maniet <arthurmaniet@me.com>2015-09-01 13:25:52 +0200
commit2df4060c776c7a6a946f2b7a164ff7bbbe5e854a (patch)
tree21d8c78356237dce5ed7575964fc3142cd29abb5 /addons/barcodes/models
parentc1cc4b830bc2fd2b4cd01ff37b250d98e9dacb13 (diff)
[ADD] barcodes: onchange-like mechanism to react to scans in form view
For a model to use it, it must : - Inherit barcodes.barcode_events_mixin and override on_barcode_scanned(self, barcode) - Add <field name=barcode_scanned widget=barcode_handler/> to each form view that should listen to barcode events and trigger on_barcode_scanned. The method on_barcode_scanned works just like an onchange.
Diffstat (limited to 'addons/barcodes/models')
-rw-r--r--addons/barcodes/models/__init__.py3
-rw-r--r--addons/barcodes/models/barcode_events_mixin.py24
2 files changed, 27 insertions, 0 deletions
diff --git a/addons/barcodes/models/__init__.py b/addons/barcodes/models/__init__.py
new file mode 100644
index 00000000000..5c31ffd4a98
--- /dev/null
+++ b/addons/barcodes/models/__init__.py
@@ -0,0 +1,3 @@
+# -*- coding: utf-8 -*-
+
+import barcode_events_mixin
diff --git a/addons/barcodes/models/barcode_events_mixin.py b/addons/barcodes/models/barcode_events_mixin.py
new file mode 100644
index 00000000000..383e622c0a1
--- /dev/null
+++ b/addons/barcodes/models/barcode_events_mixin.py
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+
+from openerp import models, fields, api
+
+class BarcodeEventsMixin(models.AbstractModel):
+ """ Mixin class for objects reacting when a barcode is scanned in their form views
+ which contains `<field name="_barcode_scanned" widget="barcode_handler"/>`.
+ Models using this mixin must implement the method on_barcode_scanned. It works
+ like an onchange and receives the scanned barcode in parameter.
+ """
+
+ _name = 'barcodes.barcode_events_mixin'
+
+ _barcode_scanned = fields.Char("Barcode Scanned", help="Value of the last barcode scanned.", store=False)
+
+ @api.onchange('_barcode_scanned')
+ def _on_barcode_scanned(self):
+ barcode = self._barcode_scanned
+ if barcode:
+ self._barcode_scanned = ""
+ return self.on_barcode_scanned(barcode)
+
+ def on_barcode_scanned(self, barcode):
+ raise NotImplementedError("In order to use barcodes.barcode_events_mixin, method on_barcode_scanned must be implemented")