Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][ADD] product_code_regex_validation #1430

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 38 additions & 0 deletions product_code_regex_validation/models/product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2023 ForgeFlow S.L. (https://www.forgeflow.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import re
from collections import defaultdict

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


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

@api.constrains("default_code", "active")
def _check_product_code_regex_validation(self):
products_by_category = defaultdict(lambda: self.env["product.product"])
for rec in self:
if not rec.default_code or not rec.active:
continue
products_by_category[rec.categ_id] |= rec
for product_category, products in products_by_category.items():
if not product_category.product_code_regex_validation:
continue
pattern = re.compile(product_category.product_code_regex_validation)
for prod in products:
if not re.fullmatch(pattern, prod.default_code):
raise ValidationError(
_("Product Code %s does not match the Product Category format.")
% prod.default_code
)


class ProductTemplate(models.Model):
_inherit = "product.template"

@api.constrains("categ_id")
def _check_product_code_regex_validation(self):
return self.mapped("product_variant_ids")._check_product_code_regex_validation()
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

Check warning on line 19 in product_code_regex_validation/models/product_category.py

View check run for this annotation

Codecov / codecov/patch

product_code_regex_validation/models/product_category.py#L19

Added line #L19 was not covered by tests
try:
pattern = re.compile(rec.product_code_regex_validation)
except re.error as err:
raise ValidationError(

Check warning on line 23 in product_code_regex_validation/models/product_category.py

View check run for this annotation

Codecov / codecov/patch

product_code_regex_validation/models/product_category.py#L22-L23

Added lines #L22 - L23 were not covered by tests
_("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

Check warning on line 29 in product_code_regex_validation/models/product_category.py

View check run for this annotation

Codecov / codecov/patch

product_code_regex_validation/models/product_category.py#L29

Added line #L29 was not covered by tests
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
Loading