Browse Source

[REF] Product_images_olbs : refactor code, change variable name due to the last change, also add unlink feature and renaming feature when file are stored on file system

sebastien beau 13 years ago
parent
commit
2377b6fefe
  1. 6
      product_images_olbs/company.py
  2. 11
      product_images_olbs/product.py
  3. 81
      product_images_olbs/product_images.py
  4. 9
      product_images_olbs/views/product_images_view.xml

6
product_images_olbs/company.py

@ -33,4 +33,10 @@ class ResCompany(osv.osv):
),
}
def get_local_media_repository(self, cr, uid, id=None, context=None):
if id:
return self.browse(cr, uid, id, context=context).local_media_repository
user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
return user.company_id.local_media_repository
ResCompany()

11
product_images_olbs/product.py

@ -1,5 +1,7 @@
# -*- encoding: utf-8 -*-
#########################################################################
# Copyright (C) 2009 Sharoon Thomas, Open Labs Business solutions #
# Copyright (C) 2011 Akretion Sébastien BEAU sebastien.beau@akretion.com#
# #
#This program is free software: you can redistribute it and/or modify #
#it under the terms of the GNU General Public License as published by #
@ -32,9 +34,8 @@ class product_product(osv.osv):
def write(self, cr, uid, ids, vals, context=None):
#note that write on default code can be only done on one id, if it's multiple id it will raise an error indeed default code should be uniq
if vals.get('default_code', False):
user = self.pool.get('res.users').browse(cr, uid, uid)
company = user.company_id
if company.local_media_repository:
local_media_repository = self.pool.get('res.company').get_local_media_repository(cr, uid, context=context)
if local_media_repository:
if isinstance(ids, list):
id =ids[0]
else:
@ -42,9 +43,9 @@ class product_product(osv.osv):
old_default_code = self.read(cr, uid, id, ['default_code'], context=context)['default_code']
res = super(product_product, self).write(cr, uid, ids, vals, context=context)
if old_default_code != vals['default_code']:
old_path = os.path.join(company.local_media_repository, old_default_code)
old_path = os.path.join(local_media_repository, old_default_code)
if os.path.isdir(old_path):
os.rename(old_path, os.path.join(company.local_media_repository, vals['default_code']))
os.rename(old_path, os.path.join(local_media_repository, vals['default_code']))
return res
return super(product_product, self).write(cr, uid, ids, vals, context=context)

81
product_images_olbs/product_images.py

