3 changed files with 87 additions and 1 deletions
@ -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.")) |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue