From 151b0e40beaa4e6f75b77db7fb8a484b6857bbc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Fri, 10 Aug 2012 10:16:32 +0200 Subject: [PATCH 01/31] [REF] res.users: now inherits from res.partner. Removed duplicated field, now on partner model. Added some compatibility fields. Also removed dead code and unused methods. bzr revid: tde@openerp.com-20120810081632-a4sg4ve68gpx6bx4 --- openerp/addons/base/res/res_users.py | 169 +++++---------------- openerp/addons/base/res/res_users_view.xml | 14 +- 2 files changed, 46 insertions(+), 137 deletions(-) diff --git a/openerp/addons/base/res/res_users.py b/openerp/addons/base/res/res_users.py index 1729e9e7784..93291cb2bdd 100644 --- a/openerp/addons/base/res/res_users.py +++ b/openerp/addons/base/res/res_users.py @@ -33,7 +33,6 @@ import openerp.exceptions from osv import fields,osv from osv.orm import browse_record import pooler -import random from service import security import tools from tools.translate import _ @@ -96,60 +95,23 @@ class groups(osv.osv): groups() -def _lang_get(self, cr, uid, context=None): - obj = self.pool.get('res.lang') - ids = obj.search(cr, uid, [('translatable','=',True)]) - res = obj.read(cr, uid, ids, ['code', 'name'], context=context) - res = [(r['code'], r['name']) for r in res] - return res - -def _tz_get(self,cr,uid, context=None): - return [(x, x) for x in pytz.all_timezones] - class users(osv.osv): + """ User class. A res.users record models an OpenERP user and is different + from an employee. + + res.users class now inherits from res.partner. The partner model is + used to store the data related to the partner: lang, name, address, + avatar, ... The user model is now dedicated to technical data. + """ __admin_ids = {} _uid_cache = {} + _inherits = { + 'res.partner': 'partner_id', + } _name = "res.users" _description = 'Users' _order = 'name' - WELCOME_MAIL_SUBJECT = u"Welcome to OpenERP" - WELCOME_MAIL_BODY = u"An OpenERP account has been created for you, "\ - "\"%(name)s\".\n\nYour login is %(login)s, "\ - "you should ask your supervisor or system administrator if you "\ - "haven't been given your password yet.\n\n"\ - "If you aren't %(name)s, this email reached you errorneously, "\ - "please delete it." - - def get_welcome_mail_subject(self, cr, uid, context=None): - """ Returns the subject of the mail new users receive (when - created via the res.config.users wizard), default implementation - is to return config_users.WELCOME_MAIL_SUBJECT - """ - return self.WELCOME_MAIL_SUBJECT - def get_welcome_mail_body(self, cr, uid, context=None): - """ Returns the subject of the mail new users receive (when - created via the res.config.users wizard), default implementation - is to return config_users.WELCOME_MAIL_BODY - """ - return self.WELCOME_MAIL_BODY - - def get_current_company(self, cr, uid): - cr.execute('select company_id, res_company.name from res_users left join res_company on res_company.id = company_id where res_users.id=%s' %uid) - return cr.fetchall() - - def send_welcome_email(self, cr, uid, id, context=None): - if isinstance(id,list): id = id[0] - user = self.read(cr, uid, id, ['email','login','name', 'user_email'], context=context) - email = user['email'] or user['user_email'] - - ir_mail_server = self.pool.get('ir.mail_server') - msg = ir_mail_server.build_email(email_from=None, # take config default - email_to=[email], - subject=self.get_welcome_mail_subject(cr, uid, context=context), - body=(self.get_welcome_mail_body(cr, uid, context=context) % user)) - return ir_mail_server.send_email(cr, uid, msg, context=context) - 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. @@ -164,74 +126,44 @@ class users(osv.osv): def _get_password(self, cr, uid, ids, arg, karg, context=None): return dict.fromkeys(ids, '') - - def _get_image(self, cr, uid, ids, name, args, context=None): - result = dict.fromkeys(ids, False) - for obj in self.browse(cr, uid, ids, context=context): - result[obj.id] = tools.image_get_resized_images(obj.image) - return result - - def _set_image(self, cr, uid, id, name, value, args, context=None): - return self.write(cr, uid, [id], {'image': tools.image_resize_image_big(value)}, context=context) _columns = { 'id': fields.integer('ID'), - 'name': fields.char('User Name', size=64, required=True, select=True, - help="The new user's real name, used for searching" - " and most listings"), + 'login_date': fields.date('Latest connection', select=1), + 'partner_id': fields.many2one('res.partner', required=True, + string='Related Partner', ondelete='cascade', + help='Partner-related data of the user'), 'login': fields.char('Login', size=64, required=True, - help="Used to log into the system"), - 'password': fields.char('Password', size=64, invisible=True, help="Keep empty if you don't want the user to be able to connect on the system."), + help="Used to log into the system"), + 'password': fields.char('Password', size=64, invisible=True, + help="Keep empty if you don't want the user to be able to connect on the system."), 'new_password': fields.function(_get_password, type='char', size=64, - fnct_inv=_set_new_password, - string='Set Password', help="Specify a value only when creating a user or if you're changing the user's password, " - "otherwise leave empty. After a change of password, the user has to login again."), - 'user_email': fields.char('Email', size=64), + fnct_inv=_set_new_password, string='Set Password', + help="Specify a value only when creating a user or if you're "\ + "changing the user's password, otherwise leave empty. After "\ + "a change of password, the user has to login again."), 'signature': fields.text('Signature', size=64), - 'image': fields.binary("Avatar", - help="This field holds the image used as avatar for the "\ - "user. The image is base64 encoded, and PIL-supported. "\ - "It is limited to a 1024x1024 px image."), - 'image_medium': fields.function(_get_image, fnct_inv=_set_image, - string="Medium-sized avatar", type="binary", multi="_get_image", - store = { - 'res.users': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10), - }, - help="Medium-sized image of the user. It is automatically "\ - "resized as a 180x180 px image, with aspect ratio preserved. "\ - "Use this field in form views or some kanban views."), - 'image_small': fields.function(_get_image, fnct_inv=_set_image, - string="Smal-sized avatar", type="binary", multi="_get_image", - store = { - 'res.users': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10), - }, - help="Small-sized image of the user. It is automatically "\ - "resized as a 50x50 px image, with aspect ratio preserved. "\ - "Use this field anywhere a small image is required."), '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."), 'groups_id': fields.many2many('res.groups', 'res_groups_users_rel', 'uid', 'gid', 'Groups'), - # Special behavior for this field: res.company.search() will only return the companies # available to the current user (should be the user's companies?), when the user_preference # context is set. 'company_id': fields.many2one('res.company', 'Company', required=True, - help="The company this user is currently working for.", context={'user_preference': True}), - + help='The company this user is currently working for.', context={'user_preference': True}), + 'context_company_id': fields.many2one('res.company', 'Company', required=True, + help='The company this user is currently working for.', context={'user_preference': True}), 'company_ids':fields.many2many('res.company','res_company_users_rel','user_id','cid','Companies'), - 'context_lang': fields.selection(_lang_get, 'Language', required=True, - help="The default language used in the graphical user interface, when translations are available. To add a new language, you can use the 'Load a Translation' wizard available from the 'Administration' menu."), - 'context_tz': fields.selection(_tz_get, 'Timezone', size=64, - help="The user's timezone, used to output proper date and time values inside printed reports. " - "It is important to set a value for this field. You should use the same timezone " - "that is otherwise used to pick and render date and time values: your computer's timezone."), - 'date': fields.datetime('Latest Connection', readonly=True), + # backward compatibility fields + 'user_email': fields.related('email', type='char', + deprecated='Use the email field instead of user_email. This field will be removed as of OpenERP 7.1.'), + 'date': fields.related('date', type='date', + deprecated='use the login_date field instead of date. This field will be removed as of OpenERP 7.1.'), } def on_change_company_id(self, cr, uid, ids, company_id): - return { - 'warning' : { + return {'warning' : { 'title': _("Company Switch Warning"), 'message': _("Please keep in mind that documents currently displayed may not be relevant after switching to another company. If you have unsaved changes, please make sure to save and close all forms before switching to a different company. (You can click on Cancel in the User Preferences now)"), } @@ -263,22 +195,6 @@ class users(osv.osv): ('login_key', 'UNIQUE (login)', 'You can not have two users with the same login !') ] - def _get_email_from(self, cr, uid, ids, context=None): - if not isinstance(ids, list): - ids = [ids] - res = dict.fromkeys(ids, False) - for user in self.browse(cr, uid, ids, context=context): - if user.user_email: - res[user.id] = "%s <%s>" % (user.name, user.user_email) - return res - - def _get_admin_id(self, cr): - if self.__admin_ids.get(cr.dbname) is None: - ir_model_data_obj = self.pool.get('ir.model.data') - mdid = ir_model_data_obj._get_id(cr, 1, 'base', 'user_root') - self.__admin_ids[cr.dbname] = ir_model_data_obj.read(cr, 1, [mdid], ['res_id'])[0]['res_id'] - return self.__admin_ids[cr.dbname] - def _get_company(self,cr, uid, context=None, uid2=False): if not uid2: uid2 = uid @@ -315,18 +231,8 @@ class users(osv.osv): pass return result - def _get_default_image(self, cr, uid, context=None): - # default image file name: avatar0 -> avatar6.png, choose randomly - image_path = openerp.modules.get_module_resource('base', 'static/src/img', 'avatar%d.png' % random.randint(0, 6)) - return tools.image_resize_image_big(open(image_path, 'rb').read().encode('base64')) - _defaults = { 'password' : '', - 'context_lang': lambda self, cr, uid, context: context.get('lang', 'en_US'), - 'context_tz': lambda self, cr, uid, context: context.get('tz', False), - 'image': _get_default_image, - 'image_small': _get_default_image, - 'image_medium': _get_default_image, 'active' : True, 'menu_id': _get_menu, 'company_id': _get_company, @@ -335,7 +241,7 @@ class users(osv.osv): } # User can write to a few of her own fields (but not her groups for example) - SELF_WRITEABLE_FIELDS = ['password', 'signature', 'action_id', 'company_id', 'user_email', 'name', 'image', 'image_medium', 'image_small'] + SELF_WRITEABLE_FIELDS = ['password', 'signature', 'action_id', 'company_id', 'email', 'name', 'image', 'image_medium', 'image_small'] def write(self, cr, uid, ids, values, context=None): if not hasattr(ids, '__iter__'): @@ -400,12 +306,18 @@ class users(osv.osv): def context_get(self, cr, uid, context=None): user = self.browse(cr, uid, uid, context) result = {} - for k in self._columns.keys(): + for k in self._all_columns.keys(): if k.startswith('context_'): + context_key = k[8:] + elif k in ['lang', 'tz']: + context_key = k + else: + context_key = False + if context_key: res = getattr(user,k) or False if isinstance(res, browse_record): res = res.id - result[k[8:]] = res or False + result[context_key] = res or False return result def action_get(self, cr, uid, context=None): @@ -565,9 +477,6 @@ class users(osv.osv): (uid, module, ext_id)) return bool(cr.fetchone()) -users() - - # # Extension of res.groups and res.users with a relation for "implied" or diff --git a/openerp/addons/base/res/res_users_view.xml b/openerp/addons/base/res/res_users_view.xml index ab67cfd532a..65a64f81f2d 100644 --- a/openerp/addons/base/res/res_users_view.xml +++ b/openerp/addons/base/res/res_users_view.xml @@ -112,8 +112,8 @@ - - + + @@ -121,7 +121,7 @@ - + @@ -146,7 +146,7 @@ - + @@ -209,15 +209,15 @@ () - + - + - +