2f58ca25bfae2340df7ce7da74417e84adb1680a
[odoo/odoo.git] / addons / website_sale / models / product.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2013-Today OpenERP SA (<http://www.openerp.com>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from openerp import tools
23 from openerp.osv import osv, fields
24
25 class product_style(osv.Model):
26     _name = "product.style"
27     _columns = {
28         'name' : fields.char('Style Name', required=True),
29         'html_class': fields.char('HTML Classes'),
30     }
31
32 class product_pricelist(osv.Model):
33     _inherit = "product.pricelist"
34     _columns = {
35         'code': fields.char('Promotional Code'),
36     }
37
38
39 class product_public_category(osv.osv):
40     _name = "product.public.category"
41     _description = "Public Category"
42     _order = "sequence, name"
43
44     _constraints = [
45         (osv.osv._check_recursion, 'Error ! You cannot create recursive categories.', ['parent_id'])
46     ]
47
48     def name_get(self, cr, uid, ids, context=None):
49         if not len(ids):
50             return []
51         reads = self.read(cr, uid, ids, ['name','parent_id'], context=context)
52         res = []
53         for record in reads:
54             name = record['name']
55             if record['parent_id']:
56                 name = record['parent_id'][1]+' / '+name
57             res.append((record['id'], name))
58         return res
59
60     def _name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
61         res = self.name_get(cr, uid, ids, context=context)
62         return dict(res)
63
64     def _get_image(self, cr, uid, ids, name, args, context=None):
65         result = dict.fromkeys(ids, False)
66         for obj in self.browse(cr, uid, ids, context=context):
67             result[obj.id] = tools.image_get_resized_images(obj.image)
68         return result
69
70     def _set_image(self, cr, uid, id, name, value, args, context=None):
71         return self.write(cr, uid, [id], {'image': tools.image_resize_image_big(value)}, context=context)
72
73     _columns = {
74         'name': fields.char('Name', required=True, translate=True),
75         'complete_name': fields.function(_name_get_fnc, type="char", string='Name'),
76         'parent_id': fields.many2one('product.public.category','Parent Category', select=True),
77         'child_id': fields.one2many('product.public.category', 'parent_id', string='Children Categories'),
78         'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of product categories."),
79
80         # NOTE: there is no 'default image', because by default we don't show thumbnails for categories. However if we have a thumbnail
81         # for at least one category, then we display a default image on the other, so that the buttons have consistent styling.
82         # In this case, the default image is set by the js code.
83         # NOTE2: image: all image fields are base64 encoded and PIL-supported
84         'image': fields.binary("Image",
85             help="This field holds the image used as image for the category, limited to 1024x1024px."),
86         'image_medium': fields.function(_get_image, fnct_inv=_set_image,
87             string="Medium-sized image", type="binary", multi="_get_image",
88             store={
89                 'product.public.category': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
90             },
91             help="Medium-sized image of the category. It is automatically "\
92                  "resized as a 128x128px image, with aspect ratio preserved. "\
93                  "Use this field in form views or some kanban views."),
94         'image_small': fields.function(_get_image, fnct_inv=_set_image,
95             string="Smal-sized image", type="binary", multi="_get_image",
96             store={
97                 'product.public.category': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
98             },
99             help="Small-sized image of the category. It is automatically "\
100                  "resized as a 64x64px image, with aspect ratio preserved. "\
101                  "Use this field anywhere a small image is required."),
102     }
103
104 class product_template(osv.Model):
105     _inherit = ["product.template", "website.seo.metadata"]
106     _order = 'website_published desc, website_sequence desc, name'
107     _name = 'product.template'
108     _mail_post_access = 'read'
109
110     def _website_url(self, cr, uid, ids, field_name, arg, context=None):
111         res = dict.fromkeys(ids, '')
112         for product in self.browse(cr, uid, ids, context=context):
113             res[product.id] = "/shop/product/%s" % (product.id,)
114         return res
115
116     _columns = {
117         # TODO FIXME tde: when website_mail/mail_thread.py inheritance work -> this field won't be necessary
118         'website_message_ids': fields.one2many(
119             'mail.message', 'res_id',
120             domain=lambda self: [
121                 '&', ('model', '=', self._name), ('type', '=', 'comment')
122             ],
123             string='Website Comments',
124         ),
125         'website_published': fields.boolean('Available in the website', copy=False),
126         'website_description': fields.html('Description for the website'),
127         'alternative_product_ids': fields.many2many('product.template','product_alternative_rel','src_id','dest_id', string='Alternative Products', help='Appear on the product page'),
128         'accessory_product_ids': fields.many2many('product.product','product_accessory_rel','src_id','dest_id', string='Accessory Products', help='Appear on the shopping cart'),
129         'website_size_x': fields.integer('Size X'),
130         'website_size_y': fields.integer('Size Y'),
131         'website_style_ids': fields.many2many('product.style', string='Styles'),
132         'website_sequence': fields.integer('Sequence', help="Determine the display order in the Website E-commerce"),
133         'website_url': fields.function(_website_url, string="Website url", type="char"),
134         'public_categ_ids': fields.many2many('product.public.category', string='Public Category', help="Those categories are used to group similar products for e-commerce."),
135     }
136
137     def _defaults_website_sequence(self, cr, uid, *l, **kwargs):
138         cr.execute('SELECT MAX(website_sequence)+1 FROM product_template')
139         next_sequence = cr.fetchone()[0] or 0
140         return next_sequence
141
142     _defaults = {
143         'website_size_x': 1,
144         'website_size_y': 1,
145         'website_sequence': _defaults_website_sequence,
146         'website_published': False,
147     }
148
149     def set_sequence_top(self, cr, uid, ids, context=None):
150         cr.execute('SELECT MAX(website_sequence) FROM product_template')
151         max_sequence = cr.fetchone()[0] or 0
152         return self.write(cr, uid, ids, {'website_sequence': max_sequence + 1}, context=context)
153
154     def set_sequence_bottom(self, cr, uid, ids, context=None):
155         cr.execute('SELECT MIN(website_sequence) FROM product_template')
156         min_sequence = cr.fetchone()[0] or 0
157         return self.write(cr, uid, ids, {'website_sequence': min_sequence -1}, context=context)
158
159     def set_sequence_up(self, cr, uid, ids, context=None):
160         product = self.browse(cr, uid, ids[0], context=context)
161         cr.execute("""  SELECT id, website_sequence FROM product_template
162                         WHERE website_sequence > %s AND website_published = %s ORDER BY website_sequence ASC LIMIT 1""" % (product.website_sequence, product.website_published))
163         prev = cr.fetchone()
164         if prev:
165             self.write(cr, uid, [prev[0]], {'website_sequence': product.website_sequence}, context=context)
166             return self.write(cr, uid, [ids[0]], {'website_sequence': prev[1]}, context=context)
167         else:
168             return self.set_sequence_top(cr, uid, ids, context=context)
169
170     def set_sequence_down(self, cr, uid, ids, context=None):
171         product = self.browse(cr, uid, ids[0], context=context)
172         cr.execute("""  SELECT id, website_sequence FROM product_template
173                         WHERE website_sequence < %s AND website_published = %s ORDER BY website_sequence DESC LIMIT 1""" % (product.website_sequence, product.website_published))
174         next = cr.fetchone()
175         if next:
176             self.write(cr, uid, [next[0]], {'website_sequence': product.website_sequence}, context=context)
177             return self.write(cr, uid, [ids[0]], {'website_sequence': next[1]}, context=context)
178         else:
179             return self.set_sequence_bottom(cr, uid, ids, context=context)
180
181 class product_product(osv.Model):
182     _inherit = "product.product"
183
184     def _website_url(self, cr, uid, ids, field_name, arg, context=None):
185         res = {}
186         for product in self.browse(cr, uid, ids, context=context):
187             res[product.id] = "/shop/product/%s" % (product.product_tmpl_id.id,)
188         return res
189
190     _columns = {
191         'website_url': fields.function(_website_url, string="Website url", type="char"),
192     }
193
194 class product_attribute(osv.Model):
195     _inherit = "product.attribute"
196     _columns = {
197         'type': fields.selection([('radio', 'Radio'), ('select', 'Select'), ('color', 'Color'), ('hidden', 'Hidden')], string="Type"),
198     }
199     _defaults = {
200         'type': lambda *a: 'radio',
201     }
202
203 class product_attribute_value(osv.Model):
204     _inherit = "product.attribute.value"
205     _columns = {
206         'color': fields.char("HTML Color Index", help="Here you can set a specific HTML color index (e.g. #ff0000) to display the color on the website if the attibute type is 'Color'."),
207     }