2 changed files with 95 additions and 1 deletions
@ -0,0 +1,93 @@ |
|||
import os |
|||
import random |
|||
from django.core.management.base import BaseCommand |
|||
from django.core.files import File |
|||
from django.conf import settings |
|||
# REMEMBER: Change 'your_app' to your actual app name |
|||
from apps.hadis.models.hadis import Hadis, HadisReference, ReferenceImage |
|||
from apps.hadis.models.reference import BookReference |
|||
|
|||
class Command(BaseCommand): |
|||
help = 'Wipes old reference data and seeds new HadisReference and ReferenceImages' |
|||
|
|||
def handle(self, *args, **kwargs): |
|||
self.stdout.write(self.style.WARNING('--- Starting Cleanup ---')) |
|||
|
|||
# 1. DELETE PREVIOUS DATA |
|||
# We delete HadisReference. Because of on_delete=models.CASCADE in ReferenceImage, |
|||
# deleting the reference will automatically delete the associated images. |
|||
# However, we can delete both explicitly to be sure and show counts. |
|||
|
|||
deleted_imgs_count, _ = ReferenceImage.objects.all().delete() |
|||
deleted_refs_count, _ = HadisReference.objects.all().delete() |
|||
|
|||
self.stdout.write(self.style.SUCCESS(f'Deleted {deleted_imgs_count} old ReferenceImages.')) |
|||
self.stdout.write(self.style.SUCCESS(f'Deleted {deleted_refs_count} old HadisReferences.')) |
|||
self.stdout.write('------------------------') |
|||
|
|||
# 2. Setup Paths for Seeding |
|||
base_image_path = os.path.join(settings.BASE_DIR, 'seeds', 'images') |
|||
# We look for ref1.png, ref2.png, ref3.png, ref4.png |
|||
image_files = [f'ref{i}.png' for i in range(1, 5)] |
|||
|
|||
# Verify images exist locally before starting |
|||
for img_name in image_files: |
|||
full_path = os.path.join(base_image_path, img_name) |
|||
if not os.path.exists(full_path): |
|||
self.stdout.write(self.style.ERROR(f'CRITICAL ERROR: Image not found at {full_path}')) |
|||
return |
|||
|
|||
# 3. Fetch Parents |
|||
books = list(BookReference.objects.all()) |
|||
hadiths = list(Hadis.objects.all()) |
|||
|
|||
if len(hadiths) < 2: |
|||
self.stdout.write(self.style.ERROR('Not enough Hadiths in DB (need at least 2).')) |
|||
return |
|||
|
|||
if not books: |
|||
self.stdout.write(self.style.ERROR('No BookReferences found in DB.')) |
|||
return |
|||
|
|||
self.stdout.write('--- Starting Seeding ---') |
|||
|
|||
counter_refs = 0 |
|||
counter_imgs = 0 |
|||
|
|||
# 4. Iterate Books (Constraint 1: Each book must relate to at least 2 hadith) |
|||
for book in books: |
|||
# Pick 2 random hadiths for this book |
|||
selected_hadiths = random.sample(hadiths, 2) |
|||
|
|||
for hadis_obj in selected_hadiths: |
|||
# Create the Link (HadisReference) |
|||
hadis_ref = HadisReference.objects.create( |
|||
hadis=hadis_obj, |
|||
book_reference=book, |
|||
description=[ |
|||
{'language_code': 'en', 'text': f'Reference for {book.title[0].get("text", "Book")} - Hadith {hadis_obj.number}'}, |
|||
{'language_code': 'ar', 'text': 'وصف مرجعي'} |
|||
] |
|||
) |
|||
counter_refs += 1 |
|||
|
|||
# 5. Connect Images (Constraint 2: Each hadith ref must have 2 images) |
|||
# We pick 2 random images from our list of 4 |
|||
selected_images = random.sample(image_files, 2) |
|||
|
|||
for priority_index, img_name in enumerate(selected_images): |
|||
img_path = os.path.join(base_image_path, img_name) |
|||
|
|||
with open(img_path, 'rb') as f: |
|||
# Create ReferenceImage |
|||
ref_img = ReferenceImage( |
|||
reference=hadis_ref, |
|||
priority=priority_index # 0 and 1 |
|||
) |
|||
# Save file to the ImageField |
|||
ref_img.thumbnail.save(img_name, File(f), save=True) |
|||
counter_imgs += 1 |
|||
|
|||
self.stdout.write(self.style.SUCCESS( |
|||
f'DONE: Created {counter_refs} HadisReferences and {counter_imgs} ReferenceImages.' |
|||
)) |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue