From 4ebf6076a0121e19c144daca8d132f26cb4cc135 Mon Sep 17 00:00:00 2001 From: mohsentaba Date: Sat, 20 Dec 2025 12:33:08 +0330 Subject: [PATCH] image script for entry points created. --- apps/hadis/admin/reference.py | 1 - apps/hadis/management/commands/seed_images.py | 86 +++++++++++++++++++ entrypoint.sh | 1 + 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 apps/hadis/management/commands/seed_images.py diff --git a/apps/hadis/admin/reference.py b/apps/hadis/admin/reference.py index f85d9aa..351b024 100644 --- a/apps/hadis/admin/reference.py +++ b/apps/hadis/admin/reference.py @@ -115,7 +115,6 @@ class BookAuthorAdmin(ModelAdmin): return first.get('text', '-') return '-' - class BookReferenceImageAdmin(ModelAdmin): # Display the custom string, plus the raw order and book link for convenience list_display = ("display_name", "order", "book_reference") diff --git a/apps/hadis/management/commands/seed_images.py b/apps/hadis/management/commands/seed_images.py new file mode 100644 index 0000000..9d3cef8 --- /dev/null +++ b/apps/hadis/management/commands/seed_images.py @@ -0,0 +1,86 @@ +import os +from django.core.management.base import BaseCommand +from django.conf import settings +from django.core.files import File +# REPLACE 'your_app' WITH YOUR ACTUAL APP NAME(S) +from apps.hadis.models import BookReference, BookReferenceImage, HadisReference, ReferenceImage + +class Command(BaseCommand): + help = 'Seeds BookReferenceImage and ReferenceImage from seeds/images directory' + + def handle(self, *args, **options): + # 1. Setup Paths + base_dir = settings.BASE_DIR + seeds_path = os.path.join(base_dir, 'seeds', 'images') + + # Define source images + book_images = [f'book{i}.png' for i in range(1, 6)] # book1.png to book5.png + ref_images = [f'ref{i}.png' for i in range(1, 5)] # ref1.png to ref4.png + + # Check if directory exists + if not os.path.exists(seeds_path): + self.stdout.write(self.style.ERROR(f"Directory not found: {seeds_path}")) + return + + self.stdout.write("Starting image seeding process...") + + # --------------------------------------------------------- + # 2. Process BookReferences + # --------------------------------------------------------- + books = BookReference.objects.all() + book_count = 0 + + if not books.exists(): + self.stdout.write(self.style.WARNING("No BookReference objects found.")) + else: + for index, book in enumerate(books): + # Cycle through the 5 images using modulo operator + image_name = book_images[index % len(book_images)] + image_path = os.path.join(seeds_path, image_name) + + if os.path.exists(image_path): + with open(image_path, 'rb') as f: + # Create the instance + book_img_instance = BookReferenceImage( + book_reference=book, + order=0, + description=[{"language_code": "en", "text": f"Auto-generated image for {book.pk}"}] + ) + # Save the file content to the ImageField + # This automatically handles the upload_to path defined in your model + book_img_instance.image.save(image_name, File(f), save=True) + book_count += 1 + else: + self.stdout.write(self.style.WARNING(f"Image not found: {image_name}")) + + self.stdout.write(self.style.SUCCESS(f"Successfully created {book_count} BookReferenceImages.")) + + # --------------------------------------------------------- + # 3. Process HadisReferences (ReferenceImage) + # --------------------------------------------------------- + # Note: Your model class is 'ReferenceImage', though you asked for 'HadisReferenceImage' + refs = HadisReference.objects.all() + ref_count = 0 + + if not refs.exists(): + self.stdout.write(self.style.WARNING("No HadisReference objects found.")) + else: + for index, ref in enumerate(refs): + # Cycle through the 4 images + image_name = ref_images[index % len(ref_images)] + image_path = os.path.join(seeds_path, image_name) + + if os.path.exists(image_path): + with open(image_path, 'rb') as f: + # Create the instance + ref_img_instance = ReferenceImage( + reference=ref, + priority=0 + ) + # Your model uses 'thumbnail' field, not 'image' + ref_img_instance.thumbnail.save(image_name, File(f), save=True) + ref_count += 1 + else: + self.stdout.write(self.style.WARNING(f"Image not found: {image_name}")) + + self.stdout.write(self.style.SUCCESS(f"Successfully created {ref_count} ReferenceImages.")) \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh index 89747c5..602bccd 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,5 +2,6 @@ sleep 20 # python manage.py migrate +python manage.py seed_images exec "$@"