[IMP] mail, email_template: mass mailing feature cleaning
authorThibault Delavallée <tde@openerp.com>
Thu, 20 Feb 2014 15:29:45 +0000 (16:29 +0100)
committerThibault Delavallée <tde@openerp.com>
Thu, 20 Feb 2014 15:29:45 +0000 (16:29 +0100)
- 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
addons/email_template/wizard/mail_compose_message.py
addons/email_template/wizard/mail_compose_message_view.xml
addons/mail/mail_mail_view.xml
addons/mail/static/src/js/mail.js
addons/mail/wizard/mail_compose_message.py
addons/mail/wizard/mail_compose_message_view.xml

index fcfe869..5167bdc 100644 (file)
@@ -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,
index beede11..959fea9 100644 (file)
@@ -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
index 4414511..2f755a6 100644 (file)
@@ -7,22 +7,6 @@
             <field name="model">mail.compose.message</field>
             <field name="inherit_id" ref="mail.email_compose_message_wizard_form"/>
             <field name="arch" type="xml">
-                <xpath expr="//field[@name='subject']" position="after">
-                    <label string="Template Recipients" for="partner_to"
-                            groups="base.group_no_one"
-                            attrs="{'invisible':[('composition_mode', '!=', 'mass_mail')]}"/>
-                    <div groups="base.group_no_one" name="template_recipients"
-                            attrs="{'invisible':[('composition_mode', '!=', 'mass_mail')]}">
-                        <group class="oe_grey">
-                            <!-- <label string="Partners" for="partner_to"/> -->
-                            <field name="partner_to" readonly="1"/>
-                            <!-- <label string="Email To" for="email_to"/> -->
-                            <field name="email_to" readonly="1"/>
-                            <!-- <label string="Email CC" for="email_cc"/> -->
-                            <field name="email_cc" readonly="1"/>
-                        </group>
-                    </div>
-                </xpath>
                 <xpath expr="//footer" position="inside">
                     <group class="oe_right oe_form" col="1">
                         <div>Use template
index cf3dd1e..e259d28 100644 (file)
@@ -36,6 +36,7 @@
                                     <div>
                                         <group string="Status">
                                             <field name="auto_delete"/>
+                                            <field name="notification"/>
                                             <field name="type"/>
                                             <field name="state"/>
                                             <field name="mail_server_id"/>
index 58d44ba..3687961 100644 (file)
@@ -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;}),
index 74aea3d..c8326ed 100644 (file)
@@ -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
index 948d9b3..4c539e4 100644 (file)
                         <field name="email_from"
                             attrs="{'invisible':[('composition_mode', '!=', 'mass_mail')]}"/>
                         <field name="subject" placeholder="Subject..." required="True"/>
-                        <!-- classic message composer -->
-                        <label for="partner_ids" string="Recipients"
-                            attrs="{'invisible':[('composition_mode', '=', 'mass_mail')]}"/>
-                        <div groups="base.group_user"
-                            attrs="{'invisible':[('composition_mode', '=', 'mass_mail')]}">
-                            <span attrs="{'invisible':[('model', '=', False)]}">
-                                Followers of
-                                <field name="record_name" readonly="1" class="oe_inline oe_compose_recipients"/>
-                                and
+                        <!-- recipients -->
+                        <label for="partner_ids" string="Recipients" groups="base.group_user"/>
+                        <div groups="base.group_user">
+                            <span attrs="{'invisible': [('composition_mode', '!=', 'mass_mail')]}">
+                                <strong>Email mass mailing</strong> on
+                                <span attrs="{'invisible': [('use_active_domain', '=', True)]}">the selected records</span>
+                                <span attrs="{'invisible': [('use_active_domain', '=', False)]}">the current search filter</span>.
+                                <br />
+                                <span>The following contacts will be mailed: </span>
                             </span>
+                            <field name="recipients_data" class="oe_inline oe_compose_recipients" readonly="1"/>
+                            <span attrs="{'invisible':['|', ('composition_mode', '!=', 'comment'), ('recipients_data', '=', False)]}">and</span>
                             <field name="partner_ids" widget="many2many_tags_email" placeholder="Add contacts to notify..."
-                                context="{'force_email':True, 'show_email':True}"/>
+                                context="{'force_email':True, 'show_email':True}"
+                                attrs="{'invisible': [('composition_mode', '!=', 'comment')]}"/>
                         </div>
-                        <!-- mass post / mass mailing -->
-                        <field name="post"
-                            attrs="{'invisible':[('composition_mode', '!=', 'mass_mail')]}"/>
+                        <!-- mass post -->
                         <field name="notify"
-                            attrs="{'invisible':['|', ('post', '!=', True), ('composition_mode', '!=', 'mass_mail')]}"/>
-                        <field name="same_thread"
-                            attrs="{'invisible':[('composition_mode', '!=', 'mass_mail')]}"/>
-                        <field name="reply_to" placeholder="Email address te redirect replies..."
+                            attrs="{'invisible':['|', ('composition_mode', '!=', 'mass_post')]}"/>
+                        <!-- mass mailing -->
+                        <label for="same_thread"/>
+                            <div>
+                                <field name="same_thread"
+                                    attrs="{'invisible':[('composition_mode', '!=', 'mass_mail')]}"/>
+                                (a copy of the message will be added in the Chatter of each document)
+                            </div>
+                        <field name="reply_to" placeholder="Email address to redirect replies..."
                             attrs="{'invisible':['|', ('same_thread', '=', True), ('composition_mode', '!=', 'mass_mail')],
-                                    'required':[('same_thread', '!=', True)]}"/>
+                                    'required':[('same_thread', '!=', True), ('composition_mode', '=', 'mass_mail')]}"/>
                     </group>
                     <field name="body"/>
                     <field name="attachment_ids" widget="many2many_binary" string="Attach a file"/>