Skip to content

Commit

Permalink
[FIX] attribute_set: target model patch fix and context fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kobros-tech committed Feb 19, 2025
1 parent 204742c commit 5405ab1
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 30 deletions.
8 changes: 4 additions & 4 deletions attribute_set/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ Attributes :
2. You need to **add a placeholder**
``<separator name="attributes_placeholder" />`` at the desired
location in the model's form view.
3. Finally, **add a context** ``{"include_native_attribute": True}`` on
the action leading to this form view if the model's view needs to
display attributes related to native fields together with the other
"custom" attributes.
3. Finally, **add a context**
``{"include_native_attribute_view_ref": True}`` on the action leading
to this form view if the model's view needs to display attributes
related to native fields together with the other "custom" attributes.

Known issues / Roadmap
======================
Expand Down
1 change: 1 addition & 0 deletions attribute_set/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
from . import attribute_set
from . import attribute_set_owner
from . import attribute_group
from . import many2one_override
4 changes: 2 additions & 2 deletions attribute_set/models/attribute_set_owner.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _build_attribute_eview(self):
("model", "=", self._name),
("attribute_set_ids", "!=", False),
]
if not self._context.get("include_native_attribute"):
if not self._context.get("include_native_attribute_view_ref"):
domain.append(("nature", "=", "custom"))

attributes = self.env["attribute.attribute"].search(domain)
Expand Down Expand Up @@ -71,7 +71,7 @@ def _insert_attribute(self, arch):
)
)

if self._context.get("include_native_attribute"):
if self._context.get("include_native_attribute_view_ref"):
self.remove_native_fields(eview)
attribute_eview = self._build_attribute_eview()

Expand Down
22 changes: 22 additions & 0 deletions attribute_set/models/many2one_override.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from odoo.fields import Many2one
from odoo.exceptions import MissingError

# Override the Many2one field
# class Many2oneOverride(fields.Many2one):

def patch_convert_to_read(self, value, record, use_display_name=True):
if use_display_name and value:
try:
return (value.id, value.sudo().display_name)
except MissingError:
return False

Check warning on line 12 in attribute_set/models/many2one_override.py

View check run for this annotation

Codecov / codecov/patch

attribute_set/models/many2one_override.py#L9-L12

Added lines #L9 - L12 were not covered by tests
elif value:
return value.id

Check warning on line 14 in attribute_set/models/many2one_override.py

View check run for this annotation

Codecov / codecov/patch

attribute_set/models/many2one_override.py#L14

Added line #L14 was not covered by tests
# In general odoo assumes use_display_name is True if value is empty
# If use_display_name is False is not in account or if value is empty
# This fix will have a PR in odoo repositoy,
# and if approved this fix code will be removed
else:
return False

Check warning on line 20 in attribute_set/models/many2one_override.py

View check run for this annotation

Codecov / codecov/patch

attribute_set/models/many2one_override.py#L20

Added line #L20 was not covered by tests

Many2one.convert_to_read = patch_convert_to_read
12 changes: 6 additions & 6 deletions attribute_set/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Even if this module does not provide views to display some model's
Attributes, it provides however a Technical menu in *Settings \>
Technical \> Database Structure \> Attributes* to **create new
Attributes, it provides however a Technical menu in _Settings \>
Technical \> Database Structure \> Attributes_ to **create new
Attributes**.

An Attribute is related to both an Attribute Group and an Attribute Set
:

- The **Attribute Set** is related to the *"model's category"*, i.e. all
- The **Attribute Set** is related to the _"model's category"_, i.e. all
the model's instances which will display the same Attributes.

- The **Attribute Group** is related to the *"attribute's category"*.
- The **Attribute Group** is related to the _"attribute's category"_.
All the attributes from the same Attribute Set and Attribute Group
will be displayed under the same field's Group in the model's view.

