<s> from functools import wraps <EOL> from django . conf import settings <EOL> from django . contrib . auth import REDIRECT_FIELD_NAME <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . utils . decorators import available_attrs <EOL> from django . utils . encoding import force_str <EOL> from django . utils . six . moves . urllib . parse import urlparse <EOL> from django . shortcuts import resolve_url <EOL> def user_passes_test ( test_func , login_url = None , redirect_field_name = REDIRECT_FIELD_NAME ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( view_func ) : <EOL> @ wraps ( view_func , assigned = available_attrs ( view_func ) ) <EOL> def _wrapped_view ( request , * args , ** kwargs ) : <EOL> if test_func ( request . user ) : <EOL> return view_func ( request , * args , ** kwargs ) <EOL> path = request . build_absolute_uri ( ) <EOL> resolved_login_url = force_str ( <EOL> resolve_url ( login_url or settings . LOGIN_URL ) ) <EOL> login_scheme , login_netloc = urlparse ( resolved_login_url ) [ : <NUM_LIT:2> ] <EOL> current_scheme , current_netloc = urlparse ( path ) [ : <NUM_LIT:2> ] <EOL> if ( ( not login_scheme or login_scheme == current_scheme ) and <EOL> ( not login_netloc or login_netloc == current_netloc ) ) : <EOL> path = request . get_full_path ( ) <EOL> from django . contrib . auth . views import redirect_to_login <EOL> return redirect_to_login ( <EOL> path , resolved_login_url , redirect_field_name ) <EOL> return _wrapped_view <EOL> return 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 , raise_exception = False ) : <EOL> """<STR_LIT>""" <EOL> def check_perms ( user ) : <EOL> if user . has_perm ( perm ) : <EOL> return True <EOL> if raise_exception : <EOL> raise PermissionDenied <EOL> return False <EOL> return user_passes_test ( check_perms , login_url = login_url ) </s>
