From d1abf31fb4f5b51182e2f7da595a822bbe606069 Mon Sep 17 00:00:00 2001 From: svs-odoo Date: Thu, 11 Feb 2021 14:57:41 +0000 Subject: [PATCH] [MOV] barcodes: split `barcodes.py` Separates `barcode.nomenclature` and `barcode.rule` in their own file. task-1968113 Co-authored-by: ryv-odoo --- addons/barcodes/models/__init__.py | 3 +- .../{barcodes.py => barcode_nomenclature.py} | 40 +----------------- addons/barcodes/models/barcode_rule.py | 42 +++++++++++++++++++ 3 files changed, 45 insertions(+), 40 deletions(-) rename addons/barcodes/models/{barcodes.py => barcode_nomenclature.py} (76%) create mode 100644 addons/barcodes/models/barcode_rule.py diff --git a/addons/barcodes/models/__init__.py b/addons/barcodes/models/__init__.py index 15631161b43..7e42a60e439 100644 --- a/addons/barcodes/models/__init__.py +++ b/addons/barcodes/models/__init__.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- -from . import barcodes from . import barcode_events_mixin +from . import barcode_nomenclature +from . import barcode_rule from . import ir_http from . import res_company diff --git a/addons/barcodes/models/barcodes.py b/addons/barcodes/models/barcode_nomenclature.py similarity index 76% rename from addons/barcodes/models/barcodes.py rename to addons/barcodes/models/barcode_nomenclature.py index 1d2ba68fb20..1cf5ca0cfe7 100644 --- a/addons/barcodes/models/barcodes.py +++ b/addons/barcodes/models/barcode_nomenclature.py @@ -1,8 +1,7 @@ import logging import re -from odoo import tools, models, fields, api, _ -from odoo.exceptions import ValidationError +from odoo import models, fields _logger = logging.getLogger(__name__) @@ -165,40 +164,3 @@ class BarcodeNomenclature(models.Model): return parsed_result return parsed_result - -class BarcodeRule(models.Model): - _name = 'barcode.rule' - _description = 'Barcode Rule' - _order = 'sequence asc' - - - name = fields.Char(string='Rule Name', size=32, required=True, help='An internal identification for this barcode nomenclature rule') - barcode_nomenclature_id = fields.Many2one('barcode.nomenclature', string='Barcode Nomenclature') - sequence = fields.Integer(string='Sequence', help='Used to order rules such that rules with a smaller sequence match first') - encoding = fields.Selection([ - ('any', 'Any'), - ('ean13', 'EAN-13'), - ('ean8', 'EAN-8'), - ('upca', 'UPC-A'), - ], string='Encoding', required=True, default='any', help='This rule will apply only if the barcode is encoded with the specified encoding') - type = fields.Selection([ - ('alias', 'Alias'), - ('product', 'Unit Product') - ], string='Type', required=True, default='product') - pattern = fields.Char(string='Barcode Pattern', size=32, help="The barcode matching pattern", required=True, default='.*') - alias = fields.Char(string='Alias', size=32, default='0', help='The matched pattern will alias to this barcode', required=True) - - @api.constrains('pattern') - def _check_pattern(self): - for rule in self: - p = rule.pattern.replace("\\\\", "X").replace("\{", "X").replace("\}", "X") - findall = re.findall("[{]|[}]", p) # p does not contain escaped { or } - if len(findall) == 2: - if not re.search("[{][N]*[D]*[}]", p): - raise ValidationError(_("There is a syntax error in the barcode pattern %(pattern)s: braces can only contain N's followed by D's.", pattern=rule.pattern)) - elif re.search("[{][}]", p): - raise ValidationError(_("There is a syntax error in the barcode pattern %(pattern)s: empty braces.", pattern=rule.pattern)) - elif len(findall) != 0: - raise ValidationError(_("There is a syntax error in the barcode pattern %(pattern)s: a rule can only contain one pair of braces.", pattern=rule.pattern)) - elif p == '*': - raise ValidationError(_(" '*' is not a valid Regex Barcode Pattern. Did you mean '.*' ?")) diff --git a/addons/barcodes/models/barcode_rule.py b/addons/barcodes/models/barcode_rule.py new file mode 100644 index 00000000000..113d294042e --- /dev/null +++ b/addons/barcodes/models/barcode_rule.py @@ -0,0 +1,42 @@ +import re + +from odoo import models, fields, api, _ +from odoo.exceptions import ValidationError + + +class BarcodeRule(models.Model): + _name = 'barcode.rule' + _description = 'Barcode Rule' + _order = 'sequence asc' + + + name = fields.Char(string='Rule Name', size=32, required=True, help='An internal identification for this barcode nomenclature rule') + barcode_nomenclature_id = fields.Many2one('barcode.nomenclature', string='Barcode Nomenclature') + sequence = fields.Integer(string='Sequence', help='Used to order rules such that rules with a smaller sequence match first') + encoding = fields.Selection([ + ('any', 'Any'), + ('ean13', 'EAN-13'), + ('ean8', 'EAN-8'), + ('upca', 'UPC-A'), + ], string='Encoding', required=True, default='any', help='This rule will apply only if the barcode is encoded with the specified encoding') + type = fields.Selection([ + ('alias', 'Alias'), + ('product', 'Unit Product') + ], string='Type', required=True, default='product') + pattern = fields.Char(string='Barcode Pattern', size=32, help="The barcode matching pattern", required=True, default='.*') + alias = fields.Char(string='Alias', size=32, default='0', help='The matched pattern will alias to this barcode', required=True) + + @api.constrains('pattern') + def _check_pattern(self): + for rule in self: + p = rule.pattern.replace("\\\\", "X").replace("\{", "X").replace("\}", "X") + findall = re.findall("[{]|[}]", p) # p does not contain escaped { or } + if len(findall) == 2: + if not re.search("[{][N]*[D]*[}]", p): + raise ValidationError(_("There is a syntax error in the barcode pattern %(pattern)s: braces can only contain N's followed by D's.", pattern=rule.pattern)) + elif re.search("[{][}]", p): + raise ValidationError(_("There is a syntax error in the barcode pattern %(pattern)s: empty braces.", pattern=rule.pattern)) + elif len(findall) != 0: + raise ValidationError(_("There is a syntax error in the barcode pattern %(pattern)s: a rule can only contain one pair of braces.", pattern=rule.pattern)) + elif p == '*': + raise ValidationError(_(" '*' is not a valid Regex Barcode Pattern. Did you mean '.*' ?"))