3 changed files with 182 additions and 54 deletions
-
129apps/hadis/management/commands/reformat_data.py
-
96apps/hadis/management/commands/seed_corrections.py
-
13apps/hadis/serializers/hadis.py
@ -0,0 +1,129 @@ |
|||||
|
import random |
||||
|
from django.core.management.base import BaseCommand |
||||
|
from django.db import transaction |
||||
|
# REPLACE 'apps.hadis.models' with your actual app path |
||||
|
from apps.hadis.models import Hadis |
||||
|
|
||||
|
class Command(BaseCommand): |
||||
|
help = 'Updates explanation and address fields with structured multilingual dummy data' |
||||
|
|
||||
|
def handle(self, *args, **options): |
||||
|
self.stdout.write("Starting bulk update of Explanation and Address fields...") |
||||
|
|
||||
|
hadis_qs = Hadis.objects.all() |
||||
|
total_hadis = hadis_qs.count() |
||||
|
|
||||
|
if total_hadis == 0: |
||||
|
self.stdout.write(self.style.WARNING("No Hadis found to update.")) |
||||
|
return |
||||
|
|
||||
|
self.stdout.write(f"Found {total_hadis} Hadis records.") |
||||
|
|
||||
|
# --- DATA POOLS FOR RANDOM VARIATION --- |
||||
|
|
||||
|
# 1. Address Data (Hierarchical strings: Book -> Chapter -> Section) |
||||
|
address_pool = [ |
||||
|
{ |
||||
|
"en": ["Sahih al-Bukhari", "Book of Revelation", "Chapter 1"], |
||||
|
"fa": ["صحیح بخاری", "کتاب وحی", "باب ۱"], |
||||
|
"ru": ["Сахих аль-Бухари", "Книга откровения", "Глава 1"] |
||||
|
}, |
||||
|
{ |
||||
|
"en": ["Riyad as-Salihin", "The Book of Knowledge", "Virtues of Seeking Knowledge"], |
||||
|
"fa": ["ریاض الصالحین", "کتاب علم", "فضیلت طلب علم"], |
||||
|
"ru": ["Рияд ас-Салихин", "Книга знаний", "Достоинства поиска знаний"] |
||||
|
}, |
||||
|
{ |
||||
|
"en": ["Sunan Abi Dawud", "Book of Prayer", "Details of Prostration"], |
||||
|
"fa": ["سنن ابی داوود", "کتاب صلاه", "جزئیات سجده"], |
||||
|
"ru": ["Сунан Абу Дауд", "Книга молитвы", "Детали земного поклона"] |
||||
|
} |
||||
|
] |
||||
|
|
||||
|
# 2. Explanation Data (Title/Detail objects) |
||||
|
explanation_pool = [ |
||||
|
{ |
||||
|
"en": [ |
||||
|
{"title": "Vocabulary", "detail": "The term 'Niyyah' implies the resolve of the heart."}, |
||||
|
{"title": "Context", "detail": "Narrated by Omar bin Al-Khattab regarding migration to Medina."} |
||||
|
], |
||||
|
"fa": [ |
||||
|
{"title": "لغات", "detail": "واژه «نیت» به معنای قصد و عزم قلبی است."}, |
||||
|
{"title": "شأن نزول", "detail": "این حدیث از عمر بن خطاب در مورد مهاجرت به مدینه روایت شده است."} |
||||
|
], |
||||
|
"ru": [ |
||||
|
{"title": "Словарь", "detail": "Термин «Ният» подразумевает решимость сердца."}, |
||||
|
{"title": "Контекст", "detail": "Передано Омаром ибн аль-Хаттабом относительно переселения в Медину."} |
||||
|
] |
||||
|
}, |
||||
|
{ |
||||
|
"en": [ |
||||
|
{"title": "Legal Ruling", "detail": "Scholars agree that this action is Mustahabb (recommended)."}, |
||||
|
{"title": "Benefits", "detail": "Increases spirituality and mindfulness in daily life."} |
||||
|
], |
||||
|
"fa": [ |
||||
|
{"title": "حکم فقهی", "detail": "علما اتفاق نظر دارند که این عمل مستحب است."}, |
||||
|
{"title": "فواید", "detail": "باعث افزایش معنویت و توجه در زندگی روزمره میشود."} |
||||
|
], |
||||
|
"ru": [ |
||||
|
{"title": "Правoвое решение", "detail": "Ученые согласны, что это действие является желательным (мустахаб)."}, |
||||
|
{"title": "Польза", "detail": "Повышает духовность и осознанность в повседневной жизни."} |
||||
|
] |
||||
|
} |
||||
|
] |
||||
|
|
||||
|
updated_count = 0 |
||||
|
|
||||
|
# Iterate and Update |
||||
|
for i, hadis in enumerate(hadis_qs, 1): |
||||
|
if i % 100 == 0: |
||||
|
self.stdout.write(f"Processing {i}/{total_hadis}...") |
||||
|
|
||||
|
# Pick random samples to make data look diverse |
||||
|
addr_sample = random.choice(address_pool) |
||||
|
exp_sample = random.choice(explanation_pool) |
||||
|
|
||||
|
# --- CONSTRUCT ADDRESS FORMAT (List of Strings) --- |
||||
|
# Format: [{"language_code": "en", "text": ["A", "B"]}, ...] |
||||
|
new_address = [ |
||||
|
{ |
||||
|
"language_code": "en", |
||||
|
"text": addr_sample["en"] |
||||
|
}, |
||||
|
{ |
||||
|
"language_code": "fa", |
||||
|
"text": addr_sample["fa"] |
||||
|
}, |
||||
|
{ |
||||
|
"language_code": "ru", |
||||
|
"text": addr_sample["ru"] |
||||
|
} |
||||
|
] |
||||
|
|
||||
|
# --- CONSTRUCT EXPLANATION FORMAT (List of Objects) --- |
||||
|
# Format: [{"language_code": "en", "text": [{"title":.., "detail":..}]}, ...] |
||||
|
new_explanation = [ |
||||
|
{ |
||||
|
"language_code": "en", |
||||
|
"text": exp_sample["en"] |
||||
|
}, |
||||
|
{ |
||||
|
"language_code": "fa", |
||||
|
"text": exp_sample["fa"] |
||||
|
}, |
||||
|
{ |
||||
|
"language_code": "ru", |
||||
|
"text": exp_sample["ru"] |
||||
|
} |
||||
|
] |
||||
|
|
||||
|
# Assign to fields |
||||
|
hadis.address = new_address |
||||
|
hadis.explanation = new_explanation |
||||
|
|
||||
|
# Save (Using save() ensures signals/dates update, though slower than bulk_update) |
||||
|
# Since we modify JSON fields, simple save is safest. |
||||
|
hadis.save() |
||||
|
updated_count += 1 |
||||
|
|
||||
|
self.stdout.write(self.style.SUCCESS(f"Successfully updated {updated_count} Hadis records with new format.")) |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue