Code

Ticket #5298: patch_5298.2.diff

File patch_5298.2.diff, 5.1 KB (added by durdinator, 7 years ago)
Line 
1Index: django/contrib/auth/views.py
2===================================================================
3--- django/contrib/auth/views.py        (revision 6206)
4+++ django/contrib/auth/views.py        (working copy)
5@@ -1,3 +1,7 @@
6+__all__ = ('login', 'logout', 'logout_then_login', 'redirect_to_login',
7+        'password_reset', 'password_reset_done', 'password_change',
8+        'password_change_done')
9+
10 from django.contrib.auth.forms import AuthenticationForm
11 from django.contrib.auth.forms import PasswordResetForm, PasswordChangeForm
12 from django import oldforms
13@@ -9,7 +13,15 @@
14 from django.contrib.auth import REDIRECT_FIELD_NAME
15 from django.utils.translation import ugettext as _
16 
17-def login(request, template_name='registration/login.html', redirect_field_name=REDIRECT_FIELD_NAME):
18+def update_context_from_extra(context, extra_context):
19+    if extra_context is None: extra_context = {}
20+    for key, value in extra_context.items():
21+        if callable(value):
22+            context[key] = value()
23+        else:
24+            context[key] = value
25+
26+def login(request, template_name='registration/login.html', redirect_field_name=REDIRECT_FIELD_NAME, extra_context=None):
27     "Displays the login form and handles the login action."
28     manipulator = AuthenticationForm(request)
29     redirect_to = request.REQUEST.get(redirect_field_name, '')
30@@ -33,18 +45,24 @@
31     else:
32         current_site = RequestSite(request)
33 
34-    return render_to_response(template_name, {
35+    context = {
36         'form': oldforms.FormWrapper(manipulator, request.POST, errors),
37         redirect_field_name: redirect_to,
38         'site_name': current_site.name,
39-    }, context_instance=RequestContext(request))
40+    }
41+    update_context_from_extra(context, extra_context)
42 
43-def logout(request, next_page=None, template_name='registration/logged_out.html'):
44+    return render_to_response(template_name, context,
45+        context_instance=RequestContext(request))
46+
47+def logout(request, next_page=None, template_name='registration/logged_out.html', extra_context=None):
48     "Logs out the user and displays 'You are logged out' message."
49     from django.contrib.auth import logout
50     logout(request)
51     if next_page is None:
52-        return render_to_response(template_name, {'title': _('Logged out')}, context_instance=RequestContext(request))
53+        context = {'title': _('Logged out')}
54+        update_context_from_extra(context, extra_context)
55+        return render_to_response(template_name, context, context_instance=RequestContext(request))
56     else:
57         # Redirect to this page until the session has been cleared.
58         return HttpResponseRedirect(next_page or request.path)
59@@ -64,7 +82,7 @@
60     return HttpResponseRedirect('%s?%s=%s' % (login_url, redirect_field_name, next))
61 
62 def password_reset(request, is_admin_site=False, template_name='registration/password_reset_form.html',
63-        email_template_name='registration/password_reset_email.html'):
64+        email_template_name='registration/password_reset_email.html', extra_context=None):
65     new_data, errors = {}, {}
66     form = PasswordResetForm()
67     if request.POST:
68@@ -76,13 +94,17 @@
69             else:
70                 form.save(email_template_name=email_template_name)
71             return HttpResponseRedirect('%sdone/' % request.path)
72-    return render_to_response(template_name, {'form': oldforms.FormWrapper(form, new_data, errors)},
73+    context = {'form': oldforms.FormWrapper(form, new_data, errors)}
74+    update_context_from_extra(context, extra_context)
75+    return render_to_response(template_name, context,
76         context_instance=RequestContext(request))
77 
78-def password_reset_done(request, template_name='registration/password_reset_done.html'):
79-    return render_to_response(template_name, context_instance=RequestContext(request))
80+def password_reset_done(request, template_name='registration/password_reset_done.html', extra_context=None):
81+    context = {}
82+    update_context_from_extra(context, extra_context)
83+    return render_to_response(template_name, context, context_instance=RequestContext(request))
84 
85-def password_change(request, template_name='registration/password_change_form.html'):
86+def password_change(request, template_name='registration/password_change_form.html', extra_context=None):
87     new_data, errors = {}, {}
88     form = PasswordChangeForm(request.user)
89     if request.POST:
90@@ -91,9 +113,11 @@
91         if not errors:
92             form.save(new_data)
93             return HttpResponseRedirect('%sdone/' % request.path)
94-    return render_to_response(template_name, {'form': oldforms.FormWrapper(form, new_data, errors)},
95+    context = {'form': oldforms.FormWrapper(form, new_data, errors)}
96+    update_context_from_extra(context, extra_context)
97+    return render_to_response(template_name, context,
98         context_instance=RequestContext(request))
99 password_change = login_required(password_change)
100 
101-def password_change_done(request, template_name='registration/password_change_done.html'):
102+def password_change_done(request, template_name='registration/password_change_done.html', extra_context=None):
103     return render_to_response(template_name, context_instance=RequestContext(request))