Nicely fix search query being always a string

This commit is contained in:
Xavier Bustamante Talavera 2018-11-11 22:18:10 +01:00
parent bcf59de383
commit 048a03801b
2 changed files with 16 additions and 6 deletions

12
ereuse_devicehub/query.py Normal file
View File

@ -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

View File

@ -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