Django

Code

Changeset 988

Show
Ignore:
Timestamp:
10/21/05 19:04:55 (3 years ago)
Author:
adrian
Message:

Added django.views.decorators.auth.user_passes_test, which is a more generic hook into authentication based on a test. Refactored login_required to use user_passes_test

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/views/decorators/auth.py

    r3 r988  
     1def user_passes_test(view_func, test_func): 
     2    """ 
     3    Decorator for views that checks that the user passes the given test, 
     4    redirecting to the log-in page if necessary. The test should be a callable 
     5    that takes the user object and returns True if the user passes. 
     6    """ 
     7    from django.views.auth.login import redirect_to_login 
     8    def _checklogin(request, *args, **kwargs): 
     9        if test_func(request.user): 
     10            return view_func(request, *args, **kwargs) 
     11        return redirect_to_login(request.path) 
     12    return _checklogin 
     13 
    114def login_required(view_func): 
    215    """ 
     
    417    to the log-in page if necessary. 
    518    """ 
    6     from django.views.auth.login import redirect_to_login 
    7     def _checklogin(request, *args, **kwargs): 
    8         if request.user.is_anonymous(): 
    9             return redirect_to_login(request.path) 
    10         else: 
    11             return view_func(request, *args, **kwargs) 
    12     return _checklogin 
     19    return user_passes_test(lambda u: not u.is_anonymous())