Handle value conversion in filtering API

This commit is contained in:
Lucas Verney 2018-10-18 09:39:53 +02:00
parent 4345c3f4e3
commit bbca4d88ea
2 changed files with 13 additions and 0 deletions

View File

@ -42,6 +42,10 @@ You can filter on a given field value using the parameter value
will return the reports with `id` 1. will return the reports with `id` 1.
_Note:_ for fields representing a date(time), the filtering value should be
encoded according to ISO 8601.
#### Combining filters #### Combining filters
All provided filters must be filled for an item to be returned. That is, if All provided filters must be filled for an item to be returned. That is, if

View File

@ -9,6 +9,7 @@ import json
import re import re
import bottle import bottle
import peewee
FILTER_RE = re.compile(r"filter\[([A-z0-9_]+?)\](\[([A-z0-9_]+\??)\])?") FILTER_RE = re.compile(r"filter\[([A-z0-9_]+?)\](\[([A-z0-9_]+\??)\])?")
@ -73,7 +74,15 @@ def JsonApiParseQuery(query, model, default_sorting=None):
if not filter_match: if not filter_match:
continue continue
field = getattr(model, filter_match.group(1)) field = getattr(model, filter_match.group(1))
for value in query.getall(param): for value in query.getall(param):
if isinstance(field, peewee.DateTimeField):
value = arrow.get(value).naive
elif isinstance(field, peewee.DoubleField):
value = float(value)
elif isinstance(field, peewee.IntegerField):
value = int(value)
# Handle operation # Handle operation
operation = filter_match.group(3) operation = filter_match.group(3)
if operation is None: if operation is None: