mirror of
https://github.com/magnum-opus-tender-hack/backend.git
synced 2025-07-10 16:12:17 +03:00
added exclude autocomplete
This commit is contained in:
parent
b301b8103c
commit
c68dfce10a
|
@ -58,6 +58,7 @@ class HintResponseSerializer(serializers.Serializer):
|
||||||
|
|
||||||
class AutoCompleteRequestSerializer(serializers.Serializer):
|
class AutoCompleteRequestSerializer(serializers.Serializer):
|
||||||
content = serializers.CharField(validators=[MinLengthValidator(3)])
|
content = serializers.CharField(validators=[MinLengthValidator(3)])
|
||||||
|
exclude = serializers.ListSerializer(child=QueryFilterSerializer())
|
||||||
|
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
|
@ -64,6 +64,6 @@ class AutoCompleteApi(APIView):
|
||||||
serializer = AutoCompleteRequestSerializer(data=request.data)
|
serializer = AutoCompleteRequestSerializer(data=request.data)
|
||||||
serializer.is_valid(raise_exception=True)
|
serializer.is_valid(raise_exception=True)
|
||||||
return Response(
|
return Response(
|
||||||
{"nodes": autocomplete_schema(serializer.data["content"])},
|
{"nodes": autocomplete_schema(serializer.data["content"], serializer.data["exclude"])},
|
||||||
status=status.HTTP_200_OK,
|
status=status.HTTP_200_OK,
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,30 +1,40 @@
|
||||||
|
from typing import List, Dict
|
||||||
|
|
||||||
from search.models import Product, Category, Characteristic
|
from search.models import Product, Category, Characteristic
|
||||||
|
|
||||||
|
|
||||||
def autocomplete_schema(val: str):
|
def autocomplete_schema(val: str, exclude: List[Dict]):
|
||||||
schema = [
|
exclude = [dict(x) for x in exclude]
|
||||||
{
|
name_exclude = [x["value"] for x in exclude if x["type"] == "Name"]
|
||||||
"coordinate": product["name"].lower().index(val.lower()),
|
category_exclude = [x["value"] for x in exclude if x["type"] == "Category"]
|
||||||
"value": {
|
schema = []
|
||||||
"type": "Name",
|
if not name_exclude:
|
||||||
"value": product["name"],
|
schema.extend(
|
||||||
},
|
[
|
||||||
}
|
{
|
||||||
for product in Product.objects.filter(name__unaccent__icontains=val).values(
|
"coordinate": product["name"].lower().index(val.lower()),
|
||||||
"name"
|
"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(
|
schema.extend(
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user