@ -1,5 +1,7 @@
# -*- encoding: utf-8 -*-
#########################################################################
# Copyright (C) 2009 Sharoon Thomas, Open Labs Business solutions #
# Copyright (C) 2011 Akretion Sébastien BEAU sebastien.beau@akretion.com#
# #
#This program is free software: you can redistribute it and/or modify #
#it under the terms of the GNU General Public License as published by #
@ -20,6 +22,9 @@ from tools.translate import _
import os
import netsvc
#TODO find a good solution in order to roll back changed done on file system
#TODO add the posibility to move from a store system to an other (example : moving existing image on database to file system)
class product_images(osv.osv):
"Products Image gallery"
_name = "product.images"
@ -27,22 +32,59 @@ class product_images(osv.osv):
_table = "product_images"
def unlink(self, cr, uid, ids, context=None):
#TODO
if not isinstance(ids, list):
ids=[ids]
local_media_repository = self.pool.get('res.company').get_local_media_repository(cr, uid, context=context)
if local_media_repository:
for image in self.browse(cr, uid, ids, context=context):
path = os.path.join(local_media_repository, image.product_id.default_code, image.name)
if os.path.isfile(path):
os.remove(path)
return super(product_images, self).unlink(cr, uid, ids, context=context)
def get_image(self, cr, uid, id):
each = self.read(cr, uid, id, ['link', 'filename', 'image', 'product_id', 'name'])
def create(self, cr, uid, vals, context=None):
if vals.get('name', False) and not vals.get('extention', False):
vals['name'], vals['extention'] = os.path.splitext(vals['name'])
return super(product_images, self).create(cr, uid, vals, context=context)
def write(self, cr, uid, ids, vals, context=None):
if not isinstance(ids, list):
ids=[ids]
if vals.get('name', False) and not vals.get('extention', False):
vals['name'], vals['extention'] = os.path.splitext(vals['name'])
if vals.get('name', False) or vals.get('extention', False):
local_media_repository = self.pool.get('res.company').get_local_media_repository(cr, uid, context=context)
if local_media_repository:
old_images = self.browse(cr, uid, ids, context=context)
res=[]
for old_image in old_images:
if vals.get('name', False) and (old_image.name != vals['name']) or vals.get('extention', False) and (old_image.extention != vals['extention']):
old_path = os.path.join(local_media_repository, old_image.product_id.default_code, '%s%s' %(old_image.name, old_image.extention))
res.append(super(product_images, self).write(cr, uid, old_image.id, vals, context=context))
if 'file' in vals:
#a new image have been loaded we should remove the old image
#TODO it's look like there is something wrong with function field in openerp indeed the preview is always added in the write :(
if os.path.isfile(old_path):
os.remove(old_path)
else:
#we have to rename the image on the file system
if os.path.isfile(old_path):
os.rename(old_path, os.path.join(local_media_repository, old_image.product_id.default_code, '%s%s' %(old_image.name, old_image.extention)))
return res
return super(product_images, self).write(cr, uid, ids, vals, context=context)
def get_image(self, cr, uid, id, context=None):
each = self.read(cr, uid, id, ['link', 'url', 'name', 'file_db_store', 'product_id', 'name', 'extention'])
if each['link']:
(filename, header) = urllib.urlretrieve(each['filename'])
(filename, header) = urllib.urlretrieve(each['url'])
f = open(filename , 'rb')
img = base64.encodestring(f.read())
f.close()
else:
user = self.pool.get('res.users').browse(cr, uid, uid)
company = user.company_id
if company.local_media_repository:
local_media_repository = self.pool.get('res.company').get_local_media_repository(cr, uid, context=context)
if local_media_repository:
product_code = self.pool.get('product.product').read(cr, uid, each['product_id'][0], ['default_code'])['default_code']
full_path = os.path.join(company.local_media_repository, product_code, each['name'])
full_path = os.path.join(local_media_repository, product_code, '%s%s'%(each['name'], each['extention']))
if os.path.exists(full_path):
try:
f = open(full_path, 'rb')
@ -57,21 +99,19 @@ class product_images(osv.osv):
logger.notifyChannel('product_images', netsvc.LOG_ERROR, "The image %s doesn't exist " %full_path)
return False
else:
img = each['image']
img = each['file_db_store']
return img
def _get_image(self, cr, uid, ids, field_name, arg, context={}):
def _get_image(self, cr, uid, ids, field_name, arg, context=None):
res = {}
for each in ids:
res[each] = self.get_image(cr, uid, each)
res[each] = self.get_image(cr, uid, each, context=context)
return res
def _check_filestore(self, image_filestore):
'''check if the filestore is created, if not it create it automatically'''
print 'create directory', image_filestore
try:
if not os.path.isdir(image_filestore):
print 'create the directory'
os.makedirs(image_filestore)
except Exception, e:
raise osv.except_osv(_('Error'), _('The image filestore can not be created, %s'%e))
@ -84,25 +124,24 @@ class product_images(osv.osv):
ofile = open(full_path, 'w')
try:
ofile.write(base64.decodestring(b64_file))
print 'write'
finally:
ofile.close()
return True
def _set_image(self, cr, uid, id, name, value, arg, context=None):
user = self.pool.get('res.users').browse(cr, uid, uid)
company = user.company_id
if company.local_media_repository:
local_media_repository = self.pool.get('res.company').get_local_media_repository(cr, uid, context=context)
if local_media_repository:
image = self.browse(cr, uid, id, context=context)
return self._save_file(os.path.join(company.local_media_repository, image.product_id.default_code), image.name , value)
return self._save_file(os.path.join(local_media_repository, image.product_id.default_code), '%s%s'%(image.name, image.extention), value)
return self.write(cr, uid, id, {'image' : value}, context=context)
_columns = {
'name':fields.char('Image Title', size=100, required=True),
'extention': fields.char('file extention', size=4),
'link':fields.boolean('Link?', help="Images can be linked from files on your file system or remote (Preferred)"),
'image':fields.binary('Image', filters='*.png,*.jpg,*.gif'),
'filename':fields.char('File Location', size=250),
'preview':fields.function(_get_image, fnct_inv=_set_image, type="image", method=True),
'file_db_store':fields.binary('Image stored in database'),
'file':fields.function(_get_image, fnct_inv=_set_image, type="image", method=True, filters='*.png,*.jpg,*.gif'),
'url':fields.char('File Location', size=250),
'comments':fields.text('Comments'),
'product_id':fields.many2one('product.product', 'Product')
}

9
product_images_olbs/views/product_images_view.xml

@ -9,15 +9,16 @@
<form string="Product Images">
<notebook colspan="4">
<page string="Image">
<group col="4" colspan="4">
<group col="6" colspan="4">
<field name="name" colspan="2" />
<field name="extention" colspan="2" />
<field name="link" colspan="2" />
</group>
<group attrs="{'invisible':[('link','=',0)]}" colspan="4">
<separator string="File Location and Preview (Only when saved)" colspan="4"/>
<field name="filename" colspan="4" widget="url" nolabel="1"/>
<field name="url" colspan="4" widget="url" nolabel="1"/>
</group>
<field name="preview" widget="image" nolabel="1" colspan="4" attrs="{'readonly':[('link','!=',0)]}"/>
<field name="file" widget="image" nolabel="1" filename="name" colspan="4" attrs="{'readonly':[('link','!=',0)]}"/>
</page>
<page string="Comments">
<field name="comments" nolabel="1" colspan="4" />
@ -33,7 +34,7 @@
<field name="arch" type="xml">
<tree string="Product Images">
<field name="name" select="1" />
<field name="image" />
<field name="file" widget="image"/>
<field name="comments" select="2" />
</tree>
</field>

Loading…
Cancel
Save