diff --git a/doc/api/user_img_specs.rst b/doc/api/user_img_specs.rst new file mode 100644 index 00000000000..4ee97999aca --- /dev/null +++ b/doc/api/user_img_specs.rst @@ -0,0 +1,9 @@ +User avatar +=========== + +This revision adds an avatar for users. This replaces the use of gravatar to emulate avatars, used in views like the tasks kanban view. Two fields have been added to the res.users model: + - avatar_big, a binary field holding the image. It is base-64 encoded, and PIL-supported. Images stored are resized to 540x450 px, to limitate the binary field size. + - avatar, a function binary field holding an automatically resized version of the avatar_big field. It is also base-64 encoded, and PIL-supported. Dimensions of the resized avatar are 180x150. This field is used as an inteface to get and set the user avatar. +When changing the avatar through the avatar function field, the new image is automatically resized to 540x450, and stored in the avatar_big field. This triggers the function field, that will compute a 180x150 resized version of the image. + +An avatar field has been added to the users form view, as well as in Preferences. When creating a new user, a default avatar is chosen among 6 possible default images. diff --git a/doc/index.rst.inc b/doc/index.rst.inc index 05c4a53640b..d208bf82eb1 100644 --- a/doc/index.rst.inc +++ b/doc/index.rst.inc @@ -6,3 +6,11 @@ OpenERP Server :maxdepth: 1 test-framework + +New feature merges +++++++++++++++++++ + +.. toctree:: + :maxdepth: 1 + + api/user_img_specs diff --git a/openerp/addons/base/base_update.xml b/openerp/addons/base/base_update.xml index c5594f76e22..85d92106fc8 100644 --- a/openerp/addons/base/base_update.xml +++ b/openerp/addons/base/base_update.xml @@ -84,12 +84,18 @@
- - - - - - + + + + + + + + + + + + @@ -118,32 +124,36 @@ - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - + + + + - - - - - - diff --git a/openerp/addons/base/res/res_users.py b/openerp/addons/base/res/res_users.py index 2617efeb4a2..6d567e6d4b8 100644 --- a/openerp/addons/base/res/res_users.py +++ b/openerp/addons/base/res/res_users.py @@ -25,17 +25,20 @@ from functools import partial import pytz -import netsvc -import pooler -import tools -from osv import fields,osv -from osv.orm import browse_record -from service import security -from tools.translate import _ -import openerp -import openerp.exceptions +import io, StringIO from lxml import etree from lxml.builder import E +import netsvc +import openerp +import openerp.exceptions +from osv import fields,osv +from osv.orm import browse_record +from PIL import Image +import pooler +import random +from service import security +import tools +from tools.translate import _ _logger = logging.getLogger(__name__) @@ -203,7 +206,6 @@ class users(osv.osv): self.write(cr, uid, ids, {'groups_id': [(4, extended_group_id)]}, context=context) return True - def _get_interface_type(self, cr, uid, ids, name, args, context=None): """Implementation of 'view' function field getter, returns the type of interface of the users. @param field_name: Name of the field @@ -215,6 +217,33 @@ 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(self, cr, uid, ids, value, context=None): + if not value: + return {'value': {'avatar_big': value, 'avatar': value} } + return {'value': {'avatar_big': self._avatar_resize(cr, uid, value, 540, 450, context=context), 'avatar': self._avatar_resize(cr, uid, value, context=context)} } + + def _set_avatar(self, cr, uid, id, name, value, args, context=None): + if not value: + vals = {'avatar_big': value} + else: + vals = {'avatar_big': self._avatar_resize(cr, uid, value, 540, 450, context=context)} + return self.write(cr, uid, [id], vals, context=context) + + def _avatar_resize(self, cr, uid, avatar, height=180, width=150, context=None): + image_stream = io.BytesIO(avatar.decode('base64')) + img = Image.open(image_stream) + img.thumbnail((height, width), Image.ANTIALIAS) + img_stream = StringIO.StringIO() + img.save(img_stream, "PNG") + return img_stream.getvalue().encode('base64') + + def _get_avatar(self, cr, uid, ids, name, args, context=None): + result = dict.fromkeys(ids, False) + for user in self.browse(cr, uid, ids, context=context): + if user.avatar_big: + result[user.id] = self._avatar_resize(cr, uid, user.avatar_big, context=context) + return result + def _set_new_password(self, cr, uid, id, name, value, args, context=None): if value is False: # Do not update the password if no value is provided, ignore silently. @@ -244,6 +273,11 @@ class users(osv.osv): "otherwise leave empty. After a change of password, the user has to login again."), 'user_email': fields.char('Email', size=64), 'signature': fields.text('Signature', size=64), + 'avatar_big': fields.binary('Big-sized avatar', help="This field holds the image used as avatar for the user. The avatar field is used as an interface to access this field. The image is base64 encoded, and PIL-supported. It is stored as a 540x450 px image, in case a bigger image must be used."), + 'avatar': fields.function(_get_avatar, fnct_inv=_set_avatar, string='Avatar', type="binary", + store = { + 'res.users': (lambda self, cr, uid, ids, c={}: ids, ['avatar_big'], 10), + }, help="Image used as avatar for the user. It is automatically resized as a 180x150 px image. This field serves as an interface to the avatar_big field."), 'active': fields.boolean('Active'), 'action_id': fields.many2one('ir.actions.actions', 'Home Action', help="If specified, this action will be opened at logon for this user, in addition to the standard menu."), 'menu_id': fields.many2one('ir.actions.actions', 'Menu Action', help="If specified, the action will replace the standard menu for this user."), @@ -355,9 +389,15 @@ class users(osv.osv): pass return result + def _get_avatar(self, cr, uid, context=None): + # default avatar file name: avatar0 -> avatar6.png, choose randomly + avatar_path = openerp.modules.get_module_resource('base', 'static/src/img', 'avatar%d.png' % random.randint(0, 6)) + return self._avatar_resize(cr, uid, open(avatar_path, 'rb').read().encode('base64'), context=context) + _defaults = { 'password' : '', 'context_lang': 'en_US', + 'avatar': _get_avatar, 'active' : True, 'menu_id': _get_menu, 'company_id': _get_company, diff --git a/openerp/addons/base/static/src/img/avatar0.png b/openerp/addons/base/static/src/img/avatar0.png new file mode 100644 index 00000000000..2bcc7c8d2e4 Binary files /dev/null and b/openerp/addons/base/static/src/img/avatar0.png differ diff --git a/openerp/addons/base/static/src/img/avatar1.png b/openerp/addons/base/static/src/img/avatar1.png new file mode 100644 index 00000000000..4f6d2f7e818 Binary files /dev/null and b/openerp/addons/base/static/src/img/avatar1.png differ diff --git a/openerp/addons/base/static/src/img/avatar2.png b/openerp/addons/base/static/src/img/avatar2.png new file mode 100644 index 00000000000..70d5df7ee60 Binary files /dev/null and b/openerp/addons/base/static/src/img/avatar2.png differ diff --git a/openerp/addons/base/static/src/img/avatar3.png b/openerp/addons/base/static/src/img/avatar3.png new file mode 100644 index 00000000000..053768aa035 Binary files /dev/null and b/openerp/addons/base/static/src/img/avatar3.png differ diff --git a/openerp/addons/base/static/src/img/avatar4.png b/openerp/addons/base/static/src/img/avatar4.png new file mode 100644 index 00000000000..1b2a99f05c3 Binary files /dev/null and b/openerp/addons/base/static/src/img/avatar4.png differ diff --git a/openerp/addons/base/static/src/img/avatar5.png b/openerp/addons/base/static/src/img/avatar5.png new file mode 100644 index 00000000000..0f6c416ecf0 Binary files /dev/null and b/openerp/addons/base/static/src/img/avatar5.png differ diff --git a/openerp/addons/base/static/src/img/avatar6.png b/openerp/addons/base/static/src/img/avatar6.png new file mode 100644 index 00000000000..d6a18c2779e Binary files /dev/null and b/openerp/addons/base/static/src/img/avatar6.png differ