Don't mask the filter built-in

There was an argument to a function called 'filter'. 'filter' is a
python built-in, so that name shouldn't be reassigned
to something else since that will mask the built-in which can be
confusing and problematic when someone wants to use the built-in.

Also, changed the name of the sql.Base member to filter_query for
the same reason.

Change-Id: I8eb13c73bd8cf0f31e08951bc0fcaf49abd49ef1
This commit is contained in:
Brant Knudson 2014-01-25 09:41:24 -06:00
parent f71276b328
commit a090d962a0
3 changed files with 27 additions and 27 deletions

View File

@ -212,7 +212,7 @@ class Assignment(sql.Base, assignment.Driver):
def list_projects(self, hints): def list_projects(self, hints):
with sql.transaction() as session: with sql.transaction() as session:
query = session.query(Project) query = session.query(Project)
project_refs = self.filter(Project, query, hints) project_refs = self.filter_query(Project, query, hints)
return [project_ref.to_dict() for project_ref in project_refs] return [project_ref.to_dict() for project_ref in project_refs]
def list_projects_in_domain(self, domain_id): def list_projects_in_domain(self, domain_id):
@ -531,7 +531,7 @@ class Assignment(sql.Base, assignment.Driver):
def list_domains(self, hints): def list_domains(self, hints):
with sql.transaction() as session: with sql.transaction() as session:
query = session.query(Domain) query = session.query(Domain)
refs = self.filter(Domain, query, hints) refs = self.filter_query(Domain, query, hints)
return [ref.to_dict() for ref in refs] return [ref.to_dict() for ref in refs]
def _get_domain(self, session, domain_id): def _get_domain(self, session, domain_id):
@ -584,7 +584,7 @@ class Assignment(sql.Base, assignment.Driver):
def list_roles(self, hints): def list_roles(self, hints):
with sql.transaction() as session: with sql.transaction() as session:
query = session.query(Role) query = session.query(Role)
refs = self.filter(Role, query, hints) refs = self.filter_query(Role, query, hints)
return [ref.to_dict() for ref in refs] return [ref.to_dict() for ref in refs]
def _get_role(self, session, role_id): def _get_role(self, session, role_id):

View File

