Skip to content

Commit

Permalink
[MIG] product_attribute_types
Browse files Browse the repository at this point in the history
  • Loading branch information
umithan-guldemir committed Jan 27, 2025
1 parent b25c4e6 commit 49417d7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 42 deletions.
2 changes: 1 addition & 1 deletion product_attribute_types/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

{
"name": "Product Attribute Types",
"version": "12.0",
"version": "16.0",
"license": "AGPL-3",
"depends": [
"product",
Expand Down
99 changes: 67 additions & 32 deletions product_attribute_types/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright 2014-2016 Mikel Arregui - AvanzOSC
# Copyright 2014-2016 Oihane Crucelaegui - AvanzOSC
# Copyright 2016 Pedro M. Baeza <[email protected]>
# Copyright 2025 Ümithan Güldemir <[email protected]>
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from odoo import api, fields, models
Expand All @@ -13,67 +14,101 @@ class ProductAttribute(models.Model):
attr_type = fields.Selection(
required=True,
string="Type",
default='select',
selection=[
('select', 'Select'),
('range', 'Range'),
('numeric', 'Numeric')
],
default="select",
selection=[("select", "Select"), ("range", "Range"), ("numeric", "Numeric")],
)


class ProductAttributeLine(models.Model):
_inherit = "product.template.attribute.line"

default = fields.Many2one('product.attribute.value', 'Default')
attr_type = fields.Selection(string='Type', store=False,
related='attribute_id.attr_type')
default = fields.Many2one("product.attribute.value", "Default")
attr_type = fields.Selection(
string="Type", store=False, related="attribute_id.attr_type"
)


class ProductAttributeValue(models.Model):
_inherit = "product.attribute.value"

attr_type = fields.Selection(string='Type',
related='attribute_id.attr_type')
numeric_value = fields.Float('Numeric Value', digits=(12, 6))
min_range = fields.Float('Min', digits=(12, 6))
max_range = fields.Float('Max', digits=(12, 6))
attr_type = fields.Selection(string="Type", related="attribute_id.attr_type")
numeric_value = fields.Float("Numeric Value", digits=(12, 6))
min_range = fields.Float("Min", digits=(12, 6))
max_range = fields.Float("Max", digits=(12, 6))

@api.onchange('name')
@api.onchange("name")
def onchange_name(self):
if self.attr_type == 'numeric':
if self.attr_type == "numeric":
try:
self.numeric_value = float((''.join([c for c in self.name if c in '1234567890,.'])).replace(',', '.'))
self.attribute_code = (''.join([c for c in self.name if c in '1234567890,.'])).replace(',', '').replace('.', '')
self.numeric_value = float(
("".join([c for c in self.name if c in "1234567890,."])).replace(
",", "."
)
)
self.attribute_code = (
("".join([c for c in self.name if c in "1234567890,."]))
.replace(",", "")
.replace(".", "")
)
except Exception:
pass

@api.one
def write(self, vals):
if vals.get('name',False):
if vals.get('attr_type') == 'numeric' or self.attr_type == 'numeric':
if vals.get('numeric_value',0.0) == 0.0 or self.numeric_value == 0.00:
if vals.get("name", False):
if vals.get("attr_type") == "numeric" or self.attr_type == "numeric":
if vals.get("numeric_value", 0.0) == 0.0 or self.numeric_value == 0.00:
try:
vals['numeric_value'] = float((''.join([c for c in vals.get('name','') if c in '1234567890,.'])).replace(',', '.'))
vals["numeric_value"] = float(
(
"".join(
[
c
for c in vals.get("name", "")
if c in "1234567890,."
]
)
).replace(",", ".")
)
except Exception:
pass
return super(ProductAttributeValue, self).write(vals)

@api.model
def create(self, vals):
create_vals = super(ProductAttributeValue, self).create(vals)
if vals.get('name',False):
if create_vals['attr_type'] == 'numeric':
if create_vals['numeric_value'] == 0.0:
def create(self, vals_list):
create_vals = super(ProductAttributeValue, self).create(vals_list)
if vals_list.get("name", False):
if create_vals["attr_type"] == "numeric":
if create_vals["numeric_value"] == 0.0:
try:
create_vals['numeric_value'] = float((''.join([c for c in vals.get('name','') if c in '1234567890,.'])).replace(',', '.'))
create_vals["numeric_value"] = float(
(
"".join(
[
c
for c in vals_list.get("name", "")
if c in "1234567890, ."
]
)
).replace(",", ".")
)
except Exception:
pass

if create_vals['numeric_value'] != 0.0:
if create_vals["numeric_value"] != 0.0:
try:
create_vals['attribute_code'] = (''.join([c for c in vals.get('name','') if c in '1234567890,.'])).\
replace(',','').replace('.', '')
create_vals["attribute_code"] = (
(
"".join(
[
c
for c in vals_list.get("name", "")
if c in "1234567890, ."
]
)
)
.replace(",", "")
.replace(".", "")
)
except Exception:
pass

Expand Down
16 changes: 7 additions & 9 deletions product_attribute_types/views/product_view.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>

<record model="ir.ui.view" id="varitants_extend_form_view">
<record model="ir.ui.view" id="variants_extend_form_view">
<field name="name">product.template.variants.extended</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_only_form_view" />
Expand All @@ -27,17 +27,15 @@
<record model="ir.ui.view"
id="product_attribute_value_extended_tree_view">
<field name="name">product.attribute.value.extended.tree</field>
<field name="model">product.attribute.value</field>
<field name="inherit_id" ref="product.variants_tree_view" />
<field name="model">product.attribute</field>
<field name="inherit_id" ref="product.product_attribute_view_form" />
<field name="arch" type="xml">
<field name="attribute_id" position="after">
<field name="attr_type" readonly="1"/>
</field>
<field name="name" position="after">

<xpath expr="//field[@name='value_ids']//field[@name='name']" position="after">
<field name="numeric_value" attrs="{'readonly':[('attr_type','!=','numeric')],'required':[('attr_type','=','numeric')]}" />
<field name="min_range" attrs="{'readonly':[('attr_type','!=','range')],'required':[('attr_type','=','range')]}" />
<field name="max_range" attrs="{'readonly':[('attr_type','!=','range')],'required':[('attr_type','=','range')]}" />
</field>
</xpath>
</field>
</record>

Expand All @@ -52,7 +50,7 @@
<group expand="0" string="Group By">

<filter string="Attribute" name="attribute" domain="[]" context="{'group_by':'attribute_id'}"/>
<filter string="Attribute Type" name="attr_type" domain="[]" context="{'group_by':'attr_type'}"/>
<filter string="Attribute Type" name="attr_type" domain="[]" context="{'group_by':'display_type'}"/>
</group>
</search>
</field>
Expand Down

0 comments on commit 49417d7

Please sign in to comment.