Browse Source
seeding hadith corrections
seeding hadith corrections
new script for hadith connections removed the cache for hadith detail pages temperory.master
3 changed files with 88 additions and 4 deletions
@ -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.")) |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue