Features: 1) Add management command to delete products by description in batches;

Fixes: 1) Correct indentation in multiple locations across `viewsets.py` for better readability; 2) Resolve inconsistent logging formatting in `signals.py`;

Extra: 1) Minor formatting adjustments in `views.py` and `signals.py`; 2) Clean up trailing blank lines across files.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-07-09 03:13:38 +03:00
parent a1db34d1c9
commit 4160e29455
4 changed files with 48 additions and 11 deletions

View file

@ -0,0 +1,35 @@
from django.core.management.base import BaseCommand
from core.models import AttributeValue, Product, ProductImage
class Command(BaseCommand):
help = "Delete Product rows with special description, in batches"
def add_arguments(self, parser):
parser.add_argument(
"-s",
"--size",
required=False,
default=5000,
help="Chunk size to delete",
)
def handle(self, *args, **options):
size = options["size"]
while True:
batch_ids = list(
Product.objects.filter(description__iexact="EVIBES_DELETED_PRODUCT").values_list("pk", flat=True)[:size]
)
if not batch_ids:
break
try:
AttributeValue.objects.filter(product_id__in=batch_ids).delete()
ProductImage.objects.filter(product_id__in=batch_ids).delete()
Product.objects.filter(pk__in=batch_ids).delete()
except Exception as e:
self.stdout.write("Couldn't delete some of the products(will retry later): %s" % str(e))
continue
self.stdout.write(f"Deleted {len(batch_ids)} products…")
self.stdout.write("✅ All unordered products removed.")

View file

@ -86,9 +86,11 @@ 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))
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)

View file

@ -74,6 +74,7 @@ def sitemap_index(request, *args, **kwargs):
response["Content-Type"] = "application/xml; charset=utf-8"
return response
@cache_page(60 * 60 * 24)
@vary_on_headers("Host")
def sitemap_detail(request, *args, **kwargs):

View file

@ -316,12 +316,11 @@ class BrandViewSet(EvibesViewSet):
else:
queryset = queryset.prefetch_related(
Prefetch("categories", queryset=Category.objects.filter(is_active=True))
)
)
return queryset
@extend_schema_view(**PRODUCT_SCHEMA)
class ProductViewSet(EvibesViewSet):
"""
@ -363,12 +362,12 @@ class ProductViewSet(EvibesViewSet):
if self.request.user.has_perm("core.view_product"):
return qs
return qs.filter(
is_active=True,
brand__is_active=True,
category__is_active=True,
stocks__isnull=False,
stocks__vendor__is_active=True,
)
is_active=True,
brand__is_active=True,
category__is_active=True,
stocks__isnull=False,
stocks__vendor__is_active=True,
)
def get_object(self):
queryset = self.filter_queryset(self.get_queryset())