Ticket #5298: patch_5298.2.diff

File patch_5298.2.diff, 5.1 KB (added by durdinator, 17 years ago)
  • django/contrib/auth/views.py

     
     1__all__ = ('login', 'logout', 'logout_then_login', 'redirect_to_login',
     2        'password_reset', 'password_reset_done', 'password_change',
     3        'password_change_done')
     4
    15from django.contrib.auth.forms import AuthenticationForm
    26from django.contrib.auth.forms import PasswordResetForm, PasswordChangeForm
    37from django import oldforms
     
    913from django.contrib.auth import REDIRECT_FIELD_NAME
    1014from django.utils.translation import ugettext as _
    1115
    12 def login(request, template_name='registration/login.html', redirect_field_name=REDIRECT_FIELD_NAME):
     16def update_context_from_extra(context, extra_context):
     17    if extra_context is None: extra_context = {}
     18    for key, value in extra_context.items():
     19        if callable(value):
     20            context[key] = value()
     21        else:
     22            context[key] = value
     23
     24def login(request, template_name='registration/login.html', redirect_field_name=REDIRECT_FIELD_NAME, extra_context=None):
    1325    "Displays the login form and handles the login action."
    1426    manipulator = AuthenticationForm(request)
    1527    redirect_to = request.REQUEST.get(redirect_field_name, '')
     
    3345    else:
    3446        current_site = RequestSite(request)
    3547
    36     return render_to_response(template_name, {
     48    context = {
    3749        'form': oldforms.FormWrapper(manipulator, request.POST, errors),
    3850        redirect_field_name: redirect_to,
    3951        'site_name': current_site.name,
    40     }, context_instance=RequestContext(request))
     52    }
     53    update_context_from_extra(context, extra_context)
    4154
    42 def logout(request, next_page=None, template_name='registration/logged_out.html'):
     55    return render_to_response(template_name, context,
     56        context_instance=RequestContext(request))
     57
     58def logout(request, next_page=None, template_name='registration/logged_out.html', extra_context=None):
    4359    "Logs out the user and displays 'You are logged out' message."
    4460    from django.contrib.auth import logout
    4561    logout(request)
    4662    if next_page is None:
    47         return render_to_response(template_name, {'title': _('Logged out')}, context_instance=RequestContext(request))
     63        context = {'title': _('Logged out')}
     64        update_context_from_extra(context, extra_context)
     65        return render_to_response(template_name, context, context_instance=RequestContext(request))
    4866    else:
    4967        # Redirect to this page until the session has been cleared.
    5068        return HttpResponseRedirect(next_page or request.path)
     
    6482    return HttpResponseRedirect('%s?%s=%s' % (login_url, redirect_field_name, next))
    6583
    6684def password_reset(request, is_admin_site=False, template_name='registration/password_reset_form.html',
    67         email_template_name='registration/password_reset_email.html'):
     85        email_template_name='registration/password_reset_email.html', extra_context=None):
    6886    new_data, errors = {}, {}
    6987    form = PasswordResetForm()
    7088    if request.POST:
     
    7694            else:
    7795                form.save(email_template_name=email_template_name)
    7896            return HttpResponseRedirect('%sdone/' % request.path)
    79     return render_to_response(template_name, {'form': oldforms.FormWrapper(form, new_data, errors)},
     97    context = {'form': oldforms.FormWrapper(form, new_data, errors)}
     98    update_context_from_extra(context, extra_context)
     99    return render_to_response(template_name, context,
    80100        context_instance=RequestContext(request))
    81101
    82 def password_reset_done(request, template_name='registration/password_reset_done.html'):
    83     return render_to_response(template_name, context_instance=RequestContext(request))
     102def password_reset_done(request, template_name='registration/password_reset_done.html', extra_context=None):
     103    context = {}
     104    update_context_from_extra(context, extra_context)
     105    return render_to_response(template_name, context, context_instance=RequestContext(request))
    84106
    85 def password_change(request, template_name='registration/password_change_form.html'):
     107def password_change(request, template_name='registration/password_change_form.html', extra_context=None):
    86108    new_data, errors = {}, {}
    87109    form = PasswordChangeForm(request.user)
    88110    if request.POST:
     
    91113        if not errors:
    92114            form.save(new_data)
    93115            return HttpResponseRedirect('%sdone/' % request.path)
    94     return render_to_response(template_name, {'form': oldforms.FormWrapper(form, new_data, errors)},
     116    context = {'form': oldforms.FormWrapper(form, new_data, errors)}
     117    update_context_from_extra(context, extra_context)
     118    return render_to_response(template_name, context,
    95119        context_instance=RequestContext(request))
    96120password_change = login_required(password_change)
    97121
    98 def password_change_done(request, template_name='registration/password_change_done.html'):
     122def password_change_done(request, template_name='registration/password_change_done.html', extra_context=None):
    99123    return render_to_response(template_name, context_instance=RequestContext(request))
Back to Top