# -*- coding: utf-8 -*- import random from django.core.management.base import BaseCommand from django.core.cache import cache from apps.hadis.models import Hadis, HadisCorrection, HadisInterpretation ISLAMIC_ENCYCLOPEDIA_SOURCES = [ ("Al-Islam Encyclopedia", "https://www.al-islam.org/articles/hadith-studies-"), ("Shamela Digital Library", "https://shamela.ws/book/"), ("WikiShia Encyclopedia", "https://en.wikishia.net/view/Hadith_"), ("Thaqalayn Hadith Library", "https://thaqalayn.net/hadith/"), ("Sunnah Hadith Collection", "https://sunnah.com/bukhari/"), ("Dorar Encyclopedia", "https://dorar.net/hadith/"), ("IslamWeb Reference", "https://www.islamweb.net/ar/hadith/"), ("HadithDB Archive", "https://hadithdb.com/tradition/"), ] def generate_islamic_links(seed_id: int) -> dict: rng = random.Random(seed_id) count = rng.randint(2, 3) chosen = rng.sample(ISLAMIC_ENCYCLOPEDIA_SOURCES, count) links = {} for title, base_url in chosen: ref_num = rng.randint(100, 4999) links[title] = f"{base_url}{ref_num}" return links class Command(BaseCommand): help = "Populates 2-3 realistic Islamic encyclopedia links for Hadis, HadisCorrection, and HadisInterpretation records." def handle(self, *args, **options): self.stdout.write("Populating Islamic encyclopedia links for Hadis, HadisCorrection, and HadisInterpretation...") for model in (HadisCorrection, HadisInterpretation, Hadis): rows = list(model.objects.only("id", "links")) to_update = [] for obj in rows: if not obj.links or obj.links == {} or obj.links == []: obj.links = generate_islamic_links(obj.id) to_update.append(obj) if to_update: model.objects.bulk_update(to_update, ["links"], batch_size=500) self.stdout.write( self.style.SUCCESS( f"Updated {len(to_update)} {model.__name__} records (total {len(rows)})." ) ) cache.clear() self.stdout.write(self.style.SUCCESS("Cleared Django cache successfully."))