details: http://www.bx.psu.edu/hg/galaxy/rev/7faa12ac9746 changeset: 3811:7faa12ac9746 user: Greg Von Kuster <greg@bx.psu.edu> date: Mon May 24 11:22:12 2010 -0400 description: Send the web freamework ( trans ) to the grid's build_initial_query() method rather than the web framework's db session so that the method can take advantage of all of the web freamwork's attributes. Change the library controller to use grids for the data libraries display. diffstat: lib/galaxy/web/controllers/admin.py | 12 +- lib/galaxy/web/controllers/history.py | 8 +- lib/galaxy/web/controllers/library.py | 88 ++++++++++++++++++---- lib/galaxy/web/controllers/library_admin.py | 14 +- lib/galaxy/web/controllers/page.py | 4 +- lib/galaxy/web/controllers/requests.py | 4 +- lib/galaxy/web/controllers/tracks.py | 4 +- lib/galaxy/web/controllers/visualization.py | 4 +- lib/galaxy/web/controllers/workflow.py | 4 +- lib/galaxy/web/framework/helpers/grids.py | 7 +- lib/galaxy/webapps/community/controllers/admin.py | 24 +++--- lib/galaxy/webapps/community/controllers/tool.py | 12 +- templates/library/browse_libraries.mako | 31 -------- templates/library/grid.mako | 1 + test/functional/test_library_security.py | 2 +- 15 files changed, 121 insertions(+), 98 deletions(-) diffs (466 lines): diff -r d22853e5963d -r 7faa12ac9746 lib/galaxy/web/controllers/admin.py --- a/lib/galaxy/web/controllers/admin.py Mon May 24 10:53:55 2010 -0400 +++ b/lib/galaxy/web/controllers/admin.py Mon May 24 11:22:12 2010 -0400 @@ -103,8 +103,8 @@ use_paging = True def get_current_item( self, trans ): return trans.user - def build_initial_query( self, session ): - return session.query( self.model_class ) + def build_initial_query( self, trans ): + return trans.sa_session.query( self.model_class ) class RoleListGrid( grids.Grid ): class NameColumn( grids.TextColumn ): @@ -197,8 +197,8 @@ use_paging = True def get_current_item( self, trans ): return None - def build_initial_query( self, session ): - return session.query( self.model_class ) + def build_initial_query( self, trans ): + return trans.sa_session.query( self.model_class ) def apply_default_filter( self, trans, query, **kwargs ): return query.filter( model.Role.type != model.Role.types.PRIVATE ) @@ -275,8 +275,8 @@ use_paging = True def get_current_item( self, trans ): return None - def build_initial_query( self, session ): - return session.query( self.model_class ) + def build_initial_query( self, trans ): + return trans.sa_session.query( self.model_class ) class AdminGalaxy( BaseController, Admin ): diff -r d22853e5963d -r 7faa12ac9746 lib/galaxy/web/controllers/history.py --- a/lib/galaxy/web/controllers/history.py Mon May 24 10:53:55 2010 -0400 +++ b/lib/galaxy/web/controllers/history.py Mon May 24 11:22:12 2010 -0400 @@ -110,8 +110,8 @@ grids.GridOperation( "Unshare" ) ] standard_filters = [] - def build_initial_query( self, session ): - return session.query( self.model_class ).join( 'users_shared_with' ) + def build_initial_query( self, trans ): + return trans.sa_session.query( self.model_class ).join( 'users_shared_with' ) def apply_default_filter( self, trans, query, **kwargs ): return query.filter( model.HistoryUserShareAssociation.user == trans.user ) @@ -138,9 +138,9 @@ key="free-text-search", visible=False, filterable="standard" ) ) operations = [] - def build_initial_query( self, session ): + def build_initial_query( self, trans ): # Join so that searching history.user makes sense. - return session.query( self.model_class ).join( model.User.table ) + return trans.sa_session.query( self.model_class ).join( model.User.table ) def apply_default_filter( self, trans, query, **kwargs ): # A public history is published, has a slug, and is not deleted. return query.filter( self.model_class.published == True ).filter( self.model_class.slug != None ).filter( self.model_class.deleted == False ) diff -r d22853e5963d -r 7faa12ac9746 lib/galaxy/web/controllers/library.py --- a/lib/galaxy/web/controllers/library.py Mon May 24 10:53:55 2010 -0400 +++ b/lib/galaxy/web/controllers/library.py Mon May 24 11:22:12 2010 -0400 @@ -1,12 +1,73 @@ from galaxy.web.base.controller import * +from galaxy.web.framework.helpers import time_ago, iff, grids from galaxy.model.orm import * from galaxy.datatypes import sniff -from galaxy import util +from galaxy import model, util from galaxy.util.odict import odict log = logging.getLogger( __name__ ) +class LibraryListGrid( grids.Grid ): + class NameColumn( grids.TextColumn ): + def get_value( self, trans, grid, library ): + return library.name + class DescriptionColumn( grids.TextColumn ): + def get_value( self, trans, grid, library ): + if library.description: + return library.description + return '' + # Grid definition + title = "Data Libraries" + model_class = model.Library + template='/library/grid.mako' + default_sort_key = "name" + columns = [ + NameColumn( "Name", + key="name", + model_class=model.Library, + link=( lambda library: dict( operation="browse", id=library.id ) ), + attach_popup=False, + filterable="advanced" ), + DescriptionColumn( "Description", + key="description", + model_class=model.Library, + attach_popup=False, + filterable="advanced" ), + ] + columns.append( grids.MulticolFilterColumn( "Search", + cols_to_filter=[ columns[0], columns[1] ], + key="free-text-search", + visible=False, + filterable="standard" ) ) + standard_filters = [] + default_filter = dict( name="All", description="All", deleted="False", purged="False" ) + num_rows_per_page = 50 + preserve_state = False + use_paging = True + def build_initial_query( self, trans ): + return trans.sa_session.query( self.model_class ).filter( self.model_class.table.c.deleted == False ) + def apply_default_filter( self, trans, query, **kwd ): + current_user_role_ids = [ role.id for role in trans.get_current_user_roles() ] + library_access_action = trans.app.security_agent.permitted_actions.LIBRARY_ACCESS.action + restricted_library_ids = [ lp.library_id for lp in trans.sa_session.query( trans.model.LibraryPermissions ) \ + .filter( trans.model.LibraryPermissions.table.c.action == library_access_action ) \ + .distinct() ] + accessible_restricted_library_ids = [ lp.library_id for lp in trans.sa_session.query( trans.model.LibraryPermissions ) \ + .filter( and_( trans.model.LibraryPermissions.table.c.action == library_access_action, + trans.model.LibraryPermissions.table.c.role_id.in_( current_user_role_ids ) ) ) ] + if not trans.user: + # Filter to get only public libraries, a library whose id + # is not in restricted_library_ids is a public library + return query.filter( not_( trans.model.Library.table.c.id.in_( restricted_library_ids ) ) ) + else: + # Filter to get libraries accessible by the current user, get both + # public libraries and restricted libraries accessible by the current user. + return query.filter( or_( not_( trans.model.Library.table.c.id.in_( restricted_library_ids ) ), + trans.model.Library.table.c.id.in_( accessible_restricted_library_ids ) ) ) class Library( BaseController ): + + library_list_grid = LibraryListGrid() + @web.expose def index( self, trans, **kwd ): params = util.Params( kwd ) @@ -18,19 +79,12 @@ status=status ) @web.expose def browse_libraries( self, trans, **kwd ): - params = util.Params( kwd ) - message = util.restore_text( params.get( 'message', '' ) ) - status = params.get( 'status', 'done' ) - current_user_roles = trans.get_current_user_roles() - all_libraries = trans.sa_session.query( trans.app.model.Library ) \ - .filter( trans.app.model.Library.table.c.deleted==False ) \ - .order_by( trans.app.model.Library.name ) - authorized_libraries = [] - for library in all_libraries: - if trans.app.security_agent.can_access_library( current_user_roles, library ): - authorized_libraries.append( library ) - return trans.fill_template( '/library/browse_libraries.mako', - libraries=authorized_libraries, - default_action=params.get( 'default_action', None ), - message=message, - status=status ) + if 'operation' in kwd: + operation = kwd['operation'].lower() + if operation == "browse": + return trans.response.send_redirect( web.url_for( controller='library_common', + action='browse_library', + cntrller='library', + **kwd ) ) + # Render the list view + return self.library_list_grid( trans, **kwd ) diff -r d22853e5963d -r 7faa12ac9746 lib/galaxy/web/controllers/library_admin.py --- a/lib/galaxy/web/controllers/library_admin.py Mon May 24 10:53:55 2010 -0400 +++ b/lib/galaxy/web/controllers/library_admin.py Mon May 24 11:22:12 2010 -0400 @@ -69,8 +69,8 @@ num_rows_per_page = 50 preserve_state = False use_paging = True - def build_initial_query( self, session ): - return session.query( self.model_class ) + def build_initial_query( self, trans ): + return trans.sa_session.query( self.model_class ) class LibraryAdmin( BaseController ): @@ -78,16 +78,16 @@ @web.expose @web.require_admin - def browse_libraries( self, trans, **kwargs ): - if 'operation' in kwargs: - operation = kwargs['operation'].lower() + def browse_libraries( self, trans, **kwd ): + if 'operation' in kwd: + operation = kwd['operation'].lower() if operation == "browse": return trans.response.send_redirect( web.url_for( controller='library_common', action='browse_library', cntrller='library_admin', - **kwargs ) ) + **kwd ) ) # Render the list view - return self.library_list_grid( trans, **kwargs ) + return self.library_list_grid( trans, **kwd ) @web.expose @web.require_admin def create_library( self, trans, **kwd ): diff -r d22853e5963d -r 7faa12ac9746 lib/galaxy/web/controllers/page.py --- a/lib/galaxy/web/controllers/page.py Mon May 24 10:53:55 2010 -0400 +++ b/lib/galaxy/web/controllers/page.py Mon May 24 11:22:12 2010 -0400 @@ -71,9 +71,9 @@ cols_to_filter=[ columns[0], columns[1], columns[2], columns[3] ], key="free-text-search", visible=False, filterable="standard" ) ) - def build_initial_query( self, session ): + def build_initial_query( self, trans ): # Join so that searching history.user makes sense. - return session.query( self.model_class ).join( model.User.table ) + return trans.sa_session.query( self.model_class ).join( model.User.table ) def apply_default_filter( self, trans, query, **kwargs ): return query.filter( self.model_class.deleted==False ).filter( self.model_class.published==True ) diff -r d22853e5963d -r 7faa12ac9746 lib/galaxy/web/controllers/requests.py --- a/lib/galaxy/web/controllers/requests.py Mon May 24 10:53:55 2010 -0400 +++ b/lib/galaxy/web/controllers/requests.py Mon May 24 11:22:12 2010 -0400 @@ -126,8 +126,8 @@ ] def apply_default_filter( self, trans, query, **kwd ): return query.filter_by( user=trans.user ) - def build_initial_query( self, session ): - return session.query( self.model_class ) + def build_initial_query( self, trans ): + return trans.sa_session.query( self.model_class ) class Requests( BaseController ): request_grid = RequestsGrid() diff -r d22853e5963d -r 7faa12ac9746 lib/galaxy/web/controllers/tracks.py --- a/lib/galaxy/web/controllers/tracks.py Mon May 24 10:53:55 2010 -0400 +++ b/lib/galaxy/web/controllers/tracks.py Mon May 24 11:22:12 2010 -0400 @@ -71,8 +71,8 @@ DbKeyColumn( "Dbkey", key="dbkey", model_class=model.HistoryDatasetAssociation, visible=False ) ] - def build_initial_query( self, session ): - return session.query( self.model_class ).join( model.History.table).join( model.Dataset.table ) + def build_initial_query( self, trans ): + return trans.sa_session.query( self.model_class ).join( model.History.table).join( model.Dataset.table ) def apply_default_filter( self, trans, query, **kwargs ): if self.available_tracks is None: self.available_tracks = trans.app.datatypes_registry.get_available_tracks() diff -r d22853e5963d -r 7faa12ac9746 lib/galaxy/web/controllers/visualization.py --- a/lib/galaxy/web/controllers/visualization.py Mon May 24 10:53:55 2010 -0400 +++ b/lib/galaxy/web/controllers/visualization.py Mon May 24 11:22:12 2010 -0400 @@ -55,9 +55,9 @@ cols_to_filter=[ columns[0], columns[1], columns[2], columns[3] ], key="free-text-search", visible=False, filterable="standard" ) ) - def build_initial_query( self, session ): + def build_initial_query( self, trans ): # Join so that searching history.user makes sense. - return session.query( self.model_class ).join( model.User.table ) + return trans.sa_session.query( self.model_class ).join( model.User.table ) def apply_default_filter( self, trans, query, **kwargs ): return query.filter( self.model_class.deleted==False ).filter( self.model_class.published==True ) diff -r d22853e5963d -r 7faa12ac9746 lib/galaxy/web/controllers/workflow.py --- a/lib/galaxy/web/controllers/workflow.py Mon May 24 10:53:55 2010 -0400 +++ b/lib/galaxy/web/controllers/workflow.py Mon May 24 11:22:12 2010 -0400 @@ -75,9 +75,9 @@ key="free-text-search", visible=False, filterable="standard" ) ) operations = [] - def build_initial_query( self, session ): + def build_initial_query( self, trans ): # Join so that searching stored_workflow.user makes sense. - return session.query( self.model_class ).join( model.User.table ) + return trans.sa_session.query( self.model_class ).join( model.User.table ) def apply_default_filter( self, trans, query, **kwargs ): # A public workflow is published, has a slug, and is not deleted. return query.filter( self.model_class.published==True ).filter( self.model_class.slug != None ).filter( self.model_class.deleted == False ) diff -r d22853e5963d -r 7faa12ac9746 lib/galaxy/web/framework/helpers/grids.py --- a/lib/galaxy/web/framework/helpers/grids.py Mon May 24 10:53:55 2010 -0400 +++ b/lib/galaxy/web/framework/helpers/grids.py Mon May 24 11:22:12 2010 -0400 @@ -45,7 +45,6 @@ webapp = kwargs.get( 'webapp', 'galaxy' ) status = kwargs.get( 'status', None ) message = kwargs.get( 'message', None ) - session = trans.sa_session # Build a base filter and sort key that is the combination of the saved state and defaults. Saved state takes preference over defaults. base_filter = {} if self.default_filter: @@ -60,7 +59,7 @@ if pref_name in trans.get_user().preferences: base_sort_key = from_json_string( trans.get_user().preferences[pref_name] ) # Build initial query - query = self.build_initial_query( session ) + query = self.build_initial_query( trans ) query = self.apply_default_filter( trans, query, **kwargs ) # Maintain sort state in generated urls extra_url_args = {} @@ -258,8 +257,8 @@ pass def get_current_item( self, trans ): return None - def build_initial_query( self, session ): - return session.query( self.model_class ) + def build_initial_query( self, trans ): + return trans.sa_session.query( self.model_class ) def apply_default_filter( self, trans, query, **kwargs): return query diff -r d22853e5963d -r 7faa12ac9746 lib/galaxy/webapps/community/controllers/admin.py --- a/lib/galaxy/webapps/community/controllers/admin.py Mon May 24 10:53:55 2010 -0400 +++ b/lib/galaxy/webapps/community/controllers/admin.py Mon May 24 11:22:12 2010 -0400 @@ -113,8 +113,8 @@ use_paging = True def get_current_item( self, trans ): return trans.user - def build_initial_query( self, session ): - return session.query( self.model_class ) + def build_initial_query( self, trans ): + return trans.sa_session.query( self.model_class ) class RoleListGrid( grids.Grid ): class NameColumn( grids.TextColumn ): @@ -211,8 +211,8 @@ use_paging = True def get_current_item( self, trans ): return None - def build_initial_query( self, session ): - return session.query( self.model_class ) + def build_initial_query( self, trans ): + return trans.sa_session.query( self.model_class ) def apply_default_filter( self, trans, query, **kwd ): return query.filter( model.Role.type != model.Role.types.PRIVATE ) @@ -294,8 +294,8 @@ use_paging = True def get_current_item( self, trans ): return None - def build_initial_query( self, session ): - return session.query( self.model_class ) + def build_initial_query( self, trans ): + return trans.sa_session.query( self.model_class ) class ManageCategoryListGrid( grids.Grid ): class NameColumn( grids.TextColumn ): @@ -360,8 +360,8 @@ use_paging = True def get_current_item( self, trans ): return None - def build_initial_query( self, session ): - return session.query( self.model_class ) + def build_initial_query( self, trans ): + return trans.sa_session.query( self.model_class ) class ToolsByCategoryListGrid( grids.Grid ): class NameColumn( grids.TextColumn ): @@ -423,8 +423,8 @@ num_rows_per_page = 50 preserve_state = False use_paging = True - def build_initial_query( self, session ): - return session.query( self.model_class ) + def build_initial_query( self, trans ): + return trans.sa_session.query( self.model_class ) def apply_default_filter( self, trans, query, **kwd ): ids = kwd.get( 'ids', False ) if ids: @@ -546,8 +546,8 @@ num_rows_per_page = 50 preserve_state = False use_paging = True - def build_initial_query( self, session ): - return session.query( self.model_class ) + def build_initial_query( self, trans ): + return trans.sa_session.query( self.model_class ) def apply_default_filter( self, trans, query, **kwd ): ids = kwd.get( 'ids', False ) if ids: diff -r d22853e5963d -r 7faa12ac9746 lib/galaxy/webapps/community/controllers/tool.py --- a/lib/galaxy/webapps/community/controllers/tool.py Mon May 24 10:53:55 2010 -0400 +++ b/lib/galaxy/webapps/community/controllers/tool.py Mon May 24 11:22:12 2010 -0400 @@ -94,8 +94,8 @@ num_rows_per_page = 50 preserve_state = False use_paging = True - def build_initial_query( self, session ): - return session.query( self.model_class ) + def build_initial_query( self, trans ): + return trans.sa_session.query( self.model_class ) def apply_default_filter( self, trans, query, **kwd ): ids = kwd.get( 'ids', False ) if not ids: @@ -218,8 +218,8 @@ num_rows_per_page = 50 preserve_state = False use_paging = True - def build_initial_query( self, session ): - return session.query( self.model_class ) + def build_initial_query( self, trans ): + return trans.sa_session.query( self.model_class ) def apply_default_filter( self, trans, query, **kwd ): ids = kwd.get( 'ids', False ) if not ids: @@ -295,8 +295,8 @@ num_rows_per_page = 50 preserve_state = False use_paging = True - def build_initial_query( self, session ): - return session.query( self.model_class ) + def build_initial_query( self, trans ): + return trans.sa_session.query( self.model_class ) class ToolController( BaseController ): diff -r d22853e5963d -r 7faa12ac9746 templates/library/browse_libraries.mako --- a/templates/library/browse_libraries.mako Mon May 24 10:53:55 2010 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,31 +0,0 @@ -<%inherit file="/base.mako"/> -<%namespace file="/message.mako" import="render_msg" /> - -<%def name="title()">Browse Data Libraries</%def> - -<h2>Data Libraries</h2> - -%if message: - ${render_msg( message, status )} -%endif - -%if not libraries: - You are not authorized to access any libraries -%else: - <table class="grid"> - <thead> - <tr> - <th>Name</th> - <th>Description</th> - </tr> - </thead> - <tbody> - %for library in libraries: - <tr class="libraryRow libraryOrFolderRow" id="libraryRow"> - <td><a href="${h.url_for( controller='library_common', action='browse_library', cntrller='library', id=trans.security.encode_id( library.id ), hidden_folder_ids='' )}">${library.name}</a></td> - <td>${library.description}</td> - </tr> - %endfor - </tbody> - </table> -%endif diff -r d22853e5963d -r 7faa12ac9746 templates/library/grid.mako --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/templates/library/grid.mako Mon May 24 11:22:12 2010 -0400 @@ -0,0 +1,1 @@ +<%inherit file="/grid_base.mako"/> diff -r d22853e5963d -r 7faa12ac9746 test/functional/test_library_security.py --- a/test/functional/test_library_security.py Mon May 24 10:53:55 2010 -0400 +++ b/test/functional/test_library_security.py Mon May 24 11:22:12 2010 -0400 @@ -192,7 +192,7 @@ # regular_user2 should not be to see the library since they do not have # Role One which is associated with the LIBRARY_ACCESS permission self.login( email=regular_user2.email ) - self.browse_libraries_regular_user( check_str1="You are not authorized to access any libraries" ) + self.browse_libraries_regular_user( check_str1="No Items" ) self.logout() # regular_user3 should not be able to see 1.bed from the analysis view's access librarys self.login( email=regular_user3.email )