Skip to content

Commit

Permalink
break up filter_query into more parts so that it is easier to add cus…
Browse files Browse the repository at this point in the history
…tom logic
  • Loading branch information
danpoland committed Sep 14, 2017
1 parent 96f5895 commit b6d3302
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions pyramid_restful/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,21 @@ def parse_query_string(self, params):
return results

def filter_query(self, request, query, view):
if not request.params:
return query

querystring_params = self.parse_query_string(request.params)
query, filter_list = self.build_filter_list(querystring_params, query, view)

return self.apply_filter(query, filter_list)

def build_filter_list(self, querystring_params, query, view):
filterable_fields = getattr(view, self.view_attribute_name, None)

if not filterable_fields or not request.params:
return query
if not filterable_fields:
return query, []

filter_list = []
querystring_params = self.parse_query_string(request.params)
available_fields = list(map(lambda x: '{}.{}'.format(x.parent.class_.__name__, x.name), filterable_fields))

for key, val in querystring_params.items():
Expand Down Expand Up @@ -89,9 +97,7 @@ def filter_query(self, request, query, view):

filter_list.append(self.build_comparision(filterable_fields[i], val))

query = self.apply_filter(query, filter_list)

return query
return query, filter_list

def apply_filter(self, query, filter_list):
"""
Expand Down

0 comments on commit b6d3302

Please sign in to comment.