-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathproduct.py
268 lines (223 loc) · 8.27 KB
/
product.py
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# -*- coding: utf-8 -*-
from datetime import datetime
from datetime import time
from trytond.model import ModelSQL, ModelView, fields
from trytond.pool import PoolMeta, Pool
from trytond.pyson import Eval
from trytond.transaction import Transaction
__metaclass__ = PoolMeta
__all__ = [
'ProductAttributeSet', 'ProductAttributeSelectionOption',
'ProductAttribute', 'ProductAttributeAttributeSet',
'Template', 'ProductProductAttribute', 'Product',
]
ATTRIBUTE_TYPES = [
('boolean', 'Boolean'),
('integer', 'Integer'),
('char', 'Char'),
('float', 'Float'),
('numeric', 'Numeric'),
('date', 'Date'),
('datetime', 'DateTime'),
('selection', 'Selection'),
]
class ProductAttributeSet(ModelSQL, ModelView):
"Product Attribute Set"
__name__ = 'product.attribute.set'
name = fields.Char('Name', required=True, select=True, translate=True)
attributes = fields.Many2Many(
'product.attribute-product.attribute-set',
'attribute_set', 'attribute', 'Attributes'
)
class ProductAttributeSelectionOption(ModelSQL, ModelView):
"Attribute Selection Option"
__name__ = 'product.attribute.selection_option'
name = fields.Char("Name", required=True, select=True, translate=True)
attribute = fields.Many2One(
"product.attribute", "Attribute", required=True, ondelete='CASCADE'
)
class ProductAttribute(ModelSQL, ModelView):
"Product Attribute"
__name__ = 'product.attribute'
sets = fields.Many2Many(
'product.attribute-product.attribute-set',
'attribute', 'attribute_set', 'Sets'
)
name = fields.Char('Name', required=True, select=True, translate=True)
display_name = fields.Char('Display Name', translate=True)
type_ = fields.Selection(
ATTRIBUTE_TYPES, 'Type', required=True, select=True
)
selection = fields.One2Many(
"product.attribute.selection_option", "attribute", "Selection",
states={
'invisible': ~(Eval('type_') == 'selection'),
}
)
def get_rec_name(self, name):
return self.display_name or self.name
@staticmethod
def default_type_():
return 'char'
class ProductAttributeAttributeSet(ModelSQL, ModelView):
"Product Attribute - Set"
__name__ = 'product.attribute-product.attribute-set'
attribute = fields.Many2One(
'product.attribute', 'Attribute',
ondelete='CASCADE', select=True, required=True
)
attribute_set = fields.Many2One(
'product.attribute.set', 'Set',
ondelete='CASCADE', select=True, required=True
)
class Template:
"Template"
__name__ = 'product.template'
attribute_set = fields.Many2One(
'product.attribute.set', 'Set', ondelete='RESTRICT'
)
class ProductProductAttribute(ModelSQL, ModelView):
"Product's Product Attribute"
__name__ = 'product.product.attribute'
product = fields.Many2One(
"product.product", "Product", select=True, required=True,
ondelete='CASCADE'
)
attribute = fields.Many2One(
"product.attribute", "Attribute", required=True, select=True,
domain=[('sets', '=', Eval('attribute_set'))],
depends=['attribute_set'], ondelete='RESTRICT'
)
attribute_type = fields.Function(
fields.Selection(ATTRIBUTE_TYPES, "Attribute Type"),
'get_attribute_type'
)
attribute_set = fields.Function(
fields.Many2One("product.attribute.set", "Attribute Set"),
'get_attribute_set'
)
value = fields.Function(
fields.Char('Attribute Value'),
getter='get_value'
)
value_char = fields.Char(
"Value Char", states={
'required': Eval('attribute_type') == 'char',
'invisible': ~(Eval('attribute_type') == 'char'),
}, depends=['attribute_type']
)
value_numeric = fields.Numeric(
"Value Numeric", states={
'required': Eval('attribute_type') == 'numeric',
'invisible': ~(Eval('attribute_type') == 'numeric'),
}, depends=['attribute_type']
)
value_float = fields.Float(
"Value Float", states={
'required': Eval('attribute_type') == 'float',
'invisible': ~(Eval('attribute_type') == 'float'),
}, depends=['attribute_type']
)
value_selection = fields.Many2One(
"product.attribute.selection_option", "Value Selection",
domain=[('attribute', '=', Eval('attribute'))],
states={
'required': Eval('attribute_type') == 'selection',
'invisible': ~(Eval('attribute_type') == 'selection'),
}, depends=['attribute', 'attribute_type'],
ondelete='RESTRICT'
)
value_boolean = fields.Boolean(
"Value Boolean", states={
'required': Eval('attribute_type') == 'boolean',
'invisible': ~(Eval('attribute_type') == 'boolean'),
}, depends=['attribute_type']
)
value_integer = fields.Integer(
"Value Integer", states={
'required': Eval('attribute_type') == 'integer',
'invisible': ~(Eval('attribute_type') == 'integer'),
}, depends=['attribute_type']
)
value_date = fields.Date(
"Value Date", states={
'required': Eval('attribute_type') == 'date',
'invisible': ~(Eval('attribute_type') == 'date'),
}, depends=['attribute_type']
)
value_datetime = fields.DateTime(
"Value Datetime", states={
'required': Eval('attribute_type') == 'datetime',
'invisible': ~(Eval('attribute_type') == 'datetime'),
}, depends=['attribute_type']
)
@fields.depends('attribute')
def on_change_attribute(self):
self.attribute_type = self.attribute and self.attribute.type_ or None
def get_attribute_type(self, name=None):
"""
Returns type of attribute
"""
return self.attribute.type_
def get_value(self, name=None):
"""
Consolidated method to return attribute value
"""
if self.attribute_type == 'selection':
return self.value_selection.name
if self.attribute_type == 'datetime':
# XXX: Localize to the timezone in context
return self.value_datetime.strftime("%Y-%m-%d %H:%M:%S")
if self.attribute_type == 'date':
return datetime.combine(self.value_date, time()). \
strftime("%Y-%m-%d")
else:
return unicode(getattr(self, 'value_' + self.attribute_type))
def get_attribute_set(self, name=None):
"""
Returns attribute set for corresponding product's template
"""
if self.product and self.product.template and \
self.product.template.attribute_set:
return self.product.template.attribute_set.id
@fields.depends('product')
def on_change_product(self):
if self.product and self.product.template.attribute_set:
self.attribute_set = self.product.template.attribute_set.id
class Product:
"Product"
__name__ = 'product.product'
attributes = fields.One2Many(
"product.product.attribute", "product", "Attributes",
domain=[(
'attribute.sets', '=',
Eval('_parent_template', {}).get('attribute_set',
Eval('attribute_set', -1)
)
)], states={
'readonly': (
~Eval('attribute_set')
& ~Eval('_parent_template', {}).get('attribute_set')
),
}, depends=['attribute_set'], context={
'attribute_set': Eval('attribute_set')
}
)
attribute_set = fields.Function(
fields.Many2One('product.attribute.set', 'Set'),
'get_attribute_set',
)
@classmethod
def get_attribute_set(cls, products, name):
res = {}
attribute_set = Transaction().context.get('attribute_set')
for product in products:
if product.template and \
getattr(product.template, "attribute_set", None):
attribute_set = product.template.attribute_set.id
res[product.id] = attribute_set
return res
@fields.depends('template')
def on_change_with_attribute_set(self, name=None):
Product = Pool().get("product.product")
return Product.get_attribute_set([self], name="attribute_set")[self.id]