Features: 1) Add digital asset file validation before updating order product status; 2) Refactor queryset logic in document class to improve readability;

Fixes: 1) Remove unused serializer import in signals; 2) Fix potential issue with empty digital assets causing errors;

Extra: 1) Clean up logging statements in order processing; 2) Minor formatting and indentation improvements for clarity;
This commit is contained in:
Egor Pavlovich Gorbunov 2025-08-22 00:03:48 +03:00
parent b282706bd2
commit b3a0fbef56
2 changed files with 15 additions and 9 deletions

View file

@ -80,11 +80,15 @@ class ProductDocument(ActiveOnlyMixin, BaseDocument):
)
def get_queryset(self):
return super().get_queryset().filter(
return (
super()
.get_queryset()
.filter(
brand__is_active=True,
category__is_active=True,
stocks__vendor__is_active=True,
)
)
class Index(BaseDocument.Index):
name = "products"

View file

@ -11,7 +11,6 @@ from django.utils.translation import gettext_lazy as _
from sentry_sdk import capture_exception
from core.models import Category, Order, Product, PromoCode, Wishlist, DigitalAssetDownload
from core.serializers import OrderProductSimpleSerializer
from core.utils import (
generate_human_readable_id,
resolve_translations_for_elasticsearch,
@ -89,16 +88,20 @@ def process_order_changes(instance, created, **_kwargs):
break
if instance.status in ["CREATED", "PAYMENT"]:
logger.debug(
"Processing order changes: %s\nWith orderproducts: %s",
str(instance.__dict__),
str(OrderProductSimpleSerializer(instance.order_products.all(), many=True).data),
)
if not instance.is_whole_digital:
send_order_created_email.delay(instance.uuid)
for order_product in instance.order_products.filter(status="DELIVERING", product__is_digital=True):
if order_product.product.stocks.filter(digital_asset__isnull=False).exists():
stocks_qs = order_product.product.stocks.filter(digital_asset__isnull=False).exclude(digital_asset="")
stock = stocks_qs.first()
has_file = False
if stock:
f = stock.digital_asset
has_file = bool(getattr(f, "name", "")) and f.storage.exists(f.name)
if has_file:
order_product.status = "FINISHED"
download = DigitalAssetDownload.objects.create(order_product=order_product)
order_product.download = download
@ -107,7 +110,6 @@ def process_order_changes(instance, created, **_kwargs):
order_product.order.user.payments_balance.save()
continue
try:
logger.debug("Trying to buy: %s", str(order_product.uuid))
vendor_name = (
order_product.product.stocks.filter(price=order_product.buy_price).first().vendor.name.lower()
)