Ticket #17431: password_reset_custom_email.diff

File password_reset_custom_email.diff, 4.3 KB (added by Ethan Jucovy, 13 years ago)
  • django/contrib/auth/tests/forms.py

     
    276276            self.assertEqual(len(mail.outbox), 1)
    277277            self.assertEqual(mail.outbox[0].subject, u'Custom password reset on example.com')
    278278
     279    def test_custom_email_constructor(self):
     280        template_path = os.path.join(os.path.dirname(__file__), 'templates')
     281        with self.settings(TEMPLATE_DIRS=(template_path,)):
     282            data = {'email': 'testclient@example.com'}
     283
     284            class CustomEmailPasswordResetForm(PasswordResetForm):
     285                def construct_email(self, ctx, user, **kwargs):
     286                    from django.core.mail import EmailMessage
     287
     288                    email_message = EmailMessage(u"Forgot your password?",
     289                                                 u"Sorry to hear you forgot your password.",
     290                                                 None, [user.email],
     291                                                 ['site_monitor@example.com'],
     292                                                 headers = {'Reply-To': 'webmaster@example.com'})
     293                    email_message.content_subtype = "text/html"
     294                    return email_message
     295
     296            form = CustomEmailPasswordResetForm(data)
     297            self.assertTrue(form.is_valid())
     298            # Since we're not providing a request object, we must provide a
     299            # domain_override to prevent the save operation from failing in the
     300            # potential case where contrib.sites is not installed. Refs #16412.
     301            form.save(domain_override='example.com')
     302            self.assertEqual(len(mail.outbox), 1)
     303            self.assertEqual(mail.outbox[0].subject, u'Forgot your password?')
     304            self.assertEqual(mail.outbox[0].bcc, ['site_monitor@example.com'])
     305            self.assertEqual(mail.outbox[0].content_subtype, "text/html")
     306
    279307    def test_bug_5605(self):
    280308        # bug #5605, preserve the case of the user name (before the @ in the
    281309        # email address) when creating a user.
  • django/contrib/auth/forms.py

     
    188188            raise forms.ValidationError(self.error_messages['unusable'])
    189189        return email
    190190
     191    def construct_email(self, ctx, user, **kwargs):
     192        """
     193        Constructs and returns a django.core.mail.EmailMessage object
     194        to be sent when the form's .save() method is called
     195        """
     196
     197        from django.core.mail import EmailMessage
     198
     199        subject_template_name = kwargs['subject_template_name']
     200        email_template_name = kwargs['email_template_name']
     201        from_email = kwargs['from_email']
     202
     203        subject = loader.render_to_string(subject_template_name, ctx)
     204        # Email subject *must not* contain newlines
     205        subject = ''.join(subject.splitlines())
     206        body = loader.render_to_string(email_template_name, ctx)
     207       
     208        email_message = EmailMessage(subject, body, from_email, [user.email])
     209        return email_message
     210
    191211    def save(self, domain_override=None,
    192212             subject_template_name='registration/password_reset_subject.txt',
    193213             email_template_name='registration/password_reset_email.html',
     
    213233                'token': token_generator.make_token(user),
    214234                'protocol': use_https and 'https' or 'http',
    215235            }
    216             subject = loader.render_to_string(subject_template_name, c)
    217             # Email subject *must not* contain newlines
    218             subject = ''.join(subject.splitlines())
    219             email = loader.render_to_string(email_template_name, c)
    220             send_mail(subject, email, from_email, [user.email])
    221236
     237            email_message = self.construct_email(c, user,
     238                                                 subject_template_name=subject_template_name,
     239                                                 email_template_name=email_template_name,
     240                                                 from_email=from_email)
     241            email_message.send()
    222242
    223243class SetPasswordForm(forms.Form):
    224244    """
Back to Top