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

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 93 lines · 72 code · 5 blank · 16 comment · 13 complexity · 6f74aa142b21305538abbb76594a08d9 MD5 · raw file

  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. log = logging.getLogger( __name__ )
  11. class LibrariesController( BaseAPIController ):
  12. @web.expose_api
  13. def index( self, trans, **kwd ):
  14. """
  15. GET /api/libraries
  16. Displays a collection (list) of libraries.
  17. """
  18. query = trans.sa_session.query( trans.app.model.Library ).filter( trans.app.model.Library.table.c.deleted == False )
  19. current_user_role_ids = [ role.id for role in trans.get_current_user_roles() ]
  20. library_access_action = trans.app.security_agent.permitted_actions.LIBRARY_ACCESS.action
  21. restricted_library_ids = [ lp.library_id for lp in trans.sa_session.query( trans.model.LibraryPermissions ) \
  22. .filter( trans.model.LibraryPermissions.table.c.action == library_access_action ) \
  23. .distinct() ]
  24. accessible_restricted_library_ids = [ lp.library_id for lp in trans.sa_session.query( trans.model.LibraryPermissions ) \
  25. .filter( and_( trans.model.LibraryPermissions.table.c.action == library_access_action,
  26. trans.model.LibraryPermissions.table.c.role_id.in_( current_user_role_ids ) ) ) ]
  27. query = query.filter( or_( not_( trans.model.Library.table.c.id.in_( restricted_library_ids ) ),
  28. trans.model.Library.table.c.id.in_( accessible_restricted_library_ids ) ) )
  29. rval = []
  30. for library in query:
  31. item = library.get_api_value()
  32. item['url'] = url_for( 'library', id=trans.security.encode_id( library.id ) )
  33. item['id'] = trans.security.encode_id( item['id'] )
  34. rval.append( item )
  35. return rval
  36. @web.expose_api
  37. def show( self, trans, id, **kwd ):
  38. """
  39. GET /api/libraries/{encoded_library_id}
  40. Displays information about a library.
  41. """
  42. library_id = id
  43. params = util.Params( kwd )
  44. try:
  45. decoded_library_id = trans.security.decode_id( library_id )
  46. except TypeError:
  47. trans.response.status = 400
  48. return "Malformed library id ( %s ) specified, unable to decode." % str( library_id )
  49. try:
  50. library = trans.sa_session.query( trans.app.model.Library ).get( decoded_library_id )
  51. except:
  52. library = None
  53. if not library or not ( trans.user_is_admin() or trans.app.security_agent.can_access_library( trans.get_current_user_roles(), library ) ):
  54. trans.response.status = 400
  55. return "Invalid library id ( %s ) specified." % str( library_id )
  56. item = library.get_api_value( view='element' )
  57. #item['contents_url'] = url_for( 'contents', library_id=library_id )
  58. item['contents_url'] = url_for( 'library_contents', library_id=library_id )
  59. return item
  60. @web.expose_api
  61. def create( self, trans, payload, **kwd ):
  62. """
  63. POST /api/libraries
  64. Creates a new library.
  65. """
  66. if not trans.user_is_admin():
  67. trans.response.status = 403
  68. return "You are not authorized to create a new library."
  69. params = util.Params( payload )
  70. name = util.restore_text( params.get( 'name', None ) )
  71. if not name:
  72. trans.response.status = 400
  73. return "Missing required parameter 'name'."
  74. description = util.restore_text( params.get( 'description', '' ) )
  75. synopsis = util.restore_text( params.get( 'synopsis', '' ) )
  76. if synopsis in [ 'None', None ]:
  77. synopsis = ''
  78. library = trans.app.model.Library( name=name, description=description, synopsis=synopsis )
  79. root_folder = trans.app.model.LibraryFolder( name=name, description='' )
  80. library.root_folder = root_folder
  81. trans.sa_session.add_all( ( library, root_folder ) )
  82. trans.sa_session.flush()
  83. encoded_id = trans.security.encode_id( library.id )
  84. rval = {}
  85. rval['url'] = url_for( 'library', id=encoded_id )
  86. rval['name'] = name
  87. rval['id'] = encoded_id
  88. return [ rval ]