Ticket #5394: configurable-redirect-field-name-2.diff

File configurable-redirect-field-name-2.diff, 3.5 KB (added by DavidReynolds, 8 years ago)

Fixing as per Adrian's suggestion

  • django/contrib/auth/views.py

     
    99from django.contrib.auth import REDIRECT_FIELD_NAME
    1010from django.utils.translation import ugettext as _
    1111
    12 def login(request, template_name='registration/login.html'):
     12def login(request, template_name='registration/login.html', redirect_field_name=REDIRECT_FIELD_NAME):
    1313    "Displays the login form and handles the login action."
    1414    manipulator = AuthenticationForm(request)
    15     redirect_to = request.REQUEST.get(REDIRECT_FIELD_NAME, '')
     15    redirect_to = request.REQUEST.get(redirect_field_name, '')
    1616    if request.POST:
    1717        errors = manipulator.get_validation_errors(request.POST)
    1818        if not errors:
     
    3535
    3636    return render_to_response(template_name, {
    3737        'form': oldforms.FormWrapper(manipulator, request.POST, errors),
    38         REDIRECT_FIELD_NAME: redirect_to,
     38        redirect_field_name: redirect_to,
    3939        'site_name': current_site.name,
    4040    }, context_instance=RequestContext(request))
    4141
     
    5656        login_url = settings.LOGIN_URL
    5757    return logout(request, login_url)
    5858
    59 def redirect_to_login(next, login_url=None):
     59def redirect_to_login(next, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
    6060    "Redirects the user to the login page, passing the given 'next' page"
    6161    if not login_url:
    6262        from django.conf import settings
    6363        login_url = settings.LOGIN_URL
    64     return HttpResponseRedirect('%s?%s=%s' % (login_url, REDIRECT_FIELD_NAME, next))
     64    return HttpResponseRedirect('%s?%s=%s' % (login_url, redirect_field_name, next))
    6565
    6666def password_reset(request, is_admin_site=False, template_name='registration/password_reset_form.html',
    6767        email_template_name='registration/password_reset_email.html'):
  • django/contrib/auth/decorators.py

     
    22from django.http import HttpResponseRedirect
    33from urllib import quote
    44
    5 def user_passes_test(test_func, login_url=None):
     5def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
    66    """
    77    Decorator for views that checks that the user passes the given test,
    88    redirecting to the log-in page if necessary. The test should be a callable
     
    1515        def _checklogin(request, *args, **kwargs):
    1616            if test_func(request.user):
    1717                return view_func(request, *args, **kwargs)
    18             return HttpResponseRedirect('%s?%s=%s' % (login_url, REDIRECT_FIELD_NAME, quote(request.get_full_path())))
     18            return HttpResponseRedirect('%s?%s=%s' % (login_url, redirect_field_name, quote(request.get_full_path())))
    1919        _checklogin.__doc__ = view_func.__doc__
    2020        _checklogin.__dict__ = view_func.__dict__
    2121
    2222        return _checklogin
    2323    return _dec
    2424
    25 login_required = user_passes_test(lambda u: u.is_authenticated())
    26 login_required.__doc__ = (
     25def login_required(redirect_field_name=REDIRECT_FIELD_NAME):
    2726    """
    2827    Decorator for views that checks that the user is logged in, redirecting
    2928    to the log-in page if necessary.
    3029    """
    31     )
     30    return user_passes_test(lambda u: u.is_authenticated(), redirect_field_name=REDIRECT_FIELD_NAME)
     31   
    3232
    3333def permission_required(perm, login_url=None):
    3434    """
Back to Top