Skip to content

Commit

Permalink
[MIG+IMP]product_category_code_unique: restriction options
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelregidor committed Jan 7, 2025
1 parent 179a8a9 commit 49d1df1
Show file tree
Hide file tree
Showing 11 changed files with 424 additions and 49 deletions.
27 changes: 22 additions & 5 deletions product_category_code_unique/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ Product Category Code Unique
: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/15.0/product_category_code_unique
:target: https://github.com/OCA/product-attribute/tree/16.0/product_category_code_unique
: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-15-0/product-attribute-15-0-product_category_code_unique
:target: https://translation.odoo-community.org/projects/product-attribute-16-0/product-attribute-16-0-product_category_code_unique
: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=15.0
: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|
Expand All @@ -36,13 +36,29 @@ If no code is provided, a code is generated by a sequence.
.. contents::
:local:

Configuration
=============

There are 3 restriction options, which can be set from Settings > General Settings > Category Code Unique, under the "Product Category Code" section.

#. **Whole system**. Product category codes must be unique within the whole system.
#. **Parent-Children**. Product category code for a given product category must be unique considering its parent category and its child categories.
#. **Category Hierarchy**. Product category codes must be unique within the same categories hierarchy.

Usage
=====

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.

If no code is manually set in a product category, one is automatically assigned following a sequence.

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_category_code_unique%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`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**>`_.

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

Expand All @@ -59,6 +75,7 @@ Contributors

* Denis Roussel <[email protected]>
* Rolando Duarte <[email protected]>
* Manuel Regidor <[email protected]>

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

|maintainer-rousseldenis| |maintainer-luisg123v|

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.
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.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
4 changes: 2 additions & 2 deletions product_category_code_unique/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
"name": "Product Category Code Unique",
"summary": """
Allows to set product category code field as unique""",
"version": "15.0.1.0.0",
"version": "16.0.1.0.0",
"license": "AGPL-3",
"author": "ACSONE SA/NV,Odoo Community Association (OCA)",
"maintainers": ["rousseldenis", "luisg123v"],
"website": "https://github.com/OCA/product-attribute",
"depends": [
"product",
"product_category_code",
],
"data": [
"views/res_config_settings_views.xml",
"data/product_category_sequence.xml",
],
"pre_init_hook": "pre_init_hook",
Expand Down
1 change: 1 addition & 0 deletions product_category_code_unique/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import product_category
from . import res_config_settings
65 changes: 56 additions & 9 deletions product_category_code_unique/models/product_category.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,73 @@
# Copyright 2021 ACSONE SA/NV
# Copyright 2024 Manuel Regidor <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

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


class ProductCategory(models.Model):

_inherit = "product.category"

_sql_constraints = [
("uniq_code", "unique(code)", "The category code must be unique!"),
]
def _get_parents(self):
self.ensure_one()
return self.search([("parent_id", "parent_of", self.id)])

def _get_children(self):
self.ensure_one()
return self.search([("parent_id", "child_of", self.id)])

def _get_hierarchy_cats(self):
self.ensure_one()
return self._get_parents() + self._get_children()

def _code_restriction(self, restriction=False):
restriction = restriction or self.env["ir.config_parameter"].get_param(
"product_code_unique.product_code_unique_restriction"
)
if restriction:
for cat in self:
domain = []
within = ""
if restriction == "system":
domain = [("code", "=", cat.code)]
within = _("the system")
elif restriction == "direct":
to_check = cat.parent_id + cat.child_id
domain = [
"|",
("id", "=", cat.id),
"&",
("code", "=", cat.code),
("id", "in", to_check.ids),
]
within = _("parent and children")
elif restriction == "hierarchy":
to_check = cat._get_hierarchy_cats()
domain = [("code", "=", cat.code), ("id", "in", to_check.ids)]
within = _("category hierarchy")
if self.search_count(domain) > 1:
raise ValidationError(
_("The category code must be unique within {within}!").format(
within=within
)
)

@api.constrains("code", "parent_id", "child_id")
def _check_code(self):
self._code_restriction()

@api.model
def _get_next_code(self):
return self.env["ir.sequence"].next_by_code("product.category")

@api.model
def create(self, vals):
if "code" not in vals or vals["code"] == "/":
vals["code"] = self._get_next_code()
return super().create(vals)
@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
if "code" not in vals or vals["code"] == "/":
vals["code"] = self._get_next_code()
return super().create(vals_list)

def write(self, vals):
for category in self:
Expand Down
37 changes: 37 additions & 0 deletions product_category_code_unique/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2023 ACSONE SA/NV
# Copyright 2024 Manuel Regidor <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import api, fields, models


class ResConfigSettings(models.TransientModel):

_inherit = "res.config.settings"

