-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathproduct.py
286 lines (250 loc) · 9.54 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# -*- coding: utf-8 -*-
from functools import partial
from trytond.model import ModelView, ModelSQL, fields, Unique
from trytond.pool import PoolMeta
from trytond.pyson import Eval
from nereid import url_for, current_website
from flask import json
from babel import numbers
__all__ = [
'Template', 'Product', 'ProductVariationAttributes', 'ProductAttribute',
]
__metaclass__ = PoolMeta
class Template:
"Product Template"
__name__ = 'product.template'
variation_attributes = fields.One2Many(
'product.variation_attributes', 'template', 'Required Attributes',
)
@classmethod
def __setup__(cls):
super(Template, cls).__setup__()
cls._error_messages.update({
'missing_attributes':
"Please define following attributes for product %s: %s"
})
def validate_variation_attributes(self):
for product in self.products_displayed_on_eshop:
product.validate_attributes()
@classmethod
def validate(cls, templates):
super(Template, cls).validate(templates)
for template in templates:
template.validate_variation_attributes()
def _get_product_variation_data(self):
"""
This method returns the variation data in a serializable format
for the main API. Extend this module to add data that your
customization may need. In most cases, just extending the serialize
api method in product and variation should be sufficient.
"""
variation_attributes = map(
lambda variation: variation.serialize(),
self.variation_attributes
)
variants = []
for product in self.products_displayed_on_eshop:
variant_data = product.serialize(purpose='variant_selection')
variant_data['attributes'] = {}
for variation in self.variation_attributes:
if variation.attribute.type_ == 'selection':
# Selection option objects are obviously not serializable
# So get the name
variant_data['attributes'][variation.attribute.id] = \
str(
product.get_attribute_value(variation.attribute).id
)
else:
variant_data['attributes'][variation.attribute.name] = \
product.get_attribute_value(variation.attribute)
variants.append(variant_data)
rv = {
'variants': variants,
'variation_attributes': variation_attributes,
}
return rv
def get_product_variation_data(self):
"""
Returns json data for product for variants. The data returned
by this method should be sufficient to render a product selection
interface based on variation data.
The structure of the data returned is::
{
'variants': [
# A list of active records of the variants if not
# requested as JSON. If JSON, the record is serialized
# with type JSON.
{
# see documentation of the serialize method
# on product.product to see values sent.
}
],
'variation_attributes': [
{
# see documentation of the serialize method
# on product.varying_attribute to see values sent.
}
...
]
}
.. tip::
If your downstream module needs more information in the
JSON, subclass and implement _get_product_variation_data
which returns a dictionary. Otherwise, it would require you
to deserialize, add value and then serialize again.
"""
return json.dumps(self._get_product_variation_data())
class Product:
"Product"
__name__ = 'product.product'
@classmethod
def __setup__(cls):
super(Product, cls).__setup__()
cls._error_messages.update({
'missing_attributes':
"Please define following attributes for product %s: %s"
})
def validate_attributes(self):
"""Check if product defines all the attributes specified in
template variation attributes.
"""
if not self.displayed_on_eshop:
return
required_attrs = set(
[v.attribute for v in self.template.variation_attributes]
)
missing = required_attrs - \
set(map(lambda attr: attr.attribute, self.attributes))
if missing:
missing = map(lambda attr: attr.name, missing)
self.raise_user_error(
"missing_attributes",
(self.rec_name, ', '.join(missing))
)
@classmethod
def validate(cls, products):
super(Product, cls).validate(products)
for product in products:
product.validate_attributes()
def get_attribute_value(self, attribute, silent=True):
"""
:param attribute: Active record of attribute
"""
for product_attr in self.attributes:
if product_attr.attribute == attribute:
return getattr(
product_attr,
'value_%s' % attribute.type_
)
else:
if silent:
return True
raise AttributeError(attribute.name)
def serialize(self, purpose=None):
"""
Return serializable dictionary suitable for use with variant
selection.
"""
if purpose != 'variant_selection':
return super(Product, self).serialize(purpose)
currency_format = partial(
numbers.format_currency,
currency=current_website.company.currency.code,
locale=current_website.default_locale.language.code
)
return {
'id': self.id,
'rec_name': self.rec_name,
'name': self.name,
'code': self.code,
'price': currency_format(self.sale_price(1)),
'url': url_for('product.product.render', uri=self.uri),
'image_urls': [
{
'large': (
image.transform_command().thumbnail(500, 500, 'a')
.url()
),
'thumbnail': (
image.transform_command().thumbnail(120, 120, 'a')
.url()
),
'regular': image.url,
}
for image in self.get_images()
],
}
class ProductVariationAttributes(ModelSQL, ModelView):
"Variation attributes for product template"
__name__ = 'product.variation_attributes'
sequence = fields.Integer('Sequence')
template = fields.Many2One('product.template', 'Template', required=True)
attribute = fields.Many2One(
'product.attribute', 'Attribute', required=True,
domain=[('sets', '=',
Eval('_parent_template', {}).get('attribute_set', -1))],
)
widget = fields.Selection([
('dropdown', 'Dropdown'),
('swatches', 'Swatches'),
], 'Widget', required=True)
@staticmethod
def default_widget():
return 'dropdown'
@staticmethod
def default_sequence():
return 10
def serialize(self, purpose=None):
"""
Returns serialized version of the attribute::
{
'sequence': 1, # Integer id to determine order
'name': 'shirt color', # Internal name of the attribute
'display_name': 'Color', # (opt) display name of attr
'rec_name': 'Color', # The name that should be shown
'widget': 'swatch', # clue on how to render widget
'options': [
# id, value of the options available to choose from
(12, 'Blue'),
(13, 'Yellow'),
...
]
}
"""
if self.attribute.type_ == 'selection':
# The attribute type needs options to choose from.
# Send only the options that the products displayed on webshop
# can have, instead of the exhaustive list of attribute options
# the attribute may have.
#
# For example, the color attribute values could be
# ['red', 'yellow', 'orange', 'green', 'black', 'blue']
# but the shirt itself might only be available in
# ['red', 'yellow']
#
# This can be avoided by returning options based on the product
# rather than on the attributes list of values
options = set()
for product in self.template.products_displayed_on_eshop:
value = product.get_attribute_value(self.attribute)
options.add((value.id, value.name))
else:
options = []
return {
'sequence': self.sequence,
'name': self.attribute.name,
'display_name': self.attribute.display_name,
'widget': self.widget,
'options': list(options),
'attribute_id': self.attribute.id,
}
class ProductAttribute:
__name__ = 'product.attribute'
@classmethod
def __setup__(cls):
super(ProductAttribute, cls).__setup__()
table = cls.__table__()
cls._sql_constraints += [
('unique_name', Unique(table, table.name),
'Attribute name must be unique!'),
]