From 048a03801bca43ed00e0b57e5426934344bdf7f7 Mon Sep 17 00:00:00 2001 From: Xavier Bustamante Talavera Date: Sun, 11 Nov 2018 22:18:10 +0100 Subject: [PATCH] Nicely fix search query being always a string --- ereuse_devicehub/query.py | 12 ++++++++++++ ereuse_devicehub/resources/device/views.py | 10 ++++------ 2 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 ereuse_devicehub/query.py diff --git a/ereuse_devicehub/query.py b/ereuse_devicehub/query.py new file mode 100644 index 00000000..463fc0b2 --- /dev/null +++ b/ereuse_devicehub/query.py @@ -0,0 +1,12 @@ +from teal.query import NestedQueryFlaskParser +from webargs.flaskparser import FlaskParser + + +class SearchQueryParser(NestedQueryFlaskParser): + + def parse_querystring(self, req, name, field): + if name == 'search': + v = FlaskParser.parse_querystring(self, req, name, field) + else: + v = super().parse_querystring(req, name, field) + return v diff --git a/ereuse_devicehub/resources/device/views.py b/ereuse_devicehub/resources/device/views.py index 7706a1c2..c13ae11a 100644 --- a/ereuse_devicehub/resources/device/views.py +++ b/ereuse_devicehub/resources/device/views.py @@ -11,6 +11,7 @@ from teal.resource import View from ereuse_devicehub import auth from ereuse_devicehub.db import db +from ereuse_devicehub.query import SearchQueryParser from ereuse_devicehub.resources import search from ereuse_devicehub.resources.device.models import Device, Manufacturer from ereuse_devicehub.resources.device.search import DeviceSearch @@ -63,8 +64,10 @@ class Sorting(query.Sort): class DeviceView(View): + QUERY_PARSER = SearchQueryParser() + class FindArgs(marshmallow.Schema): - search = f.Raw() + search = f.Str() filter = f.Nested(Filters, missing=[]) sort = f.Nested(Sorting, missing=[Device.id.asc()]) page = f.Integer(validate=v.Range(min=1), missing=1) @@ -90,11 +93,6 @@ class DeviceView(View): args = self.QUERY_PARSER.parse(self.find_args, request, locations=('querystring',)) - # todo not-nice way of de-parsing what webargs parser - # does when sees that an argument is like an int, etc - # when solving this, change too the Query.search to Str - if args.get('search', False): - args['search'] = str(args['search']) response = self.find(args) return response