You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
5.4 KiB
109 lines
5.4 KiB
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 Transmitters, TransmitterOpinion, TransmitterOriginalText, OpinionStatus
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Seeds TransmitterOpinion and TransmitterOriginalText for transmitters 84-91'
|
|
|
|
def handle(self, *args, **options):
|
|
self.stdout.write("Starting Transmitter Details seeding...")
|
|
|
|
# ---------------------------------------------------------
|
|
# 1. Fetch Target Objects
|
|
# ---------------------------------------------------------
|
|
transmitters = Transmitters.objects.filter(id__range=(84, 91))
|
|
|
|
if not transmitters.exists():
|
|
self.stdout.write(self.style.ERROR("No Transmitters found in range 84-91."))
|
|
return
|
|
|
|
# Fetch OpinionStatus objects
|
|
# We need these to populate the 'status' ForeignKey in TransmitterOpinion
|
|
statuses = list(OpinionStatus.objects.all())
|
|
|
|
# Fallback: If no statuses exist, we can't create opinions safely without knowing your Status model fields.
|
|
if not statuses:
|
|
self.stdout.write(self.style.ERROR("No 'OpinionStatus' objects found! Please create some via Admin first."))
|
|
return
|
|
|
|
self.stdout.write(f"Found {transmitters.count()} Transmitters and {len(statuses)} Opinion Statuses.")
|
|
|
|
# ---------------------------------------------------------
|
|
# 2. Data Generators (Helpers)
|
|
# ---------------------------------------------------------
|
|
scholars = [
|
|
{"en": "Al-Dhahabi", "ar": "الذهبي", "fa": "ذهبی"},
|
|
{"en": "Ibn Hajar", "ar": "ابن حجر", "fa": "ابن حجر"},
|
|
{"en": "Al-Tusi", "ar": "الطوسي", "fa": "طوسی"},
|
|
{"en": "Al-Najashi", "ar": "النجاشي", "fa": "نجاشی"},
|
|
]
|
|
|
|
opinion_texts = [
|
|
{"en": "He is trustworthy and reliable.", "ar": "هو ثقة ثبت.", "fa": "او ثقه و مورد اعتماد است."},
|
|
{"en": "His memory was weak in later years.", "ar": "كان سيء الحفظ في آخره.", "fa": "حافظه او در اواخر عمر ضعیف بود."},
|
|
{"en": "Unknown status.", "ar": "مجهول الحال.", "fa": "مجهول است."},
|
|
{"en": "Highly praised by scholars.", "ar": "ممدوح عند العلماء.", "fa": "مورد ستایش علما است."},
|
|
]
|
|
|
|
book_titles = [
|
|
{"en": "Book of Traditions", "ar": "كتاب الحديث", "fa": "کتاب حدیث"},
|
|
{"en": "Treatise on Rights", "ar": "رسالة الحقوق", "fa": "رساله حقوق"},
|
|
{"en": "The Clarification", "ar": "التبين", "fa": "تبیین"},
|
|
{"en": "Collection of Virtues", "ar": "مجموع الفضائل", "fa": "مجموعه فضائل"},
|
|
]
|
|
|
|
def make_json_field(data_dict):
|
|
"""Converts simple dict {'en': 'X', 'fa': 'Y'} to your model's JSON structure"""
|
|
return [
|
|
{"language_code": "en", "text": data_dict.get("en", "")},
|
|
{"language_code": "ar", "text": data_dict.get("ar", "")},
|
|
{"language_code": "fa", "text": data_dict.get("fa", "")},
|
|
]
|
|
|
|
# ---------------------------------------------------------
|
|
# 3. Creation Loop
|
|
# ---------------------------------------------------------
|
|
counts = {'opinions': 0, 'texts': 0}
|
|
|
|
with transaction.atomic():
|
|
for t in transmitters:
|
|
self.stdout.write(f"Processing {t.full_name[0]['text'] if t.full_name else t.id}...")
|
|
|
|
# --- A. Create TransmitterOpinion (1 to 2 per person) ---
|
|
for _ in range(random.randint(1, 2)):
|
|
scholar = random.choice(scholars)
|
|
text = random.choice(opinion_texts)
|
|
status = random.choice(statuses)
|
|
|
|
TransmitterOpinion.objects.create(
|
|
transmitter=t,
|
|
scholar_name=make_json_field(scholar),
|
|
opinion_text=make_json_field(text),
|
|
status=status
|
|
)
|
|
counts['opinions'] += 1
|
|
|
|
# --- B. Create TransmitterOriginalText (1 to 2 per person) ---
|
|
for _ in range(random.randint(1, 2)):
|
|
title = random.choice(book_titles)
|
|
# Simple dummy text
|
|
body = {
|
|
"en": f"This is an excerpt from {title['en']}...",
|
|
"ar": f"هذا مقتطف من {title['ar']}...",
|
|
"fa": f"این بخشی از {title['fa']} است..."
|
|
}
|
|
|
|
TransmitterOriginalText.objects.create(
|
|
transmitter=t,
|
|
title=make_json_field(title),
|
|
text=make_json_field(body),
|
|
translation=make_json_field(body), # Using same text for translation as placeholder
|
|
# Note: 'slug' and 'share_link' are handled by your model's save() method automatically
|
|
)
|
|
counts['texts'] += 1
|
|
|
|
self.stdout.write(self.style.SUCCESS(
|
|
f"Done! Created {counts['opinions']} Opinions and {counts['texts']} Original Texts."
|
|
))
|