diff --git a/app/search/api/serializers.py b/app/search/api/serializers.py index ce144f8..acc6ac5 100644 --- a/app/search/api/serializers.py +++ b/app/search/api/serializers.py @@ -58,6 +58,7 @@ class HintResponseSerializer(serializers.Serializer): class AutoCompleteRequestSerializer(serializers.Serializer): content = serializers.CharField(validators=[MinLengthValidator(3)]) + exclude = serializers.ListSerializer(child=QueryFilterSerializer()) def create(self, validated_data): raise NotImplementedError diff --git a/app/search/api/views.py b/app/search/api/views.py index af6b407..0b4d574 100644 --- a/app/search/api/views.py +++ b/app/search/api/views.py @@ -64,6 +64,6 @@ class AutoCompleteApi(APIView): serializer = AutoCompleteRequestSerializer(data=request.data) serializer.is_valid(raise_exception=True) return Response( - {"nodes": autocomplete_schema(serializer.data["content"])}, + {"nodes": autocomplete_schema(serializer.data["content"], serializer.data["exclude"])}, status=status.HTTP_200_OK, ) diff --git a/app/search/services/autocomplete_schema.py b/app/search/services/autocomplete_schema.py index c18deee..d0568ee 100644 --- a/app/search/services/autocomplete_schema.py +++ b/app/search/services/autocomplete_schema.py @@ -1,30 +1,40 @@ +from typing import List, Dict + from search.models import Product, Category, Characteristic -def autocomplete_schema(val: str): - schema = [ - { - "coordinate": product["name"].lower().index(val.lower()), - "value": { - "type": "Name", - "value": product["name"], - }, - } - for product in Product.objects.filter(name__unaccent__icontains=val).values( - "name" +def autocomplete_schema(val: str, exclude: List[Dict]): + exclude = [dict(x) for x in exclude] + name_exclude = [x["value"] for x in exclude if x["type"] == "Name"] + category_exclude = [x["value"] for x in exclude if x["type"] == "Category"] + schema = [] + if not name_exclude: + schema.extend( + [ + { + "coordinate": product["name"].lower().index(val.lower()), + "value": { + "type": "Name", + "value": product["name"], + }, + } + for product in Product.objects.filter( + name__unaccent__icontains=val + ).values("name") + ] + ) + if not category_exclude: + schema.extend( + [ + { + "coordinate": cat["name"].lower().index(val.lower()), + "value": {"type": "Category", "value": cat["name"]}, + } + for cat in Category.objects.filter( + name__unaccent__icontains=val + ).values("name") + ] ) - ] - schema.extend( - [ - { - "coordinate": cat["name"].lower().index(val.lower()), - "value": {"type": "Category", "value": cat["name"]}, - } - for cat in Category.objects.filter(name__unaccent__icontains=val).values( - "name" - ) - ] - ) schema.extend( [ {