PageRenderTime 40ms CodeModel.GetById 29ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/web/api/request_types.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 102 lines | 77 code | 6 blank | 19 comment | 18 complexity | cecaed808567b50bf1b3777c0a883cc2 MD5 | raw file
  1"""
  2API operations on RequestType objects.
  3"""
  4import logging
  5from galaxy.web.base.controller import BaseAPIController, url_for
  6from galaxy import web
  7from galaxy.sample_tracking.request_types import request_type_factory
  8from elementtree.ElementTree import XML
  9
 10log = logging.getLogger( __name__ )
 11
 12
 13class RequestTypeAPIController( BaseAPIController ):
 14    @web.expose_api
 15    def index( self, trans, **kwd ):
 16        """
 17        GET /api/request_types
 18        Displays a collection (list) of request_types.
 19        """
 20        rval = []
 21        for request_type in trans.app.security_agent.get_accessible_request_types( trans, trans.user ):
 22            item = request_type.get_api_value( value_mapper={ 'id': trans.security.encode_id, 'request_form_id': trans.security.encode_id, 'sample_form_id': trans.security.encode_id } )
 23            encoded_id = trans.security.encode_id( request_type.id )
 24            item['url'] = url_for( 'request_type', id=encoded_id )
 25            rval.append( item )
 26        return rval
 27
 28    @web.expose_api
 29    def show( self, trans, id, **kwd ):
 30        """
 31        GET /api/request_types/{encoded_request_type_id}
 32        Displays information about a request_type.
 33        """
 34        request_type_id = id
 35        try:
 36            decoded_request_type_id = trans.security.decode_id( request_type_id )
 37        except TypeError:
 38            trans.response.status = 400
 39            return "Malformed request type id ( %s ) specified, unable to decode." % str( request_type_id )
 40        try:
 41            request_type = trans.sa_session.query( trans.app.model.RequestType ).get( decoded_request_type_id )
 42        except:
 43            request_type = None
 44        if not request_type:# or not trans.user_is_admin():
 45            trans.response.status = 400
 46            return "Invalid request_type id ( %s ) specified." % str( request_type_id )
 47        if not trans.app.security_agent.can_access_request_type( trans.user.all_roles(), request_type ):
 48            trans.response.status = 400
 49            return "No permission to access request_type ( %s )." % str( request_type_id )
 50        item = request_type.get_api_value( view='element', value_mapper={ 'id': trans.security.encode_id, 'request_form_id': trans.security.encode_id, 'sample_form_id': trans.security.encode_id } )
 51        item['url'] = url_for( 'request_type', id=request_type_id )
 52        return item
 53
 54    @web.expose_api
 55    def create( self, trans, payload, **kwd ):
 56        """
 57        POST /api/request_types
 58        Creates a new request type (external_service configuration).
 59        """
 60        if not trans.user_is_admin():
 61            trans.response.status = 403
 62            return "You are not authorized to create a new request type (external_service configuration)."
 63        xml_text = payload.get( 'xml_text', None )
 64        if xml_text is None:
 65            trans.response.status = 400
 66            return "Missing required parameter 'xml_text'."
 67        elem = XML( xml_text )
 68        request_form_id = payload.get( 'request_form_id', None )
 69        if request_form_id is None:
 70            trans.response.status = 400
 71            return "Missing required parameter 'request_form_id'."
 72        request_form = trans.sa_session.query( trans.app.model.FormDefinition ).get( trans.security.decode_id( request_form_id ) )
 73        sample_form_id = payload.get( 'sample_form_id', None )
 74        if sample_form_id is None:
 75            trans.response.status = 400
 76            return "Missing required parameter 'sample_form_id'."
 77        sample_form = trans.sa_session.query( trans.app.model.FormDefinition ).get( trans.security.decode_id( sample_form_id ) )
 78        external_service_id = payload.get( 'external_service_id', None )
 79        if external_service_id is None:
 80            trans.response.status = 400
 81            return "Missing required parameter 'external_service_id'."
 82        external_service = trans.sa_session.query( trans.app.model.ExternalService ).get( trans.security.decode_id( external_service_id ) )
 83        request_type = request_type_factory.from_elem( elem, request_form, sample_form, external_service )
 84        #FIXME: move permission building/setting to separate abstract method call and
 85        #allow setting individual permissions by role (currently only one action, so not strictly needed)
 86        role_ids = payload.get( 'role_ids', [] )
 87        roles = [ trans.sa_session.query( trans.model.Role ).get( trans.security.decode_id( i ) ) for i in role_ids ]# if trans.app.security_agent.ok_to_display( trans.user, i ) ]
 88        permissions = {}
 89        if roles:
 90            #yikes, there has to be a better way?
 91            for k, v in trans.model.RequestType.permitted_actions.items():
 92                permissions[ trans.app.security_agent.get_action( v.action ) ] = roles
 93        if permissions:
 94            trans.app.security_agent.set_request_type_permissions( request_type, permissions )
 95        
 96        #flush objects
 97        trans.sa_session.add( request_type )
 98        trans.sa_session.flush()
 99        encoded_id = trans.security.encode_id( request_type.id )
100        item = request_type.get_api_value( view='element', value_mapper={ 'id': trans.security.encode_id, 'request_form_id': trans.security.encode_id, 'sample_form_id': trans.security.encode_id } )
101        item['url'] = url_for( 'request_type', id=encoded_id )
102        return [ item ]