/lib/galaxy/web/api/histories.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 163 lines · 121 code · 16 blank · 26 comment · 33 complexity · e0642c2e02b7204d1f87e7cf58ee5843 MD5 · raw file

  1. """
  2. API operations on a history.
  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. import galaxy.datatypes
  11. from galaxy.util.bunch import Bunch
  12. log = logging.getLogger( __name__ )
  13. class HistoriesController( BaseAPIController, UsesHistory ):
  14. @web.expose_api
  15. def index( self, trans, deleted='False', **kwd ):
  16. """
  17. GET /api/histories
  18. GET /api/histories/deleted
  19. Displays a collection (list) of histories.
  20. """
  21. rval = []
  22. deleted = util.string_as_bool( deleted )
  23. try:
  24. query = trans.sa_session.query( trans.app.model.History ).filter_by( user=trans.user, deleted=deleted ).order_by(
  25. desc(trans.app.model.History.table.c.update_time)).all()
  26. except Exception, e:
  27. rval = "Error in history API"
  28. log.error( rval + ": %s" % str(e) )
  29. trans.response.status = 500
  30. if not rval:
  31. try:
  32. for history in query:
  33. item = history.get_api_value(value_mapper={'id':trans.security.encode_id})
  34. item['url'] = url_for( 'history', id=trans.security.encode_id( history.id ) )
  35. rval.append( item )
  36. except Exception, e:
  37. rval = "Error in history API at constructing return list"
  38. log.error( rval + ": %s" % str(e) )
  39. trans.response.status = 500
  40. return rval
  41. @web.expose_api
  42. def show( self, trans, id, deleted='False', **kwd ):
  43. """
  44. GET /api/histories/{encoded_history_id}
  45. GET /api/histories/deleted/{encoded_history_id}
  46. Displays information about a history.
  47. """
  48. history_id = id
  49. params = util.Params( kwd )
  50. deleted = util.string_as_bool( deleted )
  51. def traverse( datasets ):
  52. rval = {}
  53. states = trans.app.model.Dataset.states
  54. for key, state in states.items():
  55. rval[state] = 0
  56. for dataset in datasets:
  57. item = dataset.get_api_value( view='element' )
  58. if not item['deleted']:
  59. rval[item['state']] = rval[item['state']] + 1
  60. return rval
  61. try:
  62. history = self.get_history( trans, history_id, check_ownership=True, check_accessible=True, deleted=deleted )
  63. except Exception, e:
  64. return str( e )
  65. try:
  66. item = history.get_api_value(view='element', value_mapper={'id':trans.security.encode_id})
  67. num_sets = len( [hda.id for hda in history.datasets if not hda.deleted] )
  68. states = trans.app.model.Dataset.states
  69. state = states.ERROR
  70. if num_sets == 0:
  71. state = states.NEW
  72. else:
  73. summary = traverse(history.datasets)
  74. if summary[states.ERROR] > 0 or summary[states.FAILED_METADATA] > 0:
  75. state = states.ERROR
  76. elif summary[states.RUNNING] > 0 or summary[states.SETTING_METADATA] > 0:
  77. state = states.RUNNING
  78. elif summary[states.QUEUED] > 0:
  79. state = states.QUEUED
  80. elif summary[states.OK] == num_sets:
  81. state = states.OK
  82. item['contents_url'] = url_for( 'history_contents', history_id=history_id )
  83. item['state'] = state
  84. item['state_details'] = summary
  85. except Exception, e:
  86. item = "Error in history API at showing history detail"
  87. log.error(item + ": %s" % str(e))
  88. trans.response.status = 500
  89. return item
  90. @web.expose_api
  91. def create( self, trans, payload, **kwd ):
  92. """
  93. POST /api/histories
  94. Creates a new history.
  95. """
  96. params = util.Params( payload )
  97. hist_name = None
  98. if payload.get( 'name', None ):
  99. hist_name = util.restore_text( payload['name'] )
  100. new_history = trans.app.model.History( user=trans.user, name=hist_name )
  101. trans.sa_session.add( new_history )
  102. trans.sa_session.flush()
  103. item = new_history.get_api_value(view='element', value_mapper={'id':trans.security.encode_id})
  104. return item
  105. @web.expose_api
  106. def delete( self, trans, id, **kwd ):
  107. """
  108. DELETE /api/histories/{encoded_history_id}
  109. Deletes a history
  110. """
  111. history_id = id
  112. # a request body is optional here
  113. purge = False
  114. if kwd.get( 'payload', None ):
  115. purge = util.string_as_bool( kwd['payload'].get( 'purge', False ) )
  116. try:
  117. history = self.get_history( trans, history_id, check_ownership=True, check_accessible=False, deleted=True )
  118. except Exception, e:
  119. return str( e )
  120. history.deleted = True
  121. if purge and trans.app.config.allow_user_dataset_purge:
  122. for hda in history.datasets:
  123. if hda.purged:
  124. continue
  125. hda.purged = True
  126. trans.sa_session.add( hda )
  127. trans.sa_session.flush()
  128. if hda.dataset.user_can_purge:
  129. try:
  130. hda.dataset.full_delete()
  131. trans.sa_session.add( hda.dataset )
  132. except:
  133. pass
  134. trans.sa_session.flush()
  135. trans.sa_session.flush()
  136. return 'OK'
  137. @web.expose_api
  138. def undelete( self, trans, id, **kwd ):
  139. """
  140. POST /api/histories/deleted/{encoded_quota_id}/undelete
  141. Undeletes a quota
  142. """
  143. history = self.get_history( trans, history_id, check_ownership=True, check_accessible=False, deleted=True )
  144. history.deleted = False
  145. trans.sa_session.add( history )
  146. trans.sa_session.flush()
  147. return 'OK'