Skip to content

Commit

Permalink
Eliminate code dup around item_attrs.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmchilton committed Feb 11, 2019
1 parent affab01 commit 572d742
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
23 changes: 14 additions & 9 deletions lib/galaxy/model/item_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,7 @@ def _get_item_id_filter_str(self, item, item_rating_assoc_class, webapp_model=No
# Get foreign key in item-rating association table that references item table.
if webapp_model is None:
webapp_model = galaxy.model
item_fk = None
for fk in item_rating_assoc_class.table.foreign_keys:
if fk.references(item.table):
item_fk = fk
break

if not item_fk:
raise Exception("Cannot find item id column in item-rating association table: %s, %s" % item_rating_assoc_class.__name__, item_rating_assoc_class.table.name)

item_fk = get_foreign_key(item_rating_assoc_class, item)
# TODO: can we provide a better filter than a raw string?
return "%s=%i" % (item_fk.parent.name, item.id)

Expand Down Expand Up @@ -172,7 +164,20 @@ def _get_annotation_assoc_class(self, item):
return getattr(galaxy.model, class_name, None)


def get_foreign_key(source_class, target_class):
""" Returns foreign key in source class that references target class. """
target_fk = None
for fk in source_class.table.foreign_keys:
if fk.references(target_class.table):
target_fk = fk
break
if not target_fk:
raise Exception("No foreign key found between objects: %s, %s" % source_class.table, target_class.table)
return target_fk


__all__ = (
'get_foreign_key',
'UsesAnnotations',
'UsesItemRatings',
)
12 changes: 1 addition & 11 deletions lib/galaxy/web/framework/helpers/grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from six import string_types, text_type
from sqlalchemy.sql.expression import and_, false, func, null, or_, true

from galaxy.model.item_attrs import UsesAnnotations, UsesItemRatings
from galaxy.model.item_attrs import get_foreign_key, UsesAnnotations, UsesItemRatings
from galaxy.util import restore_text, sanitize_text, unicodify
from galaxy.util.odict import odict
from galaxy.web.framework import decorators, url_for
Expand Down Expand Up @@ -584,16 +584,6 @@ def get_value(self, trans, grid, item):
item_id=trans.security.encode_id(item.id))

def sort(self, trans, query, ascending, column_name=None):
def get_foreign_key(source_class, target_class):
""" Returns foreign key in source class that references target class. """
target_fk = None
for fk in source_class.table.foreign_keys:
if fk.references(target_class.table):
target_fk = fk
break
if not target_fk:
raise Exception("No foreign key found between objects: %s, %s" % source_class.table, target_class.table)
return target_fk
# Get the columns that connect item's table and item's rating association table.
item_rating_assoc_class = getattr(trans.model, '%sRatingAssociation' % self.model_class.__name__)
foreign_key = get_foreign_key(item_rating_assoc_class, self.model_class)
Expand Down
12 changes: 1 addition & 11 deletions lib/galaxy/webapps/reports/framework/grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from six import string_types, text_type
from sqlalchemy.sql.expression import and_, false, func, null, or_, true

from galaxy.model.item_attrs import UsesAnnotations, UsesItemRatings
from galaxy.model.item_attrs import get_foreign_key, UsesAnnotations, UsesItemRatings
from galaxy.util import sanitize_text, unicodify
from galaxy.util.odict import odict
from galaxy.web.framework import decorators, url_for
Expand Down Expand Up @@ -503,16 +503,6 @@ def get_value(self, trans, grid, item):
item_id=trans.security.encode_id(item.id))

def sort(self, trans, query, ascending, column_name=None):
def get_foreign_key(source_class, target_class):
""" Returns foreign key in source class that references target class. """
target_fk = None
for fk in source_class.table.foreign_keys:
if fk.references(target_class.table):
target_fk = fk
break
if not target_fk:
raise Exception("No foreign key found between objects: %s, %s" % source_class.table, target_class.table)
return target_fk
# Get the columns that connect item's table and item's rating association table.
item_rating_assoc_class = getattr(trans.model, '%sRatingAssociation' % self.model_class.__name__)
foreign_key = get_foreign_key(item_rating_assoc_class, self.model_class)
Expand Down

0 comments on commit 572d742

Please sign in to comment.