Permit custom from-email address in auth forms email.

Patch from cassidy and Rob Hudson. Fixed #11300.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13817 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2010-09-12 22:38:01 +00:00
parent 631bb7d0f6
commit 4084bc7354
5 changed files with 19 additions and 4 deletions

View File

@ -117,7 +117,7 @@ class PasswordResetForm(forms.Form):
return email return email
def save(self, domain_override=None, email_template_name='registration/password_reset_email.html', def save(self, domain_override=None, email_template_name='registration/password_reset_email.html',
use_https=False, token_generator=default_token_generator): use_https=False, token_generator=default_token_generator, from_email=None):
""" """
Generates a one-use only link for resetting password and sends to the user Generates a one-use only link for resetting password and sends to the user
""" """
@ -140,7 +140,7 @@ class PasswordResetForm(forms.Form):
'protocol': use_https and 'https' or 'http', 'protocol': use_https and 'https' or 'http',
} }
send_mail(_("Password reset on %s") % site_name, send_mail(_("Password reset on %s") % site_name,
t.render(Context(c)), None, [user.email]) t.render(Context(c)), from_email, [user.email])
class SetPasswordForm(forms.Form): class SetPasswordForm(forms.Form):
""" """

View File

@ -16,6 +16,7 @@ urlpatterns += patterns('',
(r'^logout/custom_query/$', 'django.contrib.auth.views.logout', dict(redirect_field_name='follow')), (r'^logout/custom_query/$', 'django.contrib.auth.views.logout', dict(redirect_field_name='follow')),
(r'^logout/next_page/$', 'django.contrib.auth.views.logout', dict(next_page='/somewhere/')), (r'^logout/next_page/$', 'django.contrib.auth.views.logout', dict(next_page='/somewhere/')),
(r'^remote_user/$', remote_user_auth_view), (r'^remote_user/$', remote_user_auth_view),
(r'^password_reset_from_email/$', 'django.contrib.auth.views.password_reset', dict(from_email='staffmember@example.com')),
(r'^login_required/$', login_required(password_reset)), (r'^login_required/$', login_required(password_reset)),
(r'^login_required_login_url/$', login_required(password_reset, login_url='/somewhere/')), (r'^login_required_login_url/$', login_required(password_reset, login_url='/somewhere/')),
) )

View File

@ -62,6 +62,14 @@ class PasswordResetTest(AuthViewsTestCase):
self.assertEquals(response.status_code, 302) self.assertEquals(response.status_code, 302)
self.assertEquals(len(mail.outbox), 1) self.assertEquals(len(mail.outbox), 1)
self.assert_("http://" in mail.outbox[0].body) self.assert_("http://" in mail.outbox[0].body)
self.assertEquals(settings.DEFAULT_FROM_EMAIL, mail.outbox[0].from_email)
def test_email_found_custom_from(self):
"Email is sent if a valid email address is provided for password reset when a custom from_email is provided."
response = self.client.post('/password_reset_from_email/', {'email': 'staffmember@example.com'})
self.assertEquals(response.status_code, 302)
self.assertEquals(len(mail.outbox), 1)
self.assertEquals("staffmember@example.com", mail.outbox[0].from_email)
def _test_confirm_start(self): def _test_confirm_start(self):
# Start by creating the email # Start by creating the email

View File

@ -105,7 +105,7 @@ def redirect_to_login(next, login_url=None, redirect_field_name=REDIRECT_FIELD_N
def password_reset(request, is_admin_site=False, template_name='registration/password_reset_form.html', def password_reset(request, is_admin_site=False, template_name='registration/password_reset_form.html',
email_template_name='registration/password_reset_email.html', email_template_name='registration/password_reset_email.html',
password_reset_form=PasswordResetForm, token_generator=default_token_generator, password_reset_form=PasswordResetForm, token_generator=default_token_generator,
post_reset_redirect=None): post_reset_redirect=None, from_email=None):
if post_reset_redirect is None: if post_reset_redirect is None:
post_reset_redirect = reverse('django.contrib.auth.views.password_reset_done') post_reset_redirect = reverse('django.contrib.auth.views.password_reset_done')
if request.method == "POST": if request.method == "POST":
@ -114,6 +114,7 @@ def password_reset(request, is_admin_site=False, template_name='registration/pas
opts = {} opts = {}
opts['use_https'] = request.is_secure() opts['use_https'] = request.is_secure()
opts['token_generator'] = token_generator opts['token_generator'] = token_generator
opts['from_email'] = from_email
if is_admin_site: if is_admin_site:
opts['domain_override'] = request.META['HTTP_HOST'] opts['domain_override'] = request.META['HTTP_HOST']
else: else:

View File

@ -906,7 +906,7 @@ includes a few other useful built-in views located in
default to :file:`registration/password_change_done.html` if not default to :file:`registration/password_change_done.html` if not
supplied. supplied.
.. function:: views.password_reset(request[, is_admin_site, template_name, email_template_name, password_reset_form, token_generator, post_reset_redirect]) .. function:: views.password_reset(request[, is_admin_site, template_name, email_template_name, password_reset_form, token_generator, post_reset_redirect, from_email])
Allows a user to reset their password by generating a one-time use link Allows a user to reset their password by generating a one-time use link
that can be used to reset the password, and sending that link to the that can be used to reset the password, and sending that link to the
@ -932,6 +932,11 @@ includes a few other useful built-in views located in
* ``post_reset_redirect``: The URL to redirect to after a successful * ``post_reset_redirect``: The URL to redirect to after a successful
password change. password change.
.. versionchanged:: 1.3
* ``from_email``: A valid e-mail address. By default Django uses
the :setting:`DEFAULT_FROM_EMAIL`.
**Template context:** **Template context:**
* ``form``: The form for resetting the user's password. * ``form``: The form for resetting the user's password.