barcode_events_mixin.py 1.0 KB

1234567891011121314151617181920212223242526
  1. # -*- coding: utf-8 -*-
  2. from odoo import models, fields, api
  3. class BarcodeEventsMixin(models.AbstractModel):
  4. """ Mixin class for objects reacting when a barcode is scanned in their form views
  5. which contains `<field name="_barcode_scanned" widget="barcode_handler"/>`.
  6. Models using this mixin must implement the method on_barcode_scanned. It works
  7. like an onchange and receives the scanned barcode in parameter.
  8. """
  9. _name = 'barcodes.barcode_events_mixin'
  10. _description = 'Barcode Event Mixin'
  11. _barcode_scanned = fields.Char("Barcode Scanned", help="Value of the last barcode scanned.", store=False)
  12. @api.onchange('_barcode_scanned')
  13. def _on_barcode_scanned(self):
  14. barcode = self._barcode_scanned
  15. if barcode:
  16. self._barcode_scanned = ""
  17. return self.on_barcode_scanned(barcode)
  18. def on_barcode_scanned(self, barcode):
  19. raise NotImplementedError("In order to use barcodes.barcode_events_mixin, method on_barcode_scanned must be implemented")