details: http://www.bx.psu.edu/hg/galaxy/rev/389226e41780 changeset: 2793:389226e41780 user: jeremy goecks <jeremy.goecks@emory.edu> date: Mon Sep 28 18:57:25 2009 -0400 description: Association proxy for user preferences. 4 file(s) affected in this change: lib/galaxy/model/__init__.py lib/galaxy/model/mapping.py lib/galaxy/web/controllers/history.py lib/galaxy/web/framework/helpers/grids.py diffs (148 lines): diff -r 874817d9147a -r 389226e41780 lib/galaxy/model/__init__.py --- a/lib/galaxy/model/__init__.py Mon Sep 28 13:37:14 2009 -0400 +++ b/lib/galaxy/model/__init__.py Mon Sep 28 18:57:25 2009 -0400 @@ -1316,11 +1316,9 @@ pass class UserPreference ( object ): - def __init( self, user_id=None, name=None, value=None ): - self.user_id = user_id + def __init__( self, name=None, value=None ): self.name = name self.value = value - ## ---- Utility methods ------------------------------------------------------- diff -r 874817d9147a -r 389226e41780 lib/galaxy/model/mapping.py --- a/lib/galaxy/model/mapping.py Mon Sep 28 13:37:14 2009 -0400 +++ b/lib/galaxy/model/mapping.py Mon Sep 28 18:57:25 2009 -0400 @@ -14,6 +14,8 @@ from galaxy.model.custom_types import * from galaxy.util.bunch import Bunch from galaxy.security import GalaxyRBACAgent +from sqlalchemy.orm.collections import attribute_mapped_collection +from sqlalchemy.ext.associationproxy import association_proxy metadata = MetaData() context = Session = scoped_session( sessionmaker( autoflush=False, transactional=False ) ) @@ -754,10 +756,14 @@ stored_workflow_menu_entries=relation( StoredWorkflowMenuEntry, backref="user", cascade="all, delete-orphan", collection_class=ordering_list( 'order_index' ) ), - preferences=relation( UserPreference, backref="user", order_by=UserPreference.table.c.id), + _preferences=relation( UserPreference, backref="user", collection_class=attribute_mapped_collection('name')), # addresses=relation( UserAddress, # primaryjoin=( User.table.c.id == UserAddress.table.c.user_id ) ) ) ) + +# Set up proxy so that this syntax is possible: +# <user_obj>.preferences[pref_name] = pref_value +User.preferences = association_proxy('_preferences', 'value', creator=UserPreference) assign_mapper( context, Group, Group.table, properties=dict( users=relation( UserGroupAssociation ) ) ) diff -r 874817d9147a -r 389226e41780 lib/galaxy/web/controllers/history.py --- a/lib/galaxy/web/controllers/history.py Mon Sep 28 13:37:14 2009 -0400 +++ b/lib/galaxy/web/controllers/history.py Mon Sep 28 18:57:25 2009 -0400 @@ -125,6 +125,8 @@ grids.GridColumnFilter( "All", args=dict( deleted='All' ) ), ] default_filter = dict( deleted="False", tags="All" ) + preserve_state = False + use_paging = True def get_current_item( self, trans ): return trans.get_history() def apply_default_filter( self, trans, query, **kwargs ): diff -r 874817d9147a -r 389226e41780 lib/galaxy/web/framework/helpers/grids.py --- a/lib/galaxy/web/framework/helpers/grids.py Mon Sep 28 13:37:14 2009 -0400 +++ b/lib/galaxy/web/framework/helpers/grids.py Mon Sep 28 18:57:25 2009 -0400 @@ -25,7 +25,7 @@ preserve_state = False use_paging = False - num_rows_per_page = 5 + num_rows_per_page = 10 # Set preference names. cur_filter_pref_name = ".filter" @@ -51,18 +51,14 @@ base_sort_key = self.default_sort_key if self.preserve_state: pref_name = unicode( self.__class__.__name__ + self.cur_filter_pref_name ) - saved_filter_pref = trans.sa_session.query( UserPreference ).\ - filter_by( name=pref_name, user_id=trans.get_user().id ).first() - if saved_filter_pref: - saved_filter = from_json_string( saved_filter_pref.value ) + if pref_name in trans.get_user().preferences: + saved_filter = from_json_string( trans.get_user().preferences[pref_name] ) base_filter.update( saved_filter ) pref_name = unicode( self.__class__.__name__ + self.cur_sort_key_pref_name ) - saved_sort_key_pref = trans.sa_session.query( UserPreference ).\ - filter_by( name=pref_name, user_id=trans.get_user().id ).first() - if saved_sort_key_pref: - base_sort_key = from_json_string( saved_sort_key_pref.value ) - + 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.apply_default_filter( trans, query, **kwargs ) @@ -70,14 +66,23 @@ # Maintain sort state in generated urls extra_url_args = {} + # Determine whether use_default_filter flag is set. + use_default_filter_str = kwargs.get( 'use_default_filter' ) + use_default_filter = False + if use_default_filter_str: + use_default_filter = use_default_filter_str.lower() == 'true' + # Process filtering arguments to (a) build a query that represents the filter and (b) builds a - # dictionary that denotes the current filter. + # dictionary that denotes the current filter. cur_filter_dict = {} for column in self.columns: if column.key: - # Look for filter criterion in kwargs; if not found, look in base filter. + # Get the filter criterion for the column. Precedence is (a) if using default filter, look there; (b) look in kwargs; and (c) look in + # base filter. column_filter = None - if "f-" + column.key in kwargs: + if use_default_filter and self.default_filter: + column_filter = self.default_filter.get( column.key ) + elif "f-" + column.key in kwargs: column_filter = kwargs.get( "f-" + column.key ) elif column.key in base_filter: column_filter = base_filter.get( column.key ) @@ -136,17 +141,11 @@ # Preserve grid state: save current filter and sort key. if self.preserve_state: pref_name = unicode( self.__class__.__name__ + self.cur_filter_pref_name ) - if not saved_filter_pref: - saved_filter_pref = UserPreference( name=pref_name ) - trans.get_user().preferences.append( saved_filter_pref ) - saved_filter_pref.value = unicode( to_json_string( cur_filter_dict ) ) + trans.get_user().preferences[pref_name] = unicode( to_json_string( cur_filter_dict ) ) + if sort_key: - if not saved_sort_key_pref: - pref_name = unicode( self.__class__.__name__ + self.cur_sort_key_pref_name ) - if not saved_sort_key_pref: - saved_sort_key_pref = UserPreference( name=pref_name ) - trans.get_user().preferences.append( saved_sort_key_pref ) - saved_sort_key_pref.value = unicode( to_json_string( sort_key ) ) + pref_name = unicode( self.__class__.__name__ + self.cur_sort_key_pref_name ) + trans.get_user().preferences[pref_name] = unicode( to_json_string( sort_key ) ) trans.sa_session.flush() # Render grid. @@ -294,4 +293,4 @@ for k, v in self.args.items(): rval[ "f-" + k ] = v return rval - \ No newline at end of file +