[2] | 1 | """ |
---|
| 2 | API operations on a library. |
---|
| 3 | """ |
---|
| 4 | import logging, os, string, shutil, urllib, re, socket |
---|
| 5 | from cgi import escape, FieldStorage |
---|
| 6 | from galaxy import util, datatypes, jobs, web, util |
---|
| 7 | from galaxy.web.base.controller import * |
---|
| 8 | from galaxy.util.sanitize_html import sanitize_html |
---|
| 9 | from galaxy.model.orm import * |
---|
| 10 | |
---|
| 11 | log = logging.getLogger( __name__ ) |
---|
| 12 | |
---|
| 13 | class LibrariesController( BaseController ): |
---|
| 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 | return item |
---|
| 63 | |
---|
| 64 | @web.expose_api |
---|
| 65 | def create( self, trans, payload, **kwd ): |
---|
| 66 | """ |
---|
| 67 | POST /api/libraries |
---|
| 68 | Creates a new library. |
---|
| 69 | """ |
---|
| 70 | if not trans.user_is_admin(): |
---|
| 71 | trans.response.status = 403 |
---|
| 72 | return "You are not authorized to create a new library." |
---|
| 73 | params = util.Params( payload ) |
---|
| 74 | name = util.restore_text( params.get( 'name', None ) ) |
---|
| 75 | if not name: |
---|
| 76 | trans.response.status = 400 |
---|
| 77 | return "Missing required parameter 'name'." |
---|
| 78 | description = util.restore_text( params.get( 'description', '' ) ) |
---|
| 79 | synopsis = util.restore_text( params.get( 'synopsis', '' ) ) |
---|
| 80 | if synopsis in [ 'None', None ]: |
---|
| 81 | synopsis = '' |
---|
| 82 | library = trans.app.model.Library( name=name, description=description, synopsis=synopsis ) |
---|
| 83 | root_folder = trans.app.model.LibraryFolder( name=name, description='' ) |
---|
| 84 | library.root_folder = root_folder |
---|
| 85 | trans.sa_session.add_all( ( library, root_folder ) ) |
---|
| 86 | trans.sa_session.flush() |
---|
| 87 | encoded_id = trans.security.encode_id( library.id ) |
---|
| 88 | rval = {} |
---|
| 89 | rval['url'] = url_for( 'libraries', id=encoded_id ) |
---|
| 90 | rval['name'] = name |
---|
| 91 | rval['id'] = encoded_id |
---|
| 92 | return [ rval ] |
---|