Ticket #9900: 9900-0.diff

File 9900-0.diff, 7.9 KB (added by anonymous, 16 years ago)
  • django/contrib/auth/views.py

    diff --git a/django/contrib/auth/views.py b/django/contrib/auth/views.py
    index e1f0d43..ceb9cfc 100644
    a b from django.utils.translation import ugettext as _  
    1414from django.contrib.auth.models import User
    1515from django.views.decorators.cache import never_cache
    1616
    17 def login(request, template_name='registration/login.html', redirect_field_name=REDIRECT_FIELD_NAME):
     17def expand_callables(dictionary):
     18    result = {}
     19    for key, value in dictionary.items():
     20        if callable(value):
     21            result[key] = value()
     22        else:
     23            result[key] = value
     24    return result
     25
     26def login(request, template_name='registration/login.html', extra_context=None,
     27        redirect_field_name=REDIRECT_FIELD_NAME):
    1828    "Displays the login form and handles the login action."
     29    if extra_context is None: extra_context = {}
    1930    redirect_to = request.REQUEST.get(redirect_field_name, '')
    2031    if request.method == "POST":
    2132        form = AuthenticationForm(data=request.POST)
    def login(request, template_name='registration/login.html', redirect_field_name=  
    3546        current_site = Site.objects.get_current()
    3647    else:
    3748        current_site = RequestSite(request)
    38     return render_to_response(template_name, {
    39         'form': form,
    40         redirect_field_name: redirect_to,
    41         'site_name': current_site.name,
    42     }, context_instance=RequestContext(request))
     49    context = {'form': form,
     50        redirect_field_name: redirect_to, 'site_name': current_site.name}
     51    context.update(expand_callables(extra_context))
     52    return render_to_response(template_name, context, context_instance=RequestContext(request))
    4353login = never_cache(login)
    4454
    45 def logout(request, next_page=None, template_name='registration/logged_out.html'):
     55def logout(request, next_page=None,
     56        template_name='registration/logged_out.html', extra_context=None):
    4657    "Logs out the user and displays 'You are logged out' message."
     58    if extra_context is None: extra_context = {}
    4759    from django.contrib.auth import logout
    4860    logout(request)
    4961    if next_page is None:
    50         return render_to_response(template_name, {'title': _('Logged out')}, context_instance=RequestContext(request))
     62        context = {'title': _('Logged out')}
     63        context.update(expand_callables(extra_context))
     64        return render_to_response(template_name, context, context_instance=RequestContext(request))
    5165    else:
    5266        # Redirect to this page until the session has been cleared.
    5367        return HttpResponseRedirect(next_page or request.path)
    def redirect_to_login(next, login_url=None, redirect_field_name=REDIRECT_FIELD_N  
    7488def password_reset(request, is_admin_site=False, template_name='registration/password_reset_form.html',
    7589        email_template_name='registration/password_reset_email.html',
    7690        password_reset_form=PasswordResetForm, token_generator=default_token_generator,
    77         post_reset_redirect=None):
     91        post_reset_redirect=None, extra_context=None):
    7892    if post_reset_redirect is None:
    7993        post_reset_redirect = reverse('django.contrib.auth.views.password_reset_done')
     94    if extra_context is None:
     95        extra_context = {}
    8096    if request.method == "POST":
    8197        form = password_reset_form(request.POST)
    8298        if form.is_valid():
    def password_reset(request, is_admin_site=False, template_name='registration/pas  
    93109            return HttpResponseRedirect(post_reset_redirect)
    94110    else:
    95111        form = password_reset_form()
    96     return render_to_response(template_name, {
    97         'form': form,
    98     }, context_instance=RequestContext(request))
     112    context = {'form': form}
     113    context.update(expand_callables(extra_context))
     114    return render_to_response(template_name,
     115        context, context_instance=RequestContext(request))
    99116
    100 def password_reset_done(request, template_name='registration/password_reset_done.html'):
    101     return render_to_response(template_name, context_instance=RequestContext(request))
     117def password_reset_done(request,
     118        template_name='registration/password_reset_done.html', extra_context=None):
     119    if extra_context is None:
     120        context = {}
     121    else:
     122        context = expand_callables(extra_context)
     123    return render_to_response(template_name, context, context_instance=RequestContext(request))
    102124
    103125def password_reset_confirm(request, uidb36=None, token=None, template_name='registration/password_reset_confirm.html',
    104126                           token_generator=default_token_generator, set_password_form=SetPasswordForm,
    105                            post_reset_redirect=None):
     127                           post_reset_redirect=None, extra_context=None):
    106128    """
    107129    View that checks the hash in a password reset link and presents a
    108130    form for entering a new password.
    def password_reset_confirm(request, uidb36=None, token=None, template_name='regi  
    110132    assert uidb36 is not None and token is not None # checked by URLconf
    111133    if post_reset_redirect is None:
    112134        post_reset_redirect = reverse('django.contrib.auth.views.password_reset_complete')
     135    if extra_context is None:
     136        extra_context = {}
    113137    try:
    114138        uid_int = base36_to_int(uidb36)
    115139    except ValueError:
    def password_reset_confirm(request, uidb36=None, token=None, template_name='regi  
    130154    else:
    131155        context_instance['validlink'] = False
    132156        form = None
    133     context_instance['form'] = form   
    134     return render_to_response(template_name, context_instance=context_instance)
     157    context = {'form': form}
     158    context.update(expand_callables(extra_context))   
     159    return render_to_response(template_name, context, context_instance=context_instance)
    135160
    136 def password_reset_complete(request, template_name='registration/password_reset_complete.html'):
    137     return render_to_response(template_name, context_instance=RequestContext(request,
    138                                                                              {'login_url': settings.LOGIN_URL}))
     161def password_reset_complete(request,
     162        template_name='registration/password_reset_complete.html', extra_context=None):
     163    if extra_context is None: extra_context = {}
     164    context = {'login_url': settings.LOGIN_URL}
     165    context.update(expand_callables(extra_context))
     166    return render_to_response(template_name, context, context_instance=RequestContext(request))
    139167
    140168def password_change(request, template_name='registration/password_change_form.html',
    141                     post_change_redirect=None):
     169                    post_change_redirect=None, extra_context=None):
    142170    if post_change_redirect is None:
    143171        post_change_redirect = reverse('django.contrib.auth.views.password_change_done')
     172    if extra_context is None: extra_context = {}
    144173    if request.method == "POST":
    145174        form = PasswordChangeForm(request.user, request.POST)
    146175        if form.is_valid():
    def password_change(request, template_name='registration/password_change_form.ht  
    148177            return HttpResponseRedirect(post_change_redirect)
    149178    else:
    150179        form = PasswordChangeForm(request.user)
    151     return render_to_response(template_name, {
    152         'form': form,
    153     }, context_instance=RequestContext(request))
     180    context = {'form': form}
     181    context.update(expand_callables(extra_context))
     182    return render_to_response(template_name, context, context_instance=RequestContext(request))
    154183password_change = login_required(password_change)
    155184
    156 def password_change_done(request, template_name='registration/password_change_done.html'):
    157     return render_to_response(template_name, context_instance=RequestContext(request))
     185def password_change_done(request, template_name='registration/password_change_done.html',
     186                         extra_context=None):
     187    if extra_context is None:
     188        context = {}
     189    else:
     190        context = expand_callables(extra_context)
     191    return render_to_response(template_name, context, context_instance=RequestContext(request))
Back to Top