added exclude autocomplete

This commit is contained in:
Alexander Karpov 2022-10-22 13:11:30 +03:00
parent b301b8103c
commit c68dfce10a
3 changed files with 35 additions and 24 deletions

View File

@ -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

View File

@ -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,
)

View File

@ -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(
[
{