7 changed files with 139 additions and 4 deletions
-
2Dockerfile.prod
-
32apps/hadis/management/commands/populate_narrator_links.py
-
53apps/hadis/management/commands/seed_interpretation_books.py
-
23apps/hadis/migrations/0023_hadiscorrection_narrator_and_more.py
-
23apps/hadis/migrations/0024_hadiscorrection_links_hadisinterpretation_links.py
-
4apps/hadis/models/hadis.py
-
6apps/hadis/serializers/hadis.py
@ -0,0 +1,32 @@ |
|||||
|
# backend/apps/hadis/management/commands/populate_narrator_links.py |
||||
|
|
||||
|
from django.core.management.base import BaseCommand |
||||
|
from apps.hadis.models import HadisCorrection, HadisInterpretation |
||||
|
|
||||
|
class Command(BaseCommand): |
||||
|
help = 'Populates the default narrator and links for all existing Corrections and Interpretations.' |
||||
|
|
||||
|
def handle(self, *args, **options): |
||||
|
self.stdout.write(self.style.WARNING("Updating default values for narrator and links...")) |
||||
|
|
||||
|
default_narrator = "زُرارَةُ بْنُ أَعْيَنَ الشَّيبانِيّ" |
||||
|
default_links = [ |
||||
|
{"title": "Первая ссылка Первая ссылка ссылкассы...", "link": "google.com"}, |
||||
|
{"title": "Первая ссылка.", "link": "google.com"}, |
||||
|
{"title": "Первая ссылка.", "link": "google.com"} |
||||
|
] |
||||
|
|
||||
|
# آپدیت تمام رکوردهای جدول تصحیحات |
||||
|
corr_count = HadisCorrection.objects.all().update( |
||||
|
narrator=default_narrator, |
||||
|
links=default_links |
||||
|
) |
||||
|
|
||||
|
# آپدیت تمام رکوردهای جدول تفاسیر |
||||
|
interp_count = HadisInterpretation.objects.all().update( |
||||
|
narrator=default_narrator, |
||||
|
links=default_links |
||||
|
) |
||||
|
|
||||
|
self.stdout.write(self.style.SUCCESS(f"✅ Successfully updated {corr_count} HadisCorrections!")) |
||||
|
self.stdout.write(self.style.SUCCESS(f"✅ Successfully updated {interp_count} HadisInterpretations!")) |
||||
@ -0,0 +1,53 @@ |
|||||
|
# 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!") |
||||
|
) |
||||
@ -0,0 +1,23 @@ |
|||||
|
# Generated by Django 5.2.12 on 2026-06-27 10:45 |
||||
|
|
||||
|
from django.db import migrations, models |
||||
|
|
||||
|
|
||||
|
class Migration(migrations.Migration): |
||||
|
|
||||
|
dependencies = [ |
||||
|
('hadis', '0022_hadisinterpretation_slug'), |
||||
|
] |
||||
|
|
||||
|
operations = [ |
||||
|
migrations.AddField( |
||||
|
model_name='hadiscorrection', |
||||
|
name='narrator', |
||||
|
field=models.CharField(blank=True, max_length=255, null=True, verbose_name='Narrator'), |
||||
|
), |
||||
|
migrations.AddField( |
||||
|
model_name='hadisinterpretation', |
||||
|
name='narrator', |
||||
|
field=models.CharField(blank=True, max_length=255, null=True, verbose_name='Narrator'), |
||||
|
), |
||||
|
] |
||||
@ -0,0 +1,23 @@ |
|||||
|
# Generated by Django 5.2.12 on 2026-06-27 10:49 |
||||
|
|
||||
|
from django.db import migrations, models |
||||
|
|
||||
|
|
||||
|
class Migration(migrations.Migration): |
||||
|
|
||||
|
dependencies = [ |
||||
|
('hadis', '0023_hadiscorrection_narrator_and_more'), |
||||
|
] |
||||
|
|
||||
|
operations = [ |
||||
|
migrations.AddField( |
||||
|
model_name='hadiscorrection', |
||||
|
name='links', |
||||
|
field=models.JSONField(blank=True, default=dict, null=True, verbose_name='links'), |
||||
|
), |
||||
|
migrations.AddField( |
||||
|
model_name='hadisinterpretation', |
||||
|
name='links', |
||||
|
field=models.JSONField(blank=True, default=dict, null=True, verbose_name='links'), |
||||
|
), |
||||
|
] |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue