|
| 1 | +from drf_spectacular.utils import extend_schema |
| 2 | +from rest_framework.request import Request |
| 3 | +from rest_framework.response import Response |
| 4 | +from rest_framework.views import APIView |
| 5 | +from wagtail.api.v2.serializers import get_serializer_class |
| 6 | +from wagtail.models import Page |
| 7 | + |
| 8 | +from cms.topic.models import TopicPage |
| 9 | +from public_api.version_02.views.base import PUBLIC_API_TAG |
| 10 | + |
| 11 | + |
| 12 | +@extend_schema(tags=[PUBLIC_API_TAG]) |
| 13 | +class SearchView(APIView): |
| 14 | + """This endpoint provides search results and could in the future provide |
| 15 | + autocomplete suggestions etc. |
| 16 | +
|
| 17 | + """ |
| 18 | + |
| 19 | + def get(self, request: Request): |
| 20 | + search = request.GET.get("search") |
| 21 | + limit = int(request.GET.get("limit", 0)) |
| 22 | + fields = request.GET.get("fields", ["title", "slug"]) |
| 23 | + meta = request.GET.get("meta", ["id"]) |
| 24 | + topic_results = TopicPage.objects.all().search(search) |
| 25 | + if not limit or topic_results.count() < limit: |
| 26 | + # TODO: go get more |
| 27 | + # results = queryset |
| 28 | + results = topic_results |
| 29 | + else: |
| 30 | + results = topic_results[0:limit] |
| 31 | + print(f"AIDAN: returning {results}") |
| 32 | + serialized = get_serializer_class(Page, fields, meta)(results, many=True) |
| 33 | + return Response(serialized.data) |
0 commit comments