<s> '''<STR_LIT>''' <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . shortcuts import get_object_or_404 <EOL> from expedient . common . permissions . exceptions import PermissionDenied <EOL> from expedient . common . permissions . models import ExpedientPermission , PermissionRequest , PermissionUser <EOL> from expedient . common . permissions . utils import get_object_from_ids , register_permission_for_obj_or_class <EOL> from django . core . urlresolvers import get_callable <EOL> from expedient . common . permissions . forms import PermissionRequestForm <EOL> from django . views . generic import simple , create_update <EOL> from django . contrib . auth . models import User <EOL> def reraise_permission_denied ( request , perm_name = None , <EOL> target_ct_id = None , target_id = None , <EOL> user_ct_id = None , user_id = None ) : <EOL> """<STR_LIT>""" <EOL> target_obj_or_class = get_object_from_ids ( target_ct_id , target_id ) <EOL> user = get_object_from_ids ( user_ct_id , user_id ) <EOL> raise PermissionDenied ( perm_name , target_obj_or_class , user , False ) <EOL> def redirect_permissions_request ( request , perm_name = None , <EOL> target_ct_id = None , target_id = None , <EOL> user_ct_id = None , user_id = None ) : <EOL> """<STR_LIT>""" <EOL> permission = get_object_or_404 ( ExpedientPermission , name = perm_name ) <EOL> target_obj_or_class = get_object_from_ids ( target_ct_id , target_id ) <EOL> if type ( target_obj_or_class ) == ContentType : <EOL> target_obj_or_class = target_obj_or_class . model_class ( ) <EOL> user = get_object_from_ids ( user_ct_id , user_id ) <EOL> if not permission . view : <EOL> raise PermissionDenied ( perm_name , target_obj_or_class , user , False ) <EOL> view = get_callable ( permission . view ) <EOL> redirect_to = request . GET . get ( "<STR_LIT>" , '<STR_LIT>' ) <EOL> if not redirect_to or '<STR_LIT:U+0020>' in redirect_to or "<STR_LIT>" in redirect_to : <EOL> redirect_to = None <EOL> return view ( request , permission , user , target_obj_or_class , <EOL> redirect_to = redirect_to ) <EOL> def request_permission ( always_redirect_to , <EOL> permission_owners_func = None , <EOL> template = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> def request_permission_view ( request , permission , user , <EOL> target_obj_or_class , redirect_to = None ) : <EOL> obj_perm = register_permission_for_obj_or_class ( <EOL> target_obj_or_class , permission ) [ <NUM_LIT:0> ] <EOL> if permission_owners_func : <EOL> user_qs = permission_owners_func ( request , obj_perm , user ) <EOL> else : <EOL> user_qs = PermissionUser . objects . get_objects_queryset ( <EOL> User , <EOL> dict ( <EOL> permissioninfo__obj_permission = obj_perm , <EOL> permissioninfo__can_delegate = True ) , <EOL> { } ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> perm_request = PermissionRequest ( requesting_user = request . user , <EOL> requested_permission = obj_perm ) <EOL> form = PermissionRequestForm ( user_qs , request . POST , <EOL> instance = perm_request ) <EOL> if form . is_valid ( ) : <EOL> perm_request = form . save ( ) <EOL> return simple . redirect_to ( request , always_redirect_to , <EOL> permanent = False ) <EOL> else : <EOL> form = PermissionRequestForm ( user_qs ) <EOL> return simple . direct_to_template ( <EOL> request , template = template , <EOL> extra_context = { "<STR_LIT>" : form , "<STR_LIT>" : obj_perm } ) <EOL> return request_permission_view </s>
