schon/core/management/commands/rebuild_slugs.py
Egor fureunoir Gorbunov b15df3b72a Features: 1) Add is_digital field to ProductAdmin for filtering and display; 2) Extend admin search fields to include slug for products; 3) Introduce slug field in readonly and form layout sections for products.
Fixes: 1) Add `# type: ignore` comments for ForeignKey fields to resolve type-checking warnings in models.

Extra: 1) Refactor admin field formatting for better readability; 2) Add `# noinspection PyProtectedMember` annotation in `rebuild_slugs` management command; 3) Clean up spacing and alignment in various files.
2025-06-18 23:41:28 +03:00

54 lines
1.9 KiB
Python

from django.core.management.base import BaseCommand
from django.db import transaction
from django.utils.crypto import get_random_string
from core.models import Brand, Category, Product
# noinspection PyProtectedMember
class Command(BaseCommand):
help = "Rebuild slug field for all slugified instances"
def reset_em(self, queryset):
total = queryset.count()
self.stdout.write(
f"Starting slug rebuilding for {total} {queryset.model._meta.verbose_name_plural}"
)
for idx, instance in enumerate(queryset.iterator(), start=1):
try:
while (
queryset.filter(name=instance.name)
.exclude(uuid=instance.uuid)
.count()
>= 1
):
instance.name = f"{instance.name} - {get_random_string(length=3, allowed_chars='0123456789')}"
instance.save()
instance.slug = None
with transaction.atomic():
instance.save(update_fields=["slug"])
self.stdout.write(
self.style.SUCCESS(
f"[{idx}/{total}] ({queryset.model._meta.verbose_name_plural} UUID:"
f" {instance.pk}) slug set to '{instance.slug}'"
)
)
except Exception as e:
self.stderr.write(
self.style.ERROR(
f"[{idx}/{total}] ({queryset.model._meta.verbose_name_plural}: {instance.name}/{instance.uuid})"
f" ERROR: {e}"
)
)
def handle(self, *args, **options):
for queryset in [
Brand.objects.all(),
Category.objects.all(),
Product.objects.all(),
]:
self.reset_em(queryset)
self.stdout.write(self.style.SUCCESS("Slug rebuild complete."))