Skip to content

Commit 34543fd

Browse files
committed
✨ product_template_attribute_value_xmlid
1 parent 69edfbc commit 34543fd

File tree

22 files changed

+1162
-0
lines changed

22 files changed

+1162
-0
lines changed
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
======================================================
2+
Generate XML IDs for Product Template Attribute Values
3+
======================================================
4+
5+
..
6+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7+
!! This file is generated by oca-gen-addon-readme !!
8+
!! changes will be overwritten. !!
9+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10+
!! source digest: sha256:fdec833460da026c1c5e41298152df57ef0efcbb741b5718a833b6d1e4b4ad35
11+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12+
13+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
14+
:target: https://odoo-community.org/page/development-status
15+
:alt: Beta
16+
.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png
17+
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
18+
:alt: License: LGPL-3
19+
.. |badge3| image:: https://img.shields.io/badge/github-OdooDataFlow%2Faddons-lightgray.png?logo=github
20+
:target: https://github.com/OdooDataFlow/addons/tree/18.0/product_template_attribute_value_xmlid
21+
:alt: OdooDataFlow/addons
22+
23+
|badge1| |badge2| |badge3|
24+
25+
This Odoo module automates the generation of XML IDs for
26+
product.template.attribute.value records. These records define the
27+
specific combinations of attributes and their values used to create
28+
product variants (e.g., "Color: Red, Size: Large").
29+
30+
Functionality:
31+
--------------
32+
33+
Automatic XML ID Generation:
34+
35+
The module overrides the create() method of the
36+
product.template.attribute.value model. Whenever a new
37+
product.template.attribute.value record is created, the module
38+
automatically generates an XML ID for it. XML ID Naming Convention:
39+
40+
The generated XML ID follows a predefined pattern, composed of the XML
41+
IDs of the related records: prod_templ_id.xmlid: The XML ID of the
42+
associated product.template. attribute_id.xmlid: The XML ID of the
43+
associated product.attribute. attribute_value_id.xmlid: The XML ID of
44+
the associated product.attribute.value.
45+
46+
The parts are combined with underscorese. This ensures that the XML ID
47+
is predictable and reflects the specific attribute value combination.
48+
49+
Manual XML ID Generation:
50+
-------------------------
51+
52+
The module also provides a method (generate_attribute_value_xmlids) that
53+
can be manually triggered to generate XML IDs for existing
54+
product.template.attribute.value records that might not have them.
55+
Benefits:
56+
57+
Predictability: Ensures consistent and predictable XML IDs. Automation:
58+
Automates the XML ID creation process. Data Integrity: Maintains data
59+
integrity by generating unique XML IDs. Integration: Simplifies
60+
integration with external systems.
61+
62+
**Table of contents**
63+
64+
.. contents::
65+
:local:
66+
67+
Use Cases / Context
68+
===================
69+
70+
Purpose:
71+
--------
72+
73+
The module ensures that every product.template.attribute.value record
74+
has a predictable and consistent XML ID, which is crucial for:
75+
76+
Data Integration: Facilitating seamless data exchange with external
77+
systems that rely on stable identifiers. Automation: Simplifying
78+
automated processes within Odoo that need to reference specific
79+
attribute value combinations.
80+
81+
Use Cases:
82+
----------
83+
84+
E-commerce platforms that need to synchronize product variant data with
85+
Odoo. Warehouse management systems that rely on unique identifiers for
86+
product configurations. Any scenario where stable and predictable
87+
identifiers for product attribute combinations are required. This
88+
description provides a good overview of the module's purpose and
89+
functionality.
90+
91+
Installation
92+
============
93+
94+
[ This file must only be present if there are very specific installation
95+
instructions, such as installing non-python dependencies. The audience
96+
is systems administrators. ]
97+
98+
Configuration
99+
=============
100+
101+
To configure this module, you need to:
102+
103+
1. Ensure that the ``product`` module is installed in your Odoo
104+
instance, as this module is a dependency.
105+
2. Place the ``product_template_attribute_value_xmlid`` module directory
106+
in your Odoo addons path.
107+
3. Install the ``product_template_attribute_value_xmlid`` module through
108+
the Odoo Apps interface.
109+
110+
Usage
111+
=====
112+
113+
Usage Guide: ``product_template_attribute_value_xmlid`` Module
114+
==============================================================
115+
116+
This document provides instructions on how to use the
117+
``product_template_attribute_value_xmlid`` module in Odoo.
118+
119+
Purpose
120+
-------
121+
122+
The ``product_template_attribute_value_xmlid`` module automates the
123+
generation of XML IDs for ``product.template.attribute.value`` records,
124+
which represent specific combinations of product attributes and their
125+
values. This is crucial for data integration and automation purposes.
126+
127+
Automatic XML ID Generation
128+
---------------------------
129+
130+
Once installed, the module automatically generates XML IDs for new
131+
``product.template.attribute.value`` records. You don't need to perform
132+
any manual steps for this.
133+
134+
**Example:**
135+
136+
When you create a new product template with attributes (e.g., Color:
137+
Red, Size: Large), the corresponding
138+
``product.template.attribute.value`` records created by Odoo will
139+
automatically receive XML IDs.
140+
141+
Manual XML ID Generation (Optional)
142+
-----------------------------------
143+
144+
In some cases, you might have existing
145+
``product.template.attribute.value`` records that were created before
146+
installing this module and therefore lack XML IDs. The module provides a
147+
method to generate XML IDs for these records.
148+
149+
**Steps:**
150+
151+
1. Navigate to the "Product Attribute Values" in the "Product" module.
152+
2. Open any form view of a ``product.template.attribute.value`` record.
153+
3. Click the "Generate XML IDs" button.
154+
4. Odoo will generate XML IDs for all
155+
``product.template.attribute.value`` records that currently don't
156+
have them.
157+
158+
XML ID Naming Convention
159+
------------------------
160+
161+
The generated XML IDs follow this pattern:
162+
prod_templ\_<product_template_xmlid>\ *attr*\ <attribute_xmlid>\ *val*\ <attribute_value_xmlid>
163+
164+
**Example:** prod_templ_product_template_123_attr_color_val_red
165+
166+
- ``prod_templ_``: Prefix indicating "product template."
167+
- ``<product_template_xmlid>``: The XML ID of the related
168+
``product.template`` record.
169+
- ``attr_``: Prefix indicating "attribute."
170+
- ``<attribute_xmlid>``: The XML ID of the related
171+
``product.attribute`` record.
172+
- ``val_``: Prefix indicating "attribute value."
173+
- ``<attribute_value_xmlid>``: The XML ID of the related
174+
``product.attribute.value`` record.
175+
176+
**Note:**
177+
178+
- All parts of the XML ID are converted to lowercase.
179+
- Spaces are replaced with underscores.
180+
181+
Important Considerations
182+
------------------------
183+
184+
- Ensure that the related ``product.template``, ``product.attribute``,
185+
and ``product.attribute.value`` records have XML IDs for the
186+
automatic generation to work correctly.
187+
- The module aims to generate unique XML IDs. However, if you have
188+
existing data with conflicting names or IDs, you might need to
189+
resolve those conflicts manually.
190+
191+
Known issues / Roadmap
192+
======================
193+
194+
[ Enumerate known caveats and future potential improvements. It is
195+
mostly intended for end-users, and can also help potential new
196+
contributors discovering new features to implement. ]
197+
198+
- This module might result in unpredictable behaviour when used with
199+
enterprise module ``product_barcodelookup``.
200+
201+
Changelog
202+
=========
203+
204+
[ The change log. The goal of this file is to help readers understand
205+
changes between version. The primary audience is end users and
206+
integrators. Purely technical changes such as code refactoring must not
207+
be mentioned here.
208+
209+
This file may contain ONE level of section titles, underlined with the ~
210+
(tilde) character. Other section markers are forbidden and will likely
211+
break the structure of the README.rst or other documents where this
212+
fragment is included. ]
213+
214+
Bug Tracker
215+
===========
216+
217+
Bugs are tracked on `GitHub Issues <https://github.com/OdooDataFlow/addons/issues>`_.
218+
In case of trouble, please check there if your issue has already been reported.
219+
If you spotted it first, help us to smash it by providing a detailed and welcomed
220+
`feedback <https://github.com/OdooDataFlow/addons/issues/new?body=module:%20product_template_attribute_value_xmlid%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
221+
222+
Do not contact contributors directly about support or help with technical issues.
223+
224+
Credits
225+
=======
226+
227+
Authors
228+
-------
229+
230+
* Odoo Data Flow
231+
232+
Contributors
233+
------------
234+
235+
- bosd
236+
237+
Other credits
238+
-------------
239+
240+
241+
242+
Maintainers
243+
-----------
244+
245+
This module is part of the `OdooDataFlow/addons <https://github.com/OdooDataFlow/addons/tree/18.0/product_template_attribute_value_xmlid>`_ project on GitHub.
246+
247+
You are welcome to contribute.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2025 Odoo Data Flow
3+
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
4+
5+
# Please do not inherit from this module
6+
from . import models
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2025 Odoo Data Flow
3+
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
4+
5+
{
6+
'name': 'Generate XML IDs for Product Template Attribute Values',
7+
'version': '18.0.1.0.0',
8+
'summary': 'Generates predictable XML IDs for product.template.attribute.value records based on template and attribute values. These can be used to generate predictable combinations for product.product records.',
9+
'description': 'This module generates XML IDs for product.template.attribute.value records after importing product.template.attribute.line.',
10+
'author': 'Odoo Data Flow',
11+
'website': 'https://github.com/OdooDataFlow/addons',
12+
'category': 'Technical Settings',
13+
'version': '18.0.1.0.0',
14+
'depends': [
15+
'product',
16+
'sale_management',
17+
18+
],
19+
# 'excludes': ['product_barcodelookup',],
20+
'data': ['views/product_template_attribute_view.xml'],
21+
'license': 'LGPL-3',
22+
'installable': True,
23+
'cloc_exclude': [
24+
]
25+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import product_template_attribute_value
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from odoo import models, api, _
2+
3+
class ProductTemplateAttributeValueXMLID(models.Model):
4+
_inherit = 'product.template.attribute.value'
5+
6+
@api.model_create_multi
7+
def create(self, vals_list):
8+
"""
9+
Overrides the create method to automatically generate XML IDs.
10+
"""
11+
records = super().create(vals_list)
12+
for record in records:
13+
self._generate_xmlid_for_record(record) # Generate XML ID for each new record
14+
return records
15+
16+
def _generate_xmlid_for_record(self, attribute_value):
17+
"""
18+
Generates the XML ID for a given product.template.attribute.value record.
19+
"""
20+
# 2. Generate the XML ID using XML IDs of related records
21+
prod_templ_id = self._get_xml_id(attribute_value.product_tmpl_id) if attribute_value.product_tmpl_id else ''
22+
attribute_id = self._get_xml_id(attribute_value.attribute_id) if attribute_value.attribute_id else ''
23+
attribute_value_id = self._get_xml_id(attribute_value.product_attribute_value_id) if attribute_value.product_attribute_value_id else ''
24+
25+
xml_id = f"{prod_templ_id}_{attribute_value_id}".replace(" ", "_")
26+
27+
# 3. Create the ir.model.data record
28+
try:
29+
self.env['ir.model.data'].create({
30+
'module': 'product_template_attribute_value',
31+
'name': xml_id,
32+
'model': 'product.template.attribute.value',
33+
'res_id': attribute_value.id,
34+
'noupdate': True,
35+
})
36+
except Exception as e:
37+
self.env.cr.rollback() # Rollback in case of error
38+
self.env.cr.commit()
39+
self.env.logger.error(f"Error creating XML ID for product.template.attribute.value: {e}")
40+
41+
@api.model
42+
def generate_attribute_value_xmlids(self, *args):
43+
"""
44+
Generates XML IDs for product.template.attribute.value records
45+
using the format: XMLID = prod_templ_id + attribute_id + attribute_value_id
46+
"""
47+
# 1. Get the product.template.attribute.value records that DO NOT have an XML ID
48+
attribute_values_without_xmlid = self.env['product.template.attribute.value'].search([
49+
('id', 'not in', self.env['ir.model.data'].search([('model', '=', 'product.template.attribute.value')]).mapped('res_id'))
50+
])
51+
52+
for attribute_value in attribute_values_without_xmlid:
53+
self._generate_xmlid_for_record(attribute_value)
54+
55+
return {'type': 'ir.actions.client', 'tag': 'display_notification', 'params': {'title': _('XML IDs Created'), 'message': _('XML IDs for product.template.attribute.value records have been generated.'), 'type': 'success'}}
56+
57+
def _get_xml_id(self, record):
58+
"""Helper function to get the XML ID of a record."""
59+
if not record:
60+
return ''
61+
xml_ids = self.env['ir.model.data'].search([
62+
('model', '=', record._name),
63+
('res_id', '=', record.id),
64+
], limit=1)
65+
return xml_ids.name if xml_ids else ''
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build-system]
2+
requires = ["whool"]
3+
build-backend = "whool.buildapi"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
To configure this module, you need to:
3+
4+
1. Ensure that the `product` module is installed in your Odoo instance, as this module is a dependency.
5+
2. Place the `product_template_attribute_value_xmlid` module directory in your Odoo addons path.
6+
3. Install the `product_template_attribute_value_xmlid` module through the Odoo Apps interface.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Purpose:
2+
3+
The module ensures that every product.template.attribute.value record has a predictable and consistent XML ID, which is crucial for:
4+
5+
Data Integration: Facilitating seamless data exchange with external systems that rely on stable identifiers.
6+
Automation: Simplifying automated processes within Odoo that need to reference specific attribute value combinations.
7+
8+
## Use Cases:
9+
10+
E-commerce platforms that need to synchronize product variant data with Odoo.
11+
Warehouse management systems that rely on unique identifiers for product configurations.
12+
Any scenario where stable and predictable identifiers for product attribute combinations are required.
13+
This description provides a good overview of the module's purpose and functionality.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- bosd

product_template_attribute_value_xmlid/readme/CREDITS.md

Whitespace-only changes.

0 commit comments

Comments
 (0)