<s> '''<STR_LIT>''' <EOL> from django . views . generic import list_detail , create_update , simple <EOL> from models import Project <EOL> from forms import ProjectCreateForm <EOL> from django . http import HttpResponseRedirect , HttpResponseNotAllowed , Http404 <EOL> from django . core . urlresolvers import reverse <EOL> from django . core . exceptions import MultipleObjectsReturned <EOL> from django . shortcuts import get_object_or_404 <EOL> from expedient . clearinghouse . aggregate . models import Aggregate <EOL> import logging <EOL> from expedient . common . utils . views import generic_crud <EOL> from expedient . common . messaging . models import DatedMessage <EOL> from django . db . models import Q <EOL> from expedient . common . permissions . decorators import require_objs_permissions_for_view <EOL> from expedient . common . permissions . shortcuts import give_permission_to , has_permission , must_have_permission <EOL> from expedient . common . permissions . utils import get_queryset , get_user_from_req , get_queryset_from_class <EOL> from expedient . clearinghouse . roles . models import ProjectRole , ProjectRoleRequest <EOL> from expedient . common . permissions . models import ObjectPermission , PermissionOwnership , Permittee <EOL> from expedient . clearinghouse . project . forms import AddMemberForm , MemberForm <EOL> from expedient . common . utils . mail import send_mail <EOL> from django . contrib . auth . models import User <EOL> from django . contrib . contenttypes . models import ContentType <EOL> import uuid <EOL> from django . conf import settings <EOL> from django . contrib . auth . models import User <EOL> import ldap <EOL> from django . contrib . sites . models import Site <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> TEMPLATE_PATH = "<STR_LIT>" <EOL> DEFAULT_OWNER_PERMISSIONS = [ <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> ] <EOL> DEFAULT_RESEARCHER_PERMISSIONS = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> ] <EOL> def list ( request ) : <EOL> '''<STR_LIT>''' <EOL> qs = Project . objects . get_for_user ( request . user ) . order_by ( '<STR_LIT:id>' ) <EOL> return list_detail . object_list ( <EOL> request , <EOL> queryset = qs , <EOL> template_name = TEMPLATE_PATH + "<STR_LIT>" , <EOL> template_object_name = "<STR_LIT>" , <EOL> ) <EOL> def list_details ( request , proj_id ) : <EOL> '''<STR_LIT>''' <EOL> project = get_object_or_404 ( Project , id = proj_id ) <EOL> return simple . direct_to_template ( <EOL> request , <EOL> template = TEMPLATE_PATH + "<STR_LIT>" , <EOL> extra_context = { "<STR_LIT>" : project } , <EOL> ) <EOL> @ require_objs_permissions_for_view ( <EOL> perm_names = [ "<STR_LIT>" ] , <EOL> permittee_func = get_user_from_req , <EOL> target_func = get_queryset ( Project , "<STR_LIT>" ) , <EOL> methods = [ "<STR_LIT:GET>" , "<STR_LIT:POST>" ] , <EOL> ) <EOL> @ require_objs_permissions_for_view ( <EOL> perm_names = [ "<STR_LIT>" ] , <EOL> permittee_func = get_user_from_req , <EOL> target_func = get_queryset ( Project , "<STR_LIT>" ) , <EOL> methods = [ "<STR_LIT:GET>" , "<STR_LIT:POST>" ] , <EOL> ) <EOL> def delete ( request , proj_id ) : <EOL> '''<STR_LIT>''' <EOL> project = get_object_or_404 ( Project , id = proj_id ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> try : <EOL> for s in project . slice_set . all ( ) : <EOL> s . stop ( request . user ) <EOL> project . delete ( ) <EOL> DatedMessage . objects . post_message_to_user ( <EOL> "<STR_LIT>" % project . name , <EOL> request . user , msg_type = DatedMessage . TYPE_SUCCESS ) <EOL> except Exception as e : <EOL> if isinstance ( e , ldap . LDAPError ) : <EOL> DatedMessage . objects . post_message_to_user ( <EOL> "<STR_LIT>" % project . name , <EOL> request . user , msg_type = DatedMessage . TYPE_ERROR ) <EOL> else : <EOL> DatedMessage . objects . post_message_to_user ( <EOL> "<STR_LIT>" % ( project . name , str ( e ) ) , <EOL> request . user , msg_type = DatedMessage . TYPE_ERROR ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" ) ) <EOL> else : <EOL> from vt_plugin . models . VM import VM <EOL> if VM . objects . filter ( projectId = project . uuid ) : <EOL> DatedMessage . objects . post_message_to_user ( <EOL> "<STR_LIT>" % project . name , <EOL> request . user , msg_type = DatedMessage . TYPE_ERROR ) <EOL> return detail ( request , proj_id ) <EOL> return simple . direct_to_template ( <EOL> request , <EOL> template = TEMPLATE_PATH + "<STR_LIT>" , <EOL> extra_context = { "<STR_LIT:object>" : project } , <EOL> ) <EOL> @ require_objs_permissions_for_view ( <EOL> perm_names = [ "<STR_LIT>" ] , <EOL> permittee_func = get_user_from_req , <EOL> target_func = get_queryset ( Project , "<STR_LIT>" ) , <EOL> ) <EOL> def detail ( request , proj_id ) : <EOL> '''<STR_LIT>''' <EOL> project = get_object_or_404 ( Project , id = proj_id ) <EOL> role_reqs = ProjectRoleRequest . objects . filter ( <EOL> giver = request . user , requested_role__project = project ) <EOL> permittees = project . members_as_permittees . all ( ) <EOL> member_roles = dict ( ) <EOL> for permittee in permittees : <EOL> member_roles [ str ( permittee . object . username ) ] = { "<STR_LIT:id>" : str ( permittee . object . id ) , <EOL> "<STR_LIT>" : permittee . projectrole_set . filter ( project__name = project . name ) } <EOL> return list_detail . object_detail ( <EOL> request , <EOL> Project . objects . all ( ) , <EOL> object_id = proj_id , <EOL> template_name = TEMPLATE_PATH + "<STR_LIT>" , <EOL> template_object_name = "<STR_LIT>" , <EOL> extra_context = { <EOL> "<STR_LIT>" : role_reqs , <EOL> "<STR_LIT>" : member_roles , <EOL> "<STR_LIT>" : ( <EOL> ( "<STR_LIT>" , reverse ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" % project . name , reverse ( "<STR_LIT>" , args = [ project . id ] ) ) , <EOL> ) <EOL> } <EOL> ) <EOL> def create_project_roles ( project , user ) : <EOL> """<STR_LIT>""" <EOL> owner_role = ProjectRole . objects . create ( <EOL> name = "<STR_LIT>" , <EOL> description = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> project = project , <EOL> ) <EOL> for permission in DEFAULT_OWNER_PERMISSIONS : <EOL> obj_perm = ObjectPermission . objects . get_or_create_for_object_or_class ( <EOL> permission , project ) [ <NUM_LIT:0> ] <EOL> owner_role . obj_permissions . add ( obj_perm ) <EOL> researcher_role = ProjectRole . objects . create ( <EOL> name = "<STR_LIT>" , <EOL> description = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> project = project , <EOL> ) <EOL> for permission in DEFAULT_RESEARCHER_PERMISSIONS : <EOL> obj_perm = ObjectPermission . objects . get_or_create_for_object_or_class ( <EOL> permission , project ) [ <NUM_LIT:0> ] <EOL> researcher_role . obj_permissions . add ( obj_perm ) <EOL> owner_role . give_to_permittee ( <EOL> user , <EOL> can_delegate = True , <EOL> ) <EOL> @ require_objs_permissions_for_view ( <EOL> perm_names = [ "<STR_LIT>" ] , <EOL> permittee_func = get_user_from_req , <EOL> target_func = get_queryset_from_class ( Project ) , <EOL> ) <EOL> def create ( request ) : <EOL> '''<STR_LIT>''' <EOL> def post_save ( instance , created ) : <EOL> instance . uuid = uuid . uuid4 ( ) <EOL> create_project_roles ( instance , request . user ) <EOL> instance . save ( ) <EOL> def redirect ( instance ) : <EOL> return reverse ( "<STR_LIT>" , args = [ instance . id ] ) <EOL> try : <EOL> return generic_crud ( <EOL> request , None , <EOL> model = Project , <EOL> form_class = ProjectCreateForm , <EOL> template = TEMPLATE_PATH + "<STR_LIT>" , <EOL> post_save = post_save , <EOL> redirect = redirect , <EOL> template_object_name = "<STR_LIT>" , <EOL> extra_context = { <EOL> "<STR_LIT>" : ( <EOL> ( "<STR_LIT>" , reverse ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" , request . path ) , <EOL> ) , <EOL> } , <EOL> success_msg = lambda instance : "<STR_LIT>" % instance . name , <EOL> ) <EOL> except Exception as e : <EOL> if isinstance ( e , ldap . LDAPError ) : <EOL> DatedMessage . objects . post_message_to_user ( <EOL> "<STR_LIT>" , <EOL> request . user , msg_type = DatedMessage . TYPE_ERROR ) <EOL> else : <EOL> DatedMessage . objects . post_message_to_user ( <EOL> "<STR_LIT>" % str ( e ) , <EOL> request . user , msg_type = DatedMessage . TYPE_ERROR ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" ) ) <EOL> @ require_objs_permissions_for_view ( <EOL> perm_names = [ "<STR_LIT>" ] , <EOL> permittee_func = get_user_from_req , <EOL> target_func = get_queryset ( Project , "<STR_LIT>" ) , <EOL> ) <EOL> @ require_objs_permissions_for_view ( <EOL> perm_names = [ "<STR_LIT>" ] , <EOL> permittee_func = get_user_from_req , <EOL> target_func = get_queryset ( Project , "<STR_LIT>" ) , <EOL> methods = [ "<STR_LIT:POST>" ] , <EOL> ) <EOL> def update ( request , proj_id , iframe = False ) : <EOL> '''<STR_LIT>''' <EOL> project = get_object_or_404 ( Project , id = proj_id ) <EOL> must_have_permission ( request . user , project , "<STR_LIT>" ) <EOL> def redirect ( instance ) : <EOL> if iframe : <EOL> return reverse ( "<STR_LIT>" ) <EOL> else : <EOL> return reverse ( "<STR_LIT>" , args = [ instance . id ] ) <EOL> return generic_crud ( <EOL> request , proj_id , <EOL> model = Project , <EOL> form_class = ProjectCreateForm , <EOL> template = TEMPLATE_PATH + "<STR_LIT>" , <EOL> redirect = redirect , <EOL> template_object_name = "<STR_LIT>" , <EOL> extra_context = { <EOL> "<STR_LIT>" : ( <EOL> ( "<STR_LIT>" , reverse ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" % project . name , reverse ( "<STR_LIT>" , args = [ project . id ] ) ) , <EOL> ( "<STR_LIT>" , request . path ) , <EOL> ) , <EOL> } , <EOL> success_msg = lambda instance : "<STR_LIT>" % instance . name , <EOL> ) <EOL> @ require_objs_permissions_for_view ( <EOL> perm_names = [ "<STR_LIT>" ] , <EOL> permittee_func = get_user_from_req , <EOL> target_func = get_queryset ( Project , "<STR_LIT>" ) , <EOL> ) <EOL> def add_aggregate ( request , proj_id ) : <EOL> '''<STR_LIT>''' <EOL> project = get_object_or_404 ( Project , id = proj_id ) <EOL> aggregate_list = Aggregate . objects . exclude ( <EOL> id__in = project . aggregates . all ( ) . values_list ( "<STR_LIT:id>" , flat = True ) ) <EOL> if request . method == "<STR_LIT:GET>" : <EOL> return simple . direct_to_template ( <EOL> request , template = TEMPLATE_PATH + "<STR_LIT>" , <EOL> extra_context = { <EOL> "<STR_LIT>" : aggregate_list , <EOL> "<STR_LIT>" : project , <EOL> "<STR_LIT>" : ( <EOL> ( "<STR_LIT>" , reverse ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" % project . name , reverse ( "<STR_LIT>" , args = [ project . id ] ) ) , <EOL> ( "<STR_LIT>" , request . path ) , <EOL> ) , <EOL> } <EOL> ) <EOL> elif request . method == "<STR_LIT:POST>" : <EOL> try : <EOL> agg_id = int ( request . POST . get ( "<STR_LIT:id>" , <NUM_LIT:0> ) ) <EOL> except ValueError : <EOL> raise Http404 <EOL> if agg_id not in aggregate_list . values_list ( "<STR_LIT:id>" , flat = True ) : <EOL> raise Http404 <EOL> aggregate = get_object_or_404 ( Aggregate , id = agg_id ) . as_leaf_class ( ) <EOL> for member in project . members . exclude ( is_superuser = True ) : <EOL> give_permission_to ( "<STR_LIT>" , aggregate , member , giver = None , can_delegate = False ) <EOL> return HttpResponseRedirect ( aggregate . add_to_project ( <EOL> project , reverse ( "<STR_LIT>" , args = [ proj_id ] ) ) ) <EOL> else : <EOL> return HttpResponseNotAllowed ( "<STR_LIT:GET>" , "<STR_LIT:POST>" ) <EOL> @ require_objs_permissions_for_view ( <EOL> perm_names = [ "<STR_LIT>" ] , <EOL> permittee_func = get_user_from_req , <EOL> target_func = get_queryset ( Project , "<STR_LIT>" ) , <EOL> ) <EOL> def update_aggregate ( request , proj_id , agg_id ) : <EOL> '''<STR_LIT>''' <EOL> project = get_object_or_404 ( Project , id = proj_id ) <EOL> aggregate = get_object_or_404 ( <EOL> Aggregate , id = agg_id , id__in = project . aggregates . values_list ( <EOL> "<STR_LIT:id>" , flat = True ) ) . as_leaf_class ( ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> return HttpResponseRedirect ( aggregate . add_to_project ( <EOL> project , reverse ( "<STR_LIT>" , args = [ proj_id ] ) ) ) <EOL> else : <EOL> return HttpResponseNotAllowed ( [ "<STR_LIT:POST>" ] ) <EOL> @ require_objs_permissions_for_view ( <EOL> perm_names = [ "<STR_LIT>" ] , <EOL> permittee_func = get_user_from_req , <EOL> target_func = get_queryset ( Project , "<STR_LIT>" ) , <EOL> ) <EOL> def remove_aggregate ( request , proj_id , agg_id ) : <EOL> """<STR_LIT>""" <EOL> project = get_object_or_404 ( Project , id = proj_id ) <EOL> aggregate = get_object_or_404 ( <EOL> Aggregate , id = agg_id , id__in = project . aggregates . values_list ( <EOL> "<STR_LIT:id>" , flat = True ) ) . as_leaf_class ( ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> try : <EOL> return HttpResponseRedirect ( aggregate . remove_from_project ( <EOL> project , reverse ( "<STR_LIT>" , args = [ proj_id ] ) ) ) <EOL> except MultipleObjectsReturned as e : <EOL> DatedMessage . objects . post_message_to_user ( <EOL> str ( e ) , request . user , msg_type = DatedMessage . TYPE_ERROR ) <EOL> except : <EOL> pass <EOL> return HttpResponseRedirect ( <EOL> reverse ( '<STR_LIT>' , args = [ proj_id ] ) ) <EOL> else : <EOL> return HttpResponseNotAllowed ( [ "<STR_LIT:POST>" ] ) <EOL> @ require_objs_permissions_for_view ( <EOL> perm_names = [ "<STR_LIT>" ] , <EOL> permittee_func = get_user_from_req , <EOL> target_func = get_queryset ( Project , "<STR_LIT>" ) , <EOL> ) <EOL> def add_member ( request , proj_id ) : <EOL> """<STR_LIT>""" <EOL> project = get_object_or_404 ( Project , id = proj_id ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = AddMemberForm ( project = project , giver = request . user , data = request . POST ) <EOL> if form . is_valid ( ) : <EOL> user = User . objects . get ( id = request . POST [ '<STR_LIT:user>' ] ) <EOL> form . save ( ) <EOL> try : <EOL> project . save ( ) <EOL> except : <EOL> logger . warning ( "<STR_LIT>" % ( user . username , project . name ) ) <EOL> DatedMessage . objects . post_message_to_user ( <EOL> "<STR_LIT>" % user . username , <EOL> request . user , msg_type = DatedMessage . TYPE_ERROR ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ proj_id ] ) ) <EOL> roles = '<STR_LIT:U+002CU+0020>' . join ( repr ( role . encode ( '<STR_LIT:ascii>' ) ) for role in ProjectRole . objects . filter ( id__in = request . POST . getlist ( '<STR_LIT>' ) ) . values_list ( '<STR_LIT:name>' , flat = True ) ) <EOL> for aggregate in project . _get_aggregates ( ) : <EOL> if not has_permission ( user , aggregate , "<STR_LIT>" ) : <EOL> aggregate . add_to_user ( user , "<STR_LIT:/>" ) <EOL> try : <EOL> from expedient . clearinghouse . project import urls <EOL> project_detail_url = reverse ( "<STR_LIT>" , args = [ project . id ] ) or "<STR_LIT:/>" <EOL> site_domain_url = "<STR_LIT>" + Site . objects . get_current ( ) . domain + project_detail_url <EOL> send_mail ( <EOL> settings . EMAIL_SUBJECT_PREFIX + "<STR_LIT>" % ( project . name ) , <EOL> "<STR_LIT>" % ( project . name , roles , site_domain_url ) , <EOL> from_email = settings . DEFAULT_FROM_EMAIL , <EOL> recipient_list = [ user . email ] , <EOL> ) <EOL> except Exception as e : <EOL> print "<STR_LIT>" % str ( e ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ proj_id ] ) ) <EOL> else : <EOL> form = AddMemberForm ( project = project , giver = request . user ) <EOL> return simple . direct_to_template ( <EOL> request , <EOL> template = TEMPLATE_PATH + "<STR_LIT>" , <EOL> extra_context = { <EOL> "<STR_LIT>" : form , <EOL> "<STR_LIT>" : project , <EOL> "<STR_LIT>" : ( <EOL> ( "<STR_LIT>" , reverse ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" % project . name , reverse ( "<STR_LIT>" , args = [ project . id ] ) ) , <EOL> ( "<STR_LIT>" , request . path ) , <EOL> ) , <EOL> } , <EOL> ) <EOL> @ require_objs_permissions_for_view ( <EOL> perm_names = [ "<STR_LIT>" ] , <EOL> permittee_func = get_user_from_req , <EOL> target_func = get_queryset ( Project , "<STR_LIT>" ) , <EOL> ) <EOL> def update_member ( request , proj_id , user_id ) : <EOL> """<STR_LIT>""" <EOL> project = get_object_or_404 ( Project , id = proj_id ) <EOL> member = get_object_or_404 ( User , id = user_id ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = MemberForm ( <EOL> project = project , user = member , <EOL> giver = request . user , data = request . POST ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return HttpResponseRedirect ( <EOL> reverse ( "<STR_LIT>" , args = [ proj_id ] ) ) <EOL> else : <EOL> form = MemberForm ( <EOL> project = project , user = member , <EOL> giver = request . user ) <EOL> return simple . direct_to_template ( <EOL> request , <EOL> template = TEMPLATE_PATH + "<STR_LIT>" , <EOL> extra_context = { <EOL> "<STR_LIT>" : form , <EOL> "<STR_LIT>" : project , <EOL> "<STR_LIT>" : member , <EOL> "<STR_LIT>" : ( <EOL> ( "<STR_LIT>" , reverse ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" % project . name , reverse ( "<STR_LIT>" , args = [ project . id ] ) ) , <EOL> ( "<STR_LIT>" % member . username , request . path ) , <EOL> ) , <EOL> } , <EOL> ) <EOL> @ require_objs_permissions_for_view ( <EOL> perm_names = [ "<STR_LIT>" ] , <EOL> permittee_func = get_user_from_req , <EOL> target_func = get_queryset ( Project , "<STR_LIT>" ) , <EOL> ) <EOL> def remove_member ( request , proj_id , user_id ) : <EOL> """<STR_LIT>""" <EOL> project = get_object_or_404 ( Project , id = proj_id ) <EOL> member = get_object_or_404 ( User , id = user_id ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> member = Permittee . objects . get_as_permittee ( member ) <EOL> for role in ProjectRole . objects . filter ( project = project ) : <EOL> role . remove_from_permittee ( member ) <EOL> PermissionOwnership . objects . delete_all_for_target ( project , member ) <EOL> for projectAgg in project . _get_aggregates ( ) : <EOL> aggNotUsedAnymoreByMember = <NUM_LIT:1> <EOL> for p in Project . objects . exclude ( id = project . id ) : <EOL> if projectAgg in p . _get_aggregates ( ) and unicode ( member ) in p . members . values_list ( "<STR_LIT:username>" , flat = True ) : <EOL> aggNotUsedAnymoreByMember = <NUM_LIT:0> <EOL> break ; <EOL> if aggNotUsedAnymoreByMember and not has_permission ( member , projectAgg , "<STR_LIT>" ) : <EOL> projectAgg . remove_from_user ( member , "<STR_LIT:/>" ) <EOL> try : <EOL> project . save ( ) <EOL> except : <EOL> logger . warning ( "<STR_LIT>" % ( member . object . username , project . name ) ) <EOL> return HttpResponseRedirect ( <EOL> reverse ( "<STR_LIT>" , args = [ proj_id ] ) ) <EOL> return simple . direct_to_template ( <EOL> request , <EOL> template = TEMPLATE_PATH + "<STR_LIT>" , <EOL> extra_context = { <EOL> "<STR_LIT>" : project , <EOL> "<STR_LIT>" : member , <EOL> "<STR_LIT>" : ( <EOL> ( "<STR_LIT>" , reverse ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" % project . name , reverse ( "<STR_LIT>" , args = [ project . id ] ) ) , <EOL> ( "<STR_LIT>" % member . username , request . path ) , <EOL> ) , <EOL> } , <EOL> ) </s>
