[IMP] mail, email_template: fixed attachment management (template does not override...
authorThibault Delavallée <tde@openerp.com>
Mon, 4 Mar 2013 09:22:48 +0000 (10:22 +0100)
committerThibault Delavallée <tde@openerp.com>
Mon, 4 Mar 2013 09:22:48 +0000 (10:22 +0100)
bzr revid: tde@openerp.com-20130304092248-mqadzmrhgdu58j8y

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

index 5631b92..1221cfd 100644 (file)
@@ -84,20 +84,21 @@ 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':
-            values = self.pool.get('email.template').read(cr, uid, template_id, ['subject', 'body_html'], context)
+            values = self.pool.get('email.template').read(cr, uid, template_id, ['subject', 'body_html', 'attachment_ids'], context)
             values.pop('id')
         elif template_id:
             # 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
+            # transform attachments into attachment_ids; not attached to the document because this will
+            # be done further in the posting process, allowing to clean database if email not send
             ir_attach_obj = self.pool.get('ir.attachment')
             for attach_fname, attach_datas in values.pop('attachments', []):
                 data_attach = {
                     'name': attach_fname,
                     'datas': attach_datas,
                     'datas_fname': attach_fname,
-                    'res_model': model,
-                    'res_id': res_id,
+                    'res_model': 'mail.compose.message',
+                    'res_id': 0,
                     '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))
@@ -140,12 +141,15 @@ 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
+        values = {
+            'attachment_ids': [],
+            'partner_ids': [],
+        }
         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.update(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
-        values['partner_ids'] = []
 
+        # transform email_to, email_cc into partner_ids
         mails = tools.email_split(values.pop('email_to', '') + ' ' + values.pop('email_cc', ''))
         for mail in mails:
             partner_id = self.pool.get('res.partner').find_or_create(cr, uid, mail, context=context)
index 0ec5242..f70e362 100644 (file)
@@ -214,8 +214,10 @@ class mail_compose_message(osv.TransientModel):
                     email_dict = self.render_message(cr, uid, wizard, res_id, context=context)
                     post_values['partner_ids'] += email_dict.pop('partner_ids', [])
                     post_values['attachments'] = email_dict.pop('attachments', [])
-                    post_values['attachment_ids'] += email_dict.pop('attachment_ids', [])
-                    # we must duplicate attachments, as each document will have its own copy of the attachment
+                    # manage attachments :
+                    # - do not re-add attachments from template already displayed
+                    # - duplicate template attachments because of ownership concept in OpenERP
+                    post_values['attachment_ids'] += filter(lambda item: item not in post_values['attachment_ids'], email_dict.pop('attachment_ids', []))
                     attachment_ids = []
                     for attach_id in post_values.pop('attachment_ids'):
                         new_attach_id = ir_attachment_obj.copy(cr, uid, attach_id, {'res_model': active_model_pool_name, 'res_id': res_id}, context=context)
@@ -227,8 +229,8 @@ class mail_compose_message(osv.TransientModel):
                 active_model_pool.message_post(cr, uid, [res_id], type='comment', subtype=subtype, context=context, **post_values)
 
             # mass mailing: delete mail.compose.message attachments, added by the user and that have been duplicated
-            if mass_mail_mode and wizard.attachment_ids:
-                wizard.attachment_ids.unlink()
+            if wizard.attachment_ids:
+                ir_attachment_obj.unlink(cr, uid, [attach.id for attach in wizard.attachment_ids if attach.res_model == self._name], context=context)
 
         return {'type': 'ir.actions.act_window_close'}
 
@@ -239,6 +241,7 @@ class mail_compose_message(osv.TransientModel):
         return {
             'subject': self.render_template(cr, uid, wizard.subject, wizard.model, res_id, context),
             'body': self.render_template(cr, uid, wizard.body, wizard.model, res_id, context),
+            'attachment_ids': [attach.id for attach in wizard.attachment_ids],
         }
 
     def render_template(self, cr, uid, template, model, res_id, context=None):