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.
53 lines
2.2 KiB
53 lines
2.2 KiB
# backend/apps/hadis/management/commands/seed_interpretation_books.py
|
|
|
|
from django.core.management.base import BaseCommand
|
|
from django.db import transaction
|
|
from apps.hadis.models import HadisInterpretation, InterpretationReference, BookReference
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Assigns a default BookReference to HadisInterpretations that lack one.'
|
|
|
|
def handle(self, *args, **options):
|
|
self.stdout.write(self.style.WARNING("Scanning for interpretations without book references..."))
|
|
|
|
# پیدا کردن تفسیرهایی که هیچ رفرنسی ندارند
|
|
interpretations_without_book = HadisInterpretation.objects.filter(references__isnull=True)
|
|
count = interpretations_without_book.count()
|
|
|
|
if count == 0:
|
|
self.stdout.write(self.style.SUCCESS("✅ All interpretations already have a book reference."))
|
|
return
|
|
|
|
# پیدا کردن اولین کتاب موجود در دیتابیس برای اتصال
|
|
book = BookReference.objects.first()
|
|
|
|
# اگر هیچ کتابی در سیستم ثبت نشده بود، یک کتاب پیشفرض میسازیم
|
|
if not book:
|
|
import time
|
|
book = BookReference.objects.create(
|
|
title=[{"language_code": "en", "text": "Default Source Book"}],
|
|
slug=f"default-book-{int(time.time())}"
|
|
)
|
|
self.stdout.write(self.style.WARNING("No BookReference found. Created a default dummy book."))
|
|
|
|
self.stdout.write(self.style.WARNING(f"Linking {count} interpretations to book ID: {book.id}..."))
|
|
|
|
new_refs = []
|
|
for interp in interpretations_without_book:
|
|
new_refs.append(
|
|
InterpretationReference(
|
|
interpretation=interp,
|
|
book_reference=book,
|
|
volume="",
|
|
pages="",
|
|
address=""
|
|
)
|
|
)
|
|
|
|
# ساخت گروهی در دیتابیس
|
|
with transaction.atomic():
|
|
InterpretationReference.objects.bulk_create(new_refs, batch_size=500)
|
|
|
|
self.stdout.write(
|
|
self.style.SUCCESS(f"🎉 Successfully linked {len(new_refs)} interpretations to the book!")
|
|
)
|