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 notifications
28 - make a new user follow itself
29 - add a welcome message
32 _inherit = ['res.users', 'mail.thread']
35 'notification_email_pref': fields.selection([
37 ('comments', 'Only comments'),
38 ('to_me', 'Only when sent directly to me'),
40 ], 'Receive Feeds by Email', required=True,
41 help="Choose in which case you want to receive an email when you "\
42 "receive new feeds."),
46 'notification_email_pref': 'to_me',
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 user_id = super(res_users, self).create(cr, uid, data, context=context)
62 user = self.browse(cr, uid, [user_id], context=context)[0]
63 # make user follow itself
64 self.message_subscribe(cr, uid, [user_id], [user_id], context=context)
65 # create a welcome message
66 company_name = user.company_id.name if user.company_id else 'the company'
67 message = _('%s has joined %s! Welcome in OpenERP !') % (user.name, company_name)
68 self.message_append_note(cr, uid, [user_id], subject='Welcom to OpenERP', body=message, type='comment', context=context)
71 def message_search_get_domain(self, cr, uid, ids, context=None):
72 """ Override of message_search_get_domain for partner discussion page.
73 The purpose is to add messages directly sent to user using
76 initial_domain = super(res_users, self).message_search_get_domain(cr, uid, ids, context=context)
78 for user in self.browse(cr, uid, ids, context=context):
80 custom_domain += ['|']
81 custom_domain += ['|', ('body_text', 'like', '@%s' % (user.login)), ('body_html', 'like', '@%s' % (user.login))]
82 return ['|'] + initial_domain + custom_domain
84 class res_users_mail_group(osv.osv):
85 """ Update of res.groups class
86 - if adding/removing users from a group, check mail.groups linked to
87 this user group, and subscribe / unsubscribe them from the discussion
88 group. This is done by overriding the write method.
91 _inherit = ['res.users', 'mail.thread']
93 def write(self, cr, uid, ids, vals, context=None):
94 write_res = super(res_users_mail_group, self).write(cr, uid, ids, vals, context=context)
95 if vals.get('groups_id'):
96 # form: {'group_ids': [(3, 10), (3, 3), (4, 10), (4, 3)]} or {'group_ids': [(6, 0, [ids]}
97 user_group_ids = [command[1] for command in vals['groups_id'] if command[0] == 4]
98 user_group_ids += [id for command in vals['groups_id'] if command[0] == 6 for id in command[2]]
99 mail_group_obj = self.pool.get('mail.group')
100 mail_group_ids = mail_group_obj.search(cr, uid, [('group_ids', 'in', user_group_ids)], context=context)
101 mail_group_obj.message_subscribe(cr, uid, mail_group_ids, ids, context=context)
105 class res_groups_mail_group(osv.osv):
106 """ Update of res.groups class
107 - if adding/removing users from a group, check mail.groups linked to
108 this user group, and subscribe / unsubscribe them from the discussion
109 group. This is done by overriding the write method.
112 _inherit = 'res.groups'
114 def write(self, cr, uid, ids, vals, context=None):
115 if vals.get('users'):
116 # form: {'group_ids': [(3, 10), (3, 3), (4, 10), (4, 3)]} or {'group_ids': [(6, 0, [ids]}
117 user_ids = [command[1] for command in vals['users'] if command[0] == 4]
118 user_ids += [id for command in vals['users'] if command[0] == 6 for id in command[2]]
119 mail_group_obj = self.pool.get('mail.group')
120 mail_group_ids = mail_group_obj.search(cr, uid, [('group_ids', 'in', ids)], context=context)
121 mail_group_obj.message_subscribe(cr, uid, mail_group_ids, user_ids, context=context)
122 return super(res_groups_mail_group, self).write(cr, uid, ids, vals, context=context)