1
# -*- encoding: utf-8 -*-
2
###############################################################################
4
# product_custom_attributes for OpenERP #
5
# Copyright (C) 2011 Akretion Benoît GUILLOT <benoit.guillot@akretion.com> #
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. #
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. #
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/>. #
20
###############################################################################
22
from openerp.osv.orm import Model
23
from openerp.osv import fields
24
from openerp.osv.osv import except_osv
26
from lxml import etree
27
from openerp.tools.translate import _
31
class product_product(Model):
32
_inherit = "product.product"
34
def _build_shop_attributes_notebook(self, cr, uid, shops, context=None):
35
notebook = etree.Element('notebook', name="shop_attributes_notebook", colspan="4")
38
page = etree.SubElement(notebook, 'page', string=shop.name.capitalize())
39
for attribute in shop.shop_attribute_ids:
40
toupdate_fields.append(attribute.name)
41
self._build_attribute_field(cr, uid, page, attribute, context=context)
42
return notebook, toupdate_fields
44
def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
45
result = super(product_product, self).fields_view_get(cr, uid, view_id,view_type,context,toolbar=toolbar, submenu=submenu)
46
if view_type == 'form':
47
eview = etree.fromstring(result['arch'])
48
info_page = eview.xpath("//page[@string='Information']")
50
info_page = info_page[0]
51
shop_obj = self.pool.get('sale.shop')
52
shop_ids = shop_obj.search(cr, uid, [], context=context)
54
for shop in shop_obj.browse(cr, uid, shop_ids, context=context):
55
if shop.shop_attribute_ids:
58
attributes_notebook, toupdate_fields = self._build_shop_attributes_notebook(cr, uid, shops, context=context)
59
result['fields'].update(self.fields_get(cr, uid, toupdate_fields, context))
60
main_page = etree.Element('page', string=_('Shop Attributes'))
61
main_page.append(attributes_notebook)
62
info_page.addnext(main_page)
63
result['arch'] = etree.tostring(eview, pretty_print=True)
64
result = self._fix_size_bug(cr, uid, result, context=context)
67
def check_if_activable(self, cr, uid, vals, context=None):
68
categ_ids = [vals['categ_id']] + vals.get('categ_ids', [])
69
for key in vals.keys():
70
if re.match('x_shop.*?_attr_active', key) and vals[key]:
71
shop_id = int(key.replace('x_shop', '').replace('_attr_active', ''))
72
if not self.pool.get('product.category').check_if_in_shop_category(cr, uid, categ_ids, shop_id, context=context):
73
shop = self.pool.get('sale.shop').browse(cr, uid, shop_id, context=context)
76
_("The product must be in an children of one of this categories \"%s\" "
77
"in order to be activable on the shop \"%s\"")
78
%('", "'.join([categ.name for categ in shop.exportable_root_category_ids]), shop.name))
80
def create(self, cr, uid, vals, context=None):
81
if context is None: context={}
82
if not context.get('do_not_check_active_field_on_shop'):
83
vals['categ_ids'] = vals.get('categ_ids', [(6,0,[])])[0][2]
84
self.check_if_activable(cr, uid, vals, context=context)
85
return super(product_product, self).create(cr, uid, vals, context=context)
87
def write(self, cr, uid, ids, vals, context=None):
88
if context is None: context={}
90
if not context.get('do_not_check_active_field_on_shop'):
91
for key in vals.keys():
92
if re.match('x_shop.*?_attr_active', key) and vals[key] or key in ('categ_id', 'categ_ids'):
95
res = super(product_product, self).write(cr, uid, ids, vals, context=context)
97
if not hasattr(ids, '__iter__'):
99
field_to_read = ['categ_ids', 'categ_id'] + [key for key in self._columns if re.match('x_shop.*?_attr_active', key)]
100
for product in self.read(cr, uid, ids, field_to_read, context=context):
101
product['categ_id'] = product['categ_id'][0]
102
self.check_if_activable(cr, uid, product, context=context)
105
class product_category(Model):
106
_inherit = 'product.category'
108
def check_if_in_shop_category(self, cr, uid, categ_ids, shop_id, context=None):
109
exportable_category_ids = self.pool.get('sale.shop').read(cr, uid, shop_id, ['exportable_category_ids'])['exportable_category_ids']
110
for categ_id in categ_ids:
111
if categ_id in exportable_category_ids: