2 Commits
7e0e0bc550
...
928613dd7a
| Author | SHA1 | Message | Date |
|---|---|---|---|
|
|
928613dd7a |
query optimization
|
3 weeks ago |
|
|
f28cb75dda |
references updated with editor field
|
3 weeks ago |
6 changed files with 215 additions and 10 deletions
-
81apps/hadis/management/commands/seed_edition_editors.py
-
28apps/hadis/migrations/0033_bookeditor.py
-
37apps/hadis/models/reference.py
-
11apps/hadis/serializers/reference_v2.py
-
54apps/hadis/serializers/serializers_admin.py
-
14apps/hadis/views_admin.py
@ -0,0 +1,81 @@ |
|||
# 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." |
|||
) |
|||
) |
|||
@ -0,0 +1,28 @@ |
|||
# Generated by Django 4.2.30 on 2026-08-01 10:51 |
|||
|
|||
from django.db import migrations, models |
|||
import django.db.models.deletion |
|||
|
|||
|
|||
class Migration(migrations.Migration): |
|||
|
|||
dependencies = [ |
|||
('hadis', '0032_alter_bookauthor_slug_alter_bookreference_slug_and_more'), |
|||
] |
|||
|
|||
operations = [ |
|||
migrations.CreateModel( |
|||
name='BookEditor', |
|||
fields=[ |
|||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), |
|||
('name', models.JSONField(blank=True, default=list, verbose_name='Name')), |
|||
('slug', models.SlugField(allow_unicode=True, blank=True, max_length=255, null=True, verbose_name='slug')), |
|||
('author', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='editor_profiles', to='hadis.bookauthor', verbose_name='Author')), |
|||
('book_edition', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='editors', to='hadis.bookedition', verbose_name='Book Edition')), |
|||
], |
|||
options={ |
|||
'verbose_name': 'Book Editor', |
|||
'verbose_name_plural': 'Book Editors', |
|||
}, |
|||
), |
|||
] |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue