← Back to team overview

credativ team mailing list archive

[Branch ~credativ/openobject-addons/6.1] Rev 7081: [FIX] Mail works when using composition wizard

 

------------------------------------------------------------
revno: 7081
committer: Tom Pickering <tom.pickering@xxxxxxxxxxxxxx>
branch nick: 6.1
timestamp: Thu 2016-09-08 16:40:43 +0100
message:
  [FIX] Mail works when using composition wizard
modified:
  mail/mail_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 'mail/mail_message.py'
--- mail/mail_message.py	2016-09-07 15:32:51 +0000
+++ mail/mail_message.py	2016-09-08 15:40:43 +0000
@@ -31,7 +31,6 @@
 import time
 from email.header import decode_header
 from email.message import Message
-from contextlib import closing
 
 import tools
 from osv import osv
@@ -510,72 +509,89 @@
         if context is None:
             context = {}
         ir_mail_server = self.pool.get('ir.mail_server')
-        with closing(pooler.get_db(cr.dbname).cursor()) as _cr:
-            for msg_id in ids:
-                try:
-                    _cr.execute('SELECT * FROM mail_message WHERE id = %s FOR UPDATE NOWAIT', [msg_id])
-                    message = self.browse(_cr, uid, msg_id, context=context)
-                    self.write(_cr, uid, msg_id, {'state': 'outgoing'}, context=context)
-
-                    attachments = []
-                    for attach in message.attachment_ids:
-                        attachments.append((attach.datas_fname, base64.b64decode(attach.datas)))
-
-                    body = message.body_html if message.subtype == 'html' else message.body_text
-                    body_alternative = None
-                    subtype_alternative = None
-                    if message.subtype == 'html' and message.body_text:
-                        # we have a plain text alternative prepared, pass it to 
-                        # build_message instead of letting it build one
-                        body_alternative = message.body_text
-                        subtype_alternative = 'plain'
-
-                    msg = ir_mail_server.build_email(
-                        email_from=message.email_from,
-                        email_to=to_email(message.email_to),
-                        subject=message.subject,
-                        body=body,
-                        body_alternative=body_alternative,
-                        email_cc=to_email(message.email_cc),
-                        email_bcc=to_email(message.email_bcc),
-                        reply_to=message.reply_to,
-                        attachments=attachments, message_id=message.message_id,
-                        references = message.references,
-                        object_id=message.res_id and ('%s-%s' % (message.res_id,message.model)),
-                        subtype=message.subtype,
-                        subtype_alternative=subtype_alternative,
-                        headers=message.headers and ast.literal_eval(message.headers))
-                    res = ir_mail_server.send_email(_cr, uid, msg,
-                                                    mail_server_id=message.mail_server_id.id,
-                                                    context=context)
-                    if res:
-                        message.write({'state':'sent', 'message_id': res})
-                        message_sent = True
-                    else:
-                        message.write({'state':'exception'})
-                        message_sent = False
-
-                    # if auto_delete=True then delete that sent messages as well as attachments
-                    if message_sent and message.auto_delete:
-                        self.pool.get('ir.attachment').unlink(_cr, uid,
-                                                              [x.id for x in message.attachment_ids \
-                                                                    if x.res_model == self._name and \
-                                                                       x.res_id == message.id],
-                                                              context=context)
-                        message.unlink()
-
-                    _cr.commit()
-                except MailTransientDeliveryException:
-                    _cr.rollback()
-                except OperationalError:
-                    _cr.rollback()
-                    _logger.exception('failed sending mail.message %s and unable to update state', msg_id)
-                    # We can't use this cursor any more - attempting to update
-                    # the message state will just throw a new exception.
-                except Exception:
-                    _logger.exception('failed sending mail.message %s', msg_id)
+        failed_messages = []
+        _cr = auto_commit and pooler.get_db(cr.dbname).cursor() or cr
+        for msg_id in ids:
+            try:
+                _cr.execute('SELECT * FROM mail_message WHERE id = %s FOR UPDATE NOWAIT', [msg_id])
+                message = self.browse(_cr, uid, msg_id, context=context)
+                message.write({'state':'outgoing'})
+
+                attachments = []
+                for attach in message.attachment_ids:
+                    attachments.append((attach.datas_fname, base64.b64decode(attach.datas)))
+
+                body = message.body_html if message.subtype == 'html' else message.body_text
+                body_alternative = None
+                subtype_alternative = None
+                if message.subtype == 'html' and message.body_text:
+                    # we have a plain text alternative prepared, pass it to 
+                    # build_message instead of letting it build one
+                    body_alternative = message.body_text
+                    subtype_alternative = 'plain'
+
+                msg = ir_mail_server.build_email(
+                    email_from=message.email_from,
+                    email_to=to_email(message.email_to),
+                    subject=message.subject,
+                    body=body,
+                    body_alternative=body_alternative,
+                    email_cc=to_email(message.email_cc),
+                    email_bcc=to_email(message.email_bcc),
+                    reply_to=message.reply_to,
+                    attachments=attachments, message_id=message.message_id,
+                    references = message.references,
+                    object_id=message.res_id and ('%s-%s' % (message.res_id,message.model)),
+                    subtype=message.subtype,
+                    subtype_alternative=subtype_alternative,
+                    headers=message.headers and ast.literal_eval(message.headers))
+                res = ir_mail_server.send_email(_cr, uid, msg,
+                                                mail_server_id=message.mail_server_id.id,
+                                                context=context)
+                if res:
+                    message.write({'state':'sent', 'message_id': res})
+                    message_sent = True
+                else:
                     message.write({'state':'exception'})
-                    _cr.commit()
+                    message_sent = False
+
+                # if auto_delete=True then delete that sent messages as well as attachments
+                if message_sent and message.auto_delete:
+                    self.pool.get('ir.attachment').unlink(_cr, uid,
+                                                          [x.id for x in message.attachment_ids \
+                                                                if x.res_model == self._name and \
+                                                                   x.res_id == message.id],
+                                                          context=context)
+                    message.unlink()
+
+                if auto_commit:
+                    _cr.commit()
+            except MailTransientDeliveryException:
+                if auto_commit:
+                    _cr.rollback()
+            except OperationalError:
+                if auto_commit:
+                    _cr.rollback()
+                failed_messages.append(msg_id)
+                _logger.exception('failed sending mail.message %s and unable to update state', msg_id)
+                # We can't use this cursor any more - attempting to update
+                # the message state will just throw a new exception.
+            except Exception:
+                failed_messages.append(msg_id)
+                _logger.exception('failed sending mail.message %s', msg_id)
+                message.write({'state':'exception'})
+                if auto_commit:
+                    _cr.commit()
+
+        if auto_commit:
+            _cr.close()
+
+        if failed_messages and not auto_commit:
+            raise osv.except_osv(
+                    _('Delivery failed'),
+                    _('Unable to send the following messages at this time:\n%s'
+                     ) % '\n'.join([str(m) for m in failed_messages])
+            )
 
         return True