Browse Source

write and run scripts for populate references for corrections and original texts, and generate slugs for interpretations

master
Mohsen Taba 4 weeks ago
parent
commit
f4709a3837
  1. 41
      apps/hadis/management/commands/create_missing_correction_refs.py
  2. 41
      apps/hadis/management/commands/create_missing_original_text_refs.py
  3. 43
      apps/hadis/management/commands/sync_interpretation_slugs.py

41
apps/hadis/management/commands/create_missing_correction_refs.py

@ -0,0 +1,41 @@
# backend/apps/hadis/management/commands/create_missing_correction_refs.py
from django.core.management.base import BaseCommand
from django.db import transaction
from apps.hadis.models import HadisCorrection, CorrectionReference
class Command(BaseCommand):
help = 'Creates a blank CorrectionReference for HadisCorrections that do not have any reference linked.'
def handle(self, *args, **options):
self.stdout.write(self.style.WARNING("Checking for HadisCorrections without references..."))
# پیدا کردن تمام تصحیحاتی که هیچ رفرنسی به آن‌ها متصل نیست
missing_refs_corrections = HadisCorrection.objects.filter(references__isnull=True)
count = missing_refs_corrections.count()
if count == 0:
self.stdout.write(self.style.SUCCESS("All HadisCorrections already have at least one reference linked."))
return
self.stdout.write(self.style.WARNING(f"Found {count} corrections without references. Generating..."))
new_references = []
for correction in missing_refs_corrections:
# ساخت آبجکت رفرنس واسط متصل به تصحیح مربوطه
new_references.append(
CorrectionReference(
correction=correction,
volume="",
pages="",
address=""
)
)
# ثبت دسته‌ای در دیتابیس برای سرعت و کارایی بالا
with transaction.atomic():
CorrectionReference.objects.bulk_create(new_references, batch_size=500)
self.stdout.write(
self.style.SUCCESS(f"🎉 Successfully created and linked {len(new_references)} missing CorrectionReferences!")
)

41
apps/hadis/management/commands/create_missing_original_text_refs.py

@ -0,0 +1,41 @@
# backend/apps/hadis/management/commands/create_missing_original_text_refs.py
from django.core.management.base import BaseCommand
from django.db import transaction
from apps.hadis.models import TransmitterOriginalText, OriginalTextReference
class Command(BaseCommand):
help = 'Creates a blank OriginalTextReference for TransmitterOriginalTexts that do not have any reference linked.'
def handle(self, *args, **options):
self.stdout.write(self.style.WARNING("Checking for TransmitterOriginalTexts without references..."))
# پیدا کردن تمام متون اصلی راوی که هیچ رفرنسی به آن‌ها متصل نیست
missing_refs_texts = TransmitterOriginalText.objects.filter(references__isnull=True)
count = missing_refs_texts.count()
if count == 0:
self.stdout.write(self.style.SUCCESS("All TransmitterOriginalTexts already have at least one reference linked."))
return
self.stdout.write(self.style.WARNING(f"Found {count} original texts without references. Generating..."))
new_references = []
for original_text in missing_refs_texts:
# ساخت آبجکت رفرنس واسط متصل به متن اصلی مربوطه
new_references.append(
OriginalTextReference(
original_text=original_text,
volume="",
pages="",
address=""
)
)
# ثبت دسته‌ای در دیتابیس در یک تراکنش امن
with transaction.atomic():
OriginalTextReference.objects.bulk_create(new_references, batch_size=500)
self.stdout.write(
self.style.SUCCESS(f"🎉 Successfully created and linked {len(new_references)} missing OriginalTextReferences!")
)

43
apps/hadis/management/commands/sync_interpretation_slugs.py

@ -0,0 +1,43 @@
# backend/apps/hadis/management/commands/sync_interpretation_slugs.py
from django.core.management.base import BaseCommand
from django.db import transaction
from apps.hadis.models import HadisInterpretation
class Command(BaseCommand):
help = 'Syncs existing HadisInterpretation slugs with their Category slugs.'
def handle(self, *args, **options):
self.stdout.write(self.style.WARNING("Starting to sync interpretation slugs..."))
# استفاده از select_related برای جلوگیری از N+1 Query روی Category
interpretations = list(HadisInterpretation.objects.select_related('category').all())
if not interpretations:
self.stdout.write(self.style.ERROR("No HadisInterpretations found in the database."))
return
to_update = []
for obj in interpretations:
if obj.category and obj.category.slug:
# فقط اگر اسلاگ فرق داشت آپدیت کن تا دیتابیس بی‌دلیل درگیر نشود
if obj.slug != obj.category.slug:
obj.slug = obj.category.slug
to_update.append(obj)
if to_update:
with transaction.atomic():
batch_size = 1000
for i in range(0, len(to_update), batch_size):
HadisInterpretation.objects.bulk_update(
to_update[i:i + batch_size],
['slug']
)
self.stdout.write(
self.style.SUCCESS(f"\n🎉 Successfully synced slugs for {len(to_update)} interpretations!")
)
else:
self.stdout.write(
self.style.SUCCESS("\n✅ All interpretations already have the correct slug. Nothing to update.")
)
Loading…
Cancel
Save