mirror of
https://github.com/magnum-opus-tender-hack/backend.git
synced 2024-11-22 09:16:36 +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):
|
||||
content = serializers.CharField(validators=[MinLengthValidator(3)])
|
||||
exclude = serializers.ListSerializer(child=QueryFilterSerializer())
|
||||
|
||||
def create(self, validated_data):
|
||||
raise NotImplementedError
|
||||
|
|
|
@ -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,
|
||||
)
|
||||
|
|
|
@ -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(
|
||||
[
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue
Block a user