File: product.py

package info (click to toggle)
tryton-modules-product-classification 5.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 320 kB
  • sloc: python: 186; xml: 72; makefile: 6; sh: 3
file content (79 lines) | stat: -rw-r--r-- 2,414 bytes parent folder | download | duplicates (2)
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
68
69
70
71
72
73
74
75
76
77
78
79
# 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.model import (ModelStorage, ModelView, DeactivableMixin, fields,
    tree)
from trytond.pool import PoolMeta, Pool
from trytond.pyson import Eval

__all__ = ['ClassificationMixin', 'classification_tree',
    'Template', 'Product', 'ClassificationDummy', 'ClassificationTreeDummy']


class ClassificationMixin(DeactivableMixin):
    name = fields.Char('Name', translate=True, required=True)
    selectable = fields.Boolean('Selectable', select=True)

    @classmethod
    def default_selectable(cls):
        return True


def classification_tree(name):
    'Return a ClassificationMixin with tree structure'

    class ClassificationTreeMixin(tree(separator=' / '), ClassificationMixin):
        parent = fields.Many2One(name, 'Parent', select=True)
        childs = fields.One2Many(name, 'parent', 'Children')

    return ClassificationTreeMixin


class Template(metaclass=PoolMeta):
    __name__ = 'product.template'

    classification = fields.Reference('Classification',
        selection='get_classification',
        domain=[
            ('selectable', '=', True),
            ],
        states={
            'readonly': ~Eval('active', True),
            },
        depends=['active'])

    @classmethod
    def _get_classification(cls):
        'Return list of Model names for classification Reference'
        return []

    @classmethod
    def get_classification(cls):
        pool = Pool()
        Model = pool.get('ir.model')
        models = cls._get_classification()
        models = Model.search([
                ('model', 'in', models),
                ])
        return [(None, '')] + [(m.model, m.name) for m in models]


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

    @classmethod
    def get_classification(cls):
        pool = Pool()
        Template = pool.get('product.template')
        return Template.get_classification()


class ClassificationDummy(ClassificationMixin, ModelStorage, ModelView):
    'Dummy Product Classification'
    __name__ = 'product.classification.dummy'


class ClassificationTreeDummy(
        classification_tree('product.classification_tree.dummy'),
        ModelStorage, ModelView):
    'Dummy Product Classification Tree'
    __name__ = 'product.classification_tree.dummy'