from django.core.management.base import BaseCommand from django.db import transaction from apps.library.models import Author class Command(BaseCommand): help = 'Populates the newly added fields for all existing authors with sample English data' def handle(self, *args, **options): self.stdout.write(self.style.WARNING('--- Starting Author Fields Population ---')) authors = Author.objects.all() if not authors.exists(): self.stdout.write(self.style.ERROR('No authors found in the database.')) return self.stdout.write(self.style.SUCCESS(f'Found {authors.count()} authors. Commencing update...')) updated_count = 0 with transaction.atomic(): for author in authors: # 1. Researcher / Editor if not author.researcher_editor: author.researcher_editor = f"Edited and researched by {author.full_name}" # 2. Publisher if not author.publisher: author.publisher = "Imam Javad Publishing Institute" # 3. Publication Place if not author.publication_place: author.publication_place = "Qom, Iran" # 4. Edition Statement if not author.edition_statement: author.edition_statement = "First Edition (Revised)" # 5. Year if not author.year: author.year = "1448 AH" # 6. Number of Volumes if not author.number_of_volumes: author.number_of_volumes = 5 # 7. Notes if not author.notes: author.notes = f"This edition contains annotations and key commentaries regarding the works of {author.full_name}." # 8. Online Source if not author.online_source: author.online_source = "https://imamjavad.nwhco.ir" # 9. Tag if not author.tag: author.tag = "Reference Books" # 10. Copy thumbnail to image if thumbnail exists and image doesn't if author.thumbnail and not author.image: author.image = author.thumbnail # 11. Date of Birth (text) if not author.date_of_birth: if author.birth_date: author.date_of_birth = f"{author.birth_date.year} AD" else: author.date_of_birth = "Around 4th Century AH" # 12. Date of Death (text) if not author.date_of_death: if author.death_date: author.date_of_death = f"{author.death_date.year} AD" else: author.date_of_death = "Around 5th Century AH" author.save() updated_count += 1 self.stdout.write(f"Updated author: {author.full_name}") self.stdout.write(self.style.SUCCESS(f'--- Finished! Successfully updated {updated_count} authors ---'))