File: modelstorage.py

package info (click to toggle)
tryton-server 7.0.43-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,760 kB
  • sloc: python: 53,744; xml: 5,194; sh: 803; sql: 217; makefile: 28
file content (225 lines) | stat: -rw-r--r-- 7,720 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# 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 ModelSQL
from trytond.model import ModelStorage as ModelStorage_
from trytond.model import fields
from trytond.pool import Pool
from trytond.pyson import Eval, If
from trytond.transaction import Transaction


class ModelStorage(ModelSQL):
    'Model stored'
    __name__ = 'test.modelstorage'
    name = fields.Char('Name')


class ModelStorageRequired(ModelSQL):
    'Model stored'
    __name__ = 'test.modelstorage.required'
    name = fields.Char('Name', required=True)


class ModelStorageSaveMany2One(ModelSQL):
    "ModelStorage to test save with Many2One"
    __name__ = 'test.modelstorage.save_m2o'
    target = fields.Many2One('test.modelstorage.save_m2o.target', "Target")
    targets = fields.One2Many(
        'test.modelstorage.save_m2o.target', 'parent', "Targets")


class ModelStorageSaveMany2OneTarget(ModelSQL):
    "ModelStorage to test save with Many2One - Target"
    __name__ = 'test.modelstorage.save_m2o.target'

    parent = fields.Many2One('test.modelstorage.save_m2o', "Parent")
    target = fields.Many2One(
        'test.modelstorage.save_m2o.o2m_target.target', "Target")


class ModelStorageSaveMany2OneTargetTarget(ModelSQL):
    "ModelStorage to test save with Many2One - Target of Target"
    __name__ = 'test.modelstorage.save_m2o.o2m_target.target'


class ModelStorageSave2Many(ModelSQL):
    "Model Storage to test save with xxx2many"
    __name__ = 'test.modelstorage.save2many'
    targets = fields.One2Many(
        'test.modelstorage.save2many.target', 'parent', "Targets")
    m2m_targets = fields.Many2Many(
        'test.modelstorage.save2many.relation', 'parent', 'target', "Targets")


class ModelStorageSave2ManyTarget(ModelSQL):
    "Model Storage to test save with xxx2many"
    __name__ = 'test.modelstorage.save2many.target'
    parent = fields.Many2One('test.modelstorage.save2many', "Parent")


class ModelStorageSave2ManyRelation(ModelSQL):
    "Model Storage to test save with xxx2many"
    __name__ = 'test.modelstorage.save2many.relation'
    parent = fields.Many2One('test.modelstorage.save2many', "Parent")
    target = fields.Many2One('test.modelstorage.save2many.target', "Target")


class ModelStorageContext(ModelSQL):
    'Model Storage to test Context'
    __name__ = 'test.modelstorage.context'
    context = fields.Function(fields.Binary('Context'), 'get_context')

    def get_context(self, name):
        return Transaction().context


class ModelStoragePYSONDomain(ModelSQL):
    "Model stored with PYSON domain"
    __name__ = 'test.modelstorage.pyson_domain'
    constraint = fields.Char("Constraint")
    value = fields.Char(
        "Value",
        domain=[
            ('value', '=', Eval('constraint')),
            ],
        depends=['constraint'])


class ModelStorageRelationDomain(ModelSQL):
    "Model stored containing a relation field with a domain"
    __name__ = 'test.modelstorage.relation_domain'
    relation = fields.Many2One(
        'test.modelstorage.relation_domain.target', "Value",
        domain=[
            ('value', '=', 'valid'),
            ])
    relation_valid = fields.Boolean("Relation Valid")
    relation_pyson = fields.Many2One(
        'test.modelstorage.relation_domain.target', "Value",
        domain=[
            If(Eval('relation_valid', True),
                ('value', '=', 'valid'),
                ('value', '!=', 'valid')),
            ],
        depends=['relation_valid'])
    relation_context = fields.Many2One(
        'test.modelstorage.relation_domain.target', "Value",
        domain=[
            ('valid', '=', True),
            ],
        context={
            'valid': Eval('relation_valid', True),
            })
    relation_pyson_context = fields.Many2One(
        'test.modelstorage.relation_domain.target', "Value",
        domain=[
            If(Eval('relation_valid', True),
                ('valid', '=', True),
                ('valid', '=', None)),
            ],
        context={
            'valid': Eval('relation_valid', True),
            },
        depends=['relation_valid'])


class ModelStorageRelationDomainTarget(ModelSQL):
    "Target of Model stored containing a relation field with a domain"
    __name__ = 'test.modelstorage.relation_domain.target'
    value = fields.Char("Value")
    valid = fields.Function(
        fields.Boolean("Valid"), 'get_valid', searcher='search_valid')

    def get_valid(self, name):
        return Transaction().context.get('valid')

    @classmethod
    def search_valid(cls, name, domain):
        if Transaction().context.get('valid') == domain[2]:
            return []
        else:
            return [('id', '=', -1)]


class ModelStorageRelationMultiDomain(ModelSQL):
    "Model stored containing a relation fields with multi domain"
    __name__ = 'test.modelstorage.relation_multi_domain'
    relation = fields.Many2One(
        'test.modelstorage.relation_multi_domain.target', "Value",
        domain=[
            ('test1', '=', True),
            ('test2', '=', True),
            ])


class ModelStorageRelationMultiDomainTarget(ModelSQL):
    "Target of Model stored containing a relation field with multi domain"
    __name__ = 'test.modelstorage.relation_multi_domain.target'
    test1 = fields.Boolean("Test 1")
    test2 = fields.Boolean("Test 2")


class ModelStorageRelationDomain2(ModelSQL):
    "Model stored containing a relation field with a domain with 2 level"
    __name__ = 'test.modelstorage.relation_domain2'
    relation = fields.Many2One(
        'test.modelstorage.relation_domain2.target', "Relation",
        domain=[
            ('relation2.value', '=', 'valid'),
            ])


class ModelStorageRelationDomain2Target(ModelSQL):
    "First Target of Model stored containing a relation field with a domain"
    __name__ = 'test.modelstorage.relation_domain2.target'
    relation2 = fields.Many2One(
        'test.modelstorage.relation_domain.target', "Relation 2")


class ModelStorageEvalEnvironment(ModelStorage_):
    "Model for EvalEnvironment"
    __name__ = 'test.modelstorage.eval_environment'
    char = fields.Char("Name")
    reference = fields.Reference(
        "Reference", [
            ('test.modelstorage.eval_environment', "Reference"),
            ])
    multiselection = fields.MultiSelection([
            ('value1', "Value1"),
            ('value2', "Value2"),
            ], "MultiSelection")
    many2one = fields.Many2One(
        'test.modelstorage.eval_environment', "Many2One")
    one2many = fields.One2Many(
        'test.modelstorage.eval_environment', 'many2one', "One2Many")


class ModelStorageDomainNotRequired(ModelSQL):
    "Model for domain on not required field"
    __name__ = 'test.modelstorage.domain_not_required'

    domain_not_required = fields.Integer(
        "Domain Not Required", domain=[('domain_not_required', '>', 0)])


def register(module):
    Pool.register(
        ModelStorage,
        ModelStorageRequired,
        ModelStorageSaveMany2One,
        ModelStorageSaveMany2OneTarget,
        ModelStorageSaveMany2OneTargetTarget,
        ModelStorageSave2Many,
        ModelStorageSave2ManyTarget,
        ModelStorageSave2ManyRelation,
        ModelStorageContext,
        ModelStoragePYSONDomain,
        ModelStorageRelationDomain,
        ModelStorageRelationDomainTarget,
        ModelStorageRelationMultiDomain,
        ModelStorageRelationMultiDomainTarget,
        ModelStorageRelationDomain2,
        ModelStorageRelationDomain2Target,
        ModelStorageEvalEnvironment,
        ModelStorageDomainNotRequired,
        module=module, type_='model')