You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

74 lines
3.1 KiB

import json
from django.core.management.base import BaseCommand
from apps.hadis.models import BookReference, BookAuthor, BookEdition, BookVolume
class Command(BaseCommand):
help = 'Populates BookReference, BookEdition, and BookVolume with default test data if empty.'
def handle(self, *args, **kwargs):
self.stdout.write("Starting to populate book metadata...")
# Default values
default_title = "Musnad Abu Ya'la"
default_author_name = "Abu Ya'la al-Mawsili, Imam Hafiz Ahmad ibn Ali ibn al-Muthanna al-Tamimi"
default_summary = "Various publishers (originally compiled in manuscript form; published by multiple Islamic publishers over time, such as Dar al-Ihya al-Turath al-Arabi, Beirut)"
default_publisher = "Dar al-Hadith"
default_city = "Cairo"
default_country = "Egypt"
default_edition_stmt = "1st edition"
default_year = "1434 AH / 2013 CE"
default_source_url = "https://shamela.ws/book/181"
default_tag = "Hadith collection"
books = BookReference.objects.all()
count = 0
for book in books:
# 1. Update Title and Description if empty
if not book.title:
book.title = [{"language_code": "en", "text": default_title}]
if not book.description:
book.description = [{"language_code": "en", "text": default_summary}]
book.save()
# 2. Assign Author if none exists
if not book.authors.exists():
author, created = BookAuthor.objects.get_or_create(
slug="abu-yala-al-mawsili",
defaults={
"name": [{"language_code": "en", "text": default_author_name}]
}
)
book.authors.add(author)
# 3. Create BookEdition if none exists
if not book.editions.exists():
edition = BookEdition.objects.create(
book_reference=book,
publisher=[{"language_code": "en", "text": default_publisher}],
city_of_publication=[{"language_code": "en", "text": default_city}],
country_of_publication=[{"language_code": "en", "text": default_country}],
edition_number=default_edition_stmt,
year_of_publication=default_year,
source_url=default_source_url,
number_of_volumes=1,
tags=[{"language_code": "en", "text": default_tag}],
notes=[]
)
else:
edition = book.editions.first()
# 4. Create BookVolume if none exists
if not book.volumes.exists():
BookVolume.objects.create(
book_reference=book,
edition=edition,
title="Volume 1"
)
count += 1
self.stdout.write(self.style.SUCCESS(f"Successfully populated metadata for {count} books."))