Files
tryton/modules/product_attribute/product.py
T

80 lines
3.0 KiB
Python
Raw Normal View History

2014-12-02 12:55:32 +01:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
2021-10-10 22:09:06 +02:00
from trytond.i18n import gettext
2021-12-11 17:58:43 +01:00
from trytond.model import DictSchemaMixin, ModelSQL, ModelView, fields
2013-01-26 15:28:16 +01:00
from trytond.pool import PoolMeta
from trytond.pyson import Eval
2020-03-01 12:33:52 +01:00
class AttributeSet(ModelSQL, ModelView, metaclass=PoolMeta):
2013-01-26 15:28:16 +01:00
"Product Attribute Set"
__name__ = 'product.attribute.set'
2018-03-22 11:08:09 +01:00
name = fields.Char('Name', required=True, translate=True,
help="The main identifier of product attribute set.")
2013-01-26 15:28:16 +01:00
attributes = fields.Many2Many('product.attribute-product.attribute-set',
2018-03-22 11:08:09 +01:00
'attribute_set', 'attribute', 'Attributes',
help="Add attributes to the set.")
2013-01-26 15:28:16 +01:00
2020-03-01 12:33:52 +01:00
class Attribute(DictSchemaMixin, ModelSQL, ModelView, metaclass=PoolMeta):
2013-01-26 15:28:16 +01:00
"Product Attribute"
__name__ = 'product.attribute'
sets = fields.Many2Many('product.attribute-product.attribute-set',
2018-03-22 11:08:09 +01:00
'attribute', 'attribute_set', 'Sets',
help="Add sets to the attribute.")
2013-01-26 15:28:16 +01:00
2020-03-01 12:33:52 +01:00
class AttributeAttributeSet(ModelSQL, metaclass=PoolMeta):
2013-01-26 15:28:16 +01:00
"Product Attribute - Set"
__name__ = 'product.attribute-product.attribute-set'
attribute = fields.Many2One(
'product.attribute', "Attribute",
ondelete='CASCADE', required=True)
attribute_set = fields.Many2One(
'product.attribute.set', "Set",
ondelete='CASCADE', required=True)
2013-01-26 15:28:16 +01:00
2018-06-26 19:11:48 +02:00
class Template(metaclass=PoolMeta):
2013-01-26 15:28:16 +01:00
__name__ = 'product.template'
2018-03-22 11:08:09 +01:00
attribute_set = fields.Many2One('product.attribute.set', 'Attribute Set',
help="Select a set of attributes to apply on the variants.")
2013-01-26 15:28:16 +01:00
2018-06-26 19:11:48 +02:00
class Product(metaclass=PoolMeta):
2013-01-26 15:28:16 +01:00
__name__ = 'product.product'
2013-02-23 18:26:33 +01:00
attributes = fields.Dict('product.attribute', 'Attributes',
2013-01-26 15:28:16 +01:00
domain=[
('sets', '=', Eval('_parent_template', {}).get('attribute_set',
Eval('attribute_set', -1))),
2013-01-26 15:28:16 +01:00
],
2013-03-20 22:50:53 +01:00
states={
'readonly': (~Eval('attribute_set')
& ~Eval('_parent_template', {}).get('attribute_set')),
},
depends={'template'},
2018-03-22 11:08:09 +01:00
help="Add attributes to the variant.")
2021-10-10 22:09:06 +02:00
attributes_name = fields.Function(fields.Char(
"Attributes Name",
states={
'invisible': ~Eval('attribute_set'),
}),
2021-10-10 22:09:06 +02:00
'on_change_with_attributes_name')
@fields.depends('attribute_set', 'attributes')
def on_change_with_attributes_name(self, name=None):
if not self.attribute_set or not self.attributes:
return
def key(attribute):
return getattr(attribute, 'sequence', attribute.name)
values = []
for attribute in sorted(self.attribute_set.attributes, key=key):
if attribute.name in self.attributes:
value = self.attributes[attribute.name]
values.append(gettext('product_attribute.msg_label_value',
label=attribute.string,
value=attribute.format(value)))
return " | ".join(filter(None, values))