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 openerp import SUPERUSER_ID
24 from tools.translate import _
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
33 _inherit = ['res.users']
34 _inherits = {'mail.alias': 'alias_id'}
37 'notification_email_pref': fields.selection([
39 ('to_me', 'Only send directly to me'),
41 ], 'Receive Feeds by Email', required=True,
42 help="Choose in which case you want to receive an email when you "\
43 "receive new feeds."),
44 'alias_id': fields.many2one('mail.alias', 'Alias', ondelete="cascade", required=True,
45 help="Email address internally associated with this user. Incoming "\
46 "emails will appear in the user's notifications."),
50 'notification_email_pref': 'to_me',
51 'alias_domain': False, # always hide alias during creation
54 def __init__(self, pool, cr):
55 """ Override of __init__ to add access rights on notification_email_pref
56 field. Access rights are disabled by default, but allowed on
57 fields defined in self.SELF_WRITEABLE_FIELDS.
59 init_res = super(res_users, self).__init__(pool, cr)
60 # duplicate list to avoid modifying the original reference
61 self.SELF_WRITEABLE_FIELDS = list(self.SELF_WRITEABLE_FIELDS)
62 self.SELF_WRITEABLE_FIELDS.append('notification_email_pref')
65 def _auto_init(self, cr, context=None):
66 """Installation hook to create aliases for all users and avoid constraint errors."""
67 self.pool.get('mail.alias').migrate_to_alias(cr, self._name, self._table, super(res_users,self)._auto_init,
68 self._columns['alias_id'], 'login', alias_force_key='id', context=context)
70 def create(self, cr, uid, data, context=None):
71 # create default alias same as the login
72 mail_alias = self.pool.get('mail.alias')
73 alias_id = mail_alias.create_unique_alias(cr, uid, {'alias_name': data['login']}, model_name=self._name, context=context)
74 data['alias_id'] = alias_id
75 data.pop('alias_name', None) # prevent errors during copy()
76 # create user that follows its related partner
77 user_id = super(res_users, self).create(cr, uid, data, context=context)
78 user = self.browse(cr, uid, user_id, context=context)
79 self.pool.get('res.partner').message_subscribe(cr, uid, [user.partner_id.id], [user_id], context=context)
81 mail_alias.write(cr, SUPERUSER_ID, [alias_id], {"alias_force_thread_id": user_id}, context)
82 # create a welcome message
83 self.create_welcome_message(cr, uid, user, context=context)
86 def create_welcome_message(self, cr, uid, user, context=None):
87 company_name = user.company_id.name if user.company_id else _('the company')
88 subject = '''%s has joined %s.''' % (user.name, company_name)
89 body = '''Welcome to OpenERP !'''
90 # TODO change 1 into user.id but catch errors
91 return self.pool.get('res.partner').message_append_note(cr, SUPERUSER_ID, [user.partner_id.id],
92 subject=subject, body=body, type='comment', content_subtype='html', context=context)
94 def write(self, cr, uid, ids, vals, context=None):
95 # User alias is sync'ed with login
96 if vals.get('login'): vals['alias_name'] = vals['login']
97 return super(res_users, self).write(cr, uid, ids, vals, context=context)
99 def unlink(self, cr, uid, ids, context=None):
100 # Cascade-delete mail aliases as well, as they should not exist without the user.
101 alias_pool = self.pool.get('mail.alias')
102 alias_ids = [user.alias_id.id for user in self.browse(cr, uid, ids, context=context) if user.alias_id]
103 res = super(res_users, self).unlink(cr, uid, ids, context=context)
104 alias_pool.unlink(cr, uid, alias_ids, context=context)
107 # --------------------------------------------------
108 # Wrappers on partner methods for Chatter
109 # #FIXME: another branch holds a refactoring of mail.thread
110 # that should help cleaning those wrappers
111 # --------------------------------------------------
113 def message_append(self, cr, uid, threads, subject, body_text=None, body_html=None,
114 type='email', email_date=None, parent_id=False,
115 content_subtype='plain', state=None,
116 partner_ids=None, email_from=False, email_to=False,
117 email_cc=None, email_bcc=None, reply_to=None,
118 headers=None, message_id=False, references=None,
119 attachments=None, original=None, context=None):
120 for user in self.browse(cr, uid, threads, context=context):
121 user.partner_id.message_append(subject, body_text, body_html, type, email_date, parent_id,
122 content_subtype, state, partner_ids, email_from, email_to, email_cc, email_bcc, reply_to,
123 headers, message_id, references, attachments, original)
125 def message_read(self, cr, uid, ids, fetch_ancestors=False, ancestor_ids=None,
126 limit=100, offset=0, domain=None, context=None):
127 for user in self.browse(cr, uid, ids, context=context):
128 return user.partner_id.message_read(fetch_ancestors, ancestor_ids, limit, offset, domain)
130 def message_search(self, cr, uid, ids, fetch_ancestors=False, ancestor_ids=None,
131 limit=100, offset=0, domain=None, count=False, context=None):
132 for user in self.browse(cr, uid, ids, context=context):
133 return user.partner_id.message_search(fetch_ancestors, ancestor_ids, limit, offset, domain, count)
135 def message_subscribe(self, cr, uid, ids, user_ids = None, context=None):
136 for user in self.browse(cr, uid, ids, context=context):
137 return user.partner_id.message_subscribe(user_ids)
139 def message_unsubscribe(self, cr, uid, ids, user_ids = None, context=None):
140 for user in self.browse(cr, uid, ids, context=context):
141 return user.partner_id.message_unsubscribe(user_ids)
144 class res_users_mail_group(osv.Model):
145 """ Update of res.groups class
146 - if adding/removing users from a group, check mail.groups linked to
147 this user group, and subscribe / unsubscribe them from the discussion
148 group. This is done by overriding the write method.
151 _inherit = ['res.users']
153 def write(self, cr, uid, ids, vals, context=None):
154 write_res = super(res_users_mail_group, self).write(cr, uid, ids, vals, context=context)
155 if vals.get('groups_id'):
156 # form: {'group_ids': [(3, 10), (3, 3), (4, 10), (4, 3)]} or {'group_ids': [(6, 0, [ids]}
157 user_group_ids = [command[1] for command in vals['groups_id'] if command[0] == 4]
158 user_group_ids += [id for command in vals['groups_id'] if command[0] == 6 for id in command[2]]
159 mail_group_obj = self.pool.get('mail.group')
160 mail_group_ids = mail_group_obj.search(cr, uid, [('group_ids', 'in', user_group_ids)], context=context)
161 mail_group_obj.message_subscribe(cr, uid, mail_group_ids, ids, context=context)
164 class res_groups_mail_group(osv.Model):
165 """ Update of res.groups class
166 - if adding/removing users from a group, check mail.groups linked to
167 this user group, and subscribe / unsubscribe them from the discussion
168 group. This is done by overriding the write method.
171 _inherit = 'res.groups'
173 def write(self, cr, uid, ids, vals, context=None):
174 if vals.get('users'):
175 # form: {'group_ids': [(3, 10), (3, 3), (4, 10), (4, 3)]} or {'group_ids': [(6, 0, [ids]}
176 user_ids = [command[1] for command in vals['users'] if command[0] == 4]
177 user_ids += [id for command in vals['users'] if command[0] == 6 for id in command[2]]
178 mail_group_obj = self.pool.get('mail.group')
179 mail_group_ids = mail_group_obj.search(cr, uid, [('group_ids', 'in', ids)], context=context)
180 mail_group_obj.message_subscribe(cr, uid, mail_group_ids, user_ids, context=context)
181 return super(res_groups_mail_group, self).write(cr, uid, ids, vals, context=context)