[MERGE] Sync with website-al main branch
[odoo/odoo.git] / addons / website_sale / models / product_attributes.py
1
2 from openerp.osv import osv, fields
3
4
5 class attributes(osv.Model):
6     _name = "product.attribute"
7
8     def _get_float_max(self, cr, uid, ids, field_name, arg, context=None):
9         result = dict.fromkeys(ids, 0)
10         if ids:
11             cr.execute("""
12                 SELECT attribute_id, MAX(value)
13                 FROM product_attribute_product
14                 WHERE attribute_id in (%s)
15                 GROUP BY attribute_id
16             """ % ",".join(map(str, ids)))
17             result.update(dict(cr.fetchall()))
18         return result
19
20     def _get_float_min(self, cr, uid, ids, field_name, arg, context=None):
21         result = dict.fromkeys(ids, 0)
22         if ids:
23             cr.execute("""
24                 SELECT attribute_id, MIN(value)
25                 FROM product_attribute_product
26                 WHERE attribute_id in (%s)
27                 GROUP BY attribute_id
28             """ % ",".join(map(str, ids)))
29             result.update(dict(cr.fetchall()))
30         return result
31
32     def _get_min_max(self, cr, uid, ids, context=None):
33         result = {}
34         for value in self.pool.get('product.attribute.product').browse(cr, uid, ids, context=context):
35             if value.type == 'float':
36                 result[value.attribute_id.id] = True
37         return result.keys()
38
39     _columns = {
40         'name': fields.char('Name', size=64, translate=True, required=True),
41         'type': fields.selection([('distinct', 'Textual Value'), ('float', 'Numeric Value')], "Type", required=True),
42         'value_ids': fields.one2many('product.attribute.value', 'attribute_id', 'Values'),
43         'product_ids': fields.one2many('product.attribute.product', 'attribute_id', 'Products'),
44         'float_max': fields.function(_get_float_max, type='float', string="Max", store={
45                 'product.attribute.product': (_get_min_max, ['value','attribute_id'], 20),
46             }),
47         'float_min': fields.function(_get_float_min, type='float', string="Min", store={
48                 'product.attribute.product': (_get_min_max, ['value','attribute_id'], 20),
49             }),
50         'visible': fields.boolean('Display Filter on Website'),
51     }
52     _defaults = {
53         'type': 'distinct',
54         'visible': True,
55     }
56
57 class attributes_value(osv.Model):
58     _name = "product.attribute.value"
59     _columns = {
60         'name': fields.char('Value', size=64, translate=True, required=True),
61         'attribute_id': fields.many2one('product.attribute', 'Attribute', required=True),
62         'product_ids': fields.one2many('product.attribute.product', 'value_id', 'Products'),
63     }
64
65 class attributes_product(osv.Model):
66     _name = "product.attribute.product"
67     _order = 'attribute_id, value_id, value'
68     _columns = {
69         'value': fields.float('Numeric Value'),
70         'value_id': fields.many2one('product.attribute.value', 'Textual Value'),
71         'attribute_id': fields.many2one('product.attribute', 'Attribute', required=True),
72         'product_id': fields.many2one('product.template', 'Product', required=True),
73
74         'type': fields.related('attribute_id', 'type', type='selection',
75             selection=[('distinct', 'Distinct'), ('float', 'Float')], string='Type'),
76     }
77
78     def onchange_attribute_id(self, cr, uid, ids, attribute_id, context=None):
79         attribute = self.pool.get('product.attribute').browse(cr, uid, attribute_id, context=context)
80         return {'value': {'type': attribute.type, 'value_id': False, 'value': ''}}
81
82 class product_template(osv.Model):
83     _inherit = "product.template"
84     _columns = {
85         'website_attribute_ids': fields.one2many('product.attribute.product', 'product_id', 'Product Attributes'),
86     }