[IMP] mail cleaning

bzr revid: fp@tinyerp.com-20120816101848-0l3wyu98zjmtkfks
This commit is contained in:
Fabien Pinckaers 2012-08-16 12:18:48 +02:00
parent c7c1cb1037
commit 73827c5525
6 changed files with 80 additions and 81 deletions

View File

@ -107,17 +107,12 @@ class mail_group(osv.Model):
# FP Note: code to be improved. Check we have a code for res.users # FP Note: code to be improved. Check we have a code for res.users
# when we give them a new group. # when we give them a new group.
def _subscribe_user_with_group_m2m_command(self, cr, uid, ids, group_ids_command, context=None): def _subscribe_users(self, cr, uid, ids, context=None):
# form: {'group_ids': [(3, 10), (3, 3), (4, 10), (4, 3)]} or {'group_ids': [(6, 0, [ids]} for mail_group in self.browse(cr, uid, ids, context=context):
user_group_ids = [command[1] for command in group_ids_command if command[0] == 4] partner_ids = []
user_group_ids += [id for command in group_ids_command if command[0] == 6 for id in command[2]] for group in mail_group.group_ids:
# retrieve the user member of those groups partner_ids += [user.partner_id.id for user in group.users]
partner_ids = [] self.message_subscribe(cr, uid, ids, partner_ids, context=context)
res_groups_obj = self.pool.get('res.groups')
for group in res_groups_obj.browse(cr, uid, user_group_ids, context=context):
partner_ids += [user.partner_id.id for user in group.users]
# subscribe the users
return self.message_subscribe(cr, uid, ids, partner_ids, context=context)
def create(self, cr, uid, vals, context=None): def create(self, cr, uid, vals, context=None):
mail_alias = self.pool.get('mail.alias') mail_alias = self.pool.get('mail.alias')
@ -150,8 +145,7 @@ class mail_group(osv.Model):
mail_alias.write(cr, uid, [vals['alias_id']], {"alias_force_thread_id": mail_group_id}, context) mail_alias.write(cr, uid, [vals['alias_id']], {"alias_force_thread_id": mail_group_id}, context)
if vals.get('group_ids'): if vals.get('group_ids'):
self._subscribe_user_with_group_m2m_command(cr, uid, [mail_group_id], vals.get('group_ids'), context=context) self._subscribe_users(cr, uid, [mail_group_id], context=context)
return mail_group_id return mail_group_id
def unlink(self, cr, uid, ids, context=None): def unlink(self, cr, uid, ids, context=None):
@ -163,7 +157,8 @@ class mail_group(osv.Model):
return res return res
def write(self, cr, uid, ids, vals, context=None): def write(self, cr, uid, ids, vals, context=None):
result = super(mail_group, self).write(cr, uid, ids, vals, context=context)
if vals.get('group_ids'): if vals.get('group_ids'):
self._subscribe_user_with_group_m2m_command(cr, uid, ids, vals.get('group_ids'), context=context) self._subscribe_users(cr, uid, ids, vals.get('group_ids'), context=context)
return super(mail_group, self).write(cr, uid, ids, vals, context=context) return result

View File

