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.
81 lines
3.3 KiB
81 lines
3.3 KiB
# backend/apps/hadis/management/commands/seed_edition_editors.py
|
|
|
|
import random
|
|
from django.core.management.base import BaseCommand
|
|
from django.db import transaction
|
|
from django.utils.text import slugify
|
|
from apps.hadis.models import BookEdition, BookAuthor, BookEditor
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Assigns editors to each BookEdition: one random text-only and one linked to a random existing BookAuthor.'
|
|
|
|
def handle(self, *args, **options):
|
|
self.stdout.write(self.style.WARNING("Starting to seed edition editors..."))
|
|
|
|
editions = list(BookEdition.objects.all())
|
|
authors = list(BookAuthor.objects.all())
|
|
|
|
if not editions:
|
|
self.stdout.write(self.style.ERROR("No BookEditions found in the database."))
|
|
return
|
|
|
|
if not authors:
|
|
self.stdout.write(self.style.ERROR("No BookAuthors found. Please add some authors first."))
|
|
return
|
|
|
|
# 1. Clean existing editors
|
|
self.stdout.write("Deleting existing BookEditors...")
|
|
BookEditor.objects.all().delete()
|
|
|
|
random_editor_names = [
|
|
{"fa": "دکتر حسین نوری", "en": "Dr. Hossein Noori"},
|
|
{"fa": "استاد عباس محمودی", "en": "Professor Abbas Mahmoudi"},
|
|
{"fa": "دکتر زهرا طاهری", "en": "Dr. Zahra Taheri"},
|
|
{"fa": "مصحح مهدی حیدری", "en": "Editor Mehdi Heidari"},
|
|
{"fa": "دکتر کاظم مرادی", "en": "Dr. Kazem Moradi"},
|
|
{"fa": "استاد سارا ابراهیمی", "en": "Professor Sara Ebrahimi"},
|
|
{"fa": "دکتر قاسم باقری", "en": "Dr. Ghasem Bagheri"},
|
|
]
|
|
|
|
text_only_count = 0
|
|
linked_author_count = 0
|
|
|
|
with transaction.atomic():
|
|
for edition in editions:
|
|
# 1. First editor: Text-only (Random Name & Slug)
|
|
name_data = random.choice(random_editor_names)
|
|
name_field = [
|
|
{"language_code": "fa", "text": name_data["fa"]},
|
|
{"language_code": "en", "text": name_data["en"]},
|
|
]
|
|
# Ensure clean slug
|
|
base_slug = slugify(name_data["en"])
|
|
slug_val = base_slug
|
|
counter = 1
|
|
while BookEditor.objects.filter(slug=slug_val).exists():
|
|
slug_val = f"{base_slug}-{counter}"
|
|
counter += 1
|
|
|
|
BookEditor.objects.create(
|
|
book_edition=edition,
|
|
name=name_field,
|
|
slug=slug_val,
|
|
author=None
|
|
)
|
|
text_only_count += 1
|
|
|
|
# 2. Second editor: Linked to a random BookAuthor
|
|
random_author = random.choice(authors)
|
|
BookEditor.objects.create(
|
|
book_edition=edition,
|
|
author=random_author
|
|
)
|
|
linked_author_count += 1
|
|
|
|
self.stdout.write(
|
|
self.style.SUCCESS(
|
|
f"Successfully seeded editors for {len(editions)} BookEditions!\n"
|
|
f"- Created {text_only_count} text-only editors.\n"
|
|
f"- Created {linked_author_count} editors linked to existing authors."
|
|
)
|
|
)
|