1
# -*- encoding: utf-8 -*-
2
#########################################################################
3
# Copyright (C) 2009 Sharoon Thomas, Open Labs Business solutions #
4
# Copyright (C) 2011 Akretion Sébastien BEAU sebastien.beau@akretion.com#
6
#This program is free software: you can redistribute it and/or modify #
7
#it under the terms of the GNU General Public License as published by #
8
#the Free Software Foundation, either version 3 of the License, or #
9
#(at your option) any later version. #
11
#This program is distributed in the hope that it will be useful, #
12
#but WITHOUT ANY WARRANTY; without even the implied warranty of #
13
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14
#GNU General Public License for more details. #
16
#You should have received a copy of the GNU General Public License #
17
#along with this program. If not, see <http://www.gnu.org/licenses/>. #
18
#########################################################################
25
from osv import osv,fields
26
from tools.translate import _
28
class product_product(osv.osv):
29
_inherit = "product.product"
31
def copy(self, cr, uid, id, default=None, context=None):
34
original = self.read(cr, uid, id, fields=['default_code', 'image_ids'], context=context)
36
'default_code': original['default_code'] + '.copy',
39
local_media_repository = self.pool.get('res.company').get_local_media_repository(cr, uid, context=context)
40
if local_media_repository:
41
if original['image_ids']:
42
old_path = os.path.join(local_media_repository, original['default_code'])
43
if os.path.isdir(old_path):
45
shutil.copytree(old_path, old_path+'.copy')
47
logger = logging.getLogger('product_images_olbs')
48
logger.exception('error while trying to copy images from %s to %s', old_path, old_path+'.copy')
50
return super(product_product, self).copy(cr, uid, id, default, context=context)
52
def get_main_image(self, cr, uid, id, context=None):
53
if isinstance(id, list):
55
images_ids = self.read(cr, uid, id, ['image_ids'], context=context)['image_ids']
60
def _get_main_image(self, cr, uid, ids, field_name, arg, context=None):
62
img_obj = self.pool.get('product.images')
64
image_id = self.get_main_image(cr, uid, id, context=context)
66
image = img_obj.browse(cr, uid, image_id, context=context)
73
'image_ids':fields.one2many(
78
'default_code': fields.char('Reference', size=64, require='True'),
79
'product_image': fields.function(_get_main_image, type="binary", method=True),
82
def write(self, cr, uid, ids, vals, context=None):
83
if isinstance(ids, (int, long)):
85
# here we expect that the write on default_code is always on 1 product because there is an unique constraint on the default code
86
if vals.get('default_code', False) and ids:
87
local_media_repository = self.pool.get('res.company').get_local_media_repository(cr, uid, context=context)
88
if local_media_repository:
89
old_product = self.read(cr, uid, ids[0], ['default_code', 'image_ids'], context=context)
90
res = super(product_product, self).write(cr, uid, ids, vals, context=context)
91
if old_product['image_ids']:
92
if old_product['default_code'] != vals['default_code']:
93
old_path = os.path.join(local_media_repository, old_product['default_code'])
94
new_path = os.path.join(local_media_repository, vals['default_code'])
95
if os.path.isdir(old_path):
96
if not os.path.isdir(os.path.dirname(new_path)):
97
os.makedirs(os.path.dirname(new_path))
98
if os.path.isdir(new_path):
99
shutil.rmtree(new_path)
100
shutil.copytree(old_path, new_path)
101
shutil.rmtree(old_path)
103
return super(product_product, self).write(cr, uid, ids, vals, context=context)
105
#This constraint should be by default in openerp
106
_sql_constraints = [('default_code', 'UNIQUE(default_code)',
107
_('Default code should be uniq'))]
109
def create_image_from_url(self, cr, uid, id, url, image_name=None, context=None):
110
(filename, header) = urllib.urlretrieve(url)
111
f = open(filename , 'rb')
114
img = base64.encodestring(data)
115
filename, extention = os.path.splitext(os.path.basename(url))
116
data = {'name': image_name or filename,
117
'extention': extention,
121
new_image_id = self.pool.get('product.images').create(cr, uid, data, context=context)