[MRG]merge with lp:~openerp-dev/openobject-addons/trunk-mail-alias-jam
[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 tools.translate import _
24 from lxml import etree
25
26 class res_users(osv.osv):
27     """ Update of res.users class
28         - add a preference about sending emails about notificatoins
29         - make a new user follow itself
30     """
31     _name = 'res.users'
32     _inherit = ['res.users', 'mail.thread']
33     _inherits = {'mail.alias': 'alias_id'}
34     
35     _columns = {
36         'notification_email_pref': fields.selection([
37                         ('all', 'All feeds'),
38                         ('comments', 'Only comments'),
39                         ('to_me', 'Only when sent directly to me'),
40                         ('none', 'Never')
41                         ], 'Receive Feeds by Email', required=True,
42                         help="Choose in which case you want to receive an email when you receive new feeds."),
43         'alias_id': fields.many2one('mail.alias', 'Mail Alias', ondelete="cascade", required=True, 
44                                     help="This Unique Mail Box Alias of the User allows to manage the Seamless email communication between Mail Box and OpenERP," 
45                                          "This Alias MailBox manage the Users email communication."),
46     }
47     
48     _defaults = {
49         'notification_email_pref': 'none',
50     }
51     
52     def __init__(self, pool, cr):
53         """ Override of __init__ to add access rights on notification_email_pref
54             field. Access rights are disabled by default, but allowed on
55             fields defined in self.SELF_WRITEABLE_FIELDS.
56         """
57         init_res = super(res_users, self).__init__(pool, cr)
58         # duplicate list to avoid modifying the original reference
59         self.SELF_WRITEABLE_FIELDS = list(self.SELF_WRITEABLE_FIELDS)
60         self.SELF_WRITEABLE_FIELDS.append('notification_email_pref')
61         return init_res
62     
63     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
64         res = super(res_users,self).fields_view_get(cr, uid, view_id, view_type, context, toolbar=toolbar, submenu=submenu)
65         if view_type == 'form':
66             domain = self.pool.get("ir.config_parameter").get_param(cr, uid, "mail.catchall.domain", context=context)
67             if not domain:
68                 doc = etree.XML(res['arch'])
69                 alias_node = doc.xpath("//field[@name='alias_id']")[0]
70                 parent = alias_node.getparent()
71                 parent.remove(alias_node)
72                 res['arch'] = etree.tostring(doc)
73         return res
74     
75     def create(self, cr, uid, data, context=None):
76         # create default alias same as the login
77         alias_pool = self.pool.get('mail.alias')
78         alias_id = alias_pool.create_unique_alias(cr, uid, {'alias_name': data['name'], 'alias_model_id': self._name}, context=context)
79         data.update({'alias_id': alias_id})
80         user_id = super(res_users, self).create(cr, uid, data, context=context)
81         alias_pool.write(cr, uid, [alias_id], {"alias_force_thread_id": user_id}, context)
82         user = self.browse(cr, uid, user_id, context=context)
83         # make user follow itself
84         self.message_subscribe(cr, uid, [user_id], [user_id], context=context)
85         # create a welcome message to broadcast
86         company_name = user.company_id.name if user.company_id else 'the company'
87         message = _('%s has joined %s! You may leave him/her a message to celebrate a new arrival in the company ! You can help him/her doing its first steps on OpenERP.') % (user.name, company_name)
88         # TODO: clean the broadcast feature. As this is not cleany specified, temporarily remove the message broadcasting that is not buggy but not very nice.
89         #self.message_broadcast(cr, uid, [user.id], 'Welcome notification', message, context=context)
90         return user_id
91     
92     def write(self, cr, uid, ids, vals, context=None):
93         # if login of user have been changed then change alias of user also.
94         if 'login' in vals.keys():
95             for user in self.browse(cr, uid, ids, context=context):
96                 self.pool.get('mail.alias').write(cr, uid, [user.alias_id.id], {'alias_name': vals['login']}, context=context)
97         return super(res_users, self).write(cr, uid, ids, vals, context=context)
98
99     
100     def message_load_ids(self, cr, uid, ids, limit=100, offset=0, domain=[], ascent=False, root_ids=[False], context=None):
101         """ Override of message_load_ids
102             User discussion page :
103             - messages posted on res.users, res_id = user.id
104             - messages directly sent to user with @user_login
105         """
106         if context is None:
107             context = {}
108         msg_obj = self.pool.get('mail.message')
109         msg_ids = []
110         for user in self.browse(cr, uid, ids, context=context):
111             msg_ids += msg_obj.search(cr, uid, ['|', '|', ('body_text', 'like', '@%s' % (user.login)), ('body_html', 'like', '@%s' % (user.login)), '&', ('res_id', '=', user.id), ('model', '=', self._name)] + domain,
112             limit=limit, offset=offset, context=context)
113         if (ascent): msg_ids = self._message_add_ancestor_ids(cr, uid, ids, msg_ids, root_ids, context=context)
114         return msg_ids