2011-07-22 16:34:57 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
2012-03-13 15:06:35 +00:00
|
|
|
# Copyright (C) 2009-today OpenERP SA (<http://www.openerp.com>)
|
2011-07-22 16:34:57 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
import time
|
|
|
|
import tools
|
2011-08-23 17:58:09 +00:00
|
|
|
import base64
|
2011-07-22 16:34:57 +00:00
|
|
|
import email
|
|
|
|
from email.utils import parsedate
|
|
|
|
|
2012-03-08 11:24:45 +00:00
|
|
|
import re
|
2011-07-22 16:34:57 +00:00
|
|
|
import logging
|
|
|
|
import xmlrpclib
|
|
|
|
from osv import osv, fields
|
|
|
|
from tools.translate import _
|
|
|
|
from mail_message import decode, to_email
|
|
|
|
|
2012-02-01 16:21:36 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
2011-07-22 16:34:57 +00:00
|
|
|
|
|
|
|
class mail_thread(osv.osv):
|
|
|
|
'''Mixin model, meant to be inherited by any model that needs to
|
|
|
|
act as a discussion topic on which messages can be attached.
|
2011-08-25 12:27:57 +00:00
|
|
|
Public methods are prefixed with ``message_`` in order to avoid
|
|
|
|
name collisions with methods of the models that will inherit
|
|
|
|
from this mixin.
|
|
|
|
|
2012-03-13 15:06:35 +00:00
|
|
|
``mail.thread`` is designed to work without adding any field
|
|
|
|
to the extended models. All functionalities and expected behavior
|
|
|
|
are managed by mail.thread, using model name and record ids.
|
|
|
|
A widget has been designed for the 6.1 and following version of OpenERP
|
|
|
|
web-client. However, due to technical limitations, ``mail.thread``
|
|
|
|
adds a simulated one2many field, to display the web widget by
|
|
|
|
overriding the default field displayed. Using this field
|
|
|
|
is not recommanded has it will disappeear in future version
|
|
|
|
of OpenERP, leading to a pure mixin class.
|
2011-08-25 12:27:57 +00:00
|
|
|
|
|
|
|
Inheriting classes are not required to implement any method, as the
|
|
|
|
default implementation will work for any model. However it is common
|
|
|
|
to override at least the ``message_new`` and ``message_update``
|
|
|
|
methods (calling ``super``) to add model-specific behavior at
|
2012-03-13 15:06:35 +00:00
|
|
|
creation and update of a thread; and ``message_get_subscribers``
|
|
|
|
to manage more precisely the social aspect of the thread through
|
|
|
|
the followers.
|
2011-07-22 16:34:57 +00:00
|
|
|
'''
|
|
|
|
_name = 'mail.thread'
|
|
|
|
_description = 'Email Thread'
|
2012-03-15 10:52:10 +00:00
|
|
|
_inherit = ['base.needaction']
|
2012-02-03 11:21:16 +00:00
|
|
|
|
|
|
|
def _get_message_ids(self, cr, uid, ids, name, arg, context=None):
|
|
|
|
res = {}
|
|
|
|
for thread in self.browse(cr, uid, ids, context=context):
|
|
|
|
records = self.message_load(cr, uid, [thread.id], context=context)
|
2012-02-06 09:06:18 +00:00
|
|
|
res[thread.id] = [obj['id'] for obj in records]
|
2012-02-03 11:21:16 +00:00
|
|
|
return res
|
|
|
|
|
2012-03-13 15:06:35 +00:00
|
|
|
# OpenSocial: message_ids_social is a dummy field that should not be used
|
2011-07-22 16:34:57 +00:00
|
|
|
_columns = {
|
2012-02-06 11:26:23 +00:00
|
|
|
'message_ids_social': fields.function(_get_message_ids, method=True,
|
2012-03-16 16:38:25 +00:00
|
|
|
type='one2many', obj='mail.message', string='Temp messages',
|
|
|
|
_fields_id = 'res_id'),
|
2011-07-22 16:34:57 +00:00
|
|
|
}
|
2012-02-28 14:06:32 +00:00
|
|
|
|
|
|
|
#------------------------------------------------------
|
|
|
|
# Automatic subscription when creating/reading
|
|
|
|
#------------------------------------------------------
|
|
|
|
|
|
|
|
def create(self, cr, uid, vals, context=None):
|
|
|
|
thread_id = super(mail_thread, self).create(cr, uid, vals, context=context);
|
|
|
|
self.message_subscribe(cr, uid, [thread_id], [uid], context=context)
|
|
|
|
return thread_id;
|
|
|
|
|
|
|
|
def write(self, cr, uid, ids, vals, context=None):
|
2012-03-22 08:07:45 +00:00
|
|
|
if isinstance(ids, (int, long)):
|
2012-03-21 17:59:49 +00:00
|
|
|
ids = [ids]
|
2012-02-28 14:06:32 +00:00
|
|
|
write_res = super(mail_thread, self).write(cr, uid, ids, vals, context=context);
|
|
|
|
if write_res:
|
|
|
|
self.message_subscribe(cr, uid, ids, [uid], context=context)
|
|
|
|
return write_res;
|
2012-02-03 11:21:16 +00:00
|
|
|
|
2012-03-13 15:06:35 +00:00
|
|
|
def unlink(self, cr, uid, ids, context=None):
|
|
|
|
"""Override unlink, to automatically delete
|
|
|
|
- subscriptions
|
|
|
|
- messages
|
|
|
|
that are linked with res_model and res_id, not through
|
|
|
|
a foreign key with a 'cascade' ondelete attribute.
|
|
|
|
Notifications will be deleted with messages
|
|
|
|
"""
|
|
|
|
if context is None:
|
|
|
|
context = {}
|
|
|
|
|
|
|
|
subscr_obj = self.pool.get('mail.subscription')
|
|
|
|
msg_obj = self.pool.get('mail.message')
|
|
|
|
|
|
|
|
subscr_to_del_ids = subscr_obj.search(cr, uid, [('res_model', '=', self._name), ('res_id', 'in', ids)], context=context)
|
|
|
|
subscr_obj.unlink(cr, uid, subscr_to_del_ids, context=context)
|
|
|
|
|
|
|
|
msg_to_del_ids = msg_obj.search(cr, uid, [('model', '=', self._name), ('res_id', 'in', ids)], context=context)
|
|
|
|
msg_obj.unlink(cr, uid, msg_to_del_ids, context=context)
|
|
|
|
|
|
|
|
super(mail_thread, self).unlink(cr, uid, ids, context=context)
|
|
|
|
|
2012-02-01 16:21:36 +00:00
|
|
|
#------------------------------------------------------
|
|
|
|
# Generic message api
|
|
|
|
#------------------------------------------------------
|
2012-02-22 09:09:23 +00:00
|
|
|
|
2012-02-28 17:13:51 +00:00
|
|
|
def message_create(self, cr, uid, thread_id, vals, context=None):
|
2012-02-22 09:09:23 +00:00
|
|
|
"""OpenSocial: wrapper of mail.message create method
|
|
|
|
- creates the mail.message
|
2012-02-22 09:58:12 +00:00
|
|
|
- automatically subscribe the message writer if not already done
|
2012-02-22 09:09:23 +00:00
|
|
|
- push the message to subscribed users"""
|
2012-02-28 17:13:51 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
|
|
|
|
2012-03-13 15:06:35 +00:00
|
|
|
need_action_pushed = False
|
|
|
|
user_to_push_ids = []
|
2012-02-22 09:09:23 +00:00
|
|
|
subscription_obj = self.pool.get('mail.subscription')
|
|
|
|
notification_obj = self.pool.get('mail.notification')
|
|
|
|
|
2012-02-28 17:13:51 +00:00
|
|
|
# create message
|
2012-02-22 09:09:23 +00:00
|
|
|
msg_id = self.pool.get('mail.message').create(cr, uid, vals, context=context)
|
2012-03-13 15:06:35 +00:00
|
|
|
thread_obj = self.browse(cr, uid, [thread_id], context=context)[0]
|
2012-02-22 09:09:23 +00:00
|
|
|
|
2012-02-22 09:58:12 +00:00
|
|
|
# automatically subscribe the writer of the message if not subscribed
|
2012-03-13 15:06:35 +00:00
|
|
|
if vals['user_id'] and not self.message_is_subscriber(cr, vals['user_id'], [thread_id], context=context):
|
|
|
|
self.message_subscribe(cr, uid, [thread_id], [vals['user_id']], context=context)
|
2012-02-22 09:58:12 +00:00
|
|
|
|
2012-02-22 09:09:23 +00:00
|
|
|
# push the message to suscribed users
|
2012-02-28 17:13:51 +00:00
|
|
|
users = self.message_get_subscribers(cr, uid, [thread_id], context=context)
|
2012-03-13 15:06:35 +00:00
|
|
|
user_to_push_ids += [user['id'] for user in users]
|
2012-03-02 17:07:49 +00:00
|
|
|
# parse message to get requested users
|
2012-03-13 15:06:35 +00:00
|
|
|
user_to_push_ids += self.message_parse_users(cr, uid, [msg_id], vals['body_text'], context=context)
|
|
|
|
# push to need_action_user_id if set
|
2012-03-15 10:52:10 +00:00
|
|
|
#if thread_obj.need_action_user_id and thread_obj.need_action_user_id.id not in user_to_push_ids:
|
|
|
|
#user_to_push_ids.append(thread_obj.need_action_user_id.id)
|
2012-03-13 15:06:35 +00:00
|
|
|
|
2012-03-13 16:35:55 +00:00
|
|
|
# effectively push message to users
|
|
|
|
for id in user_to_push_ids:
|
|
|
|
notification_obj.create(cr, uid, {'user_id': id, 'message_id': msg_id}, context=context)
|
2012-03-02 17:07:49 +00:00
|
|
|
|
2012-02-22 09:09:23 +00:00
|
|
|
return msg_id
|
2012-03-02 17:07:49 +00:00
|
|
|
|
2012-03-08 11:24:45 +00:00
|
|
|
def message_parse_users(self, cr, uid, ids, string, context=None):
|
|
|
|
'''Parse message content; if find @login -(^|\s)@(\w*)-: returns the related ids'''
|
|
|
|
regex = re.compile('(^|\s)@(\w*)')
|
|
|
|
login_lst = [item[1] for item in regex.findall(string)]
|
|
|
|
if not login_lst: return []
|
|
|
|
user_ids = self.pool.get('res.users').search(cr, uid, [('login', 'in', login_lst)], context=context)
|
2012-03-02 17:07:49 +00:00
|
|
|
return user_ids
|
2012-02-22 09:09:23 +00:00
|
|
|
|
2011-12-09 14:28:39 +00:00
|
|
|
def message_capable_models(self, cr, uid, context=None):
|
|
|
|
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
|
|
|
|
|
2012-03-09 09:49:16 +00:00
|
|
|
def message_append(self, cr, uid, threads, subject, parent_id=False, body_text=None, type='email',
|
2012-02-07 17:07:46 +00:00
|
|
|
email_to=False, email_from=False, email_cc=None, email_bcc=None,
|
|
|
|
reply_to=None, email_date=None, message_id=False, references=None,
|
|
|
|
attachments=None, body_html=None, subtype=None, headers=None,
|
|
|
|
original=None, context=None):
|
2011-07-22 16:34:57 +00:00
|
|
|
"""Creates a new mail.message attached to the current mail.thread,
|
|
|
|
containing all the details passed as parameters. All attachments
|
|
|
|
will be attached to the thread record as well as to the actual
|
|
|
|
message.
|
2011-09-07 15:13:48 +00:00
|
|
|
If only the ``threads`` and ``subject`` parameters are provided,
|
|
|
|
a *event log* message is created, without the usual envelope
|
|
|
|
attributes (sender, recipients, etc.).
|
2011-07-22 16:34:57 +00:00
|
|
|
|
|
|
|
:param threads: list of thread ids, or list of browse_records representing
|
|
|
|
threads to which a new message should be attached
|
2011-09-07 15:13:48 +00:00
|
|
|
:param subject: subject of the message, or description of the event if this
|
|
|
|
is an *event log* entry.
|
2011-07-22 16:34:57 +00:00
|
|
|
:param email_to: Email-To / Recipient address
|
|
|
|
:param email_from: Email From / Sender address if any
|
|
|
|
:param email_cc: Comma-Separated list of Carbon Copy Emails To addresse if any
|
|
|
|
:param email_bcc: Comma-Separated list of Blind Carbon Copy Emails To addresses if any
|
|
|
|
:param reply_to: reply_to header
|
|
|
|
:param email_date: email date string if different from now, in server timezone
|
|
|
|
:param message_id: optional email identifier
|
|
|
|
:param references: optional email references
|
|
|
|
:param body_text: plaintext contents of the mail or log message
|
|
|
|
:param body_html: html contents of the mail or log message
|
|
|
|
:param subtype: optional type of message: 'plain' or 'html', corresponding to the main
|
|
|
|
body contents (body_text or body_html).
|
|
|
|
:param headers: mail headers to store
|
|
|
|
:param dict attachments: map of attachment filenames to binary contents, if any.
|
|
|
|
:param str original: optional full source of the RFC2822 email, for reference
|
|
|
|
:param dict context: if a ``thread_model`` value is present
|
|
|
|
in the context, its value will be used
|
|
|
|
to determine the model of the thread to
|
|
|
|
update (instead of the current model).
|
|
|
|
"""
|
2012-02-07 17:07:46 +00:00
|
|
|
if context is None:
|
2011-07-22 16:34:57 +00:00
|
|
|
context = {}
|
|
|
|
if attachments is None:
|
|
|
|
attachments = {}
|
|
|
|
|
|
|
|
if email_date:
|
|
|
|
edate = parsedate(email_date)
|
|
|
|
if edate is not None:
|
|
|
|
email_date = time.strftime('%Y-%m-%d %H:%M:%S', edate)
|
|
|
|
|
|
|
|
if all(isinstance(thread_id, (int, long)) for thread_id in threads):
|
|
|
|
model = context.get('thread_model') or self._name
|
|
|
|
model_pool = self.pool.get(model)
|
|
|
|
threads = model_pool.browse(cr, uid, threads, context=context)
|
|
|
|
|
|
|
|
ir_attachment = self.pool.get('ir.attachment')
|
|
|
|
mail_message = self.pool.get('mail.message')
|
|
|
|
|
|
|
|
for thread in threads:
|
|
|
|
to_attach = []
|
2011-10-18 03:39:13 +00:00
|
|
|
for attachment in attachments:
|
|
|
|
fname, fcontent = attachment
|
2011-07-22 16:34:57 +00:00
|
|
|
if isinstance(fcontent, unicode):
|
|
|
|
fcontent = fcontent.encode('utf-8')
|
|
|
|
data_attach = {
|
|
|
|
'name': fname,
|
2011-08-23 17:58:09 +00:00
|
|
|
'datas': base64.b64encode(str(fcontent)),
|
2011-07-22 16:34:57 +00:00
|
|
|
'datas_fname': fname,
|
|
|
|
'description': _('Mail attachment'),
|
|
|
|
'res_model': thread._name,
|
|
|
|
'res_id': thread.id,
|
|
|
|
}
|
|
|
|
to_attach.append(ir_attachment.create(cr, uid, data_attach, context=context))
|
|
|
|
|
|
|
|
partner_id = hasattr(thread, 'partner_id') and (thread.partner_id and thread.partner_id.id or False) or False
|
|
|
|
if not partner_id and thread._name == 'res.partner':
|
|
|
|
partner_id = thread.id
|
|
|
|
data = {
|
|
|
|
'subject': subject,
|
|
|
|
'user_id': uid,
|
2012-03-07 13:43:06 +00:00
|
|
|
'parent_id': parent_id,
|
2011-07-22 16:34:57 +00:00
|
|
|
'model' : thread._name,
|
|
|
|
'partner_id': partner_id,
|
|
|
|
'res_id': thread.id,
|
|
|
|
'date': time.strftime('%Y-%m-%d %H:%M:%S'),
|
|
|
|
'message_id': message_id,
|
2012-03-09 10:56:11 +00:00
|
|
|
'body_text': body_text or (hasattr(thread, 'description') and thread.description or ''),
|
2011-09-07 15:15:22 +00:00
|
|
|
'attachment_ids': [(6, 0, to_attach)],
|
2012-02-02 09:48:45 +00:00
|
|
|
'state': 'received',
|
|
|
|
'type': type,
|
2011-07-22 16:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if email_from:
|
|
|
|
for param in (email_to, email_cc, email_bcc):
|
|
|
|
if isinstance(param, list):
|
|
|
|
param = ", ".join(param)
|
|
|
|
data = {
|
|
|
|
'subject': subject or _('History'),
|
|
|
|
'user_id': uid,
|
|
|
|
'model' : thread._name,
|
|
|
|
'res_id': thread.id,
|
|
|
|
'date': email_date or time.strftime('%Y-%m-%d %H:%M:%S'),
|
|
|
|
'body_text': body_text,
|
|
|
|
'email_to': email_to,
|
|
|
|
'email_from': email_from or \
|
2011-08-09 23:44:28 +00:00
|
|
|
(hasattr(thread, 'user_id') and thread.user_id and thread.user_id.user_email),
|
2011-07-22 16:34:57 +00:00
|
|
|
'email_cc': email_cc,
|
|
|
|
'email_bcc': email_bcc,
|
|
|
|
'partner_id': partner_id,
|
|
|
|
'references': references,
|
|
|
|
'message_id': message_id,
|
|
|
|
'attachment_ids': [(6, 0, to_attach)],
|
|
|
|
'state' : 'received',
|
|
|
|
'body_html': body_html,
|
|
|
|
'subtype': subtype,
|
|
|
|
'headers': headers,
|
|
|
|
'reply_to': reply_to,
|
|
|
|
'original': original,
|
|
|
|
}
|
2012-02-28 17:13:51 +00:00
|
|
|
self.message_create(cr, uid, thread.id, data, context=context)
|
2011-07-22 16:34:57 +00:00
|
|
|
return True
|
|
|
|
|
2011-08-25 12:27:57 +00:00
|
|
|
def message_append_dict(self, cr, uid, ids, msg_dict, context=None):
|
|
|
|
"""Creates a new mail.message attached to the given threads (``ids``),
|
|
|
|
with the contents of ``msg_dict``, by calling ``message_append``
|
|
|
|
with the mail details. All attachments in msg_dict will be
|
|
|
|
attached to the object record as well as to the actual
|
|
|
|
mail message.
|
|
|
|
|
|
|
|
:param dict msg_dict: a map containing the email details and
|
|
|
|
attachments. See ``message_process()`` and
|
|
|
|
``mail.message.parse()`` for details on
|
|
|
|
the dict structure.
|
|
|
|
:param dict context: if a ``thread_model`` value is present
|
|
|
|
in the context, its value will be used
|
|
|
|
to determine the model of the thread to
|
|
|
|
update (instead of the current model).
|
|
|
|
"""
|
2012-03-13 15:06:35 +00:00
|
|
|
# OpenSocial: add default email type for old API
|
2012-02-02 09:48:45 +00:00
|
|
|
if not 'type' in msg_dict: msg_dict['type'] = 'email'
|
2011-08-25 12:27:57 +00:00
|
|
|
return self.message_append(cr, uid, ids,
|
|
|
|
subject = msg_dict.get('subject'),
|
2012-03-02 17:07:49 +00:00
|
|
|
parent_id = msg_dict.get('parent_id', False),
|
2011-08-25 12:27:57 +00:00
|
|
|
body_text = msg_dict.get('body_text'),
|
2012-02-02 09:48:45 +00:00
|
|
|
type = msg_dict.get('type'),
|
2011-08-25 12:27:57 +00:00
|
|
|
email_to = msg_dict.get('to'),
|
|
|
|
email_from = msg_dict.get('from'),
|
|
|
|
email_cc = msg_dict.get('cc'),
|
|
|
|
email_bcc = msg_dict.get('bcc'),
|
|
|
|
reply_to = msg_dict.get('reply'),
|
|
|
|
email_date = msg_dict.get('date'),
|
|
|
|
message_id = msg_dict.get('message-id'),
|
|
|
|
references = msg_dict.get('references')\
|
|
|
|
or msg_dict.get('in-reply-to'),
|
|
|
|
attachments = msg_dict.get('attachments'),
|
|
|
|
body_html= msg_dict.get('body_html'),
|
|
|
|
subtype = msg_dict.get('subtype'),
|
|
|
|
headers = msg_dict.get('headers'),
|
|
|
|
original = msg_dict.get('original'),
|
|
|
|
context = context)
|
|
|
|
|
2012-02-01 16:21:36 +00:00
|
|
|
# Message loading
|
2012-03-07 13:10:22 +00:00
|
|
|
def _message_get_parent_ids(self, cr, uid, ids, child_ids, root_ids, context=None):
|
2012-03-13 15:06:35 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
2012-03-07 13:10:22 +00:00
|
|
|
msg_obj = self.pool.get('mail.message')
|
|
|
|
msgs_tmp = msg_obj.read(cr, uid, child_ids, context=context)
|
|
|
|
parent_ids = [msg['parent_id'][0] for msg in msgs_tmp if msg['parent_id'] not in root_ids and msg['parent_id'][0] not in child_ids]
|
|
|
|
child_ids += parent_ids
|
|
|
|
cur_iter = 0; max_iter = 10;
|
|
|
|
while (parent_ids and (cur_iter < max_iter)):
|
|
|
|
cur_iter += 1
|
|
|
|
msgs_tmp = msg_obj.read(cr, uid, parent_ids, context=context)
|
|
|
|
parent_ids = [msg['parent_id'][0] for msg in msgs_tmp if msg['parent_id'] not in root_ids and msg['parent_id'][0] not in child_ids]
|
|
|
|
child_ids += parent_ids
|
|
|
|
return child_ids
|
|
|
|
|
|
|
|
def message_load_ids(self, cr, uid, ids, limit=100, offset=0, domain=[], ascent=False, root_ids=[False], context=None):
|
2012-02-16 16:03:02 +00:00
|
|
|
""" OpenSocial feature: return thread messages ids (for web compatibility)
|
|
|
|
loading messages: search in mail.messages where res_id = ids, (res_)model = current model
|
2012-03-07 13:10:22 +00:00
|
|
|
see get_pushed_messages for parameters explanation
|
2012-02-07 17:07:46 +00:00
|
|
|
"""
|
2012-03-13 15:06:35 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
2012-02-02 09:48:45 +00:00
|
|
|
msg_obj = self.pool.get('mail.message')
|
2012-03-02 17:07:49 +00:00
|
|
|
msg_ids = msg_obj.search(cr, uid, ['&', ('res_id', 'in', ids), ('model', '=', self._name)] + domain,
|
2012-02-23 17:37:09 +00:00
|
|
|
limit=limit, offset=offset, context=context)
|
2012-03-07 13:10:22 +00:00
|
|
|
if (ascent): msg_ids = self._message_get_parent_ids(cr, uid, ids, msg_ids, root_ids, context=context)
|
2012-02-16 16:03:02 +00:00
|
|
|
return msg_ids
|
2012-02-07 17:07:46 +00:00
|
|
|
|
2012-03-07 13:10:22 +00:00
|
|
|
def message_load(self, cr, uid, ids, limit=100, offset=0, domain=[], ascent=False, root_ids=[False], context=None):
|
2012-02-16 16:03:02 +00:00
|
|
|
""" OpenSocial feature: return thread messages
|
|
|
|
loading messages: search in mail.messages where res_id = ids, (res_)model = current model
|
2012-03-07 13:10:22 +00:00
|
|
|
see get_pushed_messages for parameters explanation
|
2012-02-07 17:07:46 +00:00
|
|
|
"""
|
2012-03-07 13:10:22 +00:00
|
|
|
msg_ids = self.message_load_ids(cr, uid, ids, limit, offset, domain, ascent, root_ids, context=context)
|
2012-02-16 16:03:02 +00:00
|
|
|
return self.pool.get('mail.message').read(cr, uid, msg_ids, context=context)
|
2012-02-23 17:49:36 +00:00
|
|
|
|
2012-03-07 13:10:22 +00:00
|
|
|
def get_pushed_messages(self, cr, uid, ids, limit=100, offset=0, domain=[], ascent=False, root_ids=[False], context=None):
|
2012-02-23 17:49:36 +00:00
|
|
|
"""OpenSocial: wall: get messages to display (=pushed notifications)
|
2012-03-07 13:10:22 +00:00
|
|
|
:param domain: domain to add to the search; especially child_of is interesting when dealing with threaded display
|
|
|
|
:param deep: performs an ascended search; will add to fetched msgs all their parents until root_ids
|
|
|
|
WARNING: must be used in combinaison with a child_of domain
|
|
|
|
EXAMPLE: domain = ['id', 'child_of', [32, 33]], root_ids=[32,33]
|
|
|
|
:param root_ids: root_ids when performing an ascended search
|
|
|
|
:return: list of mail.messages sorted by date
|
2012-02-23 17:49:36 +00:00
|
|
|
"""
|
2012-03-07 13:10:22 +00:00
|
|
|
if context is None: context = {}
|
2012-02-23 17:49:36 +00:00
|
|
|
notification_obj = self.pool.get('mail.notification')
|
2012-03-07 13:10:22 +00:00
|
|
|
msg_obj = self.pool.get('mail.message')
|
2012-02-24 15:17:52 +00:00
|
|
|
# get user notifications
|
|
|
|
notification_ids = notification_obj.search(cr, uid, [('user_id', '=', uid)], context=context)
|
2012-02-23 17:49:36 +00:00
|
|
|
notifications = notification_obj.browse(cr, uid, notification_ids, context=context)
|
|
|
|
msg_ids = [notification.message_id.id for notification in notifications]
|
2012-02-24 15:17:52 +00:00
|
|
|
# search messages: ids in notifications, add domain coming from wall search view
|
2012-03-02 17:07:49 +00:00
|
|
|
search_domain = [('id', 'in', msg_ids)] + domain
|
2012-03-07 13:10:22 +00:00
|
|
|
msg_ids = msg_obj.search(cr, uid, search_domain, limit=limit, offset=offset, context=context)
|
|
|
|
if (ascent): msg_ids = self._message_get_parent_ids(cr, uid, ids, msg_ids, root_ids, context=context)
|
|
|
|
msgs = msg_obj.read(cr, uid, msg_ids, context=context)
|
2012-02-23 17:49:36 +00:00
|
|
|
return msgs
|
|
|
|
|
2012-03-07 17:23:50 +00:00
|
|
|
# Message tools
|
|
|
|
def message_get_discussions_nbr(self, cr, uid, ids, context=None):
|
|
|
|
message_obj = self.pool.get('mail.message')
|
2012-03-13 15:06:35 +00:00
|
|
|
count = message_obj.search(cr, uid, [('model', '=', self._name), ('res_id', '=', ids), ('parent_id', '=', False)], count=True)
|
2012-03-07 17:23:50 +00:00
|
|
|
return count
|
|
|
|
|
|
|
|
def message_get_messages_nbr(self, cr, uid, ids, context=None):
|
|
|
|
message_obj = self.pool.get('mail.message')
|
2012-03-13 15:06:35 +00:00
|
|
|
count = message_obj.search(cr, uid, [('model', '=', self._name), ('res_id', 'in', ids)], count=True)
|
2012-03-07 17:23:50 +00:00
|
|
|
return count
|
|
|
|
|
2012-02-01 16:21:36 +00:00
|
|
|
#------------------------------------------------------
|
|
|
|
# Email specific
|
|
|
|
#------------------------------------------------------
|
|
|
|
# message_process will call either message_new or message_update.
|
2011-08-25 12:27:57 +00:00
|
|
|
|
2011-09-07 15:13:48 +00:00
|
|
|
def message_process(self, cr, uid, model, message, custom_values=None,
|
2011-09-08 00:16:51 +00:00
|
|
|
save_original=False, strip_attachments=False,
|
|
|
|
context=None):
|
2011-07-22 16:34:57 +00:00
|
|
|
"""Process an incoming RFC2822 email message related to the
|
|
|
|
given thread model, relying on ``mail.message.parse()``
|
|
|
|
for the parsing operation, and then calling ``message_new``
|
|
|
|
(if the thread record did not exist) or ``message_update``
|
2011-08-25 12:27:57 +00:00
|
|
|
(if it did), then calling ``message_forward`` to automatically
|
|
|
|
notify other people that should receive this message.
|
2011-07-22 16:34:57 +00:00
|
|
|
|
|
|
|
:param string model: the thread model for which a new message
|
|
|
|
must be processed
|
|
|
|
:param message: source of the RFC2822 mail
|
|
|
|
:type message: string or xmlrpclib.Binary
|
2011-09-13 13:23:40 +00:00
|
|
|
:type dict custom_values: optional dictionary of field values
|
2011-07-22 16:34:57 +00:00
|
|
|
to pass to ``message_new`` if a new
|
|
|
|
record needs to be created. Ignored
|
|
|
|
if the thread record already exists.
|
2011-09-07 15:13:48 +00:00
|
|
|
:param bool save_original: whether to keep a copy of the original
|
|
|
|
email source attached to the message after it is imported.
|
2011-09-08 00:16:51 +00:00
|
|
|
:param bool strip_attachments: whether to strip all attachments
|
|
|
|
before processing the message, in order to save some space.
|
2011-07-22 16:34:57 +00:00
|
|
|
"""
|
|
|
|
# extract message bytes - we are forced to pass the message as binary because
|
|
|
|
# we don't know its encoding until we parse its headers and hence can't
|
|
|
|
# convert it to utf-8 for transport between the mailgate script and here.
|
|
|
|
if isinstance(message, xmlrpclib.Binary):
|
|
|
|
message = str(message.data)
|
|
|
|
|
|
|
|
model_pool = self.pool.get(model)
|
|
|
|
if self._name != model:
|
|
|
|
if context is None: context = {}
|
2011-08-25 12:27:57 +00:00
|
|
|
context.update({'thread_model': model})
|
2011-07-22 16:34:57 +00:00
|
|
|
|
|
|
|
mail_message = self.pool.get('mail.message')
|
|
|
|
res_id = False
|
|
|
|
|
|
|
|
# Parse Message
|
|
|
|
# Warning: message_from_string doesn't always work correctly on unicode,
|
|
|
|
# we must use utf-8 strings here :-(
|
|
|
|
if isinstance(message, unicode):
|
|
|
|
message = message.encode('utf-8')
|
|
|
|
msg_txt = email.message_from_string(message)
|
2011-09-07 15:13:48 +00:00
|
|
|
msg = mail_message.parse_message(msg_txt, save_original=save_original)
|
2011-07-22 16:34:57 +00:00
|
|
|
|
2011-09-08 00:16:51 +00:00
|
|
|
if strip_attachments and 'attachments' in msg:
|
|
|
|
del msg['attachments']
|
|
|
|
|
2011-07-22 16:34:57 +00:00
|
|
|
# Create New Record into particular model
|
|
|
|
def create_record(msg):
|
|
|
|
if hasattr(model_pool, 'message_new'):
|
|
|
|
return model_pool.message_new(cr, uid, msg,
|
|
|
|
custom_values,
|
|
|
|
context=context)
|
|
|
|
res_id = False
|
|
|
|
if msg.get('references') or msg.get('in-reply-to'):
|
|
|
|
references = msg.get('references') or msg.get('in-reply-to')
|
|
|
|
if '\r\n' in references:
|
|
|
|
references = references.split('\r\n')
|
|
|
|
else:
|
|
|
|
references = references.split(' ')
|
|
|
|
for ref in references:
|
|
|
|
ref = ref.strip()
|
|
|
|
res_id = tools.reference_re.search(ref)
|
|
|
|
if res_id:
|
|
|
|
res_id = res_id.group(1)
|
|
|
|
else:
|
|
|
|
res_id = tools.res_re.search(msg['subject'])
|
|
|
|
if res_id:
|
|
|
|
res_id = res_id.group(1)
|
|
|
|
if res_id:
|
|
|
|
res_id = int(res_id)
|
|
|
|
if model_pool.exists(cr, uid, res_id):
|
|
|
|
if hasattr(model_pool, 'message_update'):
|
|
|
|
model_pool.message_update(cr, uid, [res_id], msg, {}, context=context)
|
2011-09-07 15:13:48 +00:00
|
|
|
else:
|
|
|
|
# referenced thread was not found, we'll have to create a new one
|
|
|
|
res_id = False
|
2011-07-22 16:34:57 +00:00
|
|
|
if not res_id:
|
|
|
|
res_id = create_record(msg)
|
|
|
|
#To forward the email to other followers
|
2011-08-23 17:58:09 +00:00
|
|
|
self.message_forward(cr, uid, model, [res_id], msg_txt, context=context)
|
2011-07-22 16:34:57 +00:00
|
|
|
return res_id
|
|
|
|
|
2012-02-01 16:21:36 +00:00
|
|
|
def message_new(self, cr, uid, msg_dict, custom_values=None, context=None):
|
|
|
|
"""Called by ``message_process`` when a new message is received
|
|
|
|
for a given thread model, if the message did not belong to
|
|
|
|
an existing thread.
|
|
|
|
The default behavior is to create a new record of the corresponding
|
|
|
|
model (based on some very basic info extracted from the message),
|
|
|
|
then attach the message to the newly created record
|
|
|
|
(by calling ``message_append_dict``).
|
|
|
|
Additional behavior may be implemented by overriding this method.
|
|
|
|
|
|
|
|
:param dict msg_dict: a map containing the email details and
|
|
|
|
attachments. See ``message_process`` and
|
|
|
|
``mail.message.parse`` for details.
|
|
|
|
:param dict custom_values: optional dictionary of additional
|
|
|
|
field values to pass to create()
|
|
|
|
when creating the new thread record.
|
|
|
|
Be careful, these values may override
|
|
|
|
any other values coming from the message.
|
|
|
|
:param dict context: if a ``thread_model`` value is present
|
|
|
|
in the context, its value will be used
|
|
|
|
to determine the model of the record
|
|
|
|
to create (instead of the current model).
|
|
|
|
:rtype: int
|
|
|
|
:return: the id of the newly created thread object
|
|
|
|
"""
|
|
|
|
if context is None:
|
|
|
|
context = {}
|
|
|
|
model = context.get('thread_model') or self._name
|
|
|
|
model_pool = self.pool.get(model)
|
|
|
|
fields = model_pool.fields_get(cr, uid, context=context)
|
|
|
|
data = model_pool.default_get(cr, uid, fields, context=context)
|
|
|
|
if 'name' in fields and not data.get('name'):
|
|
|
|
data['name'] = msg_dict.get('from','')
|
|
|
|
if custom_values and isinstance(custom_values, dict):
|
|
|
|
data.update(custom_values)
|
|
|
|
res_id = model_pool.create(cr, uid, data, context=context)
|
|
|
|
self.message_append_dict(cr, uid, [res_id], msg_dict, context=context)
|
|
|
|
return res_id
|
|
|
|
|
|
|
|
def message_update(self, cr, uid, ids, msg_dict, vals={}, default_act=None, context=None):
|
|
|
|
"""Called by ``message_process`` when a new message is received
|
|
|
|
for an existing thread. The default behavior is to create a
|
|
|
|
new mail.message in the given thread (by calling
|
|
|
|
``message_append_dict``)
|
|
|
|
Additional behavior may be implemented by overriding this
|
|
|
|
method.
|
|
|
|
|
|
|
|
:param dict msg_dict: a map containing the email details and
|
|
|
|
attachments. See ``message_process`` and
|
|
|
|
``mail.message.parse()`` for details.
|
|
|
|
:param dict context: if a ``thread_model`` value is present
|
|
|
|
in the context, its value will be used
|
|
|
|
to determine the model of the thread to
|
|
|
|
update (instead of the current model).
|
|
|
|
"""
|
|
|
|
return self.message_append_dict(cr, uid, ids, msg_dict, context=context)
|
|
|
|
|
|
|
|
def message_thread_followers(self, cr, uid, ids, context=None):
|
|
|
|
"""Returns a list of email addresses of the people following
|
|
|
|
this thread, including the sender of each mail, and the
|
|
|
|
people who were in CC of the messages, if any.
|
|
|
|
"""
|
|
|
|
res = {}
|
|
|
|
if isinstance(ids, (str, int, long)):
|
|
|
|
ids = [long(ids)]
|
|
|
|
for thread in self.browse(cr, uid, ids, context=context):
|
|
|
|
l = set()
|
|
|
|
for message in thread.message_ids:
|
|
|
|
l.add((message.user_id and message.user_id.user_email) or '')
|
|
|
|
l.add(message.email_from or '')
|
|
|
|
l.add(message.email_cc or '')
|
|
|
|
res[thread.id] = filter(None, l)
|
|
|
|
return res
|
2011-08-23 17:58:09 +00:00
|
|
|
|
|
|
|
def message_forward(self, cr, uid, model, thread_ids, msg, email_error=False, context=None):
|
2011-07-22 16:34:57 +00:00
|
|
|
"""Sends an email to all people following the given threads.
|
|
|
|
The emails are forwarded immediately, not queued for sending,
|
|
|
|
and not archived.
|
|
|
|
|
|
|
|
:param str model: thread model
|
|
|
|
:param list thread_ids: ids of the thread records
|
|
|
|
:param msg: email.message.Message object to forward
|
|
|
|
:param email_error: optional email address to notify in case
|
|
|
|
of any delivery error during the forward.
|
|
|
|
:return: True
|
|
|
|
"""
|
|
|
|
model_pool = self.pool.get(model)
|
|
|
|
smtp_server_obj = self.pool.get('ir.mail_server')
|
|
|
|
mail_message = self.pool.get('mail.message')
|
|
|
|
for res in model_pool.browse(cr, uid, thread_ids, context=context):
|
2011-08-23 17:58:09 +00:00
|
|
|
if hasattr(model_pool, 'message_thread_followers'):
|
|
|
|
followers = model_pool.message_thread_followers(cr, uid, [res.id])[res.id]
|
|
|
|
else:
|
|
|
|
followers = self.message_thread_followers(cr, uid, [res.id])[res.id]
|
2011-09-07 15:13:48 +00:00
|
|
|
message_followers_emails = to_email(','.join(filter(None, followers)))
|
|
|
|
message_recipients = to_email(','.join(filter(None,
|
2011-07-22 16:34:57 +00:00
|
|
|
[decode(msg['from']),
|
|
|
|
decode(msg['to']),
|
|
|
|
decode(msg['cc'])])))
|
2011-08-25 12:27:57 +00:00
|
|
|
forward_to = [i for i in message_followers_emails if (i and (i not in message_recipients))]
|
|
|
|
if forward_to:
|
2011-07-22 16:34:57 +00:00
|
|
|
# TODO: we need an interface for this for all types of objects, not just leads
|
|
|
|
if hasattr(res, 'section_id'):
|
|
|
|
del msg['reply-to']
|
|
|
|
msg['reply-to'] = res.section_id.reply_to
|
|
|
|
|
2011-09-07 20:27:02 +00:00
|
|
|
smtp_from, = to_email(msg['from'])
|
2011-07-22 16:34:57 +00:00
|
|
|
msg['from'] = smtp_from
|
2011-09-07 20:27:02 +00:00
|
|
|
msg['to'] = ", ".join(forward_to)
|
2011-07-22 16:34:57 +00:00
|
|
|
msg['message-id'] = tools.generate_tracking_message_id(res.id)
|
|
|
|
if not smtp_server_obj.send_email(cr, uid, msg) and email_error:
|
|
|
|
subj = msg['subject']
|
|
|
|
del msg['subject'], msg['to'], msg['cc'], msg['bcc']
|
|
|
|
msg['subject'] = _('[OpenERP-Forward-Failed] %s') % subj
|
|
|
|
msg['to'] = email_error
|
|
|
|
smtp_server_obj.send_email(cr, uid, msg)
|
|
|
|
return True
|
|
|
|
|
2011-08-25 12:27:57 +00:00
|
|
|
def message_partner_by_email(self, cr, uid, email, context=None):
|
2011-07-22 16:34:57 +00:00
|
|
|
"""Attempts to return the id of a partner address matching
|
2011-08-25 12:27:57 +00:00
|
|
|
the given ``email``, and the corresponding partner id.
|
|
|
|
Can be used by classes using the ``mail.thread`` mixin
|
|
|
|
to lookup the partner and use it in their implementation
|
|
|
|
of ``message_new`` to link the new record with a
|
|
|
|
corresponding partner.
|
|
|
|
The keys used in the returned dict are meant to map
|
|
|
|
to usual names for relationships towards a partner
|
|
|
|
and one of its addresses.
|
2011-07-22 16:34:57 +00:00
|
|
|
|
|
|
|
:param email: email address for which a partner
|
|
|
|
should be searched for.
|
|
|
|
:rtype: dict
|
|
|
|
:return: a map of the following form::
|
|
|
|
|
|
|
|
{ 'partner_address_id': id or False,
|
|
|
|
'partner_id': pid or False }
|
|
|
|
"""
|
|
|
|
address_pool = self.pool.get('res.partner.address')
|
|
|
|
res = {
|
|
|
|
'partner_address_id': False,
|
|
|
|
'partner_id': False
|
|
|
|
}
|
2011-09-07 16:28:19 +00:00
|
|
|
if email:
|
|
|
|
email = to_email(email)[0]
|
|
|
|
address_ids = address_pool.search(cr, uid, [('email', '=', email)])
|
|
|
|
if address_ids:
|
|
|
|
address = address_pool.browse(cr, uid, address_ids[0])
|
|
|
|
res['partner_address_id'] = address_ids[0]
|
|
|
|
res['partner_id'] = address.partner_id.id
|
2011-07-22 16:34:57 +00:00
|
|
|
return res
|
|
|
|
|
2012-02-01 16:21:36 +00:00
|
|
|
# for backwards-compatibility with old scripts
|
|
|
|
process_email = message_process
|
|
|
|
|
|
|
|
#------------------------------------------------------
|
|
|
|
# Note specific
|
|
|
|
#------------------------------------------------------
|
2012-02-07 17:07:46 +00:00
|
|
|
|
2012-03-09 09:49:16 +00:00
|
|
|
def message_append_note(self, cr, uid, ids, subject, body, parent_id=False, type='notification', context=None):
|
|
|
|
return self.message_append(cr, uid, ids, subject, body_text=body, parent_id=parent_id, type=type, context=context)
|
2012-03-13 17:41:05 +00:00
|
|
|
|
2012-02-01 16:21:36 +00:00
|
|
|
#------------------------------------------------------
|
|
|
|
# Subscription mechanism
|
|
|
|
#------------------------------------------------------
|
2012-02-07 17:07:46 +00:00
|
|
|
|
2012-03-13 15:06:35 +00:00
|
|
|
def message_get_subscribers_ids(self, cr, uid, ids, context=None):
|
|
|
|
subscr_obj = self.pool.get('mail.subscription')
|
|
|
|
subscr_ids = subscr_obj.search(cr, uid, ['&', ('res_model', '=', self._name), ('res_id', 'in', ids)], context=context)
|
|
|
|
subs = subscr_obj.read(cr, uid, subscr_ids, context=context)
|
2012-02-21 14:21:26 +00:00
|
|
|
return [sub['user_id'][0] for sub in subs]
|
2012-02-20 17:21:06 +00:00
|
|
|
|
|
|
|
def message_get_subscribers(self, cr, uid, ids, context=None):
|
2012-03-13 15:06:35 +00:00
|
|
|
user_ids = self.message_get_subscribers_ids(cr, uid, ids, context=context)
|
2012-02-22 14:03:18 +00:00
|
|
|
users = self.pool.get('res.users').read(cr, uid, user_ids, fields=['id', 'name', 'avatar_mini'], context=context)
|
2012-02-20 17:21:06 +00:00
|
|
|
return users
|
2012-02-10 12:43:48 +00:00
|
|
|
|
2012-02-28 14:38:30 +00:00
|
|
|
def message_is_subscriber(self, cr, uid, ids, user_id = None, context=None):
|
2012-02-28 14:06:32 +00:00
|
|
|
users = self.message_get_subscribers(cr, uid, ids, context=context)
|
2012-02-28 14:38:30 +00:00
|
|
|
sub_user_id = uid if user_id is None else user_id
|
|
|
|
if sub_user_id in [user['id'] for user in users]:
|
2012-02-28 14:06:32 +00:00
|
|
|
return True
|
|
|
|
return False
|
2012-02-06 15:10:09 +00:00
|
|
|
|
2012-02-20 17:21:06 +00:00
|
|
|
def message_subscribe(self, cr, uid, ids, user_ids = None, context=None):
|
2012-02-02 10:35:01 +00:00
|
|
|
subscription_obj = self.pool.get('mail.subscription')
|
2012-03-08 16:55:08 +00:00
|
|
|
to_subscribe_uids = [uid] if user_ids is None else user_ids
|
2012-02-28 14:38:30 +00:00
|
|
|
create_ids = []
|
2012-02-02 10:35:01 +00:00
|
|
|
for id in ids:
|
2012-03-08 16:55:08 +00:00
|
|
|
for user_id in to_subscribe_uids:
|
2012-02-28 14:38:30 +00:00
|
|
|
if self.message_is_subscriber(cr, uid, [id], user_id=user_id, context=context): continue
|
|
|
|
create_ids.append(subscription_obj.create(cr, uid, {'res_model': self._name, 'res_id': id, 'user_id': user_id}, context=context))
|
2012-02-20 17:21:06 +00:00
|
|
|
return create_ids
|
2012-02-01 16:21:36 +00:00
|
|
|
|
2012-03-08 16:55:08 +00:00
|
|
|
def message_unsubscribe(self, cr, uid, ids, user_ids = None, context=None):
|
2012-02-02 10:35:01 +00:00
|
|
|
subscription_obj = self.pool.get('mail.subscription')
|
2012-03-08 16:55:08 +00:00
|
|
|
to_unsubscribe_uids = [uid] if user_ids is None else user_ids
|
|
|
|
to_delete_sub_ids = subscription_obj.search(cr, uid,
|
|
|
|
['&', '&', ('res_model', '=', self._name), ('res_id', 'in', ids), ('user_id', 'in', to_unsubscribe_uids)], context=context)
|
|
|
|
subscription_obj.unlink(cr, uid, to_delete_sub_ids, context=context)
|
2012-02-02 10:35:01 +00:00
|
|
|
return True
|
2012-02-01 16:21:36 +00:00
|
|
|
|
2011-07-22 16:34:57 +00:00
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|