From c93c8935ad66606cec2ece82b34830aebee1f9c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Fri, 24 May 2013 13:48:57 +0200 Subject: [PATCH] [IMP] mail: now may raise exception instead of just logging it. They are used in auth_signup to avoid having the process considering the invite/reset password email was sent when it was not. bzr revid: tde@openerp.com-20130524114857-8wy5uob23i2d6b6p --- addons/auth_signup/res_users.py | 17 +++++++++++++---- .../test/lead2opportunity_assign_salesmen.yml | 8 ++++---- addons/email_template/email_template.py | 4 ++-- addons/mail/mail_mail.py | 13 ++++++++++--- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/addons/auth_signup/res_users.py b/addons/auth_signup/res_users.py index 85842b7a275..fe5ff7b59c7 100644 --- a/addons/auth_signup/res_users.py +++ b/addons/auth_signup/res_users.py @@ -23,6 +23,7 @@ import random from urllib import urlencode from urlparse import urljoin +from openerp.addons.base.ir.ir_mail_server import MailDeliveryException from openerp.osv import osv, fields from openerp.tools.misc import DEFAULT_SERVER_DATETIME_FORMAT from openerp.tools.safe_eval import safe_eval @@ -103,6 +104,9 @@ class res_partner(osv.Model): def action_signup_prepare(self, cr, uid, ids, context=None): return self.signup_prepare(cr, uid, ids, context=context) + def signup_cancel(self, cr, uid, ids, context=None): + return self.write(cr, uid, ids, {'signup_token': False, 'signup_type': False, 'signup_expiration': False}, context=context) + def signup_prepare(self, cr, uid, ids, signup_type="signup", expiration=False, context=None): """ generate a new token for the partners with the given validity, if necessary :param expiration: the expiration datetime of the token (string, optional) @@ -202,7 +206,7 @@ class res_users(osv.Model): }) if partner.company_id: values['company_id'] = partner.company_id.id - values['company_ids'] = [(6,0,[partner.company_id.id])] + values['company_ids'] = [(6, 0, [partner.company_id.id])] self._signup_create_user(cr, uid, values, context=context) else: # no token, sign up an external user @@ -259,13 +263,15 @@ class res_users(osv.Model): pass if not bool(template): template = self.pool.get('ir.model.data').get_object(cr, uid, 'auth_signup', 'reset_password_email') - mail_obj = self.pool.get('mail.mail') assert template._name == 'email.template' for user in self.browse(cr, uid, ids, context): if not user.email: raise osv.except_osv(_("Cannot send email: user has no email address."), user.name) - self.pool.get('email.template').send_mail(cr, uid, template.id, user.id, True, context=context) + try: + self.pool.get('email.template').send_mail(cr, uid, template.id, user.id, force_send=True, raise_exception=True, context=context) + except MailDeliveryException: + raise def create(self, cr, uid, values, context=None): if context is None: @@ -275,5 +281,8 @@ class res_users(osv.Model): user = self.browse(cr, uid, user_id, context=context) if user.email and not context.get('no_reset_password'): context.update({'create_user': True}) - self.action_reset_password(cr, uid, [user.id], context=context) + try: + self.action_reset_password(cr, uid, [user.id], context=context) + except MailDeliveryException: + self.pool.get('res.partner').signup_cancel(cr, uid, [user.partner_id.id], context=context) return user_id diff --git a/addons/crm/test/lead2opportunity_assign_salesmen.yml b/addons/crm/test/lead2opportunity_assign_salesmen.yml index 0edd3b8edce..a0755fa7315 100644 --- a/addons/crm/test/lead2opportunity_assign_salesmen.yml +++ b/addons/crm/test/lead2opportunity_assign_salesmen.yml @@ -39,22 +39,22 @@ - !record {model: res.users, id: test_res_user_01}: name: 'Test user A' - login: 'tua' + login: 'tua@example.com' new_password: 'tua' - !record {model: res.users, id: test_res_user_02}: name: 'Test user B' - login: 'tub' + login: 'tub@example.com' new_password: 'tub' - !record {model: res.users, id: test_res_user_03}: name: 'Test user C' - login: 'tuc' + login: 'tuc@example.com' new_password: 'tuc' - !record {model: res.users, id: test_res_user_04}: name: 'Test user D' - login: 'tud' + login: 'tud@example.com' new_password: 'tud' - I create a mass convert wizard and convert all the leads. diff --git a/addons/email_template/email_template.py b/addons/email_template/email_template.py index 4a767457321..2a7990e301b 100644 --- a/addons/email_template/email_template.py +++ b/addons/email_template/email_template.py @@ -358,7 +358,7 @@ class email_template(osv.osv): values['attachment_ids'] = attachment_ids return values - def send_mail(self, cr, uid, template_id, res_id, force_send=False, context=None): + def send_mail(self, cr, uid, template_id, res_id, force_send=False, raise_exception=False, context=None): """Generates a new mail message for the given template and record, and schedules it for delivery through the ``mail`` module's scheduler. @@ -400,7 +400,7 @@ class email_template(osv.osv): mail_mail.write(cr, uid, msg_id, {'attachment_ids': [(6, 0, attachment_ids)]}, context=context) if force_send: - mail_mail.send(cr, uid, [msg_id], context=context) + mail_mail.send(cr, uid, [msg_id], raise_exception=raise_exception, context=context) return msg_id # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/mail/mail_mail.py b/addons/mail/mail_mail.py index 7b2eff5e743..4eaaa72971c 100644 --- a/addons/mail/mail_mail.py +++ b/addons/mail/mail_mail.py @@ -292,7 +292,7 @@ class mail_mail(osv.Model): 'email_to': email_to, } - def send(self, cr, uid, ids, auto_commit=False, context=None): + def send(self, cr, uid, ids, auto_commit=False, raise_exception=False, context=None): """ Sends the selected emails immediately, ignoring their current state (mails that have already been sent should not be passed unless they should actually be re-sent). @@ -303,6 +303,9 @@ class mail_mail(osv.Model): :param bool auto_commit: whether to force a commit of the mail status after sending each mail (meant only for scheduler processing); should never be True during normal transactions (default: False) + :param bool raise_exception: whether to raise an exception if the + email sending process has failed; auto_commit is taken into + account. :return: True """ ir_mail_server = self.pool.get('ir.mail_server') @@ -349,8 +352,12 @@ class mail_mail(osv.Model): if mail_sent: self._postprocess_sent_message(cr, uid, mail, context=context) except Exception: - _logger.exception('failed sending mail.mail %s', mail.id) - mail.write({'state': 'exception'}) + if not raise_exception or auto_commit: + _logger.exception('failed sending mail.mail %s', mail.id) + mail.write({'state': 'exception'}) + cr.commit() + if raise_exception: + raise if auto_commit == True: cr.commit()