product_cat_code_unique_restriction = fields.Selection(
[
("system", "Whole System"),
("direct", "Parent-Children"),
("hierarchy", "Category Hierarchy"),
],
string="Product Category Code Uniqueness Restriction",
config_parameter="product_code_unique.product_code_unique_restriction",
help=(
"If no option is selected, no restriction applies.\n"
"If you select:\n"
"- Whole Sytem: Product Category Codes cannot be duplicated within the "
" whole system\n"
"- Parent-Children: Parent and Children Product Category Codes of the"
" same category cannot be duplicated.\n"
"- Category Hierarchy: Product Category Codes cannot be duplicated "
"within the same category hierarchy.\n"
),
)

@api.constrains("product_cat_code_unique_restriction")
def _check_product_cat_code_unique_restriction(self):
for sel in self.filtered("product_cat_code_unique_restriction"):
self.env["product.category"].search([])._code_restriction(
sel.product_cat_code_unique_restriction
)
5 changes: 5 additions & 0 deletions product_category_code_unique/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
There are 3 restriction options, which can be set from Settings > General Settings > Category Code Unique, under the "Product Category Code" section.

#. **Whole system**. Product category codes must be unique within the whole system.
#. **Parent-Children**. Product category code for a given product category must be unique considering its parent category and its child categories.
#. **Category Hierarchy**. Product category codes must be unique within the same categories hierarchy.
1 change: 1 addition & 0 deletions product_category_code_unique/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
* Denis Roussel <[email protected]>
* Rolando Duarte <[email protected]>
* Manuel Regidor <[email protected]>
3 changes: 3 additions & 0 deletions product_category_code_unique/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
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.

If no code is manually set in a product category, one is automatically assigned following a sequence.
44 changes: 30 additions & 14 deletions product_category_code_unique/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand Down Expand Up @@ -369,54 +368,71 @@ <h1 class="title">Product Category Code Unique</h1>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:b7c0200956cf578607572df0b8b215a886d91de7f488a5b1da447cfea042a947
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<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>
<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>
<p>This module allows to restrict unique codes on product categories.
If no code is provided, a code is generated by a sequence.</p>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#bug-tracker" id="toc-entry-1">Bug Tracker</a></li>
<li><a class="reference internal" href="#credits" id="toc-entry-2">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="toc-entry-3">Authors</a></li>
<li><a class="reference internal" href="#contributors" id="toc-entry-4">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="toc-entry-5">Maintainers</a></li>
<li><a class="reference internal" href="#configuration" id="toc-entry-1">Configuration</a></li>
<li><a class="reference internal" href="#usage" id="toc-entry-2">Usage</a></li>
<li><a class="reference internal" href="#bug-tracker" id="toc-entry-3">Bug Tracker</a></li>
<li><a class="reference internal" href="#credits" id="toc-entry-4">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="toc-entry-5">Authors</a></li>
<li><a class="reference internal" href="#contributors" id="toc-entry-6">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="toc-entry-7">Maintainers</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="configuration">
<h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
<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>
<ol class="arabic simple">
<li><strong>Whole system</strong>. Product category codes must be unique within the whole system.</li>
<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>
<li><strong>Category Hierarchy</strong>. Product category codes must be unique within the same categories hierarchy.</li>
</ol>
</div>
<div class="section" id="usage">
<h1><a class="toc-backref" href="#toc-entry-2">Usage</a></h1>
<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>
<p>If no code is manually set in a product category, one is automatically assigned following a sequence.</p>
</div>
<div class="section" id="bug-tracker">
<h1><a class="toc-backref" href="#toc-entry-1">Bug Tracker</a></h1>
<h1><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/product-attribute/issues">GitHub Issues</a>.
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
<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>
<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>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
<h1><a class="toc-backref" href="#toc-entry-2">Credits</a></h1>
<h1><a class="toc-backref" href="#toc-entry-4">Credits</a></h1>
<div class="section" id="authors">
<h2><a class="toc-backref" href="#toc-entry-3">Authors</a></h2>
<h2><a class="toc-backref" href="#toc-entry-5">Authors</a></h2>
<ul class="simple">
<li>ACSONE SA/NV</li>
</ul>
</div>
<div class="section" id="contributors">
<h2><a class="toc-backref" href="#toc-entry-4">Contributors</a></h2>
<h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<ul class="simple">
<li>Denis Roussel &lt;<a class="reference external" href="mailto:denis.roussel&#64;acsone.eu">denis.roussel&#64;acsone.eu</a>&gt;</li>
<li>Rolando Duarte &lt;<a class="reference external" href="mailto:rolando&#64;vauxoo.com">rolando&#64;vauxoo.com</a>&gt;</li>
<li>Manuel Regidor &lt;<a class="reference external" href="mailto:manuel.regidor&#64;sygel.es">manuel.regidor&#64;sygel.es</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-5">Maintainers</a></h2>
<h2><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<p>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.</p>
<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainers</a>:</p>
<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>
<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>
<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>
<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>
</div>
</div>
Expand Down
Loading

0 comments on commit 49d1df1

Please sign in to comment.