734853d3a9737aea4c802449839b27ff4633852c
[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.osv import osv, fields
23
24
25 class product_pricelist(osv.Model):
26     _inherit = "product.pricelist"
27     _columns = {
28         'code': fields.char('Promotionnal Code', size=64, translate=True),
29     }
30
31
32 class product_template(osv.Model):
33     _inherit = ["product.template", "website.seo.metadata"]
34     _order = 'website_published desc, website_sequence desc, name'
35     _name = 'product.template'
36
37     def _website_url(self, cr, uid, ids, field_name, arg, context=None):
38         """ TDE-NOTE: as someone wrote this method without any clue on about what it
39         does, fixing bugs about product_variant_ids not existing will be done
40         based on the weather, the sun and Lilo's feelings about pigs.
41
42         If you see this comment in trunk, this means that the website branch has
43         been merged without any review, which is quite questionable. """
44         res = dict.fromkeys(ids, '')
45         base_url = self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url')
46         for product in self.browse(cr, uid, ids, context=context):
47             if product.product_variant_ids:
48                 res[product.id] = "%s/shop/product/%s/" % (base_url, product.product_variant_ids[0].id)
49         return res
50
51     _columns = {
52         'website_published': fields.boolean('Available in the website'),
53         'website_description': fields.html('Description for the website'),
54         'suggested_product_id': fields.many2one('product.template', 'Suggested For Product'),
55         'suggested_product_ids': fields.one2many('product.template', 'suggested_product_id', 'Suggested Products'),
56         'website_size_x': fields.integer('Size X'),
57         'website_size_y': fields.integer('Size Y'),
58         'website_style_ids': fields.many2many('website.product.style', 'product_website_style_rel', 'product_id', 'style_id', 'Styles'),
59         'website_sequence': fields.integer('Sequence', help="Determine the display order in the Website E-commerce"),
60         'website_url': fields.function(_website_url, string="Website url", type="char"),
61     }
62     _defaults = {
63         'website_size_x': 1,
64         'website_size_y': 1,
65         'website_sequence': 1,
66         'website_published': False,
67     }
68
69     def set_sequence_top(self, cr, uid, ids, context=None):
70         cr.execute('SELECT MAX(website_sequence) FROM product_template')
71         max_sequence = cr.fetchone()[0] or 0
72         return self.write(cr, uid, ids, {'website_sequence': max_sequence + 1}, context=context)
73
74     def set_sequence_bottom(self, cr, uid, ids, context=None):
75         return self.write(cr, uid, ids, {'website_sequence': 0}, context=context)
76
77     def recommended_products(self, cr, uid, ids, context=None):
78         id = ids[0]
79         product_ids = []
80         query = """
81             SELECT      sol.product_id
82             FROM        sale_order_line as my
83             LEFT JOIN   sale_order_line as sol
84             ON          sol.order_id = my.order_id
85             WHERE       my.product_id in (%s)
86             AND         sol.product_id not in (%s)
87             GROUP BY    sol.product_id
88             ORDER BY    COUNT(sol.order_id) DESC
89             LIMIT 10
90         """
91         cr.execute(query, (id, id))
92         for p in cr.fetchall():
93             product_ids.append(p[0])
94
95         # search to apply access rules
96         product_ids = self.search(cr, uid, [("id", "in", product_ids)], limit=3)
97         return self.browse(cr, uid, product_ids)
98
99     def img(self, cr, uid, ids, field='image_small', context=None):
100         return "/website/image?model=%s&field=%s&id=%s" % (self._name, field, ids[0])
101
102
103 class product_product(osv.Model):
104     _inherit = "product.product"
105
106     def _website_url(self, cr, uid, ids, field_name, arg, context=None):
107         res = {}
108         base_url = self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url')
109         for id in ids:
110             res[id] = "%s/shop/product/%s/" % (base_url, id)
111         return res
112
113     _columns = {
114         'website_url': fields.function(_website_url, string="Website url"),
115     }
116
117     def img(self, cr, uid, ids, field='image_small', context=None):
118         temp_id = self.browse(cr, uid, ids[0], context=context).product_tmpl_id.id
119         return "/website/image?model=product.template&field=%s&id=%s" % (field, temp_id)