Browse Source
Add management commands for populating Hadith and Category English context
Add management commands for populating Hadith and Category English context
- Introduced `add_english_context_category.py` to populate missing English titles and descriptions for HadisCategory. - Added `english_context.py` to fill in missing English context for Hadith fields including title, narrator, translation, and description. - Created `arabic_text.py` to replace Hadith text with authentic Arabic texts. - Updated `CategorySerializer` to calculate the total number of Hadiths in a category and its descendants.master
4 changed files with 207 additions and 3 deletions
-
77apps/hadis/management/commands/add_english_context_category.py
-
40apps/hadis/management/commands/arabic_text.py
-
78apps/hadis/management/commands/english_context.py
-
15apps/hadis/serializers/category.py
@ -0,0 +1,77 @@ |
|||
import random |
|||
from django.core.management.base import BaseCommand |
|||
from django.db import transaction |
|||
from apps.hadis.models import HadisCategory |
|||
|
|||
class Command(BaseCommand): |
|||
help = 'Populates missing English context for Category fields: title and description.' |
|||
|
|||
def handle(self, *args, **options): |
|||
# Sample Category Titles and Descriptions based on your SourceTypes |
|||
category_samples = { |
|||
'quran': { |
|||
'titles': ["Verses of Wisdom", "Quranic Guidance", "The Clear Book", "Divine Revelations"], |
|||
'descriptions': ["Explanations and context for verses regarding daily life.", "A collection of signs pointing towards the Creator."] |
|||
}, |
|||
'hadith': { |
|||
'titles': ["Prophetic Traditions", "Sayings of the Infallibles", "Gems of Wisdom", "Path of the Faithful"], |
|||
'descriptions': ["Authenticated reports regarding religious practice and ethics.", "Guidance derived from the lives of holy figures."] |
|||
}, |
|||
'history': { |
|||
'titles': ["Biographies of the Holy", "Historical Accounts", "The Golden Era", "Chronicles of Faith"], |
|||
'descriptions': ["Detailed accounts of historical events and early Islamic society.", "Lessons from the past for the modern believer."] |
|||
}, |
|||
'fatwa': { |
|||
'titles': ["Legal Rulings", "Jurisprudence Basics", "Practical Laws", "Scholarly Guidance"], |
|||
'descriptions': ["Simplified legal rulings for common religious questions.", "Guidelines on ritual purity, prayer, and transactions."] |
|||
}, |
|||
'quote': { |
|||
'titles': ["Wise Sayings", "Aphorisms", "Reflections", "Moral Quotes"], |
|||
'descriptions': ["Short, impactful quotes regarding self-improvement and spirituality.", "Deep reflections on the nature of the soul and the world."] |
|||
} |
|||
} |
|||
|
|||
# Default samples if SourceType doesn't match |
|||
default_titles = ["Religious Category", "Islamic Knowledge", "General Studies"] |
|||
default_descriptions = ["General information regarding this specific branch of knowledge."] |
|||
|
|||
categories = HadisCategory.objects.all() |
|||
total = categories.count() |
|||
updated_count = 0 |
|||
|
|||
self.stdout.write(self.style.WARNING(f'Checking {total} Categories...')) |
|||
|
|||
with transaction.atomic(): |
|||
for cat in categories: |
|||
modified = False |
|||
|
|||
# Use source_type to get more relevant random data |
|||
samples = category_samples.get(cat.source_type, {'titles': default_titles, 'descriptions': default_descriptions}) |
|||
|
|||
# Process Title and Description |
|||
for field_name in ['title', 'description']: |
|||
current_data = getattr(cat, field_name) |
|||
|
|||
if not isinstance(current_data, list): |
|||
current_data = [] |
|||
|
|||
# Check for 'en' entry |
|||
has_en = any(item.get('language_code') == 'en' for item in current_data if isinstance(item, dict)) |
|||
|
|||
if not has_en: |
|||
sample_pool = samples['titles'] if field_name == 'title' else samples['descriptions'] |
|||
new_entry = { |
|||
"text": random.choice(sample_pool), |
|||
"language_code": "en" |
|||
} |
|||
current_data.append(new_entry) |
|||
setattr(cat, field_name, current_data) |
|||
modified = True |
|||
|
|||
if modified: |
|||
# Note: We use save() but since we fixed the SlugField allow_unicode earlier, |
|||
# this should work fine without validation errors. |
|||
cat.save() |
|||
updated_count += 1 |
|||
|
|||
self.stdout.write(self.style.SUCCESS(f'Successfully updated {updated_count} Categories with English content.')) |
|||
@ -0,0 +1,40 @@ |
|||
import random |
|||
from django.core.management.base import BaseCommand |
|||
from django.db import transaction |
|||
from apps.hadis.models import Hadis |
|||
|
|||
class Command(BaseCommand): |
|||
help = 'Replaces the text field of all Hadiths with authentic Arabic Hadith texts.' |
|||
|
|||
def handle(self, *args, **options): |
|||
# A collection of authentic Arabic Hadith texts |
|||
arabic_hadiths = [ |
|||
"إِنَّمَا الْأَعْمَالُ بِالنِّيَّاتِ، وَإِنَّمَا لِكُلِّ امْرِئٍ مَا نَوَى.", |
|||
"الدِّينُ النَّصِيحَةُ.", |
|||
"مَنْ كَانَ يُؤْمِنُ بِاللَّهِ وَالْيَوْمِ الْآخِرِ فَلْيَقُلْ خَيْرًا أَوْ لِيَصْمُتْ.", |
|||
"لَا يُؤْمِنُ أَحَدُكُمْ حَتَّى يُحِبَّ لِأَخِيهِ مَا يُحِبُّ لِنَفْسِهِ.", |
|||
"خَيْرُكُمْ مَنْ تَعَلَّمَ الْقُرْآنَ وَعَلَّمَهُ.", |
|||
"الْمُسْلِمُ مَنْ سَلِمَ الْمُسْلِمُونَ مِنْ لِسَانِهِ وَيَدِهِ.", |
|||
"اتَّقِ اللَّهَ حَيْثُمَا كُنْتَ، وَأَتْبِعِ السَّيِّئَةَ الْحَسَنَةَ تَمْحُهَا.", |
|||
"بُنِيَ الْإِسْلَامُ عَلَى خَمْسٍ: شَهَادَةِ أَنْ لَا إِلَهَ إِلَّا اللَّهُ وَأَنَّ مُحَمَّدًا رَسُولُ اللَّهِ...", |
|||
"كُلُّ مَعْرُوفٍ صَدَقَةٌ.", |
|||
"الْبِرُّ حُسْنُ الْخُلُقِ، وَالْإِثْمُ مَا حَاكَ فِي نَفْسِكَ وَكَرِهْتَ أَنْ يَطَّلِعَ عَلَيْهِ النَّاسُ." |
|||
] |
|||
|
|||
hadis_qs = Hadis.objects.all() |
|||
total = hadis_qs.count() |
|||
|
|||
if total == 0: |
|||
self.stdout.write(self.style.ERROR('No Hadiths found in database.')) |
|||
return |
|||
|
|||
self.stdout.write(self.style.WARNING(f'Replacing text for {total} Hadiths...')) |
|||
|
|||
with transaction.atomic(): |
|||
for hadis in hadis_qs: |
|||
# Randomly select an Arabic text from the pool |
|||
hadis.text = random.choice(arabic_hadiths) |
|||
# We use update_fields for performance and to bypass full_clean if slug errors exist |
|||
hadis.save(update_fields=['text']) |
|||
|
|||
self.stdout.write(self.style.SUCCESS(f'Successfully updated {total} Hadiths with Arabic text.')) |
|||
@ -0,0 +1,78 @@ |
|||
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.')) |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue