Skip to content

Commit

Permalink
[ADD] product_code_regex_validation
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiMForgeFlow committed Oct 10, 2023
1 parent 3c7a16c commit 95b639b
Show file tree
Hide file tree
Showing 14 changed files with 654 additions and 0 deletions.
91 changes: 91 additions & 0 deletions product_code_regex_validation/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
=============================
Product Code RegEx Validation
=============================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:4a4cc9ef73a7dd17a2564b4c82ecbc08e787afcb2ca8779ed357dc38b2a7f792
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |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%2Fproduct--attribute-lightgray.png?logo=github
:target: https://github.com/OCA/product-attribute/tree/16.0/product_code_regex_validation
:alt: OCA/product-attribute
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/product-attribute-16-0/product-attribute-16-0-product_code_regex_validation
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/product-attribute&target_branch=16.0
:alt: Try me on Runboat

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

This module allows configuring a regular expression on Product Categories in order to
validate that product codes inside the category follow a specific format.

**Table of contents**

.. contents::
:local:

Usage
=====

On each Product Category you can set the desired regular expression. The validation will
be done when:

- Creating a new product.
- Modifying the default code of a product.
- Modifying the Product Category of a product.
- Unarchiving a product.

Moreover, the regular expression on the Product Category will only be able to be changed
if all products in the category have a valid code format.

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/product-attribute/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/product-attribute/issues/new?body=module:%20product_code_regex_validation%0Aversion:%2016.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
~~~~~~~

* ForgeFlow

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

* Jordi Masvidal <[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/product-attribute <https://github.com/OCA/product-attribute/tree/16.0/product_code_regex_validation>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
4 changes: 4 additions & 0 deletions product_code_regex_validation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2023 ForgeFlow S.L. (https://www.forgeflow.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import models
15 changes: 15 additions & 0 deletions product_code_regex_validation/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2023 ForgeFlow S.L. (https://www.forgeflow.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Product Code RegEx Validation",
"summary": "Configure regEx validation for product codes.",
"version": "16.0.1.0.0",
"license": "AGPL-3",
"author": "ForgeFlow, Odoo Community Association (OCA)",
"category": "Product",
"website": "https://github.com/OCA/product-attribute",
"depends": ["product"],
"data": ["views/product_category.xml"],
"installable": True,
}
5 changes: 5 additions & 0 deletions product_code_regex_validation/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright 2023 ForgeFlow S.L. (https://www.forgeflow.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import product
from . import product_category
27 changes: 27 additions & 0 deletions product_code_regex_validation/models/product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2023 ForgeFlow S.L. (https://www.forgeflow.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import re

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


class ProductProduct(models.Model):
_inherit = "product.product"

@api.constrains("categ_id", "default_code", "active")
def _check_product_code_regex_validation(self):
for rec in self:
if (
not rec.default_code
or not rec.active
or not rec.categ_id.product_code_regex_validation
):
continue
pattern = re.compile(rec.categ_id.product_code_regex_validation)
if not re.fullmatch(pattern, rec.default_code):
raise ValidationError(
_("Product Code %s does not match the Product Category format.")
% rec.default_code
)
34 changes: 34 additions & 0 deletions product_code_regex_validation/models/product_category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2023 ForgeFlow S.L. (https://www.forgeflow.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import re

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


class ProductCategory(models.Model):
_inherit = "product.category"

product_code_regex_validation = fields.Char(string="Product Code RegEx Validation")

@api.constrains("product_code_regex_validation")
def _check_product_code_regex_validation(self):
for rec in self:
if not rec.product_code_regex_validation:
continue
try:
pattern = re.compile(rec.product_code_regex_validation)
except re.error as err:
raise ValidationError(
_("The following regular expression is not valid: %s") % pattern
) from err
products = self.env["product.product"].search([("categ_id", "=", rec.id)])
for prod in products:
if not prod.default_code:
continue
if not re.fullmatch(pattern, prod.default_code):
raise ValidationError(
_("Product Code %s does not match the Product Category format.")
% prod.default_code
)
1 change: 1 addition & 0 deletions product_code_regex_validation/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Jordi Masvidal <[email protected]>
2 changes: 2 additions & 0 deletions product_code_regex_validation/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This module allows configuring a regular expression on Product Categories in order to
validate that product codes inside the category follow a specific format.
9 changes: 9 additions & 0 deletions product_code_regex_validation/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
On each Product Category you can set the desired regular expression. The validation will
be done when:
- Creating a new product.
- Modifying the default code of a product.
- Modifying the Product Category of a product.
- Unarchiving a product.

Moreover, the regular expression on the Product Category will only be able to be changed
if all products in the category have a valid code format.
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 95b639b

Please sign in to comment.