Browse Source

image script for entry points created.

master
Mohsen Taba 5 months ago
parent
commit
4ebf6076a0
  1. 1
      apps/hadis/admin/reference.py
  2. 86
      apps/hadis/management/commands/seed_images.py
  3. 1
      entrypoint.sh

1
apps/hadis/admin/reference.py

@ -115,7 +115,6 @@ class BookAuthorAdmin(ModelAdmin):
return first.get('text', '-') return first.get('text', '-')
return '-' return '-'
class BookReferenceImageAdmin(ModelAdmin): class BookReferenceImageAdmin(ModelAdmin):
# Display the custom string, plus the raw order and book link for convenience # Display the custom string, plus the raw order and book link for convenience
list_display = ("display_name", "order", "book_reference") list_display = ("display_name", "order", "book_reference")

86
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."))

1
entrypoint.sh

@ -2,5 +2,6 @@
sleep 20 sleep 20
# python manage.py migrate # python manage.py migrate
python manage.py seed_images
exec "$@" exec "$@"
Loading…
Cancel
Save