<s> """<STR_LIT>""" <EOL> from functools import wraps <EOL> from django . utils . decorators import available_attrs <EOL> from django . core . exceptions import PermissionDenied <EOL> from permission . decorators . utils import redirect_to_login <EOL> def permission_required ( perm , queryset = None , <EOL> login_url = None , raise_exception = False ) : <EOL> """<STR_LIT>""" <EOL> def wrapper ( cls ) : <EOL> def view_wrapper ( view_func ) : <EOL> @ wraps ( view_func , assigned = available_attrs ( view_func ) ) <EOL> def inner ( self , request , * args , ** kwargs ) : <EOL> obj = get_object_from_classbased_instance ( <EOL> self , queryset , request , * args , ** kwargs <EOL> ) <EOL> if not request . user . has_perm ( perm , obj = obj ) : <EOL> if raise_exception : <EOL> raise PermissionDenied <EOL> else : <EOL> return redirect_to_login ( request , login_url ) <EOL> return view_func ( self , request , * args , ** kwargs ) <EOL> return inner <EOL> cls . dispatch = view_wrapper ( cls . dispatch ) <EOL> return cls <EOL> return wrapper <EOL> def get_object_from_classbased_instance ( <EOL> instance , queryset , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> from django . views . generic . edit import BaseCreateView <EOL> instance . request = request <EOL> instance . args = args <EOL> instance . kwargs = kwargs <EOL> if hasattr ( instance , '<STR_LIT>' ) and not queryset : <EOL> queryset = instance . get_queryset ( ) <EOL> elif hasattr ( instance , '<STR_LIT>' ) and not queryset : <EOL> queryset = instance . queryset <EOL> elif hasattr ( instance , '<STR_LIT>' ) and not queryset : <EOL> queryset = instance . model . _default_manager . all ( ) <EOL> if hasattr ( instance , '<STR_LIT>' ) : <EOL> try : <EOL> obj = instance . get_object ( queryset ) <EOL> except AttributeError as e : <EOL> if isinstance ( instance , BaseCreateView ) : <EOL> obj = None <EOL> else : <EOL> raise e <EOL> elif hasattr ( instance , '<STR_LIT:object>' ) : <EOL> obj = instance . object <EOL> else : <EOL> obj = None <EOL> return obj </s>
