Skip to content

Commit

Permalink
#41: Extract include into function
Browse files Browse the repository at this point in the history
  • Loading branch information
keiranjprice101 committed Sep 11, 2019
1 parent 1b76f38 commit eebf29a
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions common/database_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,25 @@ def get_rows_by_filter(table, filters):
filter_handler.apply_filters(query)
results = query.get_all_results()
if query.include_related_entities:
for query_filter in filters:
if list(query_filter)[0].lower() == "include":
return list(map(lambda x: x.to_nested_dict(query_filter["include"]), results))
return _get_results_with_include(filters, results)
return list(map(lambda x: x.to_dict(), results))
finally:
query.session.close()


def _get_results_with_include(filters, results):
"""
Given a list of entities and a list of filters, use the include filter to nest the included entities requested in
the include filter given
:param filters: The list of filters
:param results: The list of entities
:return: A list of nested dictionaries representing the entity results
"""
for query_filter in filters:
if list(query_filter)[0].lower() == "include":
return [x.to_nested_dict(query_filter["include"]) for x in results]


def get_first_filtered_row(table, filters):
"""
returns the first row that matches a given filter, in a given table
Expand Down

0 comments on commit eebf29a

Please sign in to comment.