From f6c1aa0770508c552f074771078a4b7cc4cce400 Mon Sep 17 00:00:00 2001 From: Dannon Baker Date: Mon, 29 Aug 2016 11:24:03 -0400 Subject: [PATCH] Create a new trans method for getting the most recently touched history. Swap several uses of get_history (with create=True, only), to use this new functionality where it makes sense. --- lib/galaxy/web/framework/webapp.py | 28 ++++++++++++++++--- .../webapps/galaxy/controllers/history.py | 4 +-- .../galaxy/controllers/visualization.py | 2 +- .../webapps/galaxy/workflow/display.mako | 2 +- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/lib/galaxy/web/framework/webapp.py b/lib/galaxy/web/framework/webapp.py index d34a2995d4c..188df61e1d3 100644 --- a/lib/galaxy/web/framework/webapp.py +++ b/lib/galaxy/web/framework/webapp.py @@ -660,7 +660,7 @@ class GalaxyWebTransaction( base.DefaultWebTransaction, users_last_session.current_history.deleted): history = users_last_session.current_history elif not history: - history = self.get_history( create=True ) + history = self.get_history( create=True, most_recent=True ) if history not in self.galaxy_session.histories: self.galaxy_session.add_history( history ) if history.user is None: @@ -708,10 +708,11 @@ class GalaxyWebTransaction( base.DefaultWebTransaction, """ return self.galaxy_session - def get_history( self, create=False ): + def get_history( self, create=False, most_recent=False ): """ - Load the current history, creating a new one only if there is not - current history and we're told to create. + Load the current history. + - If that isn't available, we find the most recently updated history. + - If *that* isn't available, we get or create the default history. Transactions will not always have an active history (API requests), so None is a valid response. """ @@ -719,6 +720,8 @@ class GalaxyWebTransaction( base.DefaultWebTransaction, if self.galaxy_session: if hasattr( self.galaxy_session, 'current_history' ): history = self.galaxy_session.current_history + if not history and most_recent: + history = self.get_most_recent_history() if not history and util.string_as_bool( create ): history = self.get_or_create_default_history() return history @@ -764,6 +767,23 @@ class GalaxyWebTransaction( base.DefaultWebTransaction, return history + def get_most_recent_history( self ): + """ + Gets the most recently updated history. + """ + # There must be a user to fetch histories, and without a user we have + # no recent history. + if not self.galaxy_session.user: + return None + try: + recent_history = self.sa_session.query( self.app.model.History ).filter_by( + user=self.galaxy_session.user, + deleted=False ).order_by(self.app.model.History.update_time.desc()).first() + except NoResultFound: + return None + self.set_history(recent_history) + return recent_history + def new_history( self, name=None ): """ Create a new history and associate it with the current session and diff --git a/lib/galaxy/webapps/galaxy/controllers/history.py b/lib/galaxy/webapps/galaxy/controllers/history.py index 9dac93ea710..7f9563893c5 100644 --- a/lib/galaxy/webapps/galaxy/controllers/history.py +++ b/lib/galaxy/webapps/galaxy/controllers/history.py @@ -487,7 +487,7 @@ class HistoryController( BaseUIController, SharableMixin, UsesAnnotations, UsesI history = self.history_manager.get_accessible( self.decode_id( id ), trans.user, current_history=trans.history ) else: - history = trans.get_history( create=True ) + history = trans.get_history( most_recent=True, create=True ) trans.response.set_content_type( 'text/xml' ) return trans.fill_template_mako( @@ -1351,7 +1351,7 @@ class HistoryController( BaseUIController, SharableMixin, UsesAnnotations, UsesI """Return the current user's current history in a serialized, dictionary form.""" # Prevent IE11 from caching this trans.response.headers[ 'Cache-Control' ] = ["max-age=0", "no-cache", "no-store"] - history = trans.get_history( create=True ) + history = trans.get_history( most_recent=True, create=True ) return self.history_data( trans, history ) @web.json diff --git a/lib/galaxy/webapps/galaxy/controllers/visualization.py b/lib/galaxy/webapps/galaxy/controllers/visualization.py index 3eda0900eed..f0195cf1c89 100644 --- a/lib/galaxy/webapps/galaxy/controllers/visualization.py +++ b/lib/galaxy/webapps/galaxy/controllers/visualization.py @@ -955,7 +955,7 @@ class VisualizationController( BaseUIController, SharableMixin, UsesVisualizatio """ regions = regions or '{}' # Need to create history if necessary in order to create tool form. - trans.get_history( create=True ) + trans.get_history( most_recent=True, create=True ) if id: # Loading a shared visualization. diff --git a/templates/webapps/galaxy/workflow/display.mako b/templates/webapps/galaxy/workflow/display.mako index 386875ef24c..7d3726e1ddd 100644 --- a/templates/webapps/galaxy/workflow/display.mako +++ b/templates/webapps/galaxy/workflow/display.mako @@ -90,7 +90,7 @@ <% # HACK: Rendering workflow steps requires that trans have a history; however, if its user's first visit to Galaxy is here, he won't have a history # and an error will occur. To prevent this error, make sure user has a history. - trans.get_history( create=True ) + trans.get_history( most_recent=True, create=True ) %>
StepAnnotation