Merge pull request #1284 from sebalix/8.0-fix-xmlrpc-marshall-none
[odoo/odoo.git] / addons / hr / res_users.py
1 from openerp import api
2 from openerp.osv import fields, osv
3
4
5 class res_users(osv.Model):
6     """ Update of res.users class
7         - if adding groups to an user, check if base.group_user is in it
8         (member of 'Employee'), create an employee form linked to it.
9     """
10     _name = 'res.users'
11     _inherit = ['res.users']
12
13     _columns = {
14         'display_employees_suggestions': fields.boolean("Display Employees Suggestions"),
15     }
16
17     _defaults = {
18         'display_employees_suggestions': True,
19     }
20
21     def __init__(self, pool, cr):
22         """ Override of __init__ to add access rights on
23         display_employees_suggestions fields. Access rights are disabled by
24         default, but allowed on some specific fields defined in
25         self.SELF_{READ/WRITE}ABLE_FIELDS.
26         """
27         init_res = super(res_users, self).__init__(pool, cr)
28         # duplicate list to avoid modifying the original reference
29         self.SELF_WRITEABLE_FIELDS = list(self.SELF_WRITEABLE_FIELDS)
30         self.SELF_WRITEABLE_FIELDS.append('display_employees_suggestions')
31         # duplicate list to avoid modifying the original reference
32         self.SELF_READABLE_FIELDS = list(self.SELF_READABLE_FIELDS)
33         self.SELF_READABLE_FIELDS.append('display_employees_suggestions')
34         return init_res
35
36     def stop_showing_employees_suggestions(self, cr, uid, user_id, context=None):
37         """Update display_employees_suggestions value to False"""
38         if context is None:
39             context = {}
40         self.write(cr, uid, user_id, {"display_employees_suggestions": False}, context)
41
42     def _create_welcome_message(self, cr, uid, user, context=None):
43         """Do not welcome new users anymore, welcome new employees instead"""
44         return True
45
46     def _message_post_get_eid(self, cr, uid, thread_id, context=None):
47         assert thread_id, "res.users does not support posting global messages"
48         if context and 'thread_model' in context:
49             context = dict(context or {})
50             context['thread_model'] = 'hr.employee'
51         if isinstance(thread_id, (list, tuple)):
52             thread_id = thread_id[0]
53         return self.pool.get('hr.employee').search(cr, uid, [('user_id', '=', thread_id)], context=context)
54
55     @api.cr_uid_ids_context
56     def message_post(self, cr, uid, thread_id, context=None, **kwargs):
57         """ Redirect the posting of message on res.users to the related employee.
58             This is done because when giving the context of Chatter on the
59             various mailboxes, we do not have access to the current partner_id. """
60         if kwargs.get('type') == 'email':
61             return super(res_users, self).message_post(cr, uid, thread_id, context=context, **kwargs)
62         res = None
63         employee_ids = self._message_post_get_eid(cr, uid, thread_id, context=context)
64         if not employee_ids:  # no employee: fall back on previous behavior
65             return super(res_users, self).message_post(cr, uid, thread_id, context=context, **kwargs)
66         for employee_id in employee_ids:
67             res = self.pool.get('hr.employee').message_post(cr, uid, employee_id, context=context, **kwargs)
68         return res