Browse Source

[REF] product_images_olbs : work in progress refactor and add the posibility to store teh image in a folder

sebastien beau 13 years ago
parent
commit
bb9a87738e
  1. 3
      product_images_olbs/__init__.py
  2. 1
      product_images_olbs/__openerp__.py
  3. 31
      product_images_olbs/product.py
  4. 69
      product_images_olbs/product_images.py
  5. 13
      product_images_olbs/views/product_images_view.xml

3
product_images_olbs/__init__.py

@ -16,4 +16,5 @@
#########################################################################
import product_images
import product
import product
import company

1
product_images_olbs/__openerp__.py

@ -33,6 +33,7 @@
"update_xml": [
'security/ir.model.access.csv',
'views/product_images_view.xml',
'views/company_view.xml'
],
"installable": True,
"active": False,

31
product_images_olbs/product.py

@ -15,6 +15,8 @@
#along with this program. If not, see <http://www.gnu.org/licenses/>. #
#########################################################################
from osv import osv,fields
from tools.translate import _
import os
class product_product(osv.osv):
_inherit = "product.product"
@ -23,6 +25,31 @@ class product_product(osv.osv):
'product.images',
'product_id',
'Product Images'
)
),
'default_code' : fields.char('Reference', size=64, require='True'),
}
product_product()
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:
if isinstance(ids, list):
id =ids[0]
else:
id=ids
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)
if os.path.isdir(old_path):
os.rename(old_path, os.path.join(company.local_media_repository, vals['default_code']))
return res
return super(product_product, self).write(cr, uid, ids, vals, context=context)
#This constraint should be by default in openerp
_sql_constraints = [('default_code', 'UNIQUE(defaut_code)',
_('Default code should be uniq'))]
product_product()

69
product_images_olbs/product_images.py

@ -16,6 +16,9 @@
#########################################################################
from osv import osv, fields
import base64, urllib
from tools.translate import _
import os
import netsvc
class product_images(osv.osv):
"Products Image gallery"
@ -23,15 +26,38 @@ class product_images(osv.osv):
_description = __doc__
_table = "product_images"
def unlink(self, cr, uid, ids, context=None):
#TODO
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'])
each = self.read(cr, uid, id, ['link', 'filename', 'image', 'product_id', 'name'])
if each['link']:
(filename, header) = urllib.urlretrieve(each['filename'])
f = open(filename , 'rb')
img = base64.encodestring(f.read())
f.close()
else:
img = each['image']
user = self.pool.get('res.users').browse(cr, uid, uid)
company = user.company_id
if company.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'])
if os.path.exists(full_path):
try:
f = open(full_path, 'rb')
img = base64.encodestring(f.read())
f.close()
except Exception, e:
logger = netsvc.Logger()
logger.notifyChannel('product_images', netsvc.LOG_ERROR, "Can not open the image %s, error : %s" %(full_path, e))
return False
else:
logger = netsvc.Logger()
logger.notifyChannel('product_images', netsvc.LOG_ERROR, "The image %s doesn't exist " %full_path)
return False
else:
img = each['image']
return img
def _get_image(self, cr, uid, ids, field_name, arg, context={}):
@ -39,13 +65,44 @@ class product_images(osv.osv):
for each in ids:
res[each] = self.get_image(cr, uid, each)
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))
return True
def _save_file(self, path, filename, b64_file):
"""Save a file encoded in base 64"""
full_path = os.path.join(path, filename)
self._check_filestore(path)
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:
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.write(cr, uid, id, {'image' : value}, context=context)
_columns = {
'name':fields.char('Image Title', size=100, required=True),
'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, type="binary", method=True),
'preview':fields.function(_get_image, fnct_inv=_set_image, type="image", method=True),
'comments':fields.text('Comments'),
'product_id':fields.many2one('product.product', 'Product')
}
@ -53,4 +110,8 @@ class product_images(osv.osv):
_defaults = {
'link': lambda *a: True,
}
_sql_constraints = [('uniq_name_product_id', 'UNIQUE(product_id, name)',
_('A product can have only one image with the same name'))]
product_images()

13
product_images_olbs/views/product_images_view.xml

@ -13,14 +13,11 @@
<field name="name" colspan="2" />
<field name="link" colspan="2" />
</group>
<group col="4" colspan="4" attrs="{'invisible':[('link','!=',0)]}">
<field name="image" colspan="4" />
</group>
<group col="4" colspan="4" attrs="{'invisible':[('link','=',0)]}">
<field name="filename" colspan="4" widget="url"/>
</group>
<separator string="Preview (Only when saved)" colspan="4" />
<field name="preview" widget="image" nolabel="1" colspan="4"/>
<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"/>
</group>
<field name="preview" widget="image" nolabel="1" colspan="4" attrs="{'readonly':[('link','!=',0)]}"/>
</page>
<page string="Comments">
<field name="comments" nolabel="1" colspan="4" />

Loading…
Cancel
Save