You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

27 lines
1.1 KiB

# backend/apps/hadis/management/commands/make_books_independent.py
import random
from django.core.management.base import BaseCommand
from django.db import transaction
from apps.hadis.models import BookReference, BookEdition
class Command(BaseCommand):
help = 'Makes 50% of BookReferences independent by deleting their BookEditions.'
def handle(self, *args, **options):
all_books = list(BookReference.objects.all())
if not all_books:
self.stdout.write(self.style.ERROR("No books found."))
return
# انتخاب ۵۰ درصد رندوم
num_to_detach = len(all_books) // 2
books_to_detach = random.sample(all_books, num_to_detach)
self.stdout.write(self.style.WARNING(f"Deleting editions from {num_to_detach} books..."))
with transaction.atomic():
# حذف تمام ادیشن‌های مرتبط با این ۵۰ درصد کتاب
BookEdition.objects.filter(book_reference__in=books_to_detach).delete()
self.stdout.write(self.style.SUCCESS(f"✅ {num_to_detach} books are now independent (no editions)."))