backend/app/search/services/autocomplete_schema.py

40 lines
1.1 KiB
Python
Raw Normal View History

2022-10-22 03:09:14 +03:00
from search.models import Product, Category, Characteristic
2022-10-22 05:07:25 +03:00
2022-10-22 03:09:14 +03:00
def autocomplete_schema(val: str):
2022-10-22 12:29:51 +03:00
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"
)
]
2022-10-22 03:09:14 +03:00
schema.extend(
[
{
2022-10-22 12:29:51 +03:00
"coordinate": cat["name"].lower().index(val.lower()),
2022-10-22 05:07:25 +03:00
"value": {"type": "Category", "value": cat["name"]},
}
2022-10-22 12:29:51 +03:00
for cat in Category.objects.filter(name__unaccent__icontains=val).values(
"name"
)
2022-10-22 03:09:14 +03:00
]
)
schema.extend(
[
{
2022-10-22 12:29:51 +03:00
"coordinate": char["value"].lower().index(val.lower()),
"value": {"type": char["name"], "value": char["value"]},
2022-10-22 05:07:25 +03:00
}
2022-10-22 12:29:51 +03:00
for char in Characteristic.objects.filter(
value__unaccent__icontains=val
).values("name", "value")
2022-10-22 03:09:14 +03:00
]
)
return schema