File: product.py

package info (click to toggle)
tryton-modules-product-image-attribute 7.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 332 kB
  • sloc: python: 310; xml: 32; makefile: 11; sh: 3
file content (67 lines) | stat: -rw-r--r-- 2,380 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# This file is part of Tryton.  The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from trytond.i18n import gettext
from trytond.model import fields
from trytond.pool import PoolMeta
from trytond.pyson import Eval


class Product(metaclass=PoolMeta):
    __name__ = 'product.product'

    @property
    def images_used(self):
        for image in super().images_used:
            if not image.product and image.attributes:
                if not self.attributes:
                    continue
                for key, value in image.attributes.items():
                    if value != self.attributes.get(key):
                        break
                else:
                    yield image
            else:
                yield image


class Image(metaclass=PoolMeta):
    __name__ = 'product.image'

    attributes = fields.Dict('product.attribute', "Attributes",
        domain=[
            ('sets', '=', Eval('attribute_set', -1)),
            ],
        states={
            'invisible': ~Eval('attribute_set') | Eval('product'),
            })
    attributes_name = fields.Function(fields.Char(
            "Attributes Name",
            states={
                'invisible': ~Eval('attribute_set'),
                }),
        'on_change_with_attributes_name')

    attribute_set = fields.Function(
        fields.Many2One('product.attribute.set', "Attribute Set"),
        'on_change_with_attribute_set')

    @fields.depends('template', '_parent_template.attribute_set')
    def on_change_with_attribute_set(self, name=None):
        return self.template.attribute_set if self.template else None

    @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))