From d371f01b3bd94b4c5c36214499c0d1d020a4a09c Mon Sep 17 00:00:00 2001 From: Fabian von Feilitzsch Date: Wed, 25 Nov 2015 10:50:34 -0500 Subject: [PATCH 1/2] serialize json correctly, allow query string searching --- api/webview/views.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/api/webview/views.py b/api/webview/views.py index d876a24f..bc077f54 100644 --- a/api/webview/views.py +++ b/api/webview/views.py @@ -71,11 +71,25 @@ def status(request): return HttpResponse(json.dumps({'status': 'ok'}), content_type='application/json', status=200) -@api_view(['POST']) +@api_view(['GET', 'POST']) def institutions(request): if not es: return HttpResponse('No connection to elastic search', status=503) - query = request.data.get('query') or {} + if request.data.get('query') or request.query_params.get('q'): + query = request.data.get('query') or { + 'query': { + 'query_string': { + 'query': request.query_params.get('q') + } + } + } + else: + query = { + 'query': { + 'match_all': {} + } + } + es.indices.create(index='institutions', ignore=400) res = es.search(index=settings.ELASTIC_INST_INDEX, body=query) # validate query and grab whats wanted @@ -87,4 +101,4 @@ def institutions(request): } except IndexError: return Response('Invalid query', status=400) - return Response(json.dumps(res), status=200) + return Response(res, status=200) From 329da59fbf3cd0114fc4b3b0a17be2addb7d77fb Mon Sep 17 00:00:00 2001 From: Fabian von Feilitzsch Date: Wed, 25 Nov 2015 10:50:54 -0500 Subject: [PATCH 2/2] remove unnecessary encode to bytes step --- institutions/grid.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/institutions/grid.py b/institutions/grid.py index 097b2704..6d7ac47f 100644 --- a/institutions/grid.py +++ b/institutions/grid.py @@ -9,7 +9,7 @@ logger = logging.getLogger(__name__) schema = { - 'name': ('/name', lambda x: x.encode('utf8') if x else None), + 'name': ('/name'), 'location': { 'street_address': ('/addresses', lambda x: x[0]['line_1'] if x else None), 'city': ('/addresses', lambda x: x[0]['city'] if x else None), @@ -19,7 +19,7 @@ }, 'web_url': ('/links', lambda x: x[0] if x else None), 'id_': '/id', - 'other_names': ('/aliases', '/acronyms', lambda x, y: x + y if x and y else x if x else y if y else None) + 'other_names': ('/aliases', '/acronyms', lambda x, y: (x or []) + (y or [])) }