#NEWLINE# def create_request( session , user , group , permission , argument , reason ) : #NEWLINE# #INDENT# '\n    Creates an permission request and sends notification to the responsible approvers.\n\n    Args:\n        session(sqlalchemy.orm.session.Session): database session\n        user(models.User): user requesting permission\n        group(models.Group): group requested permission would be applied to\n        permission(models.Permission): permission in question to request\n        argument(str): argument for the given permission\n        reason(str): reason the permission should be granted\n\n    Raises:\n        RequestAlreadyExists if trying to create a request that is already pending\n        NoOwnersAvailable if no owner is available for the requested perm + arg.\n    ' #NEWLINE# for ( _ , existing_perm_name , _ , existing_perm_argument , _ ) in group . my_permissions ( ) : #NEWLINE# #INDENT# if ( ( permission . name == existing_perm_name ) and ( argument == existing_perm_argument ) ) : #NEWLINE# #INDENT# raise RequestAlreadyGranted ( ) #NEWLINE# #UNINDENT# #UNINDENT# existing_count = session . query ( PermissionRequest ) . filter ( ( PermissionRequest . group_id == group . id ) , ( PermissionRequest . permission_id == permission . id ) , ( PermissionRequest . argument == argument ) , ( PermissionRequest . status == 'pending' ) ) . count ( ) #NEWLINE# if ( existing_count > 0 ) : #NEWLINE# #INDENT# raise RequestAlreadyExists ( ) #NEWLINE# #UNINDENT# owner_arg_list = get_owner_arg_list ( session , permission , argument ) #NEWLINE# if ( not owner_arg_list ) : #NEWLINE# #INDENT# raise NoOwnersAvailable ( ) #NEWLINE# #UNINDENT# pending_status = 'pending' #NEWLINE# now = datetime . utcnow ( ) #NEWLINE# request = PermissionRequest ( requester_id = user . id , group_id = group . id , permission_id = permission . id , argument = argument , status = pending_status , requested_at = now ) . add ( session ) #NEWLINE# session . flush ( ) #NEWLINE# request_status_change = PermissionRequestStatusChange ( request = request , user = user , to_status = pending_status , change_at = now ) . add ( session ) #NEWLINE# session . flush ( ) #NEWLINE# Comment ( obj_type = OBJ_TYPES_IDX . index ( 'PermissionRequestStatusChange' ) , obj_pk = request_status_change . id , user_id = user . id , comment = reason , created_on = now ) . add ( session ) #NEWLINE# email_context = { #NEWLINE# #INDENT# 'user_name' : user . name , #NEWLINE# 'group_name' : group . name , #NEWLINE# 'permission_name' : permission . name , #NEWLINE# 'argument' : argument , #NEWLINE# 'reason' : reason , #NEWLINE# 'request_id' : request . id , #NEWLINE# #UNINDENT# } #NEWLINE# mail_to = [ ] #NEWLINE# non_wildcard_owners = [ ( owner , arg ) for ( owner , arg ) in owner_arg_list if ( arg != '*' ) ] #NEWLINE# if any ( non_wildcard_owners ) : #NEWLINE# #INDENT# mailto_owner_arg_list = non_wildcard_owners #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# mailto_owner_arg_list = owner_arg_list #NEWLINE# #UNINDENT# for ( owner , arg ) in mailto_owner_arg_list : #NEWLINE# #INDENT# mail_to += [ now for ( t , u ) in owner . my_members ( ) if ( t == 'User' ) ] #NEWLINE# #UNINDENT# send_email ( session , set ( mail_to ) , 'Request for permission: {}' . format ( permission . name ) , 'pending_permission_request' , settings , email_context )