[FIX] mail: first try to fix attachment duplication.
authorThibault Delavallée <tde@openerp.com>
Thu, 28 Feb 2013 13:26:25 +0000 (14:26 +0100)
committerThibault Delavallée <tde@openerp.com>
Thu, 28 Feb 2013 13:26:25 +0000 (14:26 +0100)
bzr revid: tde@openerp.com-20130228132625-8yjggruhrvbvep21

addons/email_template/email_template.py
addons/email_template/wizard/mail_compose_message.py
addons/mail/wizard/mail_compose_message.py

index 7433840..48fdc7e 100644 (file)
@@ -292,8 +292,7 @@ class email_template(osv.osv):
                         'copyvalue': self.build_expression(field_value.name, False, null_value or False),
                         'null_value': null_value or False
                         })
-        return {'value':result}
-
+        return {'value': result}
 
     def generate_email(self, cr, uid, template_id, res_id, context=None):
         """Generates an email from the template for given (model, res_id) pair.
@@ -346,11 +345,13 @@ class email_template(osv.osv):
                 report_name += ext
             attachments.append((report_name, result))
 
+        attachment_ids = []
         # Add template attachments
         for attach in template.attachment_ids:
-            attachments.append((attach.datas_fname, attach.datas))
+            attachment_ids.append(attach.id)
 
         values['attachments'] = attachments
+        values['attachment_ids'] = attachments
         return values
 
     def send_mail(self, cr, uid, template_id, res_id, force_send=False, context=None):
@@ -365,28 +366,33 @@ class email_template(osv.osv):
                 was executed for this message only.
            :returns: id of the mail.message that was created
         """
-        if context is None: context = {}
+        if context is None:
+            context = {}
         mail_mail = self.pool.get('mail.mail')
         ir_attachment = self.pool.get('ir.attachment')
+
+        # create a mail_mail based on values, without attachments
         values = self.generate_email(cr, uid, template_id, res_id, context=context)
-        assert 'email_from' in values, 'email_from is missing or empty after template rendering, send_mail() cannot proceed'
-        attachments = values.pop('attachments') or {}
-        del values['email_recipients'] # TODO Properly use them.
-        msg_id = mail_mail.create(cr, uid, values, context=context)
-        # link attachments
-        attachment_ids = []
-        for fname, fcontent in attachments.iteritems():
+        assert values.get('email_from'), 'email_from is missing or empty after template rendering, send_mail() cannot proceed'
+        del values['email_recipients']  # TODO Properly use them.
+
+        # manage attachments
+        attachment_ids = values.pop('attachment_ids', [])
+        attachments = values.pop('attachments', [])
+        for fname, fcontent in attachments:
             attachment_data = {
                     'name': fname,
                     'datas_fname': fname,
                     'datas': fcontent,
-                    'res_model': mail_mail._name,
-                    'res_id': msg_id,
+                    'res_model': values.get('model', False),
+                    'res_id': values.get('res_id', False),
             }
             context.pop('default_type', None)
             attachment_ids.append(ir_attachment.create(cr, uid, attachment_data, context=context))
         if attachment_ids:
-            mail_mail.write(cr, uid, msg_id, {'attachment_ids': [(6, 0, attachment_ids)]}, context=context)
+            values['attachment_ids'] = [(6, 0, attachment_ids)]
+
+        msg_id = mail_mail.create(cr, uid, values, context=context)
         if force_send:
             mail_mail.send(cr, uid, [msg_id], context=context)
         return msg_id
index b39ed4e..397f913 100644 (file)
@@ -71,7 +71,6 @@ class mail_compose_message(osv.TransientModel):
             # FIXME odo: change the mail generation to avoid attachment duplication
             values = self.generate_email_for_composer(cr, uid, template_id, res_id, context=context)
             # transform attachments into attachment_ids
-            values['attachment_ids'] = []
             ir_attach_obj = self.pool.get('ir.attachment')
             for attach_fname, attach_datas in values.pop('attachments', []):
                 data_attach = {
@@ -80,7 +79,7 @@ class mail_compose_message(osv.TransientModel):
                     'datas_fname': attach_fname,
                     'res_model': model,
                     'res_id': res_id,
-                    'type': 'binary', # override default_type from context, possibly meant for another model!
+                    'type': 'binary',  # override default_type from context, possibly meant for another model!
                 }
                 values['attachment_ids'].append(ir_attach_obj.create(cr, uid, data_attach, context=context))
         else:
@@ -122,7 +121,7 @@ class mail_compose_message(osv.TransientModel):
             mail.compose.message, transform email_cc and email_to into partner_ids """
         template_values = self.pool.get('email.template').generate_email(cr, uid, template_id, res_id, context=context)
         # filter template values
-        fields = ['body_html', 'subject', 'email_to', 'email_recipients', 'email_cc', 'attachments']
+        fields = ['body_html', 'subject', 'email_to', 'email_recipients', 'email_cc', 'attachment_ids', 'attachments']
         values = dict((field, template_values[field]) for field in fields if template_values.get(field))
         values['body'] = values.pop('body_html', '')
         # transform email_to, email_cc into partner_ids
index ec0c2a8..5741aac 100644 (file)
@@ -205,13 +205,16 @@ class mail_compose_message(osv.TransientModel):
                     'body': wizard.body,
                     'parent_id': wizard.parent_id and wizard.parent_id.id,
                     'partner_ids': [(4, partner.id) for partner in wizard.partner_ids],
-                    'attachments': [(attach.datas_fname or attach.name, base64.b64decode(attach.datas)) for attach in wizard.attachment_ids],
+                    'attachments': [],
+                    'attachment_ids': [(4, attach.id) for attach in wizard.attachment_ids],
                 }
                 # mass mailing: render and override default values
                 if mass_mail_mode and wizard.model:
                     email_dict = self.render_message(cr, uid, wizard, res_id, context=context)
                     new_partner_ids = email_dict.pop('partner_ids', [])
                     post_values['partner_ids'] += [(4, partner_id) for partner_id in new_partner_ids]
+                    new_attachment_ids = email_dict.pop('attachment_ids', [])
+                    post_values['attachments'] += new_attachment_ids
                     new_attachments = email_dict.pop('attachments', [])
                     post_values['attachments'] += new_attachments
                     post_values.update(email_dict)