Browse Source

seeding hadith corrections

new script for hadith connections

removed the cache for hadith detail pages temperory.
master
Mohsen Taba 4 months ago
parent
commit
119b1094ec
  1. 84
      apps/hadis/management/commands/seed_corrections.py
  2. 6
      apps/hadis/urls.py
  3. 2
      entrypoint.sh

84
apps/hadis/management/commands/seed_corrections.py

@ -0,0 +1,84 @@
import random
from django.core.management.base import BaseCommand
from django.db import transaction
# REPLACE 'apps.hadis.models' with the actual path to your models if different
from apps.hadis.models import Hadis, HadisCorrection
class Command(BaseCommand):
help = 'Seeds HadisCorrection instances to ensure every Hadis has at least 4 corrections'
def create_json_field(self, en_text, ru_text):
"""Helper to construct the multilingual JSON structure"""
return [
{"text": en_text, "language_code": "en"},
{"text": ru_text, "language_code": "ru"}
]
def handle(self, *args, **options):
self.stdout.write("Starting HadisCorrection seeding...")
# 1. Fetch all Hadis
hadis_qs = Hadis.objects.all()
total_hadis = hadis_qs.count()
if total_hadis == 0:
self.stdout.write(self.style.WARNING("No Hadis found to correct!"))
return
self.stdout.write(f"Found {total_hadis} Hadis records. Checking correction counts...")
total_created = 0
# 2. Iterate and Create
for i, hadis in enumerate(hadis_qs, 1):
if i % 50 == 0:
self.stdout.write(f"Processing {i}/{total_hadis}...")
# Check existing count
current_count = HadisCorrection.objects.filter(hadis=hadis).count()
needed = 4 - current_count
if needed <= 0:
continue
with transaction.atomic():
for k in range(needed):
# Calculate a visual number for titles (e.g., Correction 1, Correction 2)
correction_num = current_count + k + 1
# --- Generate Content ---
# Titles: specific to grammar, context, or translation
topics_en = ["Grammar Correction", "Context Clarification", "Alternative Translation", "Scholar Note"]
topics_ru = ["Исправление грамматики", "Уточнение контекста", "Альтернативный перевод", "Заметка ученого"]
topic_idx = (correction_num - 1) % len(topics_en)
title_data = self.create_json_field(
f"{topics_en[topic_idx]} #{correction_num}",
f"{topics_ru[topic_idx]} #{correction_num}"
)
desc_data = self.create_json_field(
f"This is a detailed explanation regarding the {topics_en[topic_idx].lower()} for Hadith {hadis.number}.",
f"Это подробное объяснение, касающееся {topics_ru[topic_idx].lower()} для Хадиса {hadis.number}."
)
# Simulating a corrected translation snippet
translation_data = self.create_json_field(
f"Revised text segment for correction {correction_num}...",
f"Пересмотренный фрагмент текста для исправления {correction_num}..."
)
# --- Create Instance ---
# We use .create() so the model's .save() method is called.
# This ensures 'slug' and 'share_link' are automatically generated by your model logic.
HadisCorrection.objects.create(
hadis=hadis,
title=title_data,
description=desc_data,
translation=translation_data
)
total_created += 1
self.stdout.write(self.style.SUCCESS(f"Done! Successfully created {total_created} new HadisCorrection instances."))

6
apps/hadis/urls.py

@ -49,9 +49,9 @@ urlpatterns = [
# Hadis detail paths (with slug, more specific)
path('<str:hadis_slug>/detail/', HadisDetailView.as_view(), name='hadis-detail'),
path('<str:hadis_slug>/transmitters/', cached_view(HadisTransmittersView.as_view()), name='hadis-transmitters'),
path('<str:hadis_slug>/corrections/', cached_view(HadisCorrectionsView.as_view()), name='hadis-corrections'),
path('<str:hadis_slug>/', cached_view(HadisBasicView.as_view()), name='hadis-basic'), # ← Least specific LAST
path('<str:hadis_slug>/transmitters/', HadisTransmittersView.as_view(), name='hadis-transmitters'),
path('<str:hadis_slug>/corrections/', HadisCorrectionsView.as_view(), name='hadis-corrections'),
path('<str:hadis_slug>/', HadisBasicView.as_view(), name='hadis-basic'), # ← Least specific LAST
path('test/test-deploy',test_deploy , name='test'),
path('debug-headers',debug_headers , name='headers'),

2
entrypoint.sh

@ -6,7 +6,7 @@ python manage.py migrate
python manage.py collectstatic --noinput
# Seed Russian data (only runs once, skips if data exists)
python manage.py seed_hadis_transmitter
python manage.py seed_corrections
# python manage.py seed_russian_data
# python manage.py hadisreferences
# python manage.py seed_complete_hadis_data

Loading…
Cancel
Save