[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
This commit is contained in:
Thibault Delavallée 2012-08-10 10:16:32 +02:00
parent 5ac8f97de0
commit 151b0e40be
2 changed files with 46 additions and 137 deletions

View File

@ -33,7 +33,6 @@ import openerp.exceptions
from osv import fields,osv from osv import fields,osv
from osv.orm import browse_record from osv.orm import browse_record
import pooler import pooler
import random
from service import security from service import security
import tools import tools
from tools.translate import _ from tools.translate import _
@ -96,60 +95,23 @@ class groups(osv.osv):
groups() 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): 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 = {} __admin_ids = {}
_uid_cache = {} _uid_cache = {}
_inherits = {
'res.partner': 'partner_id',
}
_name = "res.users" _name = "res.users"
_description = 'Users' _description = 'Users'
_order = 'name' _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): def _set_new_password(self, cr, uid, id, name, value, args, context=None):
if value is False: if value is False:
# Do not update the password if no value is provided, ignore silently. # 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): def _get_password(self, cr, uid, ids, arg, karg, context=None):
return dict.fromkeys(ids, '') 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 = { _columns = {
'id': fields.integer('ID'), 'id': fields.integer('ID'),
'name': fields.char('User Name', size=64, required=True, select=True, 'login_date': fields.date('Latest connection', select=1),
help="The new user's real name, used for searching" 'partner_id': fields.many2one('res.partner', required=True,
" and most listings"), string='Related Partner', ondelete='cascade',
help='Partner-related data of the user'),
'login': fields.char('Login', size=64, required=True, 'login': fields.char('Login', size=64, required=True,
help="Used to log into 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."), '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, 'new_password': fields.function(_get_password, type='char', size=64,
fnct_inv=_set_new_password, fnct_inv=_set_new_password, string='Set Password',
string='Set Password', help="Specify a value only when creating a user or if you're changing the user's password, " help="Specify a value only when creating a user or if you're "\
"otherwise leave empty. After a change of password, the user has to login again."), "changing the user's password, otherwise leave empty. After "\
'user_email': fields.char('Email', size=64), "a change of password, the user has to login again."),
'signature': fields.text('Signature', size=64), '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'), '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."), '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."), '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'), '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 # 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 # available to the current user (should be the user's companies?), when the user_preference
# context is set. # context is set.
'company_id': fields.many2one('res.company', 'Company', required=True, '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'), 'company_ids':fields.many2many('res.company','res_company_users_rel','user_id','cid','Companies'),
'context_lang': fields.selection(_lang_get, 'Language', required=True, # backward compatibility fields
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."), 'user_email': fields.related('email', type='char',
'context_tz': fields.selection(_tz_get, 'Timezone', size=64, deprecated='Use the email field instead of user_email. This field will be removed as of OpenERP 7.1.'),
help="The user's timezone, used to output proper date and time values inside printed reports. " 'date': fields.related('date', type='date',
"It is important to set a value for this field. You should use the same timezone " deprecated='use the login_date field instead of date. This field will be removed as of OpenERP 7.1.'),
"that is otherwise used to pick and render date and time values: your computer's timezone."),
'date': fields.datetime('Latest Connection', readonly=True),
} }
def on_change_company_id(self, cr, uid, ids, company_id): def on_change_company_id(self, cr, uid, ids, company_id):
return { return {'warning' : {
'warning' : {
'title': _("Company Switch 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)"), '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 !') ('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): def _get_company(self,cr, uid, context=None, uid2=False):
if not uid2: if not uid2:
uid2 = uid uid2 = uid
@ -315,18 +231,8 @@ class users(osv.osv):
pass pass
return result 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 = { _defaults = {
'password' : '', '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, 'active' : True,
'menu_id': _get_menu, 'menu_id': _get_menu,
'company_id': _get_company, '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) # 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): def write(self, cr, uid, ids, values, context=None):
if not hasattr(ids, '__iter__'): if not hasattr(ids, '__iter__'):
@ -400,12 +306,18 @@ class users(osv.osv):
def context_get(self, cr, uid, context=None): def context_get(self, cr, uid, context=None):
user = self.browse(cr, uid, uid, context) user = self.browse(cr, uid, uid, context)
result = {} result = {}
for k in self._columns.keys(): for k in self._all_columns.keys():
if k.startswith('context_'): 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 res = getattr(user,k) or False
if isinstance(res, browse_record): if isinstance(res, browse_record):
res = res.id res = res.id
result[k[8:]] = res or False result[context_key] = res or False
return result return result
def action_get(self, cr, uid, context=None): def action_get(self, cr, uid, context=None):
@ -565,9 +477,6 @@ class users(osv.osv):
(uid, module, ext_id)) (uid, module, ext_id))
return bool(cr.fetchone()) return bool(cr.fetchone())
users()
# #
# Extension of res.groups and res.users with a relation for "implied" or # Extension of res.groups and res.users with a relation for "implied" or

View File

@ -112,8 +112,8 @@
<page string="Preferences"> <page string="Preferences">
<group> <group>
<group name="preferences"> <group name="preferences">
<field name="context_lang"/> <field name="lang"/>
<field name="context_tz"/> <field name="tz"/>
</group> </group>
<group groups="base.group_no_one"> <group groups="base.group_no_one">
<field name="action_id"/> <field name="action_id"/>
@ -121,7 +121,7 @@
</group> </group>
</group> </group>
<group> <group>
<field name="user_email" widget="email"/> <field name="email" widget="email"/>
<field name="signature"/> <field name="signature"/>
</group> </group>
</page> </page>
@ -146,7 +146,7 @@
<tree string="Users"> <tree string="Users">
<field name="name"/> <field name="name"/>
<field name="login"/> <field name="login"/>
<field name="context_lang"/> <field name="lang"/>
<field name="date"/> <field name="date"/>
</tree> </tree>
</field> </field>
@ -209,15 +209,15 @@
(<field name="login" readonly="1" class="oe_inline"/>) (<field name="login" readonly="1" class="oe_inline"/>)
</h1> </h1>
<group name="preferences" col="4"> <group name="preferences" col="4">
<field name="context_lang" readonly="0"/> <field name="lang" readonly="0"/>
<field name="company_id" readonly="0" <field name="company_id" readonly="0"
groups="base.group_multi_company" on_change="on_change_company_id(company_id)"/> groups="base.group_multi_company" on_change="on_change_company_id(company_id)"/>
<field name="context_tz" readonly="0"/> <field name="tz" readonly="0"/>
<field name="company_id" widget="selection" readonly="0" <field name="company_id" widget="selection" readonly="0"
groups="base.group_multi_company" on_change="on_change_company_id(company_id)"/> groups="base.group_multi_company" on_change="on_change_company_id(company_id)"/>
</group> </group>
<group string="Email Preferences"> <group string="Email Preferences">
<field name="user_email" widget="email" readonly="0"/> <field name="email" widget="email" readonly="0"/>
<field name="signature" readonly="0"/> <field name="signature" readonly="0"/>
</group> </group>
<footer> <footer>