From bbca4d88ea60507f0e61d8a4d5e2cce80e20bb62 Mon Sep 17 00:00:00 2001 From: "Phyks (Lucas Verney)" Date: Thu, 18 Oct 2018 09:39:53 +0200 Subject: [PATCH] Handle value conversion in filtering API --- doc/20.api.md | 4 ++++ server/jsonapi.py | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/doc/20.api.md b/doc/20.api.md index f1f0ca2..e3bec43 100644 --- a/doc/20.api.md +++ b/doc/20.api.md @@ -42,6 +42,10 @@ You can filter on a given field value using the parameter value 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 All provided filters must be filled for an item to be returned. That is, if diff --git a/server/jsonapi.py b/server/jsonapi.py index 010997d..97119f1 100644 --- a/server/jsonapi.py +++ b/server/jsonapi.py @@ -9,6 +9,7 @@ import json import re import bottle +import peewee 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: continue field = getattr(model, filter_match.group(1)) + 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 operation = filter_match.group(3) if operation is None: