[MERGE] mail/chatter complete review/refactoring
[odoo/odoo.git] / addons / mail / res_users.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2009-Today OpenERP SA (<http://www.openerp.com>)
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>
19 #
20 ##############################################################################
21
22 from osv import osv, fields
23 from openerp import SUPERUSER_ID
24 from tools.translate import _
25
26 class res_users(osv.Model):
27     """ Update of res.users class
28         - add a preference about sending emails about notifications
29         - make a new user follow itself
30         - add a welcome message
31     """
32     _name = 'res.users'
33     _inherit = ['res.users']
34     _inherits = {'mail.alias': 'alias_id'}
35
36     _columns = {
37         'alias_id': fields.many2one('mail.alias', 'Alias', ondelete="cascade", required=True, 
38             help="Email address internally associated with this user. Incoming "\
39                  "emails will appear in the user's notifications."),
40     }
41     
42     _defaults = {
43         'alias_domain': False, # always hide alias during creation
44     }
45
46     def __init__(self, pool, cr):
47         """ Override of __init__ to add access rights on notification_email_send
48             field. Access rights are disabled by default, but allowed on
49             fields defined in self.SELF_WRITEABLE_FIELDS.
50         """
51         init_res = super(res_users, self).__init__(pool, cr)
52         # duplicate list to avoid modifying the original reference
53         self.SELF_WRITEABLE_FIELDS = list(self.SELF_WRITEABLE_FIELDS)
54         self.SELF_WRITEABLE_FIELDS.append('notification_email_send')
55         return init_res
56
57     def _auto_init(self, cr, context=None):
58         """Installation hook to create aliases for all users and avoid constraint errors."""
59         self.pool.get('mail.alias').migrate_to_alias(cr, self._name, self._table, super(res_users,self)._auto_init,
60             self._columns['alias_id'], 'login', alias_force_key='id', context=context)
61
62     def create(self, cr, uid, data, context=None):
63         # create default alias same as the login
64         mail_alias = self.pool.get('mail.alias')
65         alias_id = mail_alias.create_unique_alias(cr, uid, {'alias_name': data['login']}, model_name=self._name, context=context)
66         data['alias_id'] = alias_id
67         data.pop('alias_name', None) # prevent errors during copy()
68
69         # create user that follows its related partner
70         user_id = super(res_users, self).create(cr, uid, data, context=context)
71         user = self.browse(cr, uid, user_id, context=context)
72         self.pool.get('res.partner').message_subscribe(cr, uid, [user.partner_id.id], [user.partner_id.id], context=context)
73         # alias
74         mail_alias.write(cr, SUPERUSER_ID, [alias_id], {"alias_force_thread_id": user_id}, context)
75         # create a welcome message
76         self._create_welcome_message(cr, uid, user, context=context)
77         return user_id
78
79     def _create_welcome_message(self, cr, uid, user, context=None):
80         company_name = user.company_id.name if user.company_id else _('the company')
81         body = _('%s has joined %s.') % (user.name, company_name)
82         # TODO change SUPERUSER_ID into user.id but catch errors
83         return self.pool.get('res.partner').message_post(cr, SUPERUSER_ID, [user.partner_id.id],
84             body=body, context=context)
85
86     def write(self, cr, uid, ids, vals, context=None):
87         # User alias is sync'ed with login
88         if vals.get('login'): vals['alias_name'] = vals['login']
89         return super(res_users, self).write(cr, uid, ids, vals, context=context)
90
91     def unlink(self, cr, uid, ids, context=None):
92         # Cascade-delete mail aliases as well, as they should not exist without the user.
93         alias_pool = self.pool.get('mail.alias')
94         alias_ids = [user.alias_id.id for user in self.browse(cr, uid, ids, context=context) if user.alias_id]
95         res = super(res_users, self).unlink(cr, uid, ids, context=context)
96         alias_pool.unlink(cr, uid, alias_ids, context=context)
97         return res
98
99     def message_post(self, cr, uid, thread_id, **kwargs):
100         partner_id = self.pool.get('res.users').browse(cr, uid, thread_id)[0].partner_id.id
101         return self.pool.get('res.partner').message_post(cr, uid, partner_id, **kwargs)
102
103 class res_users_mail_group(osv.Model):
104     """ Update of res.users class
105         - if adding groups to an user, check mail.groups linked to this user
106           group, and the user. This is done by overriding the write method.
107     """
108     _name = 'res.users'
109     _inherit = ['res.users']
110
111     # FP Note: to improve, post processing may be better ?
112     def write(self, cr, uid, ids, vals, context=None):
113         write_res = super(res_users_mail_group, self).write(cr, uid, ids, vals, context=context)
114         if vals.get('groups_id'):
115             # form: {'group_ids': [(3, 10), (3, 3), (4, 10), (4, 3)]} or {'group_ids': [(6, 0, [ids]}
116             user_group_ids = [command[1] for command in vals['groups_id'] if command[0] == 4]
117             user_group_ids += [id for command in vals['groups_id'] if command[0] == 6 for id in command[2]]
118             mail_group_obj = self.pool.get('mail.group')
119             mail_group_ids = mail_group_obj.search(cr, uid, [('group_ids', 'in', user_group_ids)], context=context)
120             mail_group_obj.message_subscribe_users(cr, uid, mail_group_ids, ids, context=context)
121         return write_res
122
123 class res_groups_mail_group(osv.Model):
124     """ Update of res.groups class
125         - if adding users from a group, check mail.groups linked to this user
126           group and subscribe them. This is done by overriding the write method.
127     """
128     _name = 'res.groups'
129     _inherit = 'res.groups'
130
131     # FP Note: to improve, post processeing, after the super may be better
132     def write(self, cr, uid, ids, vals, context=None):
133         write_res = super(res_groups_mail_group, self).write(cr, uid, ids, vals, context=context)
134         if vals.get('users'):
135             # form: {'group_ids': [(3, 10), (3, 3), (4, 10), (4, 3)]} or {'group_ids': [(6, 0, [ids]}
136             user_ids = [command[1] for command in vals['users'] if command[0] == 4]
137             user_ids += [id for command in vals['users'] if command[0] == 6 for id in command[2]]
138             mail_group_obj = self.pool.get('mail.group')
139             mail_group_ids = mail_group_obj.search(cr, uid, [('group_ids', 'in', ids)], context=context)
140             mail_group_obj.message_subscribe_users(cr, uid, mail_group_ids, user_ids, context=context)
141         return write_res