Skip to content

Commit bbca4d8

Browse files
committed
Handle value conversion in filtering API
1 parent 4345c3f commit bbca4d8

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

doc/20.api.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ You can filter on a given field value using the parameter value
4242

4343
will return the reports with `id` 1.
4444

45+
_Note:_ for fields representing a date(time), the filtering value should be
46+
encoded according to ISO 8601.
47+
48+
4549
#### Combining filters
4650

4751
All provided filters must be filled for an item to be returned. That is, if

server/jsonapi.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import re
1010

1111
import bottle
12+
import peewee
1213

1314
FILTER_RE = re.compile(r"filter\[([A-z0-9_]+?)\](\[([A-z0-9_]+\??)\])?")
1415

@@ -73,7 +74,15 @@ def JsonApiParseQuery(query, model, default_sorting=None):
7374
if not filter_match:
7475
continue
7576
field = getattr(model, filter_match.group(1))
77+
7678
for value in query.getall(param):
79+
if isinstance(field, peewee.DateTimeField):
80+
value = arrow.get(value).naive
81+
elif isinstance(field, peewee.DoubleField):
82+
value = float(value)
83+
elif isinstance(field, peewee.IntegerField):
84+
value = int(value)
85+
7786
# Handle operation
7887
operation = filter_match.group(3)
7988
if operation is None:

0 commit comments

Comments
 (0)