Skip to content

Commit

Permalink
[ADD] barcodes_regex_group: Migrated from 10 to 14
Browse files Browse the repository at this point in the history
  • Loading branch information
ddejong-therp committed Dec 8, 2022
1 parent 2011eb8 commit 6327714
Show file tree
Hide file tree
Showing 15 changed files with 758 additions and 0 deletions.
89 changes: 89 additions & 0 deletions barcodes_regex_group/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
=======================
Barcodes - Regex groups
=======================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fstock--logistics--barcode-lightgray.png?logo=github
:target: https://github.com/OCA/stock-logistics-barcode/tree/10.0/barcodes_regex_groups
:alt: OCA/stock-logistics-barcode
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/stock-logistics-barcode-10-0/stock-logistics-barcode-10-0-barcodes_regex_groups
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/150/10.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|

This module allows barcode nomenclatures to use regex groups to capture a part of a barcode.

Odoo's default ``barcodes`` module uses regex to match the barcode against the nomenclature rules, but in case of a match, it passes the full string to the ``on_barcode_scanned`` method.

With this module, we can use regex groups to specify which part of the barcode we want. For example, when a barcode looks like this:

::

PRODUCT X1234 SERIAL 12345

We can define a regex to capture the serial number only, looking like this:

::

PRODUCT [a-zA-Z0-9-]* SERIAL ([a-zA-Z0-9]*)

As an added feature, we can specify the Odoo models that a certain rule should apply to. Let us say that in the above case, we make a rule for product, but also for serial. We want the 'serial' rule to fire for ``stock.pack.operation`` operations, but the 'product' rule to fire for ``stock.picking`` operations, while scanning the same barcode, so, we make two rules, and configure a different model for each.

**Table of contents**

.. contents::
:local:

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/stock-logistics-barcode/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/stock-logistics-barcode/issues/new?body=module:%20barcodes_regex_groups%0Aversion:%2010.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Sunflower IT

Contributors
~~~~~~~~~~~~

* Tom Blauwendraat <[email protected]>

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/stock-logistics-barcode <https://github.com/OCA/stock-logistics-barcode/tree/10.0/barcodes_regex_groups>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 2 additions & 0 deletions barcodes_regex_group/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from . import models
22 changes: 22 additions & 0 deletions barcodes_regex_group/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Copyright 2020 Sunflower IT
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

{
"name": "Barcodes - Regex groups",
"summary": "Partial barcode capture, using regex groups in nomenclatures.",
"version": "14.0.1.0.0",
"author": "Sunflower IT, "
"Odoo Community Association (OCA)",
"website": "https://odoo-community.org/",
"license": "AGPL-3",
"category": "Extra Tools",
"depends": [
'barcodes',
],
"data": [
"views/assets_backend.xml",
"views/barcode_rule.xml"
],
'installable': True,
}
4 changes: 4 additions & 0 deletions barcodes_regex_group/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
from . import barcode_events_mixin
from . import barcode_rule
from . import barcode_nomenclature
12 changes: 12 additions & 0 deletions barcodes_regex_group/models/barcode_events_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-

from odoo import models, api


class BarcodeEventsMixin(models.AbstractModel):
_inherit = 'barcodes.barcode_events_mixin'

@api.onchange('_barcode_scanned')
def _on_barcode_scanned(self):
#self.env.cache['_barcode_active_model'] = self._name
return super(BarcodeEventsMixin, self)._on_barcode_scanned()
63 changes: 63 additions & 0 deletions barcodes_regex_group/models/barcode_nomenclature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# -*- coding: utf-8 -*-
# Copyright 2020 Sunflower IT
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import re
from typing import Match

from odoo import models


class BarcodeNomenclature(models.Model):
_inherit = 'barcode.nomenclature'

def match_pattern(self, barcode, pattern):
match = super(BarcodeNomenclature, self).match_pattern(
barcode, pattern)
print("AAAAAAA")

# We cannot use numerical content and matching groups at the same time
import pdb; pdb.set_trace()
numerical_content = re.search("[{][N]*[D]*[}]", pattern)
if numerical_content:
return match

# If there are no regex groups in pattern, use normal matching
has_regex_groups = re.search("[(].*[)]", pattern)
if not has_regex_groups:
return match

# Perform pattern matching using 'search' instead of 'match'
# And don't truncate barcode on search pattern length
match['match'] = re.search(pattern, match['base_code'])

# Abuse 'value' to store the result, since it goes to 'parsed_result'
if match['match'] and self.env.context.get('barcodes_regex_groups'):
match['value'] = match['match']
return match

def parse_barcode(self, barcode):
model = self.env.cache['_barcode_active_model']
this = self.with_context(barcodes_regex_groups=True)

# filter rules by their applicability to the currently active model
if model:
rule_ids_filtered = this.rule_ids.filtered(
lambda r: (not r.model_ids)
or model in r.model_ids.mapped('model')).ids
rule_ids_backup = this._cache['rule_ids']
this._cache['rule_ids'] = rule_ids_filtered

parsed_result = super(BarcodeNomenclature, this).parse_barcode(barcode)

# restore rule_ids
if model:
this._cache['rule_ids'] = rule_ids_backup

# Post-process any result of group-matching
if isinstance(parsed_result['value'], Match):
match = parsed_result['value']
parsed_result['value'] = 0
parsed_result['code'] = match.group(1)

return parsed_result
15 changes: 15 additions & 0 deletions barcodes_regex_group/models/barcode_rule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import logging
import re

from odoo import tools, models, fields, api, _
from odoo.exceptions import ValidationError


class BarcodeRule(models.Model):
_inherit = 'barcode.rule'

model_ids = fields.Many2many(
string="Applicable models",
comodel_name="ir.model",
domain=[('field_id.name', '=', '_barcode_scanned')])
pattern = fields.Char(size=64)
1 change: 1 addition & 0 deletions barcodes_regex_group/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Tom Blauwendraat <[email protected]>
17 changes: 17 additions & 0 deletions barcodes_regex_group/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
This module allows barcode nomenclatures to use regex groups to capture a part of a barcode.

Odoo's default ``barcodes`` module uses regex to match the barcode against the nomenclature rules, but in case of a match, it passes the full string to the ``on_barcode_scanned`` method.

With this module, we can use regex groups to specify which part of the barcode we want. For example, when a barcode looks like this:

::

PRODUCT X1234 SERIAL 12345

We can define a regex to capture the serial number only, looking like this:

::

PRODUCT [a-zA-Z0-9-]* SERIAL ([a-zA-Z0-9]*)

As an added feature, we can specify the Odoo models that a certain rule should apply to. Let us say that in the above case, we make a rule for product, but also for serial. We want the 'serial' rule to fire for ``stock.pack.operation`` operations, but the 'product' rule to fire for ``stock.picking`` operations, while scanning the same barcode, so, we make two rules, and configure a different model for each.
Binary file added barcodes_regex_group/static/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 6327714

Please sign in to comment.