Expand All @@ -18,7 +18,7 @@ An Attribute is related to both an Attribute Group and an Attribute Set
> Setting **"Advanced Attribute Set settings"**
> (`group_advanced_attribute_set`).
------------------------------------------------------------------------
---

If you want to create a module displaying some specific model's
Attributes :
Expand All @@ -28,7 +28,7 @@ Attributes :
2. You need to **add a placeholder**
`<separator name="attributes_placeholder" />` at the desired
location in the model's form view.
3. Finally, **add a context** `{"include_native_attribute": True}` on
3. Finally, **add a context** `{"include_native_attribute_view_ref": True}` on
the action leading to this form view if the model's view needs to
display attributes related to native fields together with the other
"custom" attributes.
8 changes: 4 additions & 4 deletions attribute_set/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,10 @@ <h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
<li>You need to <strong>add a placeholder</strong>
<tt class="docutils literal">&lt;separator <span class="pre">name=&quot;attributes_placeholder&quot;</span> /&gt;</tt> at the desired
location in the model’s form view.</li>
<li>Finally, <strong>add a context</strong> <tt class="docutils literal">{&quot;include_native_attribute&quot;: True}</tt> on
the action leading to this form view if the model’s view needs to
display attributes related to native fields together with the other
“custom” attributes.</li>
<li>Finally, <strong>add a context</strong>
<tt class="docutils literal">{&quot;include_native_attribute_view_ref&quot;: True}</tt> on the action leading
to this form view if the model’s view needs to display attributes
related to native fields together with the other “custom” attributes.</li>
</ol>
</div>
<div class="section" id="known-issues-roadmap">
Expand Down
10 changes: 6 additions & 4 deletions attribute_set/tests/test_build_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,12 @@ def test_render_all_field_type(self):
self.assertFalse(attr.get("nolabel", False))

# TEST on NATIVE ATTRIBUTES
def _get_eview_from_get_views(self, include_native_attribute=True):
def _get_eview_from_get_views(self, include_native_attribute_view_ref=True):
result = (
self.env["res.partner"]
.with_context(include_native_attribute=include_native_attribute)
.with_context(
include_native_attribute_view_ref=include_native_attribute_view_ref
)
.get_views([(self.view.id, "form")])
)
return etree.fromstring(result["views"]["form"]["arch"])
Expand All @@ -314,8 +316,8 @@ def test_native_readonly(self):
self.assertTrue(attr[0].get("readonly"))

def test_no_include_native_attr(self):
# Run get_views on the test view with no "include_native_attribute"
eview = self._get_eview_from_get_views(include_native_attribute=False)
# Run get_views on the test view with no "include_native_attribute_view_ref"
eview = self._get_eview_from_get_views(include_native_attribute_view_ref=False)
attr = eview.xpath(f"//field[@name='{self.attr_native.name}']")

# Only one field with this name
Expand Down
13 changes: 3 additions & 10 deletions attribute_set/wizard/attribute_option_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class AttributeOptionWizard(models.TransientModel):
"attribute.attribute",
"Product Attribute",
required=True,
default=lambda self: self.env.context.get("attribute_id", False),
# if the context is passed in correctly by suffixing it with _view_ref
# we will no longer need default_get method to assign attribute_id a value
default=lambda self: self.env.context.get("attribute_id_view_ref", False),
ondelete="cascade",
)
option_ids = fields.One2many(
Expand Down Expand Up @@ -52,15 +54,6 @@ def create(self, vals_list):
del vals["option_ids"]
return super().create(vals_list)

@api.model
def default_get(self, fields_list):
res = super().default_get(fields_list)
# Retrieve attribute_id_view_ref from context
context = self.env.context
if context.get("attribute_id_view_ref"):
res["attribute_id"] = context.get("attribute_id_view_ref")
return res

@api.model
def get_views(self, views, options=None):
context = self.env.context
Expand Down

0 comments on commit 5405ab1

Please sign in to comment.