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.'))