From ef89c81fb199f2791f607e4c42754ec975f456fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Kl=C3=BCnder?= Date: Fri, 12 May 2017 13:49:42 +0200 Subject: [PATCH] add search to location API --- src/c3nav/mapdata/api.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/c3nav/mapdata/api.py b/src/c3nav/mapdata/api.py index eda47106..fab5d9ea 100644 --- a/src/c3nav/mapdata/api.py +++ b/src/c3nav/mapdata/api.py @@ -168,6 +168,21 @@ class LocationViewSet(RetrieveModelMixin, GenericViewSet): def redirects(self, request): return Response([obj.serialize(include_type=False) for obj in LocationRedirect.objects.all().order_by('id')]) + @list_route(methods=['get']) + def search(self, request): + # todo: implement caching here + results = sorted(chain(*(model.objects.filter(can_search=True) + for model in LOCATION_MODELS)), key=lambda obj: obj.id) + search = request.GET.get('s') + if not search: + return Response([obj.serialize(include_type=True, detailed='detailed' in request.GET) for obj in results]) + + words = search.lower().split(' ')[:10] + for word in words: + results = [r for r in results if (word in r.title.lower() or (r.slug and word in r.slug.lower()))] + # todo: rank results + return Response([obj.serialize(include_type=True, detailed='detailed' in request.GET) for obj in results]) + class SourceViewSet(MapdataViewSet): queryset = Source.objects.all()