← Back to team overview

credativ team mailing list archive

[Branch ~credativ/openobject-addons/6.1] Rev 7068: [FIX] When bulk emailing a template which uses a report, recreate the report for each selected re...

 

------------------------------------------------------------
revno: 7068
committer: Craig Gowing (credativ) <craig.gowing@xxxxxxxxxxxxxx>
branch nick: addons
timestamp: Wed 2015-01-21 13:33:19 +0000
message:
  [FIX] When bulk emailing a template which uses a report, recreate the report for each selected record instead of sending the first report in all emails
modified:
  email_template/wizard/mail_compose_message.py


--
lp:~credativ/openobject-addons/6.1
https://code.launchpad.net/~credativ/openobject-addons/6.1

Your team credativ is subscribed to branch lp:~credativ/openobject-addons/6.1.
To unsubscribe from this branch go to https://code.launchpad.net/~credativ/openobject-addons/6.1/+edit-subscription
=== modified file 'email_template/wizard/mail_compose_message.py'
--- email_template/wizard/mail_compose_message.py	2012-08-20 15:56:01 +0000
+++ email_template/wizard/mail_compose_message.py	2015-01-21 13:33:19 +0000
@@ -94,22 +94,23 @@
                 
                 values['attachments'] = False
                 attachments = {}
-                if template.report_template:
-                    report_name = self.render_template(cr, uid, template.report_name, template.model, res_id, context=context)
-                    report_service = 'report.' + report_xml_pool.browse(cr, uid, template.report_template.id, context).report_name
-                    # Ensure report is rendered using template's language
-                    ctx = context.copy()
-                    if template.lang:
-                        ctx['lang'] = self.render_template(cr, uid, template.lang, template.model, res_id, context)
-                    service = netsvc.LocalService(report_service)
-                    (result, format) = service.create(cr, uid, [res_id], {'model': template.model}, ctx)
-                    result = base64.b64encode(result)
-                    if not report_name:
-                        report_name = report_service
-                    ext = "." + format
-                    if not report_name.endswith(ext):
-                        report_name += ext
-                    attachments[report_name] = result
+                # FIX: Do not send the same copy of the report to ALL resources! Recreate the report when sending.
+                # if template.report_template:
+                #     report_name = self.render_template(cr, uid, template.report_name, template.model, res_id, context=context)
+                #     report_service = 'report.' + report_xml_pool.browse(cr, uid, template.report_template.id, context).report_name
+                #     # Ensure report is rendered using template's language
+                #     ctx = context.copy()
+                #     if template.lang:
+                #         ctx['lang'] = self.render_template(cr, uid, template.lang, template.model, res_id, context)
+                #     service = netsvc.LocalService(report_service)
+                #     (result, format) = service.create(cr, uid, [res_id], {'model': template.model}, ctx)
+                #     result = base64.b64encode(result)
+                #     if not report_name:
+                #         report_name = report_service
+                #     ext = "." + format
+                #     if not report_name.endswith(ext):
+                #         report_name += ext
+                #     attachments[report_name] = result
 
                 # Add document attachments
                 for attach in template.attachment_ids:
@@ -131,7 +132,7 @@
                             'res_id' : ids[0] if ids else False
                         }
                         att_ids.append(attachment_obj.create(cr, uid, data_attach))
-                    values['attachment_ids'] = att_ids              
+                    values['attachment_ids'] = att_ids
             else:
                 # render the mail as one-shot
                 values = self.pool.get('email.template').generate_email(cr, uid, template_id, res_id, context=context)
@@ -210,4 +211,54 @@
     def _prepare_render_template_context(self, cr, uid, model, res_id, context=None):
         return self.pool.get('email.template')._prepare_render_template_context(cr, uid, model, res_id, context=context)
 
+    def send_mail(self, cr, uid, ids, context=None):
+        """ Override the base functionality to add some additional logic:
+            1. Are we mass mailing multiple objects
+            2. Does the email template contain a report
+            3. If both yes, for each id generate a unique report attachment, generate email, delete """
+        if context is None:
+            context = {}
+        report_xml_pool = self.pool.get('ir.actions.report.xml')
+        attachment_obj = self.pool.get('ir.attachment')
+        working_on_multi_resources = len(context.get('active_ids') or []) > 1 and True or False
+        if context.get('mail.compose.message.mode') == 'mass_mail' and working_on_multi_resources:
+            for mail in self.browse(cr, uid, ids, context=context):
+                res_id = context.get('mail.compose.target.id') or context.get('active_id') or False
+                template = mail.template_id and self.pool.get('email.template').get_email_template(cr, uid, mail.template_id, res_id, context)
+                if template.report_template:
+                    for active_id in context.get('active_ids'):
+                        report_name = self.render_template(cr, uid, template.report_name, template.model, active_id, context=context)
+                        report_service = 'report.' + report_xml_pool.browse(cr, uid, template.report_template.id, context).report_name
+                        # Ensure report is rendered using template's language
+                        ctx = context.copy()
+                        if template.lang:
+                            ctx['lang'] = self.render_template(cr, uid, template.lang, template.model, active_id, context)
+                        service = netsvc.LocalService(report_service)
+                        (result, format) = service.create(cr, uid, [active_id], {'model': template.model}, ctx)
+                        result = base64.b64encode(result)
+                        if not report_name:
+                            report_name = report_service
+                        ext = "." + format
+                        if not report_name.endswith(ext):
+                            report_name += ext
+                        data_attach = {
+                            'name': report_name,
+                            'datas': result,
+                            'datas_fname': report_name,
+                            'description': report_name,
+                            'res_model' : self._name,
+                            'res_id' : mail.id,
+                        }
+                        att_id = attachment_obj.create(cr, uid, data_attach)
+                        self.write(cr, uid, [mail.id], {'attachment_ids': [(4, att_id)]}) # Add relationship
+                        ctx = context.copy()
+                        ctx.update({'active_id': active_id, 'active_ids': [active_id,]})
+                        super(mail_compose_message, self).send_mail(cr, uid, [mail.id], context=ctx)
+                        self.write(cr, uid, [mail.id], {'attachment_ids': [(2, att_id)]}) # Remove relationship and unlink
+                else:
+                    super(mail_compose_message, self).send_mail(cr, uid, [mail.id], context=context)
+        else:
+            super(mail_compose_message, self).send_mail(cr, uid, ids, context=context)
+        return {'type': 'ir.actions.act_window_close'}
+
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: