[IMP] point_of_sale: Add a photo on the pos category object

bzr revid: stw@openerp.com-20120607131930-tmov89slcyucgi15
This commit is contained in:
Stephane Wirtel 2012-06-07 15:19:30 +02:00
parent bee48de20b
commit da5d0d890c
2 changed files with 54 additions and 0 deletions

View File

@ -19,6 +19,8 @@
#
##############################################################################
import pdb
import io
import addons
import time
from datetime import datetime
@ -1133,6 +1135,8 @@ class pos_order_line(osv.osv):
pos_order_line()
class pos_category(osv.osv):
PHOTO_SIZE_HEIGHT = 540
PHOTO_SIZE_WIDTH = 450
_name = 'pos.category'
_description = "Point of Sale Category"
_order = "sequence, name"
@ -1150,6 +1154,38 @@ class pos_category(osv.osv):
(_check_recursion, 'Error ! You cannot create recursive categories.', ['parent_id'])
]
def onchange_photo(self, cr, uid, ids, value, context=None):
if not value:
return {'value': {'photo_big': value, 'photo': value} }
return {
'value': {
'photo_big': self._photo_resize(cr, uid, value, self.PHOTO_SIZE_HEIGHT, self.PHOTO_SIZE_WIDTH, context=context),
'photo': self._photo_resize(cr, uid, value, context=context)
}
}
def _set_photo(self, cr, uid, id, name, value, args, context=None):
if not value:
vals = {'photo_big': value}
else:
vals = {'photo_big': self._photo_resize(cr, uid, value, self.PHOTO_SIZE_HEIGHT, self.PHOTO_SIZE_WIDTH, context=context)}
return self.write(cr, uid, [id], vals, context=context)
def _photo_resize(self, cr, uid, photo, heigth=180, width=150, context=None):
image_stream = io.BytesIO(photo.decode('base64'))
img = Image.open(image_stream)
img.thumbnail((heigth, width), Image.ANTIALIAS)
img_stream = StringIO.StringIO()
img.save(img_stream, "JPEG")
return img_stream.getvalue().encode('base64')
def _get_photo(self, cr, uid, ids, name, args, context=None):
result = dict.fromkeys(ids, False)
for category in self.browse(cr, uid, ids, context=context):
if category.photo_big:
result[category.id] = self._photo_resize(cr, uid, category.photo_big, context=context)
return result
def name_get(self, cr, uid, ids, context=None):
if not len(ids):
return []
@ -1172,7 +1208,22 @@ class pos_category(osv.osv):
'parent_id': fields.many2one('pos.category','Parent Category', select=True),
'child_id': fields.one2many('pos.category', 'parent_id', string='Children Categories'),
'sequence': fields.integer('Sequence', help="Gives the sequence order when displaying a list of product categories."),
'photo_big': fields.binary('Big-sized employee photo', help="This field holds the photo of the employee. The photo field is used as an interface to access this field. The image is base64 encoded, and PIL-supported. Full-sized photo are however resized to 540x450 px."),
'photo': fields.function(_get_photo, fnct_inv=_set_photo, string='Employee photo', type="binary",
store = {
'hr.employee': (lambda self, cr, uid, ids, c={}: ids, ['photo_big'], 10),
}, help="Image used as photo for the employee. It is automatically resized as a 180x150 px image. A larger photo is stored inside the photo_big field."),
}
def _default_photo(self, cr, uid, context=None):
photo_path = addons.get_module_resource('point_of_sale','images','default_category_photo.png')
return self._photo_resize(cr, uid, open(photo_path, 'rb').read().encode('base64'), context=context)
_defaults = {
'photo' : _default_photo,
}
pos_category()
import io, StringIO

View File

@ -648,6 +648,9 @@
<field name="parent_id"/>
<field name="sequence"/>
</group>
<group colspan="2" col="1">
<field name="photo" widget='image' nolabel="1" on_change="onchange_photo(photo)"/>
</group>
</form>
</field>
</record>