PageRenderTime 52ms CodeModel.GetById 43ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/web/api/libraries.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 93 lines | 72 code | 5 blank | 16 comment | 17 complexity | 6f74aa142b21305538abbb76594a08d9 MD5 | raw file
 1"""
 2API operations on a library.
 3"""
 4import logging, os, string, shutil, urllib, re, socket
 5from cgi import escape, FieldStorage
 6from galaxy import util, datatypes, jobs, web, util
 7from galaxy.web.base.controller import *
 8from galaxy.util.sanitize_html import sanitize_html
 9from galaxy.model.orm import *
10
11log = logging.getLogger( __name__ )
12
13class LibrariesController( BaseAPIController ):
14    
15    @web.expose_api
16    def index( self, trans, **kwd ):
17        """
18        GET /api/libraries
19        Displays a collection (list) of libraries.
20        """
21        query = trans.sa_session.query( trans.app.model.Library ).filter( trans.app.model.Library.table.c.deleted == False )
22        current_user_role_ids = [ role.id for role in trans.get_current_user_roles() ]
23        library_access_action = trans.app.security_agent.permitted_actions.LIBRARY_ACCESS.action
24        restricted_library_ids = [ lp.library_id for lp in trans.sa_session.query( trans.model.LibraryPermissions ) \
25                                                                           .filter( trans.model.LibraryPermissions.table.c.action == library_access_action ) \
26                                                                           .distinct() ]
27        accessible_restricted_library_ids = [ lp.library_id for lp in trans.sa_session.query( trans.model.LibraryPermissions ) \
28                                                                                      .filter( and_( trans.model.LibraryPermissions.table.c.action == library_access_action,
29                                                                                                     trans.model.LibraryPermissions.table.c.role_id.in_( current_user_role_ids ) ) ) ]
30        query = query.filter( or_( not_( trans.model.Library.table.c.id.in_( restricted_library_ids ) ),
31                           trans.model.Library.table.c.id.in_( accessible_restricted_library_ids ) ) )
32        rval = []
33        for library in query:
34            item = library.get_api_value()
35            item['url'] = url_for( 'library', id=trans.security.encode_id( library.id ) )
36            item['id'] = trans.security.encode_id( item['id'] )
37            rval.append( item )
38        return rval
39
40    @web.expose_api
41    def show( self, trans, id, **kwd ):
42        """
43        GET /api/libraries/{encoded_library_id}
44        Displays information about a library.
45        """
46        library_id = id
47        params = util.Params( kwd )
48        try:
49            decoded_library_id = trans.security.decode_id( library_id )
50        except TypeError:
51            trans.response.status = 400
52            return "Malformed library id ( %s ) specified, unable to decode." % str( library_id )
53        try:
54            library = trans.sa_session.query( trans.app.model.Library ).get( decoded_library_id )
55        except:
56            library = None
57        if not library or not ( trans.user_is_admin() or trans.app.security_agent.can_access_library( trans.get_current_user_roles(), library ) ):
58            trans.response.status = 400
59            return "Invalid library id ( %s ) specified." % str( library_id )
60        item = library.get_api_value( view='element' )
61        #item['contents_url'] = url_for( 'contents', library_id=library_id )
62        item['contents_url'] = url_for( 'library_contents', library_id=library_id )
63        return item
64
65    @web.expose_api
66    def create( self, trans, payload, **kwd ):
67        """
68        POST /api/libraries
69        Creates a new library.
70        """
71        if not trans.user_is_admin():
72            trans.response.status = 403
73            return "You are not authorized to create a new library."
74        params = util.Params( payload )
75        name = util.restore_text( params.get( 'name', None ) )
76        if not name:
77            trans.response.status = 400
78            return "Missing required parameter 'name'."
79        description = util.restore_text( params.get( 'description', '' ) )
80        synopsis = util.restore_text( params.get( 'synopsis', '' ) )
81        if synopsis in [ 'None', None ]:
82            synopsis = ''
83        library = trans.app.model.Library( name=name, description=description, synopsis=synopsis )
84        root_folder = trans.app.model.LibraryFolder( name=name, description='' )
85        library.root_folder = root_folder
86        trans.sa_session.add_all( ( library, root_folder ) )
87        trans.sa_session.flush()
88        encoded_id = trans.security.encode_id( library.id )
89        rval = {}
90        rval['url'] = url_for( 'library', id=encoded_id )
91        rval['name'] = name
92        rval['id'] = encoded_id
93        return [ rval ]