Skip to content

Commit

Permalink
Add ability to query API for certain years (#4)
Browse files Browse the repository at this point in the history
* Add explicitly-handled start_year and end_year args

* Organize year range handling into own function, add more value checks
  • Loading branch information
bleonard33 authored and makmanalp committed Jun 14, 2018
1 parent 703c6e4 commit 425dcaa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
24 changes: 24 additions & 0 deletions atlas_core/query_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,33 @@ def request_to_query(request):
}

query["arguments"] = arguments
query['year_range'] = handle_year_range(request)

return query


def handle_year_range(request):
start_year = request.args.get("start_year", None)
end_year = request.args.get("end_year", None)

if start_year and not start_year.isdigit():
msg = "Start year ({}) is not integer."\
.format(start_year)
abort(400, message=msg)
if end_year and not end_year.isdigit():
msg = "End year ({}) is not integer."\
.format(end_year)
abort(400, message=msg)

if start_year and end_year:
if start_year > end_year:
msg = "Start year specified ({}) must be less than or equal to end year ({})."\
.format(start_year, end_year)
abort(400, message=msg)

return {"start": start_year, "end": end_year}


def get_or_fail(name, dictionary):
"""Lookup a key in a dict, abort with helpful error on failure."""
thing = dictionary.get(name, None)
Expand Down
12 changes: 11 additions & 1 deletion atlas_core/slice_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ def fetch(self, slice_def, query, json=True):
level_predicate = (level_column == query["result"]["level"])
filter_predicates.append(level_predicate)

# Filter by year ranges
year_column = self.get_column_by_name('year')

if query['year_range']['start']:
start_predicate = (year_column >= query['year_range']['start'])
filter_predicates.append(start_predicate)

if query['year_range']['end']:
end_predicate = (year_column <= query['year_range']['end'])
filter_predicates.append(end_predicate)

q = list(db.session.query(*self.get_all_model_columns()).filter(*filter_predicates).all())

return lima.marshal(self.schema, q, json=json)
Expand All @@ -66,4 +77,3 @@ def __init__(self, df, schema=None):

def fetch(self, slice_def, query):
raise NotImplementedError()

0 comments on commit 425dcaa

Please sign in to comment.