2011-07-22 16:34:57 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2012-08-31 12:56:05 +00:00
|
|
|
import base64
|
2011-07-22 16:34:57 +00:00
|
|
|
import re
|
|
|
|
import tools
|
2012-08-30 08:51:16 +00:00
|
|
|
|
2011-07-22 16:34:57 +00:00
|
|
|
from osv import osv
|
|
|
|
from osv import fields
|
|
|
|
from tools.safe_eval import safe_eval as eval
|
|
|
|
from tools.translate import _
|
|
|
|
|
|
|
|
# main mako-like expression pattern
|
|
|
|
EXPRESSION_PATTERN = re.compile('(\$\{.+?\})')
|
|
|
|
|
2012-07-06 07:38:52 +00:00
|
|
|
class mail_compose_message(osv.TransientModel):
|
2012-08-22 16:21:51 +00:00
|
|
|
""" Generic message composition wizard. You may inherit from this wizard
|
|
|
|
at model and view levels to provide specific features.
|
|
|
|
|
2012-09-04 09:28:20 +00:00
|
|
|
The behavior of the wizard depends on the composition_mode field:
|
2012-08-22 16:21:51 +00:00
|
|
|
- 'reply': reply to a previous message. The wizard is pre-populated
|
|
|
|
via ``get_message_data``.
|
|
|
|
- 'comment': new post on a record. The wizard is pre-populated via
|
|
|
|
``get_record_data``
|
|
|
|
- 'mass_mail': wizard in mass mailing mode where the mail details can
|
|
|
|
contain template placeholders that will be merged with actual data
|
|
|
|
before being sent to each recipient.
|
2011-07-22 16:34:57 +00:00
|
|
|
"""
|
|
|
|
_name = 'mail.compose.message'
|
2012-08-16 16:43:11 +00:00
|
|
|
_inherit = 'mail.message'
|
2012-06-25 13:42:53 +00:00
|
|
|
_description = 'Email composition wizard'
|
2012-08-28 09:50:46 +00:00
|
|
|
_log_access = True
|
2011-07-22 16:34:57 +00:00
|
|
|
|
|
|
|
def default_get(self, cr, uid, fields, context=None):
|
2012-08-22 16:21:51 +00:00
|
|
|
""" Handle composition mode. Some details about context keys:
|
|
|
|
- comment: default mode, model and ID of a record the user comments
|
|
|
|
- default_model or active_model
|
|
|
|
- default_res_id or active_id
|
|
|
|
- reply: active_id of a message the user replies to
|
2012-09-04 09:28:20 +00:00
|
|
|
- default_parent_id or message_id or active_id: ID of the
|
|
|
|
mail.message we reply to
|
2012-08-22 16:21:51 +00:00
|
|
|
- message.res_model or default_model
|
|
|
|
- message.res_id or default_res_id
|
2012-09-04 09:28:20 +00:00
|
|
|
- mass_mail: model and IDs of records the user mass-mails
|
2012-08-22 16:21:51 +00:00
|
|
|
- active_ids: record IDs
|
|
|
|
- default_model or active_model
|
2011-07-22 16:34:57 +00:00
|
|
|
"""
|
2012-08-22 16:21:51 +00:00
|
|
|
# get some important values from context
|
2011-07-22 16:34:57 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
2012-07-10 13:06:31 +00:00
|
|
|
result = super(mail_compose_message, self).default_get(cr, uid, fields, context=context)
|
|
|
|
|
2012-08-22 16:21:51 +00:00
|
|
|
# get some important values from context
|
2012-08-30 14:35:49 +00:00
|
|
|
composition_mode = context.get('default_composition_mode', context.get('mail.compose.message.mode'))
|
2012-08-22 16:21:51 +00:00
|
|
|
model = context.get('default_model', context.get('active_model'))
|
|
|
|
res_id = context.get('default_res_id', context.get('active_id'))
|
2012-08-31 17:15:07 +00:00
|
|
|
message_id = context.get('default_parent_id', context.get('message_id', context.get('active_id')))
|
2012-08-22 16:21:51 +00:00
|
|
|
active_ids = context.get('active_ids')
|
|
|
|
|
2012-07-10 13:06:31 +00:00
|
|
|
# get default values according to the composition mode
|
2012-08-30 17:44:52 +00:00
|
|
|
if composition_mode == 'reply':
|
2012-08-31 17:15:07 +00:00
|
|
|
vals = self.get_message_data(cr, uid, message_id, context=context)
|
2012-08-30 17:44:52 +00:00
|
|
|
elif composition_mode == 'comment' and model and res_id:
|
2012-08-22 16:21:51 +00:00
|
|
|
vals = self.get_record_data(cr, uid, model, res_id, context=context)
|
2012-08-30 17:44:52 +00:00
|
|
|
elif composition_mode == 'mass_mail' and model and active_ids:
|
2012-08-30 14:35:49 +00:00
|
|
|
vals = {'model': model, 'res_id': res_id, 'content_subtype': 'html'}
|
2012-08-22 16:21:51 +00:00
|
|
|
else:
|
|
|
|
vals = {'model': model, 'res_id': res_id}
|
|
|
|
if composition_mode:
|
|
|
|
vals['composition_mode'] = composition_mode
|
|
|
|
|
2011-09-25 01:47:59 +00:00
|
|
|
for field in vals:
|
|
|
|
if field in fields:
|
2012-07-06 07:38:52 +00:00
|
|
|
result[field] = vals[field]
|
2011-07-22 16:34:57 +00:00
|
|
|
return result
|
|
|
|
|
2012-08-24 12:29:28 +00:00
|
|
|
def _get_composition_mode_selection(self, cr, uid, context=None):
|
|
|
|
return [('comment', 'Comment a document'), ('reply', 'Reply to a message'), ('mass_mail', 'Mass mailing')]
|
|
|
|
|
2011-07-22 16:34:57 +00:00
|
|
|
_columns = {
|
2012-08-24 12:29:28 +00:00
|
|
|
'composition_mode': fields.selection(
|
|
|
|
lambda s, *a, **k: s._get_composition_mode_selection(*a, **k),
|
|
|
|
string='Composition mode'),
|
2012-08-22 16:21:51 +00:00
|
|
|
'partner_ids': fields.many2many('res.partner',
|
2012-08-21 14:27:47 +00:00
|
|
|
'mail_compose_message_res_partner_rel',
|
2012-08-22 16:21:51 +00:00
|
|
|
'wizard_id', 'partner_id', 'Additional contacts'),
|
|
|
|
'attachment_ids': fields.many2many('ir.attachment',
|
|
|
|
'mail_compose_message_ir_attachments_rel',
|
2012-08-21 14:27:47 +00:00
|
|
|
'wizard_id', 'attachment_id', 'Attachments'),
|
2011-07-22 16:34:57 +00:00
|
|
|
'filter_id': fields.many2one('ir.filters', 'Filters'),
|
2012-09-04 11:54:16 +00:00
|
|
|
'body_text': fields.text('Plain-text Contents'),
|
2012-08-21 14:27:47 +00:00
|
|
|
'content_subtype': fields.char('Message content subtype', size=32, readonly=1,
|
2012-08-20 19:02:31 +00:00
|
|
|
help="Type of message, usually 'html' or 'plain', used to select "\
|
|
|
|
"plain-text or rich-text contents accordingly"),
|
2011-07-22 16:34:57 +00:00
|
|
|
}
|
2012-08-21 14:27:47 +00:00
|
|
|
|
2012-08-20 19:02:31 +00:00
|
|
|
_defaults = {
|
2012-08-22 16:21:51 +00:00
|
|
|
'composition_mode': 'comment',
|
2012-08-20 19:02:31 +00:00
|
|
|
'content_subtype': lambda self,cr, uid, context={}: 'plain',
|
2012-08-22 16:21:51 +00:00
|
|
|
'body_text': lambda self,cr, uid, context={}: False,
|
2012-08-21 14:27:47 +00:00
|
|
|
'body': lambda self,cr, uid, context={}: '',
|
2012-08-22 16:21:51 +00:00
|
|
|
'subject': lambda self,cr, uid, context={}: False,
|
2012-08-20 19:02:31 +00:00
|
|
|
}
|
|
|
|
|
2012-08-23 18:06:48 +00:00
|
|
|
def notify(self, cr, uid, newid, context=None):
|
|
|
|
""" Override specific notify method of mail.message, because we do
|
|
|
|
not want that feature in the wizard. """
|
|
|
|
return
|
|
|
|
|
2012-08-22 16:21:51 +00:00
|
|
|
def get_record_data(self, cr, uid, model, res_id, context=None):
|
2012-07-10 15:33:16 +00:00
|
|
|
""" Returns a defaults-like dict with initial values for the composition
|
|
|
|
wizard when sending an email related to the document record
|
|
|
|
identified by ``model`` and ``res_id``.
|
|
|
|
|
|
|
|
:param str model: model name of the document record this mail is
|
|
|
|
related to.
|
2012-08-22 16:21:51 +00:00
|
|
|
:param int res_id: id of the document record this mail is related to
|
|
|
|
"""
|
|
|
|
return {'model': model, 'res_id': res_id}
|
|
|
|
|
|
|
|
def get_message_data(self, cr, uid, message_id, context=None):
|
|
|
|
""" Returns a defaults-like dict with initial values for the composition
|
|
|
|
wizard when replying to the given message (e.g. including the quote
|
|
|
|
of the initial message, and the correct recipients).
|
|
|
|
|
|
|
|
:param int message_id: id of the mail.message to which the user
|
|
|
|
is replying.
|
2011-07-22 16:34:57 +00:00
|
|
|
"""
|
2012-08-22 16:21:51 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
2012-07-10 15:33:16 +00:00
|
|
|
result = {}
|
2012-08-22 16:21:51 +00:00
|
|
|
if not message_id:
|
|
|
|
return result
|
|
|
|
|
|
|
|
current_user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
|
|
|
|
message_data = self.pool.get('mail.message').browse(cr, uid, message_id, context=context)
|
|
|
|
|
|
|
|
# create subject
|
2012-08-31 17:15:07 +00:00
|
|
|
re_prefix = _('Re:')
|
2012-08-22 16:21:51 +00:00
|
|
|
reply_subject = tools.ustr(message_data.subject or '')
|
|
|
|
if not (reply_subject.startswith('Re:') or reply_subject.startswith(re_prefix)):
|
|
|
|
reply_subject = "%s %s" % (re_prefix, reply_subject)
|
|
|
|
# create the reply in the body
|
|
|
|
reply_header = _('On %(date)s, %(sender_name)s wrote:') % {
|
|
|
|
'date': message_data.date if message_data.date else '',
|
|
|
|
'sender_name': message_data.author_id.name }
|
2012-08-31 17:15:07 +00:00
|
|
|
reply_body = '<div>%s<blockquote>%s</blockquote></div>' % (reply_header, message_data.body)
|
2012-08-22 16:21:51 +00:00
|
|
|
# get partner_ids from original message
|
|
|
|
partner_ids = [partner.id for partner in message_data.partner_ids] if message_data.partner_ids else []
|
|
|
|
|
|
|
|
# update the result
|
2012-07-10 15:33:16 +00:00
|
|
|
result.update({
|
2012-08-22 16:21:51 +00:00
|
|
|
'model': message_data.model,
|
|
|
|
'res_id': message_data.res_id,
|
|
|
|
'parent_id': message_data.id,
|
|
|
|
'body': reply_body,
|
|
|
|
'subject': reply_subject,
|
|
|
|
'partner_ids': partner_ids,
|
|
|
|
'content_subtype': 'html',
|
2012-07-10 15:33:16 +00:00
|
|
|
})
|
|
|
|
return result
|
2011-07-22 16:34:57 +00:00
|
|
|
|
2012-08-22 16:21:51 +00:00
|
|
|
def toggle_content_subtype(self, cr, uid, ids, context=None):
|
2012-08-21 14:27:47 +00:00
|
|
|
""" hit toggle formatting mode button: calls onchange_formatting to
|
|
|
|
emulate an on_change, then writes the value to update the form. """
|
|
|
|
for record in self.browse(cr, uid, ids, context=context):
|
2012-08-21 14:49:30 +00:00
|
|
|
content_st_new_value = 'plain' if record.content_subtype == 'html' else 'html'
|
|
|
|
onchange_res = self.onchange_content_subtype(cr, uid, ids, content_st_new_value, record.model, record.res_id, context=context)
|
2012-08-21 14:27:47 +00:00
|
|
|
self.write(cr, uid, [record.id], onchange_res['value'], context=context)
|
2012-08-22 16:21:51 +00:00
|
|
|
return True
|
2012-07-13 14:23:33 +00:00
|
|
|
|
2012-08-21 14:49:30 +00:00
|
|
|
def onchange_content_subtype(self, cr, uid, ids, value, model, res_id, context=None):
|
|
|
|
""" onchange_content_subtype (values: 'plain' or 'html'). This onchange
|
|
|
|
on the subtype allows to have some specific behavior when switching
|
|
|
|
between text or html mode.
|
2012-07-13 14:23:33 +00:00
|
|
|
This method can be overridden for models that want to have their
|
2012-08-30 14:35:49 +00:00
|
|
|
specific behavior. """
|
2012-08-21 14:49:30 +00:00
|
|
|
return {'value': {'content_subtype': value}}
|
2012-08-21 14:27:47 +00:00
|
|
|
|
2012-08-30 14:35:49 +00:00
|
|
|
def _verify_partner_email(self, cr, uid, partner_ids, context=None):
|
|
|
|
""" Verify that selected partner_ids have an email_address defined.
|
|
|
|
Otherwise throw a warning. """
|
2012-08-21 14:27:47 +00:00
|
|
|
partner_wo_email_lst = []
|
|
|
|
for partner in self.pool.get('res.partner').browse(cr, uid, partner_ids, context=context):
|
|
|
|
if not partner.email:
|
|
|
|
partner_wo_email_lst.append(partner)
|
|
|
|
if not partner_wo_email_lst:
|
2012-08-30 14:35:49 +00:00
|
|
|
return {}
|
2012-08-21 14:27:47 +00:00
|
|
|
warning_msg = _('The following partners chosen as recipients for the email have no email address linked :')
|
|
|
|
for partner in partner_wo_email_lst:
|
|
|
|
warning_msg += '\n- %s' % (partner.name)
|
2012-08-30 14:35:49 +00:00
|
|
|
return {'warning': {
|
|
|
|
'title': _('Partners email addresses not found'),
|
|
|
|
'message': warning_msg }
|
|
|
|
}
|
|
|
|
|
|
|
|
def onchange_partner_ids(self, cr, uid, ids, value, context=None):
|
|
|
|
""" onchange_partner_ids (value format: [[6, False, [3, 4]]]). The
|
|
|
|
basic purpose of this method is to check that destination partners
|
|
|
|
effectively have email addresses. Otherwise a warning is thrown.
|
|
|
|
"""
|
|
|
|
res = {'value': {}}
|
|
|
|
if not value or not value[0] or not value[0][0] == 6:
|
|
|
|
return
|
|
|
|
res.update(self._verify_partner_email(cr, uid, value[0][2], context=context))
|
|
|
|
return res
|
2012-07-13 14:23:33 +00:00
|
|
|
|
2012-08-31 17:15:07 +00:00
|
|
|
def unlink(self, cr, uid, ids, context=None):
|
|
|
|
# Cascade delete all attachments, as they are owned by the composition wizard
|
|
|
|
for wizard in self.read(cr, uid, ids, ['attachment_ids'], context=context):
|
|
|
|
self.pool.get('ir.attachment').unlink(cr, uid, wizard['attachment_ids'], context=context)
|
|
|
|
return super(mail_compose_message,self).unlink(cr, uid, ids, context=context)
|
|
|
|
|
|
|
|
def dummy(self, cr, uid, ids, context=None):
|
|
|
|
""" TDE: defined to have buttons that do basically nothing. It is
|
|
|
|
currently impossible to have buttons that do nothing special
|
|
|
|
in views (if type not specified, considered as 'object'). """
|
|
|
|
return True
|
|
|
|
|
|
|
|
#------------------------------------------------------
|
|
|
|
# Wizard validation and send
|
|
|
|
#------------------------------------------------------
|
|
|
|
|
2012-07-10 15:33:16 +00:00
|
|
|
def send_mail(self, cr, uid, ids, context=None):
|
2012-08-22 16:21:51 +00:00
|
|
|
""" Process the wizard content and proceed with sending the related
|
|
|
|
email(s), rendering any template patterns on the fly if needed. """
|
2012-07-06 15:51:41 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
2012-08-30 14:35:49 +00:00
|
|
|
active_ids = context.get('active_ids')
|
2012-08-20 14:09:01 +00:00
|
|
|
|
2012-08-22 16:21:51 +00:00
|
|
|
for wizard in self.browse(cr, uid, ids, context=context):
|
|
|
|
mass_mail_mode = wizard.composition_mode == 'mass_mail'
|
|
|
|
active_model_pool = self.pool.get(wizard.model if wizard.model else 'mail.thread')
|
2012-08-30 14:35:49 +00:00
|
|
|
|
|
|
|
# wizard works in batch mode: [res_id] or active_ids
|
|
|
|
res_ids = active_ids if mass_mail_mode and wizard.model and active_ids else [wizard.res_id]
|
|
|
|
for res_id in res_ids:
|
|
|
|
# default values, according to the wizard options
|
|
|
|
post_values = {
|
|
|
|
'subject': wizard.subject if wizard.content_subtype == 'html' else False,
|
|
|
|
'body': wizard.body if wizard.content_subtype == 'html' else '<pre>%s</pre>' % tools.ustr(wizard.body_text),
|
|
|
|
'partner_ids': [(4, partner.id) for partner in wizard.partner_ids],
|
2012-08-31 12:56:05 +00:00
|
|
|
'attachments': [(attach.name or attach.datas_fname, base64.b64decode(attach.datas)) for attach in wizard.attachment_ids],
|
2012-08-30 14:35:49 +00:00
|
|
|
}
|
|
|
|
# mass mailing: render and override default values
|
|
|
|
if mass_mail_mode and wizard.model:
|
|
|
|
email_dict = self.render_message(cr, uid, wizard, wizard.model, res_id, context=context)
|
2012-08-31 17:15:07 +00:00
|
|
|
new_partner_ids = email_dict.pop('partner_ids', [])
|
|
|
|
post_values['partner_ids'] += new_partner_ids
|
|
|
|
new_attachments = email_dict.pop('attachments', [])
|
|
|
|
post_values['attachments'] += new_attachments
|
|
|
|
post_values.update(email_dict)
|
2012-08-30 14:35:49 +00:00
|
|
|
# post the message
|
2012-08-31 17:15:07 +00:00
|
|
|
active_model_pool.message_post(cr, uid, [res_id], msg_type='comment', context=context, **post_values)
|
2012-07-06 15:51:41 +00:00
|
|
|
|
|
|
|
return {'type': 'ir.actions.act_window_close'}
|
|
|
|
|
2012-08-30 14:35:49 +00:00
|
|
|
def render_message(self, cr, uid, wizard, model, res_id, context=None):
|
|
|
|
""" Generate an email from the template for given (model, res_id) pair.
|
|
|
|
This method is meant to be inherited by email_template that will
|
|
|
|
produce a more complete dictionary, with email_to, ...
|
|
|
|
"""
|
|
|
|
return {
|
|
|
|
'subject': self.render_template(cr, uid, wizard.subject, model, res_id, context),
|
|
|
|
'body': self.render_template(cr, uid, wizard.body, model, res_id, context),
|
|
|
|
}
|
|
|
|
|
2011-07-22 16:34:57 +00:00
|
|
|
def render_template(self, cr, uid, template, model, res_id, context=None):
|
2012-08-30 14:35:49 +00:00
|
|
|
""" Render the given template text, replace mako-like expressions ``${expr}``
|
|
|
|
with the result of evaluating these expressions with an evaluation context
|
|
|
|
containing:
|
2011-07-22 16:34:57 +00:00
|
|
|
|
|
|
|
* ``user``: browse_record of the current user
|
|
|
|
* ``object``: browse_record of the document record this mail is
|
|
|
|
related to
|
|
|
|
* ``context``: the context passed to the mail composition wizard
|
|
|
|
|
2012-08-30 14:35:49 +00:00
|
|
|
:param str template: the template text to render
|
|
|
|
:param str model: model name of the document record this mail is related to.
|
|
|
|
:param int res_id: id of the document record this mail is related to.
|
2011-07-22 16:34:57 +00:00
|
|
|
"""
|
|
|
|
if context is None:
|
|
|
|
context = {}
|
|
|
|
def merge(match):
|
|
|
|
exp = str(match.group()[2:-1]).strip()
|
2012-08-31 17:15:07 +00:00
|
|
|
result = eval(exp, {
|
|
|
|
'user' : self.pool.get('res.users').browse(cr, uid, uid, context=context),
|
|
|
|
'object' : self.pool.get(model).browse(cr, uid, res_id, context=context),
|
|
|
|
'context': dict(context), # copy context to prevent side-effects of eval
|
|
|
|
})
|
|
|
|
return result and tools.ustr(result) or ''
|
2011-07-22 16:34:57 +00:00
|
|
|
return template and EXPRESSION_PATTERN.sub(merge, template)
|