Remark from MAT: invoice date wasn't passed on in original code after the original...
[odoo/odoo.git] / addons / hr / res_users.py
1 from openerp.osv import fields, osv
2 from openerp.tools.translate import _
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['thread_model'] = 'hr.employee'
50         if isinstance(thread_id, (list, tuple)):
51             thread_id = thread_id[0]
52         return self.pool.get('hr.employee').search(cr, uid, [('user_id', '=', thread_id)], context=context)
53
54     def message_post(self, cr, uid, thread_id, context=None, **kwargs):
55         """ Redirect the posting of message on res.users to the related employee.
56             This is done because when giving the context of Chatter on the
57             various mailboxes, we do not have access to the current partner_id. """
58         if kwargs.get('type') == 'email':
59             return super(res_users, self).message_post(cr, uid, thread_id, context=context, **kwargs)
60         res = None
61         employee_ids = self._message_post_get_eid(cr, uid, thread_id, context=context)
62         if not employee_ids:  # no employee: fall back on previous behavior
63             return super(res_users, self).message_post(cr, uid, thread_id, context=context, **kwargs)
64         for employee_id in employee_ids:
65             res = self.pool.get('hr.employee').message_post(cr, uid, employee_id, context=context, **kwargs)
66         return res