diff --git a/addons/mail/mail_thread.py b/addons/mail/mail_thread.py index b129d741de1..119e687056f 100644 --- a/addons/mail/mail_thread.py +++ b/addons/mail/mail_thread.py @@ -214,11 +214,11 @@ class mail_thread(osv.osv): ret_dict[model_name] = model._description return ret_dict - def message_append(self, cr, uid, threads, subject, parent_id=False, body_text=None, - type='email', 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): + def message_append(self, cr, uid, threads, subject, body_text=None, body_html=None, + parent_id=False, type='email', subtype=None, state=None, + 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, headers=None, original=None, context=None): """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 @@ -233,6 +233,13 @@ class mail_thread(osv.osv): threads to which a new message should be attached :param subject: subject of the message, or description of the event if this is an *event log* entry. + :param body_text: plaintext contents of the mail or log message + :param body_html: html contents of the mail or log message + :param parent_id: id of the parent message (threaded messaging model) + :param type: optional type of message: 'email', 'comment', 'notification' + :param subtype: optional subtype of message: 'plain' or 'html', corresponding to the main + body contents (body_text or body_html). + :param state: optional state of message; 'received' by default :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 @@ -241,10 +248,6 @@ class mail_thread(osv.osv): :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 @@ -271,6 +274,7 @@ class mail_thread(osv.osv): ir_attachment = self.pool.get('ir.attachment') mail_message = self.pool.get('mail.message') + new_msg_ids = [] for thread in threads: to_attach = [] for attachment in attachments: @@ -292,25 +296,25 @@ class mail_thread(osv.osv): partner_id = thread.id data = { 'subject': subject, - 'user_id': uid, - 'parent_id': parent_id, - 'model' : thread._name, - 'partner_id': partner_id, - 'res_id': thread.id, - 'date': email_date or fields.datetime.now(), - 'message_id': message_id, 'body_text': body_text or (hasattr(thread, 'description') and thread.description or ''), 'body_html': body_html or '', - 'attachment_ids': [(6, 0, to_attach)], + 'parent_id': parent_id, + 'date': email_date or fields.datetime.now(), 'type': type, 'subtype': subtype, + 'state': state, + 'message_id': message_id, + 'attachment_ids': [(6, 0, to_attach)], + 'user_id': uid, + 'model' : thread._name, + 'res_id': thread.id, + 'partner_id': partner_id, } if email_from or type == 'email': for param in (email_to, email_cc, email_bcc): if isinstance(param, list): param = ", ".join(param) - data.update({ 'subject': subject or _('History'), 'body_text': body_text or '', @@ -324,8 +328,8 @@ class mail_thread(osv.osv): 'reply_to': reply_to, 'original': original, }) - self.message_create(cr, uid, thread.id, data, context=context) - return True + new_msg_ids.append(self.message_create(cr, uid, thread.id, data, context=context)) + return new_msg_ids def message_append_dict(self, cr, uid, ids, msg_dict, context=None): """Creates a new mail.message attached to the given threads (``ids``), @@ -345,9 +349,12 @@ class mail_thread(osv.osv): """ return self.message_append(cr, uid, ids, subject = msg_dict.get('subject'), + body_text = msg_dict.get('body_text'), + body_html= msg_dict.get('body_html'), parent_id = msg_dict.get('parent_id', False), - body_text = msg_dict.get('body_text', None), type = msg_dict.get('type', 'email'), + subtype = msg_dict.get('subtype', 'plain'), + state = msg_dict.get('state', 'received'), email_from = msg_dict.get('from', msg_dict.get('email_from')), email_to = msg_dict.get('to', msg_dict.get('email_to')), email_cc = msg_dict.get('cc', msg_dict.get('email_cc')), @@ -358,8 +365,6 @@ class mail_thread(osv.osv): 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', 'plain'), headers = msg_dict.get('headers'), original = msg_dict.get('original'), context = context) diff --git a/addons/mail/wizard/mail_compose_message.py b/addons/mail/wizard/mail_compose_message.py index af8baa314de..dd07cbce9b2 100644 --- a/addons/mail/wizard/mail_compose_message.py +++ b/addons/mail/wizard/mail_compose_message.py @@ -200,13 +200,8 @@ class mail_compose_message(osv.osv_memory): body = mail.body_html if mail.subtype == 'html' else mail.body_text - # Reply Email - if context.get('mail.compose.message.mode') == 'reply' and mail.message_id: - references = (mail.references or '') + " " + mail.message_id - headers['In-Reply-To'] = mail.message_id - + # Get model, and check whether it is OpenChatter enabled, aka inherit from mail.thread if context.get('mail.compose.message.mode') == 'mass_mail': - # Mass mailing: must render the template patterns if context.get('active_ids') and context.get('active_model'): active_ids = context['active_ids'] active_model = context['active_model'] @@ -214,7 +209,22 @@ class mail_compose_message(osv.osv_memory): active_model = mail.model active_model_pool = self.pool.get(active_model) active_ids = active_model_pool.search(cr, uid, ast.literal_eval(mail.filter_id.domain), context=ast.literal_eval(mail.filter_id.context)) + else: + active_model = mail.model + active_ids = [int(mail.res_id)] + active_model_pool = self.pool.get(active_model) + if hasattr(active_model_pool, '_inherit') and 'mail.thread' in active_model_pool._inherit: + mail_thread_enabled = True + else: + mail_thread_enabled = False + + # Reply Email + if context.get('mail.compose.message.mode') == 'reply' and mail.message_id: + references = (mail.references or '') + " " + mail.message_id + headers['In-Reply-To'] = mail.message_id + if context.get('mail.compose.message.mode') == 'mass_mail': + # Mass mailing: must render the template patterns for active_id in active_ids: subject = self.render_template(cr, uid, mail.subject, active_model, active_id) rendered_body = self.render_template(cr, uid, body, active_model, active_id) @@ -223,21 +233,33 @@ class mail_compose_message(osv.osv_memory): email_cc = self.render_template(cr, uid, mail.email_cc, active_model, active_id) email_bcc = self.render_template(cr, uid, mail.email_bcc, active_model, active_id) reply_to = self.render_template(cr, uid, mail.reply_to, active_model, active_id) - + # in mass-mailing mode we only schedule the mail for sending, it will be # processed as soon as the mail scheduler runs. - mail_message.schedule_with_attach(cr, uid, email_from, to_email(email_to), subject, rendered_body, - model=mail.model, email_cc=to_email(email_cc), email_bcc=to_email(email_bcc), reply_to=reply_to, - attachments=attachment, references=references, res_id=active_id, - subtype=mail.subtype, headers=headers, context=context) + if mail_thread_enabled: + active_model_pool.message_append(cr, uid, [active_id], + subject, body_text=mail.body_text, body_html=mail.body_html, subtype=mail.subtype, state='outgoing', + email_to=email_to, email_from=email_from, email_cc=email_cc, email_bcc=email_bcc, + reply_to=reply_to, references=references, attachments=attachment, headers=headers, context=context) + else: + mail_message.schedule_with_attach(cr, uid, email_from, to_email(email_to), subject, rendered_body, + model=mail.model, email_cc=to_email(email_cc), email_bcc=to_email(email_bcc), reply_to=reply_to, + attachments=attachment, references=references, res_id=active_id, + subtype=mail.subtype, headers=headers, context=context) else: # normal mode - no mass-mailing - msg_id = mail_message.schedule_with_attach(cr, uid, mail.email_from, to_email(mail.email_to), mail.subject, body, - model=mail.model, email_cc=to_email(mail.email_cc), email_bcc=to_email(mail.email_bcc), reply_to=mail.reply_to, - attachments=attachment, references=references, res_id=int(mail.res_id), - subtype=mail.subtype, headers=headers, context=context) + if mail_thread_enabled: + msg_ids = active_model_pool.message_append(cr, uid, active_ids, + mail.subject, body_text=mail.body_text, body_html=mail.body_html, subtype=mail.subtype, state='outgoing', + email_to=mail.email_to, email_from=mail.email_from, email_cc=mail.email_cc, email_bcc=mail.email_bcc, + reply_to=mail.reply_to, references=references, attachments=attachment, headers=headers, context=context) + else: + msg_ids = [mail_message.schedule_with_attach(cr, uid, mail.email_from, to_email(mail.email_to), mail.subject, body, + model=mail.model, email_cc=to_email(mail.email_cc), email_bcc=to_email(mail.email_bcc), reply_to=mail.reply_to, + attachments=attachment, references=references, res_id=int(mail.res_id), + subtype=mail.subtype, headers=headers, context=context)] # in normal mode, we send the email immediately, as the user expects us to (delay should be sufficiently small) - mail_message.send(cr, uid, [msg_id], context=context) + mail_message.send(cr, uid, msg_ids, context=context) return {'type': 'ir.actions.act_window_close'}