Skip to content

Commit 3df71c1

Browse files
committed
[MIG+IMP]product_category_code_unique: restriction options
1 parent 179a8a9 commit 3df71c1

File tree

11 files changed

+423
-44
lines changed

11 files changed

+423
-44
lines changed

product_category_code_unique/README.rst

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ Product Category Code Unique
1717
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
1818
:alt: License: AGPL-3
1919
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fproduct--attribute-lightgray.png?logo=github
20-
:target: https://github.com/OCA/product-attribute/tree/15.0/product_category_code_unique
20+
:target: https://github.com/OCA/product-attribute/tree/16.0/product_category_code_unique
2121
:alt: OCA/product-attribute
2222
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23-
:target: https://translation.odoo-community.org/projects/product-attribute-15-0/product-attribute-15-0-product_category_code_unique
23+
:target: https://translation.odoo-community.org/projects/product-attribute-16-0/product-attribute-16-0-product_category_code_unique
2424
:alt: Translate me on Weblate
2525
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26-
:target: https://runboat.odoo-community.org/builds?repo=OCA/product-attribute&target_branch=15.0
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/product-attribute&target_branch=16.0
2727
:alt: Try me on Runboat
2828

2929
|badge1| |badge2| |badge3| |badge4| |badge5|
@@ -36,13 +36,29 @@ If no code is provided, a code is generated by a sequence.
3636
.. contents::
3737
:local:
3838

39+
Configuration
40+
=============
41+
42+
There are 3 restriction options, which can be set from Settings > General Settings > Category Code Unique, under the "Product Category Code" section.
43+
44+
#. **Whole system**. Product category codes must be unique within the whole system.
45+
#. **Parent-Children**. Product category code for a given product category must be unique considering its parent category and its child categories.
46+
#. **Category Hierarchy**. Product category codes must be unique within the same categories hierarchy.
47+
48+
Usage
49+
=====
50+
51+
All product category codes are checked every time the category restriction is changed. An error message is shown when it is not possible to change the restriction because some codes are duplicated depending on the restriction selected.
52+
53+
If no code is manually set in a product category, one is automatically assigned following a sequence.
54+
3955
Bug Tracker
4056
===========
4157

4258
Bugs are tracked on `GitHub Issues <https://github.com/OCA/product-attribute/issues>`_.
4359
In case of trouble, please check there if your issue has already been reported.
4460
If you spotted it first, help us to smash it by providing a detailed and welcomed
45-
`feedback <https://github.com/OCA/product-attribute/issues/new?body=module:%20product_category_code_unique%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
61+
`feedback <https://github.com/OCA/product-attribute/issues/new?body=module:%20product_category_code_unique%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
4662

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

@@ -59,6 +75,7 @@ Contributors
5975

6076
* Denis Roussel <[email protected]>
6177
* Rolando Duarte <[email protected]>
78+
* Manuel Regidor <[email protected]>
6279

6380
Maintainers
6481
~~~~~~~~~~~
@@ -84,6 +101,6 @@ Current `maintainers <https://odoo-community.org/page/maintainer-role>`__:
84101

85102
|maintainer-rousseldenis| |maintainer-luisg123v|
86103

87-
This module is part of the `OCA/product-attribute <https://github.com/OCA/product-attribute/tree/15.0/product_category_code_unique>`_ project on GitHub.
104+
This module is part of the `OCA/product-attribute <https://github.com/OCA/product-attribute/tree/16.0/product_category_code_unique>`_ project on GitHub.
88105

89106
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

