Features: 1) Add populate_index function for Elasticsearch indexing and integrate it into update_products; 2) Enhance CategoryViewSet queryset with prefetching of "parent" and "children";

Fixes: 1) Refine logic in vendor category and brand handling to correctly handle multiple active/inactive entries;

Extra: 1) Add missing import for `registry` in Elasticsearch module; 2) Minor cleanup and spacing adjustments in Elasticsearch and vendor modules.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-05-12 20:22:01 +03:00
parent c9d2881f99
commit ae17cc1371
4 changed files with 17 additions and 3 deletions

View file

@ -3,6 +3,7 @@ from django.http import Http404
from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _
from django_elasticsearch_dsl import fields
from django_elasticsearch_dsl.registries import registry
from elasticsearch import NotFoundError
from elasticsearch.dsl import Q, Search
@ -184,3 +185,9 @@ def _add_multilang_fields(cls):
),
)
setattr(cls, f"prepare_{desc_field}", make_prepare(desc_field))
def populate_index():
for doc in registry.get_documents(set(registry.get_models())):
qs = doc().get_indexing_queryset()
doc().update(qs, parallel=True, refresh=True)

View file

@ -11,6 +11,7 @@ from celery.utils.log import get_task_logger
from constance import config
from django.core.cache import cache
from core.elasticsearch import populate_index
from core.models import Product, Promotion
from core.utils.caching import set_default_cache
from core.vendors import delete_stale
@ -38,6 +39,7 @@ def update_products_task():
if not update_products_task_running:
cache.set("update_products_task_running", True, 86400)
populate_index()
vendors_classes = []
for vendor_class in vendors_classes:
@ -48,6 +50,7 @@ def update_products_task():
logger.warning(f"Skipping {vendor_class} due to error: {e!s}")
delete_stale()
populate_index()
cache.delete("update_products_task_running")

View file

@ -114,8 +114,10 @@ class AbstractVendor:
categories = Category.objects.filter(name=category_name)
if not categories.exists():
return Category.objects.create(name=category_name, is_active=False)
elif categories.count() > 1:
elif categories.filter(is_active=True).count() > 1:
categories = categories.filter(is_active=True)
elif categories.filter(is_active=False).count() > 1:
categories = categories.filter(is_active=False)
chosen = categories.first()
categories.exclude(uuid=chosen.uuid)
categories.delete()
@ -137,8 +139,10 @@ class AbstractVendor:
brands = Brand.objects.filter(name=brand_name)
if not brands.exists():
return Brand.objects.create(name=brand_name, is_active=False)
elif brands.count() > 1:
elif brands.filter(is_active=True).count() > 1:
brands = brands.filter(is_active=True)
elif brands.filter(is_active=False).count() > 1:
brands = brands.filter(is_active=False)
chosen = brands.first()
brands.exclude(uuid=chosen.uuid)
brands.delete()

View file

@ -127,7 +127,7 @@ class AttributeValueViewSet(EvibesViewSet):
@extend_schema_view(**CATEGORY_SCHEMA)
class CategoryViewSet(EvibesViewSet):
queryset = Category.objects.all().prefetch_related()
queryset = Category.objects.all().prefetch_related("parent", "children")
filter_backends = [DjangoFilterBackend]
filterset_class = CategoryFilter
serializer_class = CategoryDetailSerializer