1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# -*- coding: utf-8 -*-
##############################################################################
#
# Author: Guewen Baconnier
# Copyright 2011-2012 Camptocamp SA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import copy
from osv import osv, fields
class Product(osv.osv):
"""Inherit product to use the default code as the Magento SKU.
Copy the default code into the magento_sku field, but doesn't
change it if the product has already been exported to an
external referential."""
_inherit = 'product.product'
_columns = {
'magento_sku': fields.char('Magento SKU', size=64, readonly=True),
}
_sql_constraints = [
('code_uniq', 'unique(default_code)', 'The code must be unique')
]
def _get_sku(self, cr, uid, vals, product=None, context=None):
"""
Returns the SKU value.
@param vals: vals to be created / modified
@param product: optional browse instance of the product (when writing)
"""
return vals['default_code'].strip()
def create(self, cr, uid, vals, context=None):
if vals.get('default_code'):
vals['magento_sku'] = self._get_sku(cr, uid, vals, context=context)
return super(Product, self).create(cr, uid, vals, context)
def write(self, cr, uid, ids, vals, context=None):
if isinstance(ids, (int, long)):
ids = [ids]
ids_to_write = ids[:]
if vals.get('default_code'):
for product in self.browse(cr, uid, ids, context=context):
# write separately on each product if they
# are not already exported. because the sku value
# will change for each product. Concretely, when a value
# for default_code is given, we should have only one product
# in the write
if not product.magento_exported:
atomic_vals = copy.deepcopy(vals)
atomic_vals['magento_sku'] = self._get_sku(
cr, uid, atomic_vals, product=product, context=context)
super(Product, self).write(
cr, uid, [product.id], atomic_vals, context=context)
ids_to_write.remove(product.id)
return super(Product, self).write(
cr, uid, ids_to_write, vals, context=context)
def copy(self, cr, uid, id, default=None, context=None):
if not default is None: default = {}
default['default_code'] = False
return super(Product, self).copy(
cr, uid, id, default=default, context=context)
Product()
|