1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2009-Today OpenERP SA (<http://www.openerp.com>)
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
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
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/>
20 ##############################################################################
22 from osv import osv, fields
23 from tools.translate import _
25 class res_users(osv.osv):
26 """ Update of res.users class
27 - add a preference about sending emails about notificatoins
28 - make a new user follow itself
31 _inherit = ['res.users', 'mail.thread']
32 _inherits = {'mail.alias': 'alias_id'}
35 'notification_email_pref': fields.selection([
37 ('comments', 'Only comments'),
38 ('to_me', 'Only when sent directly to me'),
40 ], 'Receive Feeds by E-mail', required=True,
41 help="Choose in which case you want to receive an email when you receive new feeds."),
42 'alias_id': fields.many2one('mail.alias', 'Mail Alias', ondelete="cascade", required=True),
46 'notification_email_pref': 'none',
49 def __init__(self, pool, cr):
50 """ Override of __init__ to add access rights on notification_email_pref
51 field. Access rights are disabled by default, but allowed on
52 fields defined in self.SELF_WRITEABLE_FIELDS.
54 init_res = super(res_users, self).__init__(pool, cr)
55 # duplicate list to avoid modifying the original reference
56 self.SELF_WRITEABLE_FIELDS = list(self.SELF_WRITEABLE_FIELDS)
57 self.SELF_WRITEABLE_FIELDS.append('notification_email_pref')
60 def create(self, cr, uid, data, context=None):
61 # create default alias same as the login
62 model_pool = self.pool.get('ir.model.data')
63 alias_pool = self.pool.get('mail.alias')
64 res_id = model_pool.get_object( cr, uid, "mail", "model_res_users")
65 data.update({'alias_name': data.get('login'),
66 'alias_model_id': res_id.id})
67 name = alias_pool.create_unique_alias(cr, uid, data, sequence=False ,context=context)
68 user_id = super(res_users, self).create(cr, uid, data, context=context)
69 user = self.browse(cr, uid, user_id, context=context)
70 alias_pool.write(cr, uid, [user.alias_id.id], {"alias_force_thread_id": user.id}, context)
71 # make user follow itself
72 self.message_subscribe(cr, uid, [user_id], [user_id], context=context)
73 # create a welcome message to broadcast
74 company_name = user.company_id.name if user.company_id else 'the company'
75 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)
76 # TODO: clean the broadcast feature. As this is not cleany specified, temporarily remove the message broadcasting that is not buggy but not very nice.
77 #self.message_broadcast(cr, uid, [user.id], 'Welcome notification', message, context=context)
80 def write(self, cr, uid, ids, vals, context=None):
81 # if login of user have been changed then change alias of user also.
82 if 'login' in vals.keys():
83 for user in self.browse(cr, uid, ids, context=context):
84 domain = self.pool.get("ir.config_parameter").get_param(cr, uid, "mail.catchall.domain", context=context)
85 name = "%s@%s"%(vals['login'], domain)
86 self.pool.get('mail.alias').write(cr, uid, [user.alias_id.id], {'alias_name': name}, context=context)
87 return super(res_users, self).write(cr, uid, ids, vals, context=context)
90 def message_load_ids(self, cr, uid, ids, limit=100, offset=0, domain=[], ascent=False, root_ids=[False], context=None):
91 """ Override of message_load_ids
92 User discussion page :
93 - messages posted on res.users, res_id = user.id
94 - messages directly sent to user with @user_login
98 msg_obj = self.pool.get('mail.message')
100 for user in self.browse(cr, uid, ids, context=context):
101 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,
102 limit=limit, offset=offset, context=context)
103 if (ascent): msg_ids = self._message_add_ancestor_ids(cr, uid, ids, msg_ids, root_ids, context=context)