add hint schema

This commit is contained in:
ilia 2022-10-21 23:22:14 +03:00
parent dbd347d71e
commit 17e1c066a4
5 changed files with 35 additions and 6 deletions

View File

@ -1,7 +1,8 @@
from django.urls import path from django.urls import path
from search.api.views import SearchApi from search.api.views import SearchApi, HintApi
urlpatterns = [ urlpatterns = [
path("search", SearchApi.as_view(), name="search_api") path("search", SearchApi.as_view(), name="search_api"),
path("hint", HintApi.as_view(), name="hint api")
] ]

View File

@ -19,3 +19,15 @@ class ResponseSerializer(serializers.Serializer):
def update(self, instance, validated_data): def update(self, instance, validated_data):
raise NotImplemented raise NotImplemented
class HintRequestSerializer(serializers.Serializer):
content = serializers.CharField()
def create(self, validated_data):
raise NotImplemented
class HintResponseSerializer(serializers.Serializer):
type = serializers.CharField()
content = serializers.CharField()

View File

@ -3,12 +3,13 @@ from drf_yasg.utils import swagger_auto_schema
from rest_framework import status from rest_framework import status
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.views import APIView from rest_framework.views import APIView
from search.api.serializers import HintRequestSerializer
from search.api.serializers import SearchSerializer, ResponseSerializer from search.api.serializers import SearchSerializer, ResponseSerializer, HintResponseSerializer
from search.services.search import process_string from search.services.search import process_string
user_response = openapi.Response("search results", ResponseSerializer) user_response = openapi.Response("search results", ResponseSerializer)
hint_response = openapi.Response("hints", HintResponseSerializer)
class SearchApi(APIView): class SearchApi(APIView):
@swagger_auto_schema(request_body=SearchSerializer, responses={200: user_response}) @swagger_auto_schema(request_body=SearchSerializer, responses={200: user_response})
@ -18,3 +19,17 @@ class SearchApi(APIView):
return Response( return Response(
process_string(serializer.data["body"]), status=status.HTTP_200_OK process_string(serializer.data["body"]), status=status.HTTP_200_OK
) )
class HintApi(APIView):
@swagger_auto_schema(request_body=HintRequestSerializer, responses={200: hint_response})
def post(self, request, format=None):
serializer = HintRequestSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
return Response(
{
'type': 'category',
'value': serializer.data['content']
},
status=status.HTTP_200_OK
)

View File

@ -1,5 +1,6 @@
from search.models import Product from search.models import Product
from typing import List
def process_string(text: str) -> [dict]: def process_string(text: str) -> List[dict]:
return [x.serialize_self() for x in Product.objects.filter(name__contains=text)[5:]] return [x.serialize_self() for x in Product.objects.filter(name__contains=text)[5:]]

View File

@ -2,7 +2,7 @@ djangorestframework==3.14.0
Django==4.0.8 Django==4.0.8
django-cors-headers==3.13.0 django-cors-headers==3.13.0
django-environ==0.9.0 django-environ==0.9.0
"drf-yasg[validation]" drf-yasg[validation]
typing-extensions typing-extensions
Pillow==9.2.0 Pillow==9.2.0