import random from django.core.management.base import BaseCommand from django.core.exceptions import ObjectDoesNotExist # Ensure these imports match your actual app structure from apps.hadis.models import ( Hadis, HadisCollection, HadisInCollection, HadisCorrection ) class Command(BaseCommand): help = 'Seeds Hadis Collections and Corrections for Hadis IDs 1877-1888' def create_json_field(self, en_text, fa_text, ru_text): """Helper to create your specific JSON format with Russian added""" return [ {"text": en_text, "language_code": "en"}, {"text": fa_text, "language_code": "fa"}, {"text": ru_text, "language_code": "ru"} ] def handle(self, *args, **options): self.stdout.write("--- Starting Hadis Extras Seeding ---") # --------------------------------------------------------- # 1. Create Hadis Collections # --------------------------------------------------------- self.stdout.write("Creating Hadis Collections...") collections_data = [ { "title": ("The Book of Intellect and Ignorance", "کتاب عقل و جهل", "Книга разума и невежества"), "summary": ( "A collection of narrations regarding the importance of reason.", "مجموعه‌ای از روایات درباره اهمیت عقل و خرد.", "Сборник преданий о важности разума." ) }, { "title": ("The Book of Monotheism", "کتاب التوحید", "Книга Единобожия"), "summary": ( "Hadiths explaining the oneness of God.", "احادیثی که یگانگی خداوند را توضیح می‌دهند.", "Хадисы, объясняющие единство Бога." ) } ] created_collections = [] for c_data in collections_data: # We use the English title as a loose identifier to prevent duplicates slug_base = c_data["title"][0].lower().replace(" ", "-") # Simple check if exists to avoid duplication in multiple runs if HadisCollection.objects.filter(slug__startswith=slug_base).exists(): collection = HadisCollection.objects.filter(slug__startswith=slug_base).first() self.stdout.write(f"Collection '{c_data['title'][0]}' already exists.") else: collection = HadisCollection.objects.create( title=self.create_json_field(*c_data["title"]), summary=self.create_json_field(*c_data["summary"]), status=True ) self.stdout.write(self.style.SUCCESS(f"Created Collection: {c_data['title'][0]}")) created_collections.append(collection) # --------------------------------------------------------- # 2. Process Hadis (1877 - 1888) # --------------------------------------------------------- self.stdout.write("Processing Hadis IDs 1877-1888...") target_ids = range(1877, 1889) for hadis_id in target_ids: try: hadis_obj = Hadis.objects.get(id=hadis_id) except ObjectDoesNotExist: self.stdout.write(self.style.WARNING(f"Hadis ID {hadis_id} not found. Skipping.")) continue # --- A. Add to Collection --- # Clear previous collection links for this hadis to avoid clutter HadisInCollection.objects.filter(hadis=hadis_obj).delete() # Assign to a random collection from our list target_collection = random.choice(created_collections) HadisInCollection.objects.create( hadis=hadis_obj, collection=target_collection, order=hadis_id # Just using ID as order for simplicity ) self.stdout.write(f" -> Linked Hadis {hadis_id} to '{target_collection}'") # --- B. Create Correction --- # Mocking a correction entry # We'll assume these are translation corrections or scholarly notes correction_title = ( f"Correction for Hadis {hadis_id}", f"اصلاحیه برای حدیث {hadis_id}", f"Исправление для хадиса {hadis_id}" ) correction_desc = ( "The word 'Wali' here implies authority, not just friendship.", "کلمه «ولی» در اینجا دلالت بر ولایت دارد، نه صرفاً دوستی.", "Слово «Вали» здесь означает власть, а не просто дружбу." ) correction_trans = ( "Verily, I am the authority over the believers...", "همانا من ولی و سرپرست مؤمنان هستم...", "Воистину, я — покровитель верующих..." ) HadisCorrection.objects.create( hadis=hadis_obj, title=self.create_json_field(*correction_title), description=self.create_json_field(*correction_desc), translation=self.create_json_field(*correction_trans), share_link=f"https://example.com/corrections/{hadis_id}" ) self.stdout.write(f" -> Created Correction for Hadis {hadis_id}") self.stdout.write(self.style.SUCCESS("--- Hadis Extras Seeding Complete ---"))