@ -49,6 +49,7 @@ class mail_mail(osv.Model):
'content_subtype': 'plain', 'content_subtype': 'plain',
} }
# FP Note: should we use a dict instead ?
def schedule_with_attach(self, cr, uid, email_from, email_to, subject, body, model=False, type='email', def schedule_with_attach(self, cr, uid, email_from, email_to, subject, body, model=False, type='email',
email_cc=None, reply_to=False, partner_ids=None, attachments=None, email_cc=None, reply_to=False, partner_ids=None, attachments=None,
message_id=False, references=False, res_id=False, content_subtype='plain', message_id=False, references=False, res_id=False, content_subtype='plain',

View File

@ -360,5 +360,3 @@ class mail_message(osv.Model):
msg['body'] = msg['body_text'] msg['body'] = msg['body_text']
msg['sub_type'] = msg['content_subtype'] or 'plain' msg['sub_type'] = msg['content_subtype'] or 'plain'
return msg return msg

View File

@ -103,8 +103,8 @@ class mail_thread(osv.Model):
return res return res
# FP Note: todo # FP Note: todo
def _search_state(self, tobj, cr, uid, obj=None, name=None, domain=None, context=None): def _search_unread(self, tobj, cr, uid, obj=None, name=None, domain=None, context=None):
return [('id','in',[])] return []
_columns = { _columns = {
'message_is_follower': fields.function(_get_is_follower, 'message_is_follower': fields.function(_get_is_follower,
@ -116,7 +116,7 @@ class mail_thread(osv.Model):
domain=lambda self: [('model','=',self._name)], domain=lambda self: [('model','=',self._name)],
string='Related Messages', string='Related Messages',
help="All messages related to the current document."), help="All messages related to the current document."),
'message_unread': fields.function(_get_message_data, fnct_search=_search_state, 'Message Read', 'message_unread': fields.function(_get_message_data, fnct_search=_search_unread, 'Has Unread Messages',
help="When checked, new messages require your attention.", help="When checked, new messages require your attention.",
multi="_get_message_data"), multi="_get_message_data"),
'message_summary': fields.function(_get_message_data, method=True, 'message_summary': fields.function(_get_message_data, method=True,
@ -131,12 +131,10 @@ class mail_thread(osv.Model):
#------------------------------------------------------ #------------------------------------------------------
def create(self, cr, uid, vals, context=None): def create(self, cr, uid, vals, context=None):
""" Override of create to subscribe : """ Override of create to subscribe the current user
- the writer
- followers given by the monitored fields
""" """
thread_id = super(mail_thread, self).create(cr, uid, vals, context=context) thread_id = super(mail_thread, self).create(cr, uid, vals, context=context)
self.message_subscribe(cr, uid, [thread_id], [uid], context=context) self.message_subscribe_users(cr, uid, [thread_id], [uid], context=context)
return thread_id return thread_id
def unlink(self, cr, uid, ids, context=None): def unlink(self, cr, uid, ids, context=None):
@ -155,6 +153,8 @@ class mail_thread(osv.Model):
# mail.message wrappers and tools # mail.message wrappers and tools
#------------------------------------------------------ #------------------------------------------------------
# FP Note: should we support attachment ? Also, this method must be on
# the mail.message object, not on the thread.
def message_create(self, cr, uid, thread_id, vals, context=None): def message_create(self, cr, uid, thread_id, vals, context=None):
""" OpenChatter: wrapper of mail.message create method """ OpenChatter: wrapper of mail.message create method
- creates the mail.message - creates the mail.message
@ -177,14 +177,7 @@ class mail_thread(osv.Model):
# Generic message api # Generic message api
#------------------------------------------------------ #------------------------------------------------------
def message_capable_models(self, cr, uid, context=None): # I propose to remove this. Everyone should use message_create instead.
ret_dict = {}
for model_name in self.pool.obj_list():
model = self.pool.get(model_name)
if 'mail.thread' in getattr(model, '_inherit', []):
ret_dict[model_name] = model._description
return ret_dict
def message_append(self, cr, uid, threads, subject, body_text=None, body_html=None, def message_append(self, cr, uid, threads, subject, body_text=None, body_html=None,
type='email', email_date=None, parent_id=False, type='email', email_date=None, parent_id=False,
content_subtype='plain', state=None, content_subtype='plain', state=None,
@ -313,6 +306,7 @@ class mail_thread(osv.Model):
new_msg_ids.append(self.message_create(cr, uid, thread.id, data, context=context)) new_msg_ids.append(self.message_create(cr, uid, thread.id, data, context=context))
return new_msg_ids return new_msg_ids
# to be removed completly
def message_append_dict(self, cr, uid, ids, msg_dict, context=None): def message_append_dict(self, cr, uid, ids, msg_dict, context=None):
"""Creates a new mail.message attached to the given threads (``ids``), """Creates a new mail.message attached to the given threads (``ids``),
with the contents of ``msg_dict``, by calling ``message_append`` with the contents of ``msg_dict``, by calling ``message_append``
@ -866,19 +860,37 @@ class mail_thread(osv.Model):
return True return True
return False return False
def message_subscribe(self, cr, uid, ids, partner_ids=None, context=None): def message_subscribe_users(self, cr, uid, ids, user_ids=None, context=None):
if not user_ids: user_ids = [uid]
partners = {}
for user in self.pool.get('res.users').browse(cr, uid, user_ids, context=context):
partners[user.partner_id.id] = True
return self.message_subscribe(cr, uid, ids, partners.keys(), context=context)
def message_subscribe(self, cr, uid, ids, partner_ids, context=None):
""" """
:param user_ids: a list of user_ids; if not set, subscribe :param partner_ids: a list of user_ids; if not set, subscribe
uid instead uid instead
:param return: new value of followers, for Chatter :param return: new value of followers, for Chatter
""" """
subscription_obj = self.pool.get('mail.subscription') obj = self.pool.get('mail.followers')
objids = obj.search(cr, uid, [
('res_id', 'in', ids),
('res_model', '=', self._name),
('partner_id', 'in', partner_ids),
], context=context)
followers = {}
for follow in obj.browse(cr, uid, objids, context=context)
followers.setdefault(follow.partner_id.id, {})[follow.res_id] = True
create_ids = [] create_ids = []
for id in ids: for res_id in ids:
already_subscribed_user_ids = self.message_get_followers(cr, uid, [id], context=context) for partner_id in partner_ids:
for user_id in to_subscribe_uids: if followers.get(partner_id, {}).get(res_id, False):
if user_id in already_subscribed_user_ids: continue continue
create_ids.append(subscription_obj.create(cr, uid, {'res_model': self._name, 'res_id': id, 'user_id': user_id}, context=context)) create_ids.append(obj.create(cr, uid, {
'res_model': self._name,
'res_id': res_id, 'partner_id': partner_id
}, context=context))
return create_ids return create_ids
def message_unsubscribe(self, cr, uid, ids, user_ids = None, context=None): def message_unsubscribe(self, cr, uid, ids, user_ids = None, context=None):

View File

@ -31,18 +31,11 @@ class res_partner_mail(osv.Model):
The purpose is to add messages directly sent to the partner. It also The purpose is to add messages directly sent to the partner. It also
adds messages pushed to the related user, if any, using @login. adds messages pushed to the related user, if any, using @login.
""" """
initial_domain = super(res_partner_mail, self).message_search_get_domain(cr, uid, ids, context=context)
# to avoid models inheriting from res.partner
if self._name != 'res.partner': if self._name != 'res.partner':
return initial_domain return super(res_partner_mail, self).message_search_get_domain(cr, uid, ids, context=context)
# add message linked to the partner # add message linked to the partner
search_domain = ['|'] + initial_domain + ['|', ('partner_id', 'in', ids), ('partner_ids', 'in', ids)] return [('partner_ids', 'in', ids)]
# if partner is linked to a user: find @login
res_users_obj = self.pool.get('res.users')
user_ids = res_users_obj.search(cr, uid, [('partner_id', 'in', ids)], context=context)
for user in res_users_obj.browse(cr, uid, user_ids, context=context):
search_domain = ['|'] + search_domain + ['|', ('body_text', 'like', '@%s' % (user.login)), ('body_html', 'like', '@%s' % (user.login))]
return search_domain
_columns = { _columns = {
'notification_email_pref': fields.selection([ 'notification_email_pref': fields.selection([
('all', 'All feeds'), ('all', 'All feeds'),

View File

@ -98,17 +98,16 @@ class res_users(osv.Model):
alias_id = mail_alias.create_unique_alias(cr, uid, {'alias_name': data['login']}, model_name=self._name, context=context) alias_id = mail_alias.create_unique_alias(cr, uid, {'alias_name': data['login']}, model_name=self._name, context=context)
data['alias_id'] = alias_id data['alias_id'] = alias_id
data.pop('alias_name', None) # prevent errors during copy() data.pop('alias_name', None) # prevent errors during copy()
# create user that follows its related partner # create user that follows its related partner
user_id = super(res_users, self).create(cr, uid, data, context=context) self.pool.get('res.partner').message_subscribe_users(cr, uid, [user_id], [user_id], context=context)
user = self.browse(cr, uid, user_id, context=context)
self.pool.get('res.partner').message_subscribe(cr, uid, [user.partner_id.id], [user_id], context=context)
# alias # alias
mail_alias.write(cr, SUPERUSER_ID, [alias_id], {"alias_force_thread_id": user_id}, context) mail_alias.write(cr, SUPERUSER_ID, [alias_id], {"alias_force_thread_id": user_id}, context)
# create a welcome message # create a welcome message
self.create_welcome_message(cr, uid, user, context=context) self._create_welcome_message(cr, uid, user, context=context)
return user_id return user_id
def create_welcome_message(self, cr, uid, user, context=None): def _create_welcome_message(self, cr, uid, user, context=None):
company_name = user.company_id.name if user.company_id else _('the company') company_name = user.company_id.name if user.company_id else _('the company')
subject = '''%s has joined %s.''' % (user.name, company_name) subject = '''%s has joined %s.''' % (user.name, company_name)
body = '''Welcome to OpenERP !''' body = '''Welcome to OpenERP !'''
@ -135,35 +134,35 @@ class res_users(osv.Model):
# that should help cleaning those wrappers # that should help cleaning those wrappers
# -------------------------------------------------- # --------------------------------------------------
def message_append(self, cr, uid, threads, subject, body_text=None, body_html=None, #def message_append(self, cr, uid, threads, subject, body_text=None, body_html=None,
type='email', email_date=None, parent_id=False, # type='email', email_date=None, parent_id=False,
content_subtype='plain', state=None, # content_subtype='plain', state=None,
partner_ids=None, email_from=False, email_to=False, # partner_ids=None, email_from=False, email_to=False,
email_cc=None, email_bcc=None, reply_to=None, # email_cc=None, email_bcc=None, reply_to=None,
headers=None, message_id=False, references=None, # headers=None, message_id=False, references=None,
attachments=None, original=None, context=None): # attachments=None, original=None, context=None):
for user in self.browse(cr, uid, threads, context=context): # for user in self.browse(cr, uid, threads, context=context):
user.partner_id.message_append(subject, body_text, body_html, type, email_date, parent_id, # user.partner_id.message_append(subject, body_text, body_html, type, email_date, parent_id,
content_subtype, state, partner_ids, email_from, email_to, email_cc, email_bcc, reply_to, # content_subtype, state, partner_ids, email_from, email_to, email_cc, email_bcc, reply_to,
headers, message_id, references, attachments, original) # headers, message_id, references, attachments, original)
def message_read(self, cr, uid, ids, fetch_ancestors=False, ancestor_ids=None, #def message_read(self, cr, uid, ids, fetch_ancestors=False, ancestor_ids=None,
limit=100, offset=0, domain=None, context=None): # limit=100, offset=0, domain=None, context=None):
for user in self.browse(cr, uid, ids, context=context): # for user in self.browse(cr, uid, ids, context=context):
return user.partner_id.message_read(fetch_ancestors, ancestor_ids, limit, offset, domain) # return user.partner_id.message_read(fetch_ancestors, ancestor_ids, limit, offset, domain)
def message_search(self, cr, uid, ids, fetch_ancestors=False, ancestor_ids=None, #def message_search(self, cr, uid, ids, fetch_ancestors=False, ancestor_ids=None,
limit=100, offset=0, domain=None, count=False, context=None): # limit=100, offset=0, domain=None, count=False, context=None):
for user in self.browse(cr, uid, ids, context=context): # for user in self.browse(cr, uid, ids, context=context):
return user.partner_id.message_search(fetch_ancestors, ancestor_ids, limit, offset, domain, count) # return user.partner_id.message_search(fetch_ancestors, ancestor_ids, limit, offset, domain, count)
def message_subscribe(self, cr, uid, ids, user_ids = None, context=None): #def message_subscribe(self, cr, uid, ids, user_ids = None, context=None):
for user in self.browse(cr, uid, ids, context=context): # for user in self.browse(cr, uid, ids, context=context):
return user.partner_id.message_subscribe(user_ids) # return user.partner_id.message_subscribe(user_ids)
def message_unsubscribe(self, cr, uid, ids, user_ids = None, context=None): #def message_unsubscribe(self, cr, uid, ids, user_ids = None, context=None):
for user in self.browse(cr, uid, ids, context=context): # for user in self.browse(cr, uid, ids, context=context):
return user.partner_id.message_unsubscribe(user_ids) # return user.partner_id.message_unsubscribe(user_ids)
class res_users_mail_group(osv.Model): class res_users_mail_group(osv.Model):
@ -175,6 +174,7 @@ class res_users_mail_group(osv.Model):
_name = 'res.users' _name = 'res.users'
_inherit = ['res.users'] _inherit = ['res.users']
# FP Note: to improve
def write(self, cr, uid, ids, vals, context=None): def write(self, cr, uid, ids, vals, context=None):
write_res = super(res_users_mail_group, self).write(cr, uid, ids, vals, context=context) write_res = super(res_users_mail_group, self).write(cr, uid, ids, vals, context=context)
if vals.get('groups_id'): if vals.get('groups_id'):
@ -195,6 +195,7 @@ class res_groups_mail_group(osv.Model):
_name = 'res.groups' _name = 'res.groups'
_inherit = 'res.groups' _inherit = 'res.groups'
# FP Note: to improve
def write(self, cr, uid, ids, vals, context=None): def write(self, cr, uid, ids, vals, context=None):
if vals.get('users'): if vals.get('users'):
# form: {'group_ids': [(3, 10), (3, 3), (4, 10), (4, 3)]} or {'group_ids': [(6, 0, [ids]} # form: {'group_ids': [(3, 10), (3, 3), (4, 10), (4, 3)]} or {'group_ids': [(6, 0, [ids]}
@ -205,4 +206,3 @@ class res_groups_mail_group(osv.Model):
mail_group_obj.message_subscribe(cr, uid, mail_group_ids, user_ids, context=context) mail_group_obj.message_subscribe(cr, uid, mail_group_ids, user_ids, context=context)
return super(res_groups_mail_group, self).write(cr, uid, ids, vals, context=context) return super(res_groups_mail_group, self).write(cr, uid, ids, vals, context=context)
# vim:et: