Skip to content

Commit

Permalink
#2: Add limit and order filters
Browse files Browse the repository at this point in the history
  • Loading branch information
keiranjprice101 committed Jun 24, 2019
1 parent c45075a commit 8f776cd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
16 changes: 10 additions & 6 deletions common/database_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,13 @@ def get_rows_by_filter(table, filters):
where_part = filter[key]
for k in where_part:
column = getattr(table, k.upper())
base_query = base_query.filter(column.in_([where_part[k]]))
elif list(filter)[0].lower() == "order":
field = filter["order"].split(" ")[0]
direction = filter["order"].split(" ")[1]
base_query = base_query.filter(column.in_([where_part[k]]), column.in_([where_part[k]]))

elif list(filter)[0].lower() == "order":
for key in filter:
field = filter[key].split(" ")[0]
direction = filter[key].split(" ")[1]
# Limit then order, or order then limit
if is_limited:
if direction.upper() == "ASC":
base_query = base_query.from_self().order_by(asc(getattr(table, field)))
Expand All @@ -148,19 +150,21 @@ def get_rows_by_filter(table, filters):
base_query = base_query.order_by(desc(getattr(table, field)))
else:
raise BadFilterError(f" Bad filter given, filter: {filter}")

elif list(filter)[0].lower() == "skip":
for key in filter:
skip = filter[key]
base_query = base_query.offset(skip)
elif list(filter)[0].lower() == "include":
base_query.include() # do something probably not .include

elif list(filter)[0].lower() == "limit":
is_limited = True
for key in filter:
limit = filter[key]
base_query = base_query.limit(limit)

else:
raise BadFilterError(f"Invalid filters provided recieved {filters}")

log.info(" Closing DB session")
session.close()
return list(map(lambda x: x.to_dict(), base_query.all()))
Expand Down
2 changes: 1 addition & 1 deletion common/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def wrapper_requires_session(*args, **kwargs):
log.info(" Consumer authenticated")
return method(*args, **kwargs)
else:
log.info(" Closing DB session")
log.info(" Could not authenticate consumer, closing DB session")
session.close()
return "Forbidden", 403
except AuthenticationError:
Expand Down
2 changes: 1 addition & 1 deletion test/test_base/base_rest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def tearDown(self):

def expect_status_code(self, expected_status_code, response):
"""
Asserts whethere the returned status code is equal to the expected
Asserts whether the returned status code is equal to the expected
:param expected_status_code: int: The status code that is expected
:param response: The response to be checked
"""
Expand Down

0 comments on commit 8f776cd

Please sign in to comment.