From 1ac8e7cdfddbf8ac6b7c4b843582392705f10a73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Thu, 20 Feb 2014 16:29:45 +0100 Subject: [PATCH 01/90] [IMP] mail, email_template: mass mailing feature cleaning - now differentiates mass mailing and mass post. - mass mailing is a true mass mailing - using same_thread log a copy in the document, without using message_post (using notification field, + model and res_id) - improved form view of composer, adding a filed explaining a bit the various recipients - removed unnecessary fields coming from the template (partner_to, ...) because they are confusing -> composer should be easier to understand and use - removed some unnecessary code - removed double body computation when using templates (one for tmeplate, then the wizard -> not necessary) This commit will be followed by other to try to improve the mass mailing and mass post. bzr revid: tde@openerp.com-20140220152945-ash0hfkzevzamihq --- addons/email_template/email_template.py | 13 +- .../wizard/mail_compose_message.py | 38 ++++-- .../wizard/mail_compose_message_view.xml | 16 --- addons/mail/mail_mail_view.xml | 1 + addons/mail/static/src/js/mail.js | 2 +- addons/mail/wizard/mail_compose_message.py | 116 +++++++++--------- .../mail/wizard/mail_compose_message_view.xml | 42 ++++--- 7 files changed, 120 insertions(+), 108 deletions(-) diff --git a/addons/email_template/email_template.py b/addons/email_template/email_template.py index fcfe869da90..5167bdc3d79 100644 --- a/addons/email_template/email_template.py +++ b/addons/email_template/email_template.py @@ -356,18 +356,19 @@ class email_template(osv.osv): results = dict() for template, template_res_ids in templates_to_res_ids.iteritems(): # generate fields value for all res_ids linked to the current template - for field in ['subject', 'body_html', 'email_from', 'email_to', 'partner_to', 'email_cc', 'reply_to']: + for field in fields: generated_field_values = self.render_template_batch(cr, uid, getattr(template, field), template.model, template_res_ids, context=context) for res_id, field_value in generated_field_values.iteritems(): results.setdefault(res_id, dict())[field] = field_value # update values for all res_ids for res_id in template_res_ids: values = results[res_id] - if template.user_signature: - signature = self.pool.get('res.users').browse(cr, uid, uid, context).signature - values['body_html'] = tools.append_content_to_html(values['body_html'], signature) - if values['body_html']: - values['body'] = tools.html_sanitize(values['body_html']) + if 'body_html' in fields: + if template.user_signature: + signature = self.pool.get('res.users').browse(cr, uid, uid, context).signature + values['body_html'] = tools.append_content_to_html(values['body_html'], signature) + if values['body_html']: + values['body'] = tools.html_sanitize(values['body_html']) values.update( mail_server_id=template.mail_server_id.id or False, auto_delete=template.auto_delete, diff --git a/addons/email_template/wizard/mail_compose_message.py b/addons/email_template/wizard/mail_compose_message.py index beede1122dc..959fea94420 100644 --- a/addons/email_template/wizard/mail_compose_message.py +++ b/addons/email_template/wizard/mail_compose_message.py @@ -55,14 +55,23 @@ class mail_compose_message(osv.TransientModel): ) return res + def get_recipients_data(self, cr, uid, values, context=None): + if values['composition_mode'] != 'mass_mail': + return super(mail_compose_message, self).get_recipients_data(cr, uid, values, context=context) + model, res_id, template_id = values['model'], values['res_id'], values.get('template_id') + active_ids = context.get('active_ids', list()) + if not active_ids or not template_id: + return False + template = self.pool['email.template'].browse(cr, uid, template_id, context=context) + partner_to = self.render_template_batch(cr, uid, template.partner_to, model, active_ids[:3], context=context) + partner_ids = [int(data) for key, data in partner_to.iteritems() if data] + rec_names = [rec_name[1] for rec_name in self.pool['res.partner'].name_get(cr, SUPERUSER_ID, partner_ids, context=context)] + recipients = ', '.join(rec_names) + recipients += ' and %d more.' % (len(active_ids) - 3) if len(active_ids) > 3 else '.' + return recipients + _columns = { 'template_id': fields.many2one('email.template', 'Use template', select=True), - 'partner_to': fields.char('To (Partner IDs)', - help="Comma-separated list of recipient partners ids (placeholders may be used here)"), - 'email_to': fields.char('To (Emails)', - help="Comma-separated recipient addresses (placeholders may be used here)",), - 'email_cc': fields.char('Cc (Emails)', - help="Carbon copy recipients (placeholders may be used here)"), } def send_mail(self, cr, uid, ids, context=None): @@ -92,7 +101,7 @@ class mail_compose_message(osv.TransientModel): """ - mass_mailing: we cannot render, so return the template values - normal mode: return rendered values """ if template_id and composition_mode == 'mass_mail': - fields = ['subject', 'body_html', 'email_from', 'email_to', 'partner_to', 'email_cc', 'reply_to', 'attachment_ids', 'mail_server_id'] + fields = ['subject', 'body_html', 'email_from', 'reply_to', 'attachment_ids', 'mail_server_id'] template_values = self.pool.get('email.template').read(cr, uid, template_id, fields, context) values = dict((field, template_values[field]) for field in fields if template_values.get(field)) elif template_id: @@ -162,16 +171,18 @@ class mail_compose_message(osv.TransientModel): partner_ids += self.pool['res.partner'].exists(cr, SUPERUSER_ID, tpl_partner_ids, context=context) return partner_ids - def generate_email_for_composer_batch(self, cr, uid, template_id, res_ids, context=None): + def generate_email_for_composer_batch(self, cr, uid, template_id, res_ids, fields=None, context=None): """ Call email_template.generate_email(), get fields relevant for mail.compose.message, transform email_cc and email_to into partner_ids """ # filter template values - fields = ['subject', 'body_html', 'email_from', 'email_to', 'partner_to', 'email_cc', 'reply_to', 'attachment_ids', 'attachments', 'mail_server_id'] + if fields is None: + fields = ['subject', 'body_html', 'email_from', 'email_to', 'partner_to', 'email_cc', 'reply_to', 'attachment_ids', 'mail_server_id'] + returned_fields = fields + ['attachments'] values = dict.fromkeys(res_ids, False) - template_values = self.pool.get('email.template').generate_email_batch(cr, uid, template_id, res_ids, context=context) + template_values = self.pool.get('email.template').generate_email_batch(cr, uid, template_id, res_ids, fields=fields, context=context) for res_id in res_ids: - res_id_values = dict((field, template_values[res_id][field]) for field in fields if template_values[res_id].get(field)) + res_id_values = dict((field, template_values[res_id][field]) for field in returned_fields if template_values[res_id].get(field)) res_id_values['body'] = res_id_values.pop('body_html', '') # transform email_to, email_cc into partner_ids @@ -189,7 +200,10 @@ class mail_compose_message(osv.TransientModel): """ Override to handle templates. """ # generate template-based values if wizard.template_id: - template_values = self.generate_email_for_composer_batch(cr, uid, wizard.template_id.id, res_ids, context=context) + template_values = self.generate_email_for_composer_batch( + cr, uid, wizard.template_id.id, res_ids, + fields=['email_to', 'partner_to', 'email_cc', 'attachment_ids', 'mail_server_id'], + context=context) else: template_values = dict.fromkeys(res_ids, dict()) # generate composer values diff --git a/addons/email_template/wizard/mail_compose_message_view.xml b/addons/email_template/wizard/mail_compose_message_view.xml index 44145114fff..2f755a625d6 100644 --- a/addons/email_template/wizard/mail_compose_message_view.xml +++ b/addons/email_template/wizard/mail_compose_message_view.xml @@ -7,22 +7,6 @@ mail.compose.message - -
Use template diff --git a/addons/mail/mail_mail_view.xml b/addons/mail/mail_mail_view.xml index cf3dd1e8233..e259d28a2b7 100644 --- a/addons/mail/mail_mail_view.xml +++ b/addons/mail/mail_mail_view.xml @@ -36,6 +36,7 @@
+ diff --git a/addons/mail/static/src/js/mail.js b/addons/mail/static/src/js/mail.js index 58d44bab50e..3687961bb6a 100644 --- a/addons/mail/static/src/js/mail.js +++ b/addons/mail/static/src/js/mail.js @@ -507,7 +507,7 @@ openerp.mail = function (session) { } $.when(recipient_done).done(function (partner_ids) { var context = { - 'default_composition_mode': default_composition_mode, + // 'default_composition_mode': default_composition_mode, 'default_parent_id': self.id, 'default_body': mail.ChatterUtils.get_text2html(self.$el ? (self.$el.find('textarea:not(.oe_compact)').val() || '') : ''), 'default_attachment_ids': _.map(self.attachment_ids, function (file) {return file.id;}), diff --git a/addons/mail/wizard/mail_compose_message.py b/addons/mail/wizard/mail_compose_message.py index 74aea3dfc01..c8326ed48cd 100644 --- a/addons/mail/wizard/mail_compose_message.py +++ b/addons/mail/wizard/mail_compose_message.py @@ -68,28 +68,25 @@ class mail_compose_message(osv.TransientModel): if context is None: context = {} result = super(mail_compose_message, self).default_get(cr, uid, fields, context=context) - # get some important values from context - composition_mode = context.get('default_composition_mode', context.get('mail.compose.message.mode')) - model = context.get('default_model', context.get('active_model')) - res_id = context.get('default_res_id', context.get('active_id')) - message_id = context.get('default_parent_id', context.get('message_id', context.get('active_id'))) - active_ids = context.get('active_ids') + + # v6.1 compatibility mode + result['composition_mode'] = result.get('composition_mode', context.get('mail.compose.message.mode')) + result['model'] = result.get('model', context.get('active_model')) + result['res_id'] = result.get('res_id', context.get('active_id')) + result['parent_id'] = result.get('parent_id', context.get('message_id')) + + # default values according to composition mode - NOTE: reply is deprecated, fall back on comment + if result['composition_mode'] == 'reply': + result['composition_mode'] = 'comment' + vals = {} if 'active_domain' in context: # not context.get() because we want to keep global [] domains - result['use_active_domain'] = True - result['active_domain'] = '%s' % context.get('active_domain') - elif not result.get('active_domain'): - result['active_domain'] = '' - # get default values according to the composition mode - if composition_mode == 'reply': - vals = self.get_message_data(cr, uid, message_id, context=context) - elif composition_mode == 'comment' and model and res_id: - vals = self.get_record_data(cr, uid, model, res_id, context=context) - elif composition_mode == 'mass_mail' and model and active_ids: - vals = {'model': model, 'res_id': res_id} - else: - vals = {'model': model, 'res_id': res_id} - if composition_mode: - vals['composition_mode'] = composition_mode + vals['use_active_domain'] = True + vals['active_domain'] = '%s' % context.get('active_domain') + if result.get('parent_id'): + vals.update(self.get_message_data(cr, uid, result.get('parent_id'), context=context)) + if result['composition_mode'] == 'comment' and result['model'] and result['res_id']: + vals.update(self.get_record_data(cr, uid, result['model'], result['res_id'], context=context)) + result['recipients_data'] = self.get_recipients_data(cr, uid, result, context=context) for field in vals: if field in fields: @@ -102,13 +99,15 @@ class mail_compose_message(osv.TransientModel): # but when creating the mail.message to create the mail.compose.message # access rights issues may rise # We therefore directly change the model and res_id - if result.get('model') == 'res.users' and result.get('res_id') == uid: + if result['model'] == 'res.users' and result['res_id'] == uid: result['model'] = 'res.partner' result['res_id'] = self.pool.get('res.users').browse(cr, uid, uid).partner_id.id return result def _get_composition_mode_selection(self, cr, uid, context=None): - return [('comment', 'Comment a document'), ('reply', 'Reply to a message'), ('mass_mail', 'Mass mailing')] + return [('comment', 'Post on a document'), + ('mass_mail', 'Email Mass Mailing'), + ('mass_post', 'Post on Multiple Documents')] _columns = { 'composition_mode': fields.selection( @@ -116,11 +115,11 @@ class mail_compose_message(osv.TransientModel): string='Composition mode'), 'partner_ids': fields.many2many('res.partner', 'mail_compose_message_res_partner_rel', - 'wizard_id', 'partner_id', 'Additional contacts'), + 'wizard_id', 'partner_id', 'Additional Contacts'), + 'recipients_data': fields.text(string='Recipients Data', + help='Helper field used in mass mailing to display a sample of recipients'), 'use_active_domain': fields.boolean('Use active domain'), 'active_domain': fields.char('Active domain', readonly=True), - 'post': fields.boolean('Post a copy in the document', - help='Post a copy of the message on the document communication history.'), 'notify': fields.boolean('Notify followers', help='Notify followers of the document'), 'same_thread': fields.boolean('Replies in the document', @@ -136,8 +135,7 @@ class mail_compose_message(osv.TransientModel): 'body': lambda self, cr, uid, ctx={}: '', 'subject': lambda self, cr, uid, ctx={}: False, 'partner_ids': lambda self, cr, uid, ctx={}: [], - 'post': False, - 'notify': False, + 'notify': True, 'same_thread': True, } @@ -169,6 +167,21 @@ class mail_compose_message(osv.TransientModel): not want that feature in the wizard. """ return + def get_recipients_data(self, cr, uid, values, context=None): + """ Returns a string explaining the targetted recipients, to ease the use + of the wizard. """ + composition_mode, model, res_id = values['composition_mode'], values['model'], values['res_id'] + if composition_mode == 'comment' and model and res_id: + doc_name = self.pool[model].name_get(cr, uid, [res_id], context=context) + return doc_name and 'Followers of %s' % doc_name[0][1] or False + elif composition_mode == 'mass_post' and model: + active_ids = context.get('active_ids', list()) + if not active_ids: + return False + name_gets = [rec_name[1] for rec_name in self.pool[model].name_get(cr, uid, active_ids[:3], context=context)] + return 'Followers of selected documents (' + ', '.join(name_gets) + len(active_ids) > 3 and ', ...' or '' + ')' + return False + def get_record_data(self, cr, uid, model, res_id, context=None): """ Returns a defaults-like dict with initial values for the composition wizard when sending an email related to the document record @@ -179,17 +192,10 @@ class mail_compose_message(osv.TransientModel): :param int res_id: id of the document record this mail is related to """ doc_name_get = self.pool[model].name_get(cr, uid, [res_id], context=context) - record_name = False - if doc_name_get: - record_name = doc_name_get[0][1] - values = { - 'model': model, - 'res_id': res_id, - 'record_name': record_name, + return { + 'record_name': doc_name_get and doc_name_get[0][1] or False, + 'subject': doc_name_get and 'Re: %s' % doc_name_get[0][1] or False, } - if record_name: - values['subject'] = 'Re: %s' % record_name - return values def get_message_data(self, cr, uid, message_id, context=None): """ Returns a defaults-like dict with initial values for the composition @@ -208,23 +214,20 @@ class mail_compose_message(osv.TransientModel): # create subject re_prefix = _('Re:') reply_subject = tools.ustr(message_data.subject or message_data.record_name or '') - if not (reply_subject.startswith('Re:') or reply_subject.startswith(re_prefix)) and message_data.subject: + if not (reply_subject.startswith('Re:') or reply_subject.startswith(re_prefix)): reply_subject = "%s %s" % (re_prefix, reply_subject) + # get partner_ids from original message partner_ids = [partner.id for partner in message_data.partner_ids] if message_data.partner_ids else [] partner_ids += context.get('default_partner_ids', []) - if context.get('is_private',False) and message_data.author_id : #check message is private then add author also in partner list. + if context.get('is_private') and message_data.author_id: # check message is private then add author also in partner list. partner_ids += [message_data.author_id.id] # update the result - result = { + return { 'record_name': message_data.record_name, - 'model': message_data.model, - 'res_id': message_data.res_id, - 'parent_id': message_data.id, 'subject': reply_subject, 'partner_ids': partner_ids, } - return result #------------------------------------------------------ # Wizard validation and send @@ -244,16 +247,16 @@ class mail_compose_message(osv.TransientModel): is_log = context.get('mail_compose_log', False) for wizard in self.browse(cr, uid, ids, context=context): - mass_mail_mode = wizard.composition_mode == 'mass_mail' + mass_mode = wizard.composition_mode in ('mass_mail', 'mass_post') active_model_pool = self.pool[wizard.model if wizard.model else 'mail.thread'] if not hasattr(active_model_pool, 'message_post'): context['thread_model'] = wizard.model active_model_pool = self.pool['mail.thread'] # wizard works in batch mode: [res_id] or active_ids or active_domain - if mass_mail_mode and wizard.use_active_domain and wizard.model: + if mass_mode and wizard.use_active_domain and wizard.model: res_ids = self.pool[wizard.model].search(cr, uid, eval(wizard.active_domain), context=context) - elif mass_mail_mode and wizard.model and active_ids: + elif mass_mode and wizard.model and active_ids: res_ids = active_ids else: res_ids = [wizard.res_id] @@ -261,7 +264,9 @@ class mail_compose_message(osv.TransientModel): all_mail_values = self.get_mail_values(cr, uid, wizard, res_ids, context=context) for res_id, mail_values in all_mail_values.iteritems(): - if mass_mail_mode and not wizard.post: + if wizard.composition_mode == 'mass_mail': + if wizard.same_thread: # same thread: keep a copy of the message in the chatter to enable the reply redirection + mail_values.update(notification=True, model=wizard.model, res_id=res_id) m2m_attachment_ids = self.pool['mail.thread']._message_preprocess_attachments( cr, uid, mail_values.pop('attachments', []), mail_values.pop('attachment_ids', []), @@ -271,11 +276,9 @@ class mail_compose_message(osv.TransientModel): self.pool.get('mail.mail').create(cr, uid, mail_values, context=context) else: subtype = 'mail.mt_comment' - if is_log: # log a note: subtype is False + if is_log or (wizard.composition_mode == 'mass_post' and not wizard.notify): # log a note: subtype is False subtype = False - elif mass_mail_mode: # mass mail: is a log pushed to recipients unless specified, author not added - if not wizard.notify: - subtype = False + if wizard.composition_mode == 'mass_post': context = dict(context, mail_notify_force_send=False, # do not send emails directly but use the queue instead mail_create_nosubscribe=True) # add context key to avoid subscribing the author @@ -326,8 +329,7 @@ class mail_compose_message(osv.TransientModel): else: email_dict.pop('reply_to', None) mail_values.update(email_dict) - # mass mailing without post: mail_mail values - if mass_mail_mode and not wizard.post: + # mail_mail values if 'mail_auto_delete' in context: mail_values['auto_delete'] = context.get('mail_auto_delete') mail_values['body_html'] = mail_values.get('body', '') @@ -335,6 +337,10 @@ class mail_compose_message(osv.TransientModel): results[res_id] = mail_values return results + #------------------------------------------------------ + # Template rendering + #------------------------------------------------------ + def render_message_batch(self, cr, uid, wizard, res_ids, context=None): """Generate template-based values of wizard, for the document records given by res_ids. This method is meant to be inherited by email_template that diff --git a/addons/mail/wizard/mail_compose_message_view.xml b/addons/mail/wizard/mail_compose_message_view.xml index 948d9b3832c..4c539e426d9 100644 --- a/addons/mail/wizard/mail_compose_message_view.xml +++ b/addons/mail/wizard/mail_compose_message_view.xml @@ -28,29 +28,35 @@ - - + + + Imported Contacts + mail.mass_mailing.contact + + + Customers + res.partner + + [('customer', '=', True)] + + + + + Aristide Antario + aa@example.com + + + + Beverly Bridge + bb@example.com + + + + Carol Cartridge + cc@example.com + + + + + Partners Newsletter @@ -25,12 +162,14 @@ First Newsletter + done - + Second Newsletter + test @@ -63,20 +202,5 @@ - - - 1111005@OpenERP.com - - - - - 1111006@OpenERP.com - - - - - 1111007@OpenERP.com - - diff --git a/addons/mass_mailing/mass_mailing_view.xml b/addons/mass_mailing/mass_mailing_view.xml index e8848fba44f..b9549ebd176 100644 --- a/addons/mass_mailing/mass_mailing_view.xml +++ b/addons/mass_mailing/mass_mailing_view.xml @@ -2,6 +2,147 @@ + + + + + + + + mail.mass_mailing.contact.search + mail.mass_mailing.contact + + + + + + + + + + + + + + + + mail.mass_mailing.contact.tree + mail.mass_mailing.contact + 10 + + + + + + + + + + + + mail.mass_mailing.contact.form + mail.mass_mailing.contact + +
+ + + + + + + + +
+
+
+ + + Mass Mailing Contacts + mail.mass_mailing.contact + form + tree,form + {'search_default_not_opt_out': 1} + + + + + + + mail.mass_mailing.list.search + mail.mass_mailing.list + + + + + + + + + + mail.mass_mailing.list.tree + mail.mass_mailing.list + 10 + + + + + + + + + + + mail.mass_mailing.list.form + mail.mass_mailing.list + +
+
+
+ + + + + +
+
+
+ + + Contact Lists + mail.mass_mailing.list + form + tree,form + + + + mail.mass_mailing.search @@ -13,6 +154,7 @@ @@ -32,7 +174,8 @@ - + @@ -43,19 +186,51 @@ mail.mass_mailing
+
+
- + + + - - - +

Here be some graphs

- + + +
@@ -76,7 +251,7 @@ mail.mass_mailing.kanban mail.mass_mailing - + @@ -87,8 +262,9 @@

- Sent:
- Campaign: + Sent:
+ Campaign: +

@@ -112,11 +288,11 @@

Opened


- +

Replied


- +
@@ -147,6 +323,10 @@ + + mail.mass_mailing.campaign.search @@ -154,10 +334,13 @@ + - + @@ -171,6 +354,8 @@ + + @@ -182,14 +367,16 @@
+ - + - + + + + + + + + + +
selected + + + + @@ -258,12 +273,13 @@