product_category_code_unique/__manifest__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
"name": "Product Category Code Unique",
66
"summary": """
77
Allows to set product category code field as unique""",
8-
"version": "15.0.1.0.0",
8+
"version": "16.0.1.0.0",
99
"license": "AGPL-3",
1010
"author": "ACSONE SA/NV,Odoo Community Association (OCA)",
1111
"maintainers": ["rousseldenis", "luisg123v"],
1212
"website": "https://github.com/OCA/product-attribute",
1313
"depends": [
14-
"product",
1514
"product_category_code",
1615
],
1716
"data": [
17+
"views/res_config_settings_views.xml",
1818
"data/product_category_sequence.xml",
1919
],
2020
"pre_init_hook": "pre_init_hook",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from . import product_category
2+
from . import res_config_settings

product_category_code_unique/models/product_category.py

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,67 @@
11
# Copyright 2021 ACSONE SA/NV
2+
# Copyright 2024 Manuel Regidor <[email protected]>
23
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
34

4-
from odoo import api, models
5+
from odoo import _, api, models
6+
from odoo.exceptions import ValidationError
57

68

79
class ProductCategory(models.Model):
810

911
_inherit = "product.category"
1012

11-
_sql_constraints = [
12-
("uniq_code", "unique(code)", "The category code must be unique!"),
13-
]
13+
def _get_parents(self):
14+
self.ensure_one()
15+
parent = self.parent_id
16+
parents = self.env["product.category"]
17+
while parent:
18+
parents += parent
19+
parent = parent.parent_id
20+
return parents
21+
22+
def _get_children(self):
23+
self.ensure_one()
24+
return self.search([("parent_id", "child_of", self.id)])
25+
26+
def _get_hierarchy_cats(self):
27+
self.ensure_one()
28+
return self + self._get_parents() + self._get_children()
29+
30+
def _code_restriction(self, restriction=False):
31+
restriction = restriction or self.env["ir.config_parameter"].get_param(
32+
"product_code_unique.product_code_unique_restriction"
33+
)
34+
if restriction:
35+
for cat in self:
36+
domain = []
37+
within = ""
38+
if restriction == "system":
39+
domain = [("code", "=", cat.code)]
40+
within = _("the system")
41+
elif restriction == "direct":
42+
to_check = cat.parent_id + cat.child_id
43+
domain = [
44+
"|",
45+
("id", "=", cat.id),
46+
"&",
47+
("code", "=", cat.code),
48+
("id", "in", to_check.ids),
49+
]
50+
within = _("parent and children")
51+
elif restriction == "hierarchy":
52+
to_check = cat._get_hierarchy_cats()
53+
domain = [("code", "=", cat.code), ("id", "in", to_check.ids)]
54+
within = _("category hierarchy")
55+
if self.search_count(domain) > 1:
56+
raise ValidationError(
57+
_("The category code must be unique within {within}!").format(
58+
within=within
59+
)
60+
)
61+
62+
@api.constrains("code")
63+
def _check_code(self):
64+
self._code_restriction()
1465

