Skip to content

Commit

Permalink
Fixed #903 -- Added login_url argument to user_passes_test view decor…
Browse files Browse the repository at this point in the history
…ator. Didn't add it to login_required decorator because that would turn login_required into a callable decorator, which would break backwards compatibility.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1440 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
adrianholovaty committed Nov 26, 2005
1 parent d058a8a commit 8128f44
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
9 changes: 5 additions & 4 deletions django/views/auth/login.py
Expand Up @@ -6,6 +6,7 @@
from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect

REDIRECT_FIELD_NAME = 'next'
LOGIN_URL = '/accounts/login/'

def login(request):
"Displays the login form and handles the login action."
Expand Down Expand Up @@ -39,10 +40,10 @@ def logout(request, next_page=None):
# Redirect to this page until the session has been cleared.
return HttpResponseRedirect(next_page or request.path)

def logout_then_login(request):
def logout_then_login(request, login_url=LOGIN_URL):
"Logs out the user if he is logged in. Then redirects to the log-in page."
return logout(request, '/accounts/login/')
return logout(request, login_url)

def redirect_to_login(next):
def redirect_to_login(next, login_url=LOGIN_URL):
"Redirects the user to the login page, passing the given 'next' page"
return HttpResponseRedirect('/accounts/login/?%s=%s' % (REDIRECT_FIELD_NAME, next))
return HttpResponseRedirect('%s?%s=%s' % (login_url, REDIRECT_FIELD_NAME, next))
7 changes: 4 additions & 3 deletions django/views/decorators/auth.py
@@ -1,15 +1,16 @@
def user_passes_test(test_func):
from django.views.auth import login

def user_passes_test(test_func, login_url=login.LOGIN_URL):
"""
Decorator for views that checks that the user passes the given test,
redirecting to the log-in page if necessary. The test should be a callable
that takes the user object and returns True if the user passes.
"""
def _dec(view_func):
def _checklogin(request, *args, **kwargs):
from django.views.auth.login import redirect_to_login
if test_func(request.user):
return view_func(request, *args, **kwargs)
return redirect_to_login(request.path)
return login.redirect_to_login(request.path, login_url)
return _checklogin
return _dec

Expand Down
20 changes: 20 additions & 0 deletions docs/authentication.txt
Expand Up @@ -314,6 +314,26 @@ Here's the same thing, using Python 2.4's decorator syntax::
Note that ``user_passes_test`` does not automatically check that the ``User``
is not anonymous.

**New in the Django development version**: ``user_passes_test()`` takes an
optional ``login_url`` argument, which lets you specify the URL for your login
page (``/accounts/login/`` by default).

Example in Python 2.3 syntax::

from django.views.decorators.auth import user_passes_test

def my_view(request):
# ...
my_view = user_passes_test(lambda u: u.has_perm('polls.can_vote'), login_url='/login/')(my_view)

Example in Python 2.4 syntax::

from django.views.decorators.auth import user_passes_test

@user_passes_test(lambda u: u.has_perm('polls.can_vote'), login_url='/login/')
def my_view(request):
# ...

Limiting access to generic views
--------------------------------

Expand Down

0 comments on commit 8128f44

Please sign in to comment.