Merge lp:~camptocamp/product-extra-addons/extra-trunk-fix-1017960 into lp:product-extra-addons

Proposed by Alexandre Fayolle - camptocamp
Status: Merged
Approved by: Sébastien BEAU - http://www.akretion.com
Approved revision: 48
Merged at revision: 61
Proposed branch: lp:~camptocamp/product-extra-addons/extra-trunk-fix-1017960
Merge into: lp:product-extra-addons
Diff against target: 129 lines (+36/-14)
2 files modified
product_images_olbs/product.py (+21/-7)
product_images_olbs/product_images.py (+15/-7)
To merge this branch: bzr merge lp:~camptocamp/product-extra-addons/extra-trunk-fix-1017960
Reviewer Review Type Date Requested Status
Sébastien BEAU - http://www.akretion.com Pending
Guewen Baconnier @ Camptocamp Pending
Nicolas Bessi - Camptocamp Pending
Raphaël Valyi - http://www.akretion.com Pending
Review via email: mp+112316@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Sébastien BEAU - http://www.akretion.com (sebastien.beau) wrote :

Hi Alex.
I don't know if the best solution will be to not copy the image. Indeed when you duplicate a product, I am not sure that the customer want to keep the same image, so maybe it's simplier to not copy the image_ids and set it to false.

What do you think?

Thank you

Revision history for this message
Alexandre Fayolle - camptocamp (alexandre-fayolle-c2c) wrote :

My current customer uses the duplicate product to handle the Sales period, and he wants the images to be copied... I can always overload the code for him but I'd rather have the images copied out of the box.

Revision history for this message
Sébastien BEAU - http://www.akretion.com (sebastien.beau) wrote :

Ok I finally agree.
I merge it in rev 61.

Thank you

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'product_images_olbs/product.py'
2--- product_images_olbs/product.py 2012-05-17 12:00:14 +0000
3+++ product_images_olbs/product.py 2012-06-27 10:40:24 +0000
4@@ -16,22 +16,36 @@
5 #You should have received a copy of the GNU General Public License #
6 #along with this program. If not, see <http://www.gnu.org/licenses/>. #
7 #########################################################################
8-from osv import osv,fields
9-from tools.translate import _
10 import os
11+import shutil
12+import logging
13 import unicodedata
14 import base64, urllib
15
16+from osv import osv,fields
17+from tools.translate import _
18+
19 class product_product(osv.osv):
20 _inherit = "product.product"
21
22 def copy(self, cr, uid, id, default=None, context=None):
23 if not default:
24 default = {}
25+ original = self.read(cr, uid, id, fields=['default_code', 'image_ids'], context=context)
26 default.update({
27- 'default_code': False,
28 'images_ids': False,
29 })
30+ local_media_repository = self.pool.get('res.company').get_local_media_repository(cr, uid, context=context)
31+ if local_media_repository:
32+ if original['image_ids']:
33+ old_path = os.path.join(local_media_repository, original['default_code'])
34+ if os.path.isdir(old_path):
35+ try:
36+ shutil.copytree(old_path, old_path + '-copy')
37+ except:
38+ logger = logging.getLogger('product_images_olbs')
39+ logger.exception('error while trying to copy images from %s to %s', old_path, old_path+'.copy')
40+
41 return super(product_product, self).copy(cr, uid, id, default, context=context)
42
43 def get_main_image(self, cr, uid, id, context=None):
44@@ -41,7 +55,7 @@
45 if images_ids:
46 return images_ids[0]
47 return False
48-
49+
50 def _get_main_image(self, cr, uid, ids, field_name, arg, context=None):
51 res = {}
52 img_obj = self.pool.get('product.images')
53@@ -61,12 +75,12 @@
54 'Product Images'
55 ),
56 'product_image': fields.function(_get_main_image, type="binary", method=True),
57- }
58+ }
59
60 def write(self, cr, uid, ids, vals, context=None):
61 if isinstance(ids, (int, long)):
62 ids = [ids]
63- # here we expect that the write on default_code is alwayse on 1 product because there is an unique constraint on the default code
64+ # here we expect that the write on default_code is always on 1 product because there is an unique constraint on the default code
65 if vals.get('default_code', False) and ids:
66 local_media_repository = self.pool.get('res.company').get_local_media_repository(cr, uid, context=context)
67 if local_media_repository:
68@@ -95,5 +109,5 @@
69 new_image_id = self.pool.get('product.images').create(cr, uid, data, context=context)
70 return True
71
72-
73+
74 product_product()
75
76=== modified file 'product_images_olbs/product_images.py'
77--- product_images_olbs/product_images.py 2012-04-28 16:16:47 +0000
78+++ product_images_olbs/product_images.py 2012-06-27 10:40:24 +0000
79@@ -20,7 +20,9 @@
80 import base64, urllib
81 from tools.translate import _
82 import os
83-import netsvc
84+
85+import logging
86+_logger = logging.getLogger(__name__)
87
88 #TODO find a good solution in order to roll back changed done on file system
89 #TODO add the posibility to move from a store system to an other (example : moving existing image on database to file system)
90@@ -30,7 +32,7 @@
91 _name = "product.images"
92 _description = __doc__
93 _table = "product_images"
94-
95+
96 def unlink(self, cr, uid, ids, context=None):
97 if isinstance(ids, (int, long)):
98 ids = [ids]
99@@ -95,19 +97,25 @@
100 with open(filename , 'rb') as f:
101 img = base64.b64encode(f.read())
102 else:
103- full_path = self._image_path(cr, uid, image, context=context)
104+ try:
105+ if isinstance(image.product_id.default_code, bool):
106+ _logger.debug('product not completely setup, no image available')
107+ full_path = False
108+ else:
109+ full_path = self._image_path(cr, uid, image, context=context)
110+ except Exception, e:
111+ _logger.error("Can not find the path for image %s: %s", id, e, exc_info=True)
112+ return False
113 if full_path:
114 if os.path.exists(full_path):
115 try:
116 with open(full_path, 'rb') as f:
117 img = base64.b64encode(f.read())
118 except Exception, e:
119- logger = netsvc.Logger()
120- logger.notifyChannel('product_images', netsvc.LOG_ERROR, "Can not open the image %s, error : %s" %(full_path, e))
121+ _logger.error("Can not open the image %s, error : %s", full_path, e, exc_info=True)
122 return False
123 else:
124- logger = netsvc.Logger()
125- logger.notifyChannel('product_images', netsvc.LOG_ERROR, "The image %s doesn't exist " %full_path)
126+ _logger.error("The image %s doesn't exist ", full_path)
127 return False
128 else:
129 img = image.file_db_store

Subscribers

People subscribed via source and target branches