@ -167,12 +167,12 @@ class Base(object):
:returns query: query, updated with any filters satisfied :returns query: query, updated with any filters satisfied
""" """
def inexact_filter(model, query, filter, hints): def inexact_filter(model, query, filter_, hints):
"""Applies an inexact filter to a query. """Applies an inexact filter to a query.
:param model: the table model in question :param model: the table model in question
:param query: query to apply filters to :param query: query to apply filters to
:param filter: the dict that describes this filter :param filter_: the dict that describes this filter
:param hints: contains the list of filters yet to be satisfied. :param hints: contains the list of filters yet to be satisfied.
Any filters satisfied here will be removed so that Any filters satisfied here will be removed so that
the caller will know if any filters remain. the caller will know if any filters remain.
@ -181,7 +181,7 @@ class Base(object):
satisfy satisfy
""" """
column_attr = getattr(model, filter['name']) column_attr = getattr(model, filter_['name'])
# TODO(henry-nash): Sqlalchemy 0.7 defaults to case insensitivity # TODO(henry-nash): Sqlalchemy 0.7 defaults to case insensitivity
# so once we find a way of changing that (maybe on a call-by-call # so once we find a way of changing that (maybe on a call-by-call
@ -189,28 +189,28 @@ class Base(object):
# the filters below. For now, these case sensitive versions will # the filters below. For now, these case sensitive versions will
# be handled at the controller level. # be handled at the controller level.
if filter['case_sensitive']: if filter_['case_sensitive']:
return query return query
if filter['comparator'] == 'contains': if filter_['comparator'] == 'contains':
query_term = column_attr.ilike('%%%s%%' % filter['value']) query_term = column_attr.ilike('%%%s%%' % filter_['value'])
elif filter['comparator'] == 'startswith': elif filter_['comparator'] == 'startswith':
query_term = column_attr.ilike('%s%%' % filter['value']) query_term = column_attr.ilike('%s%%' % filter_['value'])
elif filter['comparator'] == 'endswith': elif filter_['comparator'] == 'endswith':
query_term = column_attr.ilike('%%%s' % filter['value']) query_term = column_attr.ilike('%%%s' % filter_['value'])
else: else:
# It's a filter we don't understand, so let the caller # It's a filter we don't understand, so let the caller
# work out if they need to do something with it. # work out if they need to do something with it.
return query return query
hints.remove(filter) hints.remove(filter_)
return query.filter(query_term) return query.filter(query_term)
def exact_filter(model, filter, cumlative_filter_dict, hints): def exact_filter(model, filter_, cumlative_filter_dict, hints):
"""Applies an exact filter to a query. """Applies an exact filter to a query.
:param model: the table model in question :param model: the table model in question
:param filter: the dict that describes this filter :param filter_: the dict that describes this filter
:param cumlative_filter_dict: a dict that describes the set of :param cumlative_filter_dict: a dict that describes the set of
exact filters built up so far exact filters built up so far
:param hints: contains the list of filters yet to be satisfied. :param hints: contains the list of filters yet to be satisfied.
@ -220,23 +220,23 @@ class Base(object):
:returns cumlative_filter_dict: updated cumulative dict :returns cumlative_filter_dict: updated cumulative dict
""" """
key = filter['name'] key = filter_['name']
if isinstance(getattr(model, key).property.columns[0].type, if isinstance(getattr(model, key).property.columns[0].type,
sql.types.Boolean): sql.types.Boolean):
filter_dict[key] = utils.attr_as_boolean(filter['value']) filter_dict[key] = utils.attr_as_boolean(filter_['value'])
else: else:
filter_dict[key] = filter['value'] filter_dict[key] = filter_['value']
hints.remove(filter) hints.remove(filter_)
return filter_dict return filter_dict
filter_dict = {} filter_dict = {}
for filter in hints.filters(): for filter_ in hints.filters():
# TODO(henry-nash): Check if name is valid column, if not skip # TODO(henry-nash): Check if name is valid column, if not skip
if filter['comparator'] == 'equals': if filter_['comparator'] == 'equals':
filter_dict = exact_filter(model, filter, filter_dict, hints) filter_dict = exact_filter(model, filter_, filter_dict, hints)
else: else:
query = inexact_filter(model, query, filter, hints) query = inexact_filter(model, query, filter_, hints)
# Apply any exact filters we built up # Apply any exact filters we built up
if filter_dict: if filter_dict:
@ -244,7 +244,7 @@ class Base(object):
return query return query
def filter(self, model, query, hints): def filter_query(self, model, query, hints):
"""Applies filtering to a query. """Applies filtering to a query.
:param model: table model :param model: table model

View File

@ -123,7 +123,7 @@ class Identity(sql.Base, identity.Driver):
def list_users(self, hints): def list_users(self, hints):
session = db_session.get_session() session = db_session.get_session()
query = session.query(User) query = session.query(User)
user_refs = self.filter(User, query, hints) user_refs = self.filter_query(User, query, hints)
return [identity.filter_user(x.to_dict()) for x in user_refs] return [identity.filter_user(x.to_dict()) for x in user_refs]
def _get_user(self, session, user_id): def _get_user(self, session, user_id):
@ -255,7 +255,7 @@ class Identity(sql.Base, identity.Driver):
def list_groups(self, hints): def list_groups(self, hints):
session = db_session.get_session() session = db_session.get_session()
query = session.query(Group) query = session.query(Group)
refs = self.filter(Group, query, hints) refs = self.filter_query(Group, query, hints)
return [ref.to_dict() for ref in refs] return [ref.to_dict() for ref in refs]
def _get_group(self, session, group_id): def _get_group(self, session, group_id):