[IMP] Improved default avatar (now takes randomly between 6 avatars); cleaned code about avatar and avatar_mini management; fixed bug when creating a new user where the chosen avatar was not saved

bzr revid: tde@openerp.com-20120313102916-w5rdtwco72xe8m7n
This commit is contained in:
Thibault Delavallée 2012-03-13 11:29:16 +01:00
parent 0790cbbfd7
commit 9f2a5f2d03
3 changed files with 23 additions and 13 deletions

View File

@ -130,7 +130,7 @@
<group colspan="7" col="7">
<group col="2" colspan="1">
<separator string="Avatar" colspan="2"/>
<field name="avatar_mini" widget='image' nolabel="1" colspan="2"/>
<field name="avatar_mini" widget='image' nolabel="1" colspan="2" on_change="onchange_avatar_mini(avatar_mini)"/>
</group>
<group col="3" colspan="2">
<separator string="Preferences" colspan="3"/>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.6 KiB

View File

@ -35,8 +35,11 @@ from tools.translate import _
import openerp
import openerp.exceptions
# for avatar resizing
import io, StringIO
from PIL import Image
# for default avatar choice
import random
_logger = logging.getLogger(__name__)
@ -214,22 +217,27 @@ class users(osv.osv):
extended_users = group_obj.read(cr, uid, extended_group_id, ['users'], context=context)['users']
return dict(zip(ids, ['extended' if user in extended_users else 'simple' for user in ids]))
def onchange_avatar_mini(self, cr, uid, ids, value, context=None):
return {'value': {'avatar': value } }
def _set_avatar_mini(self, cr, uid, id, name, value, args, context=None):
return self.write(cr, uid, [id], {'avatar': value}, context=context)
def _avatar_resize(self, cr, uid, avatar, context=None):
image_stream = io.BytesIO(avatar.decode('base64'))
img = Image.open(image_stream)
img.thumbnail((180, 150), Image.ANTIALIAS)
img_stream = StringIO.StringIO()
img.save(img_stream, "JPEG")
return img_stream.getvalue().encode('base64')
def _get_avatar_mini(self, cr, uid, ids, name, args, context=None):
result = {}
for user in self.browse(cr, uid, ids, context=context):
if not user.avatar:
result[user.id] = False
continue
image_stream = io.BytesIO(user.avatar.decode('base64'))
img = Image.open(image_stream)
img.thumbnail((180, 150), Image.ANTIALIAS)
img_stream = StringIO.StringIO()
img.save(img_stream, "JPEG")
result[user.id] = img_stream.getvalue().encode('base64')
else:
result[user.id] = self._avatar_resize(cr, uid, user.avatar)
return result
def _set_new_password(self, cr, uid, id, name, value, args, context=None):
@ -378,13 +386,15 @@ class users(osv.osv):
return result
def _get_avatar(self, cr, uid, context=None):
avatar_path = openerp.modules.get_module_resource('base','images','photo.png')
return open(avatar_path, 'rb').read().encode('base64')
# default avatar file name: avatar0 -> avatar6, choose randomly
random.seed()
avatar_path = openerp.modules.get_module_resource('base', 'images', 'avatar%d.jpg' % random.randint(0, 6))
return self._avatar_resize(cr, uid, open(avatar_path, 'rb').read().encode('base64'))
_defaults = {
'password' : '',
'context_lang': 'en_US',
'avatar': _get_avatar,
'avatar_mini': _get_avatar,
'active' : True,
'menu_id': _get_menu,
'company_id': _get_company,