~openerp-connector-core-editors/openerp-connector/7.0-e-commerce-addons

« back to all changes in this revision

Viewing changes to product_custom_attributes_shop/product.py

  • Committer: Guewen Baconnier
  • Date: 2014-01-15 09:34:09 UTC
  • Revision ID: guewen.baconnier@camptocamp.com-20140115093409-pmehemodjm372daa
[DEL] remove dead modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- encoding: utf-8 -*-
2
 
###############################################################################
3
 
#                                                                             #
4
 
#   product_custom_attributes for OpenERP                                      #
5
 
#   Copyright (C) 2011 Akretion Benoît GUILLOT <benoit.guillot@akretion.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.orm import Model
23
 
from openerp.osv import fields
24
 
from openerp.osv.osv import except_osv
25
 
import netsvc
26
 
from lxml import etree
27
 
from openerp.tools.translate import _
28
 
import re
29
 
 
30
 
 
31
 
class product_product(Model):
32
 
    _inherit = "product.product"
33
 
 
34
 
    def _build_shop_attributes_notebook(self, cr, uid, shops, context=None):
35
 
        notebook = etree.Element('notebook', name="shop_attributes_notebook", colspan="4")
36
 
        toupdate_fields = []
37
 
        for shop in shops:
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
43
 
 
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']")
49
 
            if info_page:
50
 
                info_page = info_page[0]
51
 
                shop_obj = self.pool.get('sale.shop')
52
 
                shop_ids = shop_obj.search(cr, uid, [], context=context)
53
 
                shops = []
54
 
                for shop in shop_obj.browse(cr, uid, shop_ids, context=context):
55
 
                    if shop.shop_attribute_ids:
56
 
                        shops.append(shop)
57
 
                if shops:
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)
65
 
        return result
66
 
 
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)
74
 
                    raise except_osv(
75
 
                        _("User Error"), 
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))
79
 
 
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)
86
 
 
87
 
    def write(self, cr, uid, ids, vals, context=None):
88
 
        if context is None: context={}
89
 
        need_check = False
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'):
93
 
                    need_check = True
94
 
                    break
95
 
        res = super(product_product, self).write(cr, uid, ids, vals, context=context)
96
 
        if need_check:
97
 
            if not hasattr(ids, '__iter__'):
98
 
                ids = [ids]
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)
103
 
        return res
104
 
        
105
 
class product_category(Model):
106
 
    _inherit = 'product.category'
107
 
    
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:
112
 
                return True
113
 
        return False