<s> try : <EOL> from functools import update_wrapper , wraps <EOL> except ImportError : <EOL> from django . utils . functional import update_wrapper , wraps <EOL> from django . contrib . auth import REDIRECT_FIELD_NAME <EOL> from django . http import HttpResponseRedirect , HttpResponseForbidden <EOL> from django . utils . decorators import available_attrs <EOL> from django . utils . http import urlquote <EOL> def user_passes_test ( test_func , login_url = None , redirect_field_name = REDIRECT_FIELD_NAME ) : <EOL> """<STR_LIT>""" <EOL> if not login_url : <EOL> from django . conf import settings <EOL> login_url = settings . LOGIN_URL <EOL> def decorator ( view_func ) : <EOL> def _wrapped_view ( self , request , * args , ** kwargs ) : <EOL> if test_func ( request . user ) : <EOL> return view_func ( self , request , * args , ** kwargs ) <EOL> path = urlquote ( request . get_full_path ( ) ) <EOL> tup = login_url , redirect_field_name , path <EOL> if request . is_ajax ( ) : <EOL> return HttpResponseForbidden ( ) <EOL> else : <EOL> return HttpResponseRedirect ( '<STR_LIT>' % tup ) <EOL> return wraps ( view_func , assigned = available_attrs ( view_func ) ) ( _wrapped_view ) <EOL> return decorator <EOL> def must_be_member_of_group ( group_name = None , redirect_field_name = REDIRECT_FIELD_NAME , login_url = None ) : <EOL> if type ( group_name ) not in ( list , tuple ) : <EOL> group_name = [ group_name ] <EOL> actual_decorator = user_passes_test ( <EOL> lambda u : ( u . is_superuser or u . groups . filter ( name__in = group_name ) . count ( ) > <NUM_LIT:0> ) , <EOL> login_url = login_url , <EOL> redirect_field_name = redirect_field_name <EOL> ) <EOL> return actual_decorator <EOL> def superuser_required ( function = None , redirect_field_name = REDIRECT_FIELD_NAME ) : <EOL> actual_decorator = user_passes_test ( <EOL> lambda u : u . is_superuser , <EOL> redirect_field_name = redirect_field_name <EOL> ) <EOL> if function : <EOL> return actual_decorator ( function ) <EOL> return actual_decorator <EOL> def login_required ( function = None , redirect_field_name = REDIRECT_FIELD_NAME , login_url = None ) : <EOL> """<STR_LIT>""" <EOL> actual_decorator = user_passes_test ( <EOL> lambda u : u . is_authenticated ( ) , <EOL> login_url = login_url , <EOL> redirect_field_name = redirect_field_name <EOL> ) <EOL> if function : <EOL> return actual_decorator ( function ) <EOL> return actual_decorator <EOL> def permission_required ( perm , login_url = None ) : <EOL> """<STR_LIT>""" <EOL> return user_passes_test ( lambda u : u . has_perm ( perm ) , login_url = login_url ) </s>