1566
@api.model
1667
def _get_next_code(self):
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2023 ACSONE SA/NV
2+
# Copyright 2024 Manuel Regidor <[email protected]>
3+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
4+
5+
from odoo import api, fields, models
6+
7+
8+
class ResConfigSettings(models.TransientModel):
9+
10+
_inherit = "res.config.settings"
11+
12+
product_cat_code_unique_restriction = fields.Selection(
13+
[
14+
("system", "Whole System"),
15+
("direct", "Parent-Children"),
16+
("hierarchy", "Category Hierarchy"),
17+
],
18+
string="Product Category Code Uniqueness Restriction",
19+
config_parameter="product_code_unique.product_code_unique_restriction",
20+
help=(
21+
"If no option is selected, no restriction applies.\n"
22+
"If you select:\n"
23+
"- Whole Sytem: Product Category Codes cannot be duplicated within the "
24+
" whole system\n"
25+
"- Parent-Children: Parent and Children Product Category Codes of the"
26+
" same category cannot be duplicated.\n"
27+
"- Category Hierarchy: Product Category Codes cannot be duplicated "
28+
"within the same category hierarchy.\n"
29+
),
30+
)
31+
32+
@api.constrains("product_cat_code_unique_restriction")
33+
def _check_product_cat_code_unique_restriction(self):
34+
for sel in self.filtered("product_cat_code_unique_restriction"):
35+
self.env["product.category"].search([])._code_restriction(
36+
sel.product_cat_code_unique_restriction
37+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
There are 3 restriction options, which can be set from Settings > General Settings > Category Code Unique, under the "Product Category Code" section.
2+
3+
#. **Whole system**. Product category codes must be unique within the whole system.
4+
#. **Parent-Children**. Product category code for a given product category must be unique considering its parent category and its child categories.
5+
#. **Category Hierarchy**. Product category codes must be unique within the same categories hierarchy.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
* Denis Roussel <[email protected]>
22
* Rolando Duarte <[email protected]>
3+
* Manuel Regidor <[email protected]>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
All product category codes are checked every time the category restriction is changed. An error message is shown when it is not possible to change the restriction because some codes are duplicated depending on the restriction selected.
2+
3+
If no code is manually set in a product category, one is automatically assigned following a sequence.

product_category_code_unique/static/description/index.html

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<?xml version="1.0" encoding="utf-8"?>
21
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
32
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
43
<head>
@@ -369,54 +368,71 @@ <h1 class="title">Product Category Code Unique</h1>
369368
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
370369
!! source digest: sha256:b7c0200956cf578607572df0b8b215a886d91de7f488a5b1da447cfea042a947
371370
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
372-
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/product-attribute/tree/15.0/product_category_code_unique"><img alt="OCA/product-attribute" src="https://img.shields.io/badge/github-OCA%2Fproduct--attribute-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/product-attribute-15-0/product-attribute-15-0-product_category_code_unique"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/product-attribute&amp;target_branch=15.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
371+
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/product-attribute/tree/16.0/product_category_code_unique"><img alt="OCA/product-attribute" src="https://img.shields.io/badge/github-OCA%2Fproduct--attribute-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/product-attribute-16-0/product-attribute-16-0-product_category_code_unique"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/product-attribute&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
373372
<p>This module allows to restrict unique codes on product categories.
374373
If no code is provided, a code is generated by a sequence.</p>
375374
<p><strong>Table of contents</strong></p>
376375
<div class="contents local topic" id="contents">
377376
<ul class="simple">
378-
<li><a class="reference internal" href="#bug-tracker" id="toc-entry-1">Bug Tracker</a></li>
379-
<li><a class="reference internal" href="#credits" id="toc-entry-2">Credits</a><ul>
380-
<li><a class="reference internal" href="#authors" id="toc-entry-3">Authors</a></li>
381-
<li><a class="reference internal" href="#contributors" id="toc-entry-4">Contributors</a></li>
382-
<li><a class="reference internal" href="#maintainers" id="toc-entry-5">Maintainers</a></li>
377+
<li><a class="reference internal" href="#configuration" id="toc-entry-1">Configuration</a></li>
378+
<li><a class="reference internal" href="#usage" id="toc-entry-2">Usage</a></li>
379+
<li><a class="reference internal" href="#bug-tracker" id="toc-entry-3">Bug Tracker</a></li>
380+
<li><a class="reference internal" href="#credits" id="toc-entry-4">Credits</a><ul>
381+
<li><a class="reference internal" href="#authors" id="toc-entry-5">Authors</a></li>
382+
<li><a class="reference internal" href="#contributors" id="toc-entry-6">Contributors</a></li>
383+
<li><a class="reference internal" href="#maintainers" id="toc-entry-7">Maintainers</a></li>
383384
</ul>
384385
</li>
385386
</ul>
386387
</div>
388+
<div class="section" id="configuration">
389+
<h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
390+
<p>There are 3 restriction options, which can be set from Settings &gt; General Settings &gt; Category Code Unique, under the “Product Category Code” section.</p>
391+
<ol class="arabic simple">
392+
<li><strong>Whole system</strong>. Product category codes must be unique within the whole system.</li>
393+
<li><strong>Parent-Children</strong>. Product category code for a given product category must be unique considering its parent category and its child categories.</li>
394+
<li><strong>Category Hierarchy</strong>. Product category codes must be unique within the same categories hierarchy.</li>
395+
</ol>
396+
</div>
397+
<div class="section" id="usage">
398+
<h1><a class="toc-backref" href="#toc-entry-2">Usage</a></h1>
399+
<p>All product category codes are checked every time the category restriction is changed. An error message is shown when it is not possible to change the restriction because some codes are duplicated depending on the restriction selected.</p>
400+
<p>If no code is manually set in a product category, one is automatically assigned following a sequence.</p>
401+
</div>
387402
<div class="section" id="bug-tracker">
388-
<h1><a class="toc-backref" href="#toc-entry-1">Bug Tracker</a></h1>
403+
<h1><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h1>
389404
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/product-attribute/issues">GitHub Issues</a>.
390405
In case of trouble, please check there if your issue has already been reported.
391406
If you spotted it first, help us to smash it by providing a detailed and welcomed
392-
<a class="reference external" href="https://github.com/OCA/product-attribute/issues/new?body=module:%20product_category_code_unique%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
407+
<a class="reference external" href="https://github.com/OCA/product-attribute/issues/new?body=module:%20product_category_code_unique%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
393408
<p>Do not contact contributors directly about support or help with technical issues.</p>
394409
</div>
395410
<div class="section" id="credits">
396-
<h1><a class="toc-backref" href="#toc-entry-2">Credits</a></h1>
411+
<h1><a class="toc-backref" href="#toc-entry-4">Credits</a></h1>
397412
<div class="section" id="authors">
398-
<h2><a class="toc-backref" href="#toc-entry-3">Authors</a></h2>
413+
<h2><a class="toc-backref" href="#toc-entry-5">Authors</a></h2>
399414
<ul class="simple">
400415
<li>ACSONE SA/NV</li>
401416
</ul>
402417
</div>
403418
<div class="section" id="contributors">
404-
<h2><a class="toc-backref" href="#toc-entry-4">Contributors</a></h2>
419+
<h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
405420
<ul class="simple">
406421
<li>Denis Roussel &lt;<a class="reference external" href="mailto:denis.roussel&#64;acsone.eu">denis.roussel&#64;acsone.eu</a>&gt;</li>
407422
<li>Rolando Duarte &lt;<a class="reference external" href="mailto:rolando&#64;vauxoo.com">rolando&#64;vauxoo.com</a>&gt;</li>
423+
<li>Manuel Regidor &lt;<a class="reference external" href="mailto:manuel.regidor&#64;sygel.es">manuel.regidor&#64;sygel.es</a>&gt;</li>
408424
</ul>
409425
</div>
410426
<div class="section" id="maintainers">
411-
<h2><a class="toc-backref" href="#toc-entry-5">Maintainers</a></h2>
427+
<h2><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h2>
412428
<p>This module is maintained by the OCA.</p>
413429
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
414430
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
415431
mission is to support the collaborative development of Odoo features and
416432
promote its widespread use.</p>
417433
<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainers</a>:</p>
418434
<p><a class="reference external image-reference" href="https://github.com/rousseldenis"><img alt="rousseldenis" src="https://github.com/rousseldenis.png?size=40px" /></a> <a class="reference external image-reference" href="https://github.com/luisg123v"><img alt="luisg123v" src="https://github.com/luisg123v.png?size=40px" /></a></p>
419-
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/product-attribute/tree/15.0/product_category_code_unique">OCA/product-attribute</a> project on GitHub.</p>
435+
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/product-attribute/tree/16.0/product_category_code_unique">OCA/product-attribute</a> project on GitHub.</p>
420436
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
421437
</div>
422438
</div>

0 commit comments

Comments
 (0)