<s> '''<STR_LIT>''' <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . shortcuts import get_object_or_404 <EOL> from django . core . urlresolvers import get_callable <EOL> from django . views . generic import simple <EOL> from expedient . common . permissions . exceptions import PermissionDenied <EOL> from expedient . common . permissions . models import ExpedientPermission , PermissionRequest , Permittee , ObjectPermission <EOL> from expedient . common . permissions . utils import get_object_from_ids <EOL> from expedient . common . permissions . forms import PermissionRequestForm , ProjectRequestForm <EOL> from expedient . common . messaging . models import DatedMessage <EOL> from django . contrib . auth . models import User <EOL> import logging <EOL> from expedient . common . utils . mail import send_mail <EOL> from django . conf import settings <EOL> from django . contrib . auth . models import User <EOL> from django . http import QueryDict <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> def reraise_permission_denied ( request , perm_name = None , <EOL> target_ct_id = None , target_id = None , <EOL> permittee_ct_id = None , permittee_id = None ) : <EOL> """<STR_LIT>""" <EOL> target_obj_or_class = get_object_from_ids ( target_ct_id , target_id ) <EOL> permittee = get_object_from_ids ( permittee_ct_id , permittee_id ) <EOL> raise PermissionDenied ( perm_name , target_obj_or_class , permittee , False ) <EOL> def redirect_permissions_request ( request , perm_name = None , <EOL> target_ct_id = None , target_id = None , <EOL> permittee_ct_id = None , permittee_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> permittee = get_object_from_ids ( permittee_ct_id , permittee_id ) <EOL> if not permission . view : <EOL> raise PermissionDenied ( perm_name , target_obj_or_class , permittee , False ) <EOL> view = get_callable ( permission . view ) <EOL> logger . debug ( "<STR_LIT>" % permission . view ) <EOL> redirect_to = request . session . 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 , permittee , target_obj_or_class , <EOL> redirect_to = redirect_to ) <EOL> def request_permission ( always_redirect_to = None , <EOL> permission_owners_func = None , <EOL> extra_context = { } , <EOL> template = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> def request_permission_view ( request , permission , permittee , <EOL> target_obj_or_class , redirect_to = None ) : <EOL> obj_perm = ObjectPermission . objects . get_or_create_for_object_or_class ( <EOL> permission , target_obj_or_class ) [ <NUM_LIT:0> ] <EOL> perm_name = obj_perm . permission . name <EOL> if permission_owners_func : <EOL> user_qs = permission_owners_func ( request , obj_perm , permittee ) <EOL> else : <EOL> user_qs = User . objects . filter ( is_superuser = <NUM_LIT:1> ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> permittee = Permittee . objects . get_or_create_from_instance ( <EOL> permittee ) [ <NUM_LIT:0> ] <EOL> perm_request = PermissionRequest ( requesting_user = request . user , <EOL> permittee = permittee , <EOL> requested_permission = obj_perm ) <EOL> posted_message = "<STR_LIT>" % permission . name <EOL> if perm_name == "<STR_LIT>" : <EOL> form = ProjectRequestForm ( user_qs , request . POST , <EOL> instance = perm_request ) <EOL> posted_message = "<STR_LIT>" % str ( request . POST [ "<STR_LIT:name>" ] ) <EOL> else : <EOL> form = PermissionRequestForm ( user_qs , request . POST , <EOL> instance = perm_request ) <EOL> if form . is_valid ( ) : <EOL> perm_request = form . save ( ) <EOL> DatedMessage . objects . post_message_to_user ( <EOL> "<STR_LIT>" % <EOL> ( posted_message , perm_request . permission_owner ) , <EOL> user = request . user , msg_type = DatedMessage . TYPE_SUCCESS ) <EOL> try : <EOL> send_mail ( <EOL> settings . EMAIL_SUBJECT_PREFIX + "<STR_LIT>" % ( posted_message , request . user ) , <EOL> "<STR_LIT>" % ( permission . name , request . user , request . user . email , settings . SITE_IP_ADDR , perm_request . message ) , <EOL> from_email = settings . DEFAULT_FROM_EMAIL , <EOL> recipient_list = [ perm_request . permission_owner . email ] , <EOL> ) <EOL> except Exception as e : <EOL> print "<STR_LIT>" % ( permission . name , request . user ) <EOL> if callable ( always_redirect_to ) : <EOL> redirect_to = always_redirect_to ( request ) <EOL> else : <EOL> redirect_to = always_redirect_to or redirect_to <EOL> return simple . redirect_to ( request , redirect_to , <EOL> permanent = False ) <EOL> else : <EOL> if perm_name == "<STR_LIT>" : <EOL> form = ProjectRequestForm ( user_qs ) <EOL> else : <EOL> form = PermissionRequestForm ( user_qs ) <EOL> ec = { "<STR_LIT>" : form , "<STR_LIT>" : obj_perm , "<STR_LIT>" : perm_name } <EOL> ec . update ( extra_context ) <EOL> return simple . direct_to_template ( <EOL> request , template = template , <EOL> extra_context = ec ) <EOL> return request_permission_view </s>
