diff --git a/apps/hadis/management/commands/hadis_completing.py b/apps/hadis/management/commands/hadis_completing.py new file mode 100644 index 0000000..ff7f851 --- /dev/null +++ b/apps/hadis/management/commands/hadis_completing.py @@ -0,0 +1,83 @@ +import random +from django.core.management.base import BaseCommand +from django.db.models import Q +from apps.hadis.models import Hadis, HadisStatus, HadisTag # REPLACE 'your_app_name' + +class Command(BaseCommand): + help = 'Enriches existing Hadis records with Links, Statuses, and Tags based on specific IDs.' + + def handle(self, *args, **kwargs): + self.stdout.write(self.style.WARNING('Starting Hadis data enrichment...')) + + # 1. FETCH TARGET DATA + # --------------------------------------------------------- + + # Status IDs: 126-134 + status_ids = list(range(126, 135)) + statuses = list(HadisStatus.objects.filter(id__in=status_ids)) + + # Tag IDs: 507-517 AND 547-571 + tag_ids = list(range(507, 518)) + list(range(547, 572)) + tags = list(HadisTag.objects.filter(id__in=tag_ids)) + + # Check if we found the requirements + if not statuses: + self.stdout.write(self.style.ERROR(f"No HadisStatus found for IDs {status_ids}. Please check your DB.")) + return + if not tags: + self.stdout.write(self.style.ERROR(f"No HadisTags found for IDs {tag_ids}. Please check your DB.")) + return + + self.stdout.write(f"Found {len(statuses)} Statuses and {len(tags)} Tags to distribute.") + + # 2. ITERATE AND UPDATE HADISES + # --------------------------------------------------------- + hadises = Hadis.objects.all() + count = 0 + + # Sample data to generate random links + link_sources = [ + ("Sunnah.com", "https://sunnah.com/bukhari/"), + ("IslamWeb", "https://www.islamweb.net/ar/fatwa/"), + ("HadithDB", "https://hadithdb.com/"), + ("Dorar", "https://dorar.net/hadith/"), + ("IslamicFinder", "https://www.islamicfinder.org/"), + ("Al-Islam", "https://www.al-islam.org/") + ] + + for hadis in hadises: + # A. Update Status (One random status from the list) + random_status = random.choice(statuses) + hadis.hadis_status = random_status + + # B. Generate Links (At least 3) + # Since links is a JSONField dict, we create a dictionary structure + num_links = random.randint(3, 5) # Generate 3 to 5 links + selected_sources = random.sample(link_sources, num_links) + + links_data = {} + for name, base_url in selected_sources: + # Adding a random number to URL to make it look unique + links_data[name] = f"{base_url}{random.randint(1000, 9999)}" + + hadis.links = links_data + + # Save the direct fields (status and links) + hadis.save() + + # C. Update Tags (At least 4-5) + # Note: We must save() the model before adding ManyToMany relations + num_tags = random.randint(4, 5) + # Ensure we don't try to sample more tags than exist + sample_size = min(num_tags, len(tags)) + selected_tags = random.sample(tags, sample_size) + + # Clear existing tags if you want a fresh start, otherwise remove .clear() + hadis.tags.clear() + hadis.tags.add(*selected_tags) + + count += 1 + if count % 10 == 0: + self.stdout.write(f"Processed {count} Hadiths...") + + self.stdout.write(self.style.SUCCESS(f'Successfully updated {count} Hadis records!')) \ No newline at end of file diff --git a/apps/hadis/management/commands/hadisreferences.py b/apps/hadis/management/commands/hadisreferences.py new file mode 100644 index 0000000..39cb024 --- /dev/null +++ b/apps/hadis/management/commands/hadisreferences.py @@ -0,0 +1,79 @@ +import os +import random +from django.core.management.base import BaseCommand +from django.core.files import File +from django.conf import settings +# REPLACE 'your_app_name' with your actual app name +from apps.hadis.models import Hadis, HadisReference, BookReference, ReferenceImage + +class Command(BaseCommand): + help = 'Creates HadisReferences with random Books (IDs 34-43) and attaches ref1-4 images.' + + def handle(self, *args, **kwargs): + self.stdout.write(self.style.WARNING('Starting Reference & Image creation...')) + + # 1. SETUP IMAGES PATH + # --------------------------------------------------------- + seeds_path = os.path.join(settings.BASE_DIR, 'seeds', 'images') + image_files = ['ref1.png', 'ref2.png', 'ref3.png', 'ref4.png'] + + # Validate images exist + for img in image_files: + if not os.path.exists(os.path.join(seeds_path, img)): + self.stdout.write(self.style.ERROR(f"Image not found: {os.path.join(seeds_path, img)}")) + self.stdout.write(self.style.ERROR("Please create 'seeds/images' folder and add ref1.png to ref4.png.")) + return + + # 2. FETCH SPECIFIC BOOK REFERENCES (IDs 34-43) + # --------------------------------------------------------- + target_ids = list(range(34, 44)) # range(34, 44) produces 34...43 + book_references = list(BookReference.objects.filter(id__in=target_ids)) + + if not book_references: + self.stdout.write(self.style.ERROR(f"No BookReferences found with IDs {target_ids}. Please check your database.")) + return + + self.stdout.write(f"Found {len(book_references)} Book References to distribute randomly.") + + # 3. PROCESS HADIS RECORDS + # --------------------------------------------------------- + hadises = Hadis.objects.all() + processed_count = 0 + + for hadis in hadises: + # Pick a random book from the list + random_book = random.choice(book_references) + + # A. Create HadisReference + # We use get_or_create to prevent duplicates if run multiple times + hadis_ref, created = HadisReference.objects.get_or_create( + hadis=hadis, + defaults={ + 'book_reference': random_book, + 'description': [ + {"text": f"Reference from {random_book.title[0]['text']}", "language_code": "en"}, + {"text": f"Ссылка из {random_book.title[0]['text']}", "language_code": "ru"} + ] + } + ) + + # Only attach images if this reference was just created + # (To avoid adding duplicate images if you run the script twice) + if created: + # B. Attach Images (ref1 - ref4) + for index, img_name in enumerate(image_files): + file_path = os.path.join(seeds_path, img_name) + + with open(file_path, 'rb') as f: + ref_image = ReferenceImage( + reference=hadis_ref, + priority=index + 1 # 1, 2, 3, 4 + ) + # Save image with unique name prefix to avoid overwrite issues + ref_image.thumbnail.save(f"{hadis.id}_{img_name}", File(f), save=True) + + processed_count += 1 + if processed_count % 10 == 0: + self.stdout.write(f"Processed references for {processed_count} Hadiths...") + + self.stdout.write(self.style.SUCCESS(f'Successfully created references for {processed_count} new Hadis records.')) \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh index b8bc026..5dbc1d0 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -6,7 +6,8 @@ python manage.py migrate python manage.py collectstatic --noinput # Seed Russian data (only runs once, skips if data exists) -python manage.py seed_russian_data +# python manage.py seed_russian_data +python manage.py hadisreferences # python manage.py populate_books # python manage.py populate_book_reference