Features: 1) Add overwrite=True and max_length=88 to slugs in Brand, Category, and Product models with enhanced slug generation logic; 2) Extend process_query function to include additional fields rating and brand_priority for product results.

Fixes: 1) Add missing type annotation for the return value of `process_query` in `core/elasticsearch/__init__.py`.

Extra: 1) Update migration file to reflect new slug field changes; 2) Minor refactor to improve clarity in query result construction.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-06-21 20:19:08 +03:00
parent 4b367944b9
commit 67794a7997
3 changed files with 78 additions and 9 deletions

View file

@ -33,7 +33,9 @@ SMART_FIELDS = [
]
def process_query(query: str = "", request: Request | None = None):
def process_query(
query: str = "", request: Request | None = None
) -> dict[str, list[dict]] | None:
"""
Perform a lenient, typotolerant, multiindex search.
@ -120,14 +122,18 @@ def process_query(query: str = "", request: Request | None = None):
if cat.image:
image_url = request.build_absolute_uri(cat.image.url)
results[idx].append(
{
"uuid": str(obj_uuid),
"name": obj_name,
"slug": obj_slug,
"image": image_url,
}
)
hit_result = {
"uuid": str(obj_uuid),
"name": obj_name,
"slug": obj_slug,
"image": image_url,
}
if idx == "products":
hit_result["rating"] = getattr(hit, "rating", 0)
hit_result["brand_priority"] = getattr(hit, "brand_priority", 0)
results[idx].append(hit_result)
return results
except NotFoundError:

View file

@ -0,0 +1,57 @@
# Generated by Django 5.2 on 2025-06-21 17:14
import django_extensions.db.fields
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("core", "0031_alter_product_slug"),
]
operations = [
migrations.AlterField(
model_name="brand",
name="slug",
field=django_extensions.db.fields.AutoSlugField(
allow_unicode=True,
blank=True,
editable=False,
max_length=88,
null=True,
overwrite=True,
populate_from=("name",),
unique=True,
verbose_name="brand slug",
),
),
migrations.AlterField(
model_name="category",
name="slug",
field=django_extensions.db.fields.AutoSlugField(
allow_unicode=True,
blank=True,
editable=False,
max_length=88,
null=True,
overwrite=True,
populate_from=("uuid", "name"),
unique=True,
),
),
migrations.AlterField(
model_name="product",
name="slug",
field=django_extensions.db.fields.AutoSlugField(
allow_unicode=True,
blank=True,
editable=False,
max_length=88,
null=True,
overwrite=True,
populate_from=("name", "brand__slug", "category__slug", "uuid"),
unique=True,
),
),
]

View file

@ -221,6 +221,8 @@ class Category(ExportModelOperationsMixin("category"), NiceModel, MPTTModel):
allow_unicode=True,
unique=True,
editable=False,
max_length=88,
overwrite=True,
null=True,
)
tags: CategoryTag = ManyToManyField( # type: ignore
@ -289,6 +291,8 @@ class Brand(ExportModelOperationsMixin("brand"), NiceModel):
allow_unicode=True,
unique=True,
editable=False,
max_length=88,
overwrite=True,
null=True,
verbose_name=_("brand slug"),
)
@ -364,6 +368,8 @@ class Product(ExportModelOperationsMixin("product"), NiceModel):
"category__slug",
"uuid",
),
max_length=88,
overwrite=True,
allow_unicode=True,
unique=True,
editable=False,