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.
78 lines
3.4 KiB
78 lines
3.4 KiB
import random
|
|
from django.core.management.base import BaseCommand
|
|
from django.db import transaction
|
|
from apps.hadis.models import Hadis
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Populates missing English context for Hadith fields: title, title_narrator, translation, and description.'
|
|
|
|
def handle(self, *args, **options):
|
|
# Sample Islamic content for relevant test data
|
|
titles = [
|
|
"On the Virtues of Patience", "The Importance of Intentions",
|
|
"Kindness to Parents", "The Merit of Seeking Knowledge",
|
|
"Excellence of Character", "On Truthfulness", "The Value of Time"
|
|
]
|
|
narrators = [
|
|
"Abu Huraira", "Aisha",
|
|
"Anas bin Malik", "Ibn Umar",
|
|
"Jabir bin Abdullah"
|
|
]
|
|
translations = [
|
|
"Actions are but by intentions, and every man shall have only that which he intended.",
|
|
"The best among you are those who have the best manners and character.",
|
|
"A Muslim is the one from whose tongue and hands the Muslims are safe.",
|
|
"None of you will have faith till he wishes for his brother what he likes for himself.",
|
|
"The most beloved of deeds to Allah are those that are most consistent, even if they are small."
|
|
]
|
|
descriptions = [
|
|
"This hadith emphasizes the spiritual foundation of all outward actions.",
|
|
"A core teaching regarding social ethics and communal harmony in Islam.",
|
|
"Explains the internal discipline required for a believer's growth.",
|
|
"Provides guidance on how to treat neighbors and family members.",
|
|
"Focuses on the relationship between the Creator and the creation."
|
|
]
|
|
|
|
hadis_qs = Hadis.objects.all()
|
|
total = hadis_qs.count()
|
|
updated_count = 0
|
|
|
|
self.stdout.write(self.style.WARNING(f'Starting update for {total} Hadiths...'))
|
|
|
|
with transaction.atomic():
|
|
for hadis in hadis_qs:
|
|
modified = False
|
|
|
|
# Define the target fields
|
|
field_map = {
|
|
'title': titles,
|
|
'title_narrator': narrators,
|
|
'translation': translations,
|
|
'description': descriptions
|
|
}
|
|
|
|
for field_name, sample_list in field_map.items():
|
|
current_data = getattr(hadis, field_name)
|
|
|
|
# Ensure current_data is a list (JSONField default)
|
|
if not isinstance(current_data, list):
|
|
current_data = []
|
|
|
|
# Check if English context already exists
|
|
has_en = any(item.get('language_code') == 'en' for item in current_data if isinstance(item, dict))
|
|
|
|
if not has_en:
|
|
# Append the new English entry
|
|
new_entry = {
|
|
"text": random.choice(sample_list),
|
|
"language_code": "en"
|
|
}
|
|
current_data.append(new_entry)
|
|
setattr(hadis, field_name, current_data)
|
|
modified = True
|
|
|
|
if modified:
|
|
hadis.save()
|
|
updated_count += 1
|
|
|
|
self.stdout.write(self.style.SUCCESS(f'Successfully updated {updated_count} Hadiths with English content.'))
|