[hg] galaxy 2395: Simplifying the grid helper a bit. Fix a bug w...
details: http://www.bx.psu.edu/hg/galaxy/rev/df824c53f2f3 changeset: 2395:df824c53f2f3 user: James Taylor <james@jamestaylor.org> date: Fri May 01 17:19:17 2009 -0400 description: Simplifying the grid helper a bit. Fix a bug where sort/filter were not retained when performing actions on multiple items 4 file(s) affected in this change: lib/galaxy/web/controllers/history.py lib/galaxy/web/framework/helpers/__init__.py lib/galaxy/web/framework/helpers/grids.py templates/grid.mako diffs (279 lines): diff -r 80e00a9cdeb0 -r df824c53f2f3 lib/galaxy/web/controllers/history.py --- a/lib/galaxy/web/controllers/history.py Fri May 01 12:24:24 2009 -0400 +++ b/lib/galaxy/web/controllers/history.py Fri May 01 17:19:17 2009 -0400 @@ -1,5 +1,5 @@ from galaxy.web.base.controller import * -from galaxy.web.framework.helpers.grids import * +from galaxy.web.framework.helpers import time_ago, iff, grids import webhelpers from datetime import datetime from cgi import escape @@ -9,42 +9,33 @@ # States for passing messages SUCCESS, INFO, WARNING, ERROR = "done", "info", "warning", "error" -def time_ago( x ): - return webhelpers.date.distance_of_time_in_words( x, datetime.utcnow() ) - -def iff( a, b, c ): - if a: - return b - else: - return c - -class HistoryListGrid( Grid ): +class HistoryListGrid( grids.Grid ): title = "Stored histories" model_class = model.History default_sort_key = "-create_time" columns = [ - GridColumn( "Name", key="name", + grids.GridColumn( "Name", key="name", link=( lambda item: iff( item.deleted, None, dict( operation="switch", id=item.id ) ) ), attach_popup=True ), - GridColumn( "Datasets (by state)", method='_build_datasets_by_state', ncells=4 ), - GridColumn( "Status", method='_build_status' ), - GridColumn( "Age", key="create_time", format=time_ago ), - GridColumn( "Last update", key="update_time", format=time_ago ), + grids.GridColumn( "Datasets (by state)", method='_build_datasets_by_state', ncells=4 ), + grids.GridColumn( "Status", method='_build_status' ), + grids.GridColumn( "Age", key="create_time", format=time_ago ), + grids.GridColumn( "Last update", key="update_time", format=time_ago ), # Valid for filtering but invisible - GridColumn( "Deleted", key="deleted", visible=False ) + grids.GridColumn( "Deleted", key="deleted", visible=False ) ] operations = [ - GridOperation( "Switch", allow_multiple=False, condition=( lambda item: not item.deleted ) ), - GridOperation( "Share", condition=( lambda item: not item.deleted ) ), - GridOperation( "Rename", condition=( lambda item: not item.deleted ) ), - GridOperation( "Delete", condition=( lambda item: not item.deleted ) ), - GridOperation( "Undelete", condition=( lambda item: item.deleted ) ) + grids.GridOperation( "Switch", allow_multiple=False, condition=( lambda item: not item.deleted ) ), + grids.GridOperation( "Share", condition=( lambda item: not item.deleted ) ), + grids.GridOperation( "Rename", condition=( lambda item: not item.deleted ) ), + grids.GridOperation( "Delete", condition=( lambda item: not item.deleted ) ), + grids.GridOperation( "Undelete", condition=( lambda item: item.deleted ) ) ] standard_filters = [ - GridColumnFilter( "Active", args=dict( deleted=False ) ), - GridColumnFilter( "Deleted", args=dict( deleted=True ) ), - GridColumnFilter( "All", args=dict( deleted='All' ) ) + grids.GridColumnFilter( "Active", args=dict( deleted=False ) ), + grids.GridColumnFilter( "Deleted", args=dict( deleted=True ) ), + grids.GridColumnFilter( "All", args=dict( deleted='All' ) ) ] default_filter = dict( deleted=False ) @@ -53,41 +44,7 @@ def apply_default_filter( self, trans, query ): return query.filter_by( user=trans.user, purged=False ) - - def handle_operation( self, trans, operation, history_ids ): - # Display no message by default - status, message = None, None - refresh_history = False - # Load the histories and ensure they all belong to the current user - histories = [] - for hid in history_ids: - history = model.History.get( hid ) - if history: - # Ensure history is owned by current user - if history.user_id != None and trans.user: - assert trans.user.id == history.user_id, "History does not belong to current user" - histories.append( history ) - else: - log.warn( "Invalid history id '%r' passed to list", hid ) - operation = operation.lower() - if operation == "switch": - status, message = self._list_switch( trans, histories ) - # Current history changed, refresh history frame - trans.template_context['refresh_frames'] = ['history'] - elif operation == "share": - ## Caught above for now - pass - elif operation == "rename": - ## Caught above for now - pass - elif operation == "delete": - status, message = self._list_delete( trans, histories ) - elif operation == "undelete": - status, message = self._list_undelete( trans, histories ) - trans.sa_session.flush() - # Render the list view - return status, message - + def _build_datasets_by_state( self, trans, history ): rval = [] for state in ( 'ok', 'running', 'queued', 'error' ): @@ -102,6 +59,64 @@ if history.deleted: return "deleted" return "" + +class HistoryController( BaseController ): + + @web.expose + def index( self, trans ): + return "" + + @web.expose + def list_as_xml( self, trans ): + """ + XML history list for functional tests + """ + return trans.fill_template( "/history/list_as_xml.mako" ) + + list_grid = HistoryListGrid() + + @web.expose + @web.require_login( "work with multiple histories" ) + def list( self, trans, **kwargs ): + """ + List all available histories + """ + status = message = None + if 'operation' in kwargs: + operation = kwargs['operation'].lower() + if operation == "share": + return self.share( trans, **kwargs ) + elif operation == "rename": + return self.rename( trans, **kwargs ) + # Display no message by default + status, message = None, None + refresh_history = False + # Load the histories and ensure they all belong to the current user + history_ids = kwargs.get( 'id', [] ) + if type( history_ids ) is not list: + history_ids = [ history_ids ] + histories = [] + for hid in history_ids: + history = model.History.get( hid ) + if history: + # Ensure history is owned by current user + if history.user_id != None and trans.user: + assert trans.user.id == history.user_id, "History does not belong to current user" + histories.append( history ) + else: + log.warn( "Invalid history id '%r' passed to list", hid ) + if histories: + if operation == "switch": + status, message = self._list_switch( trans, histories ) + # Current history changed, refresh history frame + trans.template_context['refresh_frames'] = ['history'] + elif operation == "delete": + status, message = self._list_delete( trans, histories ) + elif operation == "undelete": + status, message = self._list_undelete( trans, histories ) + trans.sa_session.flush() + # Render the list view + return self.list_grid( trans, status=status, message=message, **kwargs ) def _list_delete( self, trans, histories ): """Delete histories""" @@ -164,37 +179,6 @@ trans.log_event( "History switched to id: %s, name: '%s'" % (str(new_history.id), new_history.name ) ) # No message return None, None - -class HistoryController( BaseController ): - - @web.expose - def index( self, trans ): - return "" - - @web.expose - def list_as_xml( self, trans ): - """ - XML history list for functional tests - """ - return trans.fill_template( "/history/list_as_xml.mako" ) - - _list_grid = HistoryListGrid() - - @web.expose - @web.require_login( "work with multiple histories" ) - def list( self, trans, *args, **kwargs ): - """ - List all available histories - """ - # TODO: these two operations need to be updates still - operation = kwargs.get( 'operation', None ) - if operation: - operation = operation.lower() - if operation == "share": - return self.share( trans, **kwargs ) - elif operation == "rename": - return self.rename( trans, **kwargs ) - return self._list_grid( trans, *args, **kwargs ) @web.expose def delete_current( self, trans ): diff -r 80e00a9cdeb0 -r df824c53f2f3 lib/galaxy/web/framework/helpers/__init__.py --- a/lib/galaxy/web/framework/helpers/__init__.py Fri May 01 12:24:24 2009 -0400 +++ b/lib/galaxy/web/framework/helpers/__init__.py Fri May 01 17:19:17 2009 -0400 @@ -0,0 +1,12 @@ +from webhelpers import * + +from datetime import datetime + +def time_ago( x ): + return date.distance_of_time_in_words( x, datetime.utcnow() ) + +def iff( a, b, c ): + if a: + return b + else: + return c \ No newline at end of file diff -r 80e00a9cdeb0 -r df824c53f2f3 lib/galaxy/web/framework/helpers/grids.py --- a/lib/galaxy/web/framework/helpers/grids.py Fri May 01 12:24:24 2009 -0400 +++ b/lib/galaxy/web/framework/helpers/grids.py Fri May 01 17:19:17 2009 -0400 @@ -16,18 +16,13 @@ standard_filters = [] default_filter = None default_sort_key = None + pass_through_operations = {} def __init__( self ): pass def __call__( self, trans, **kwargs ): - # Session + status = kwargs.get( 'status', None ) + message = kwargs.get( 'message', None ) session = trans.sa_session - # Process any actions - status = message = None - ids = self.get_ids( **kwargs ) - operation = kwargs.get( 'operation', None ) - if operation: - operation = operation.lower() - status, message = self.handle_operation( trans, operation, ids ) # Build initial query query = self.build_initial_query( session ) query = self.apply_default_filter( trans, query ) @@ -83,7 +78,7 @@ encoded_sort_key=encoded_sort_key, sort_order=sort_order, current_item=current_item, - ids = ids, + ids = kwargs.get( 'id', [] ), url = url, message_type = status, message = message ) diff -r 80e00a9cdeb0 -r df824c53f2f3 templates/grid.mako --- a/templates/grid.mako Fri May 01 12:24:24 2009 -0400 +++ b/templates/grid.mako Fri May 01 17:19:17 2009 -0400 @@ -85,9 +85,7 @@ %endfor </div> -<form name="history_actions" action="${h.url_for()}" method="post" > - - <input type="hidden" name="sort" value="${sort_key}"> +<form name="history_actions" action="${url()}" method="post" > <table class="grid"> <thead>
participants (1)
-
Nate Coraor