11 changed files with 295 additions and 66 deletions
-
14apps/hadis/admin/reference.py
-
81apps/hadis/management/commands/seed_edition_researchers.py
-
51apps/hadis/migrations/0030_remove_bookauthor_book_references_and_more.py
-
52apps/hadis/models/reference.py
-
7apps/hadis/serializers/hadis.py
-
35apps/hadis/serializers/reference_v2.py
-
60apps/hadis/serializers/serializers_admin.py
-
21apps/hadis/views/hadis.py
-
31apps/hadis/views/reference_v2.py
-
4apps/hadis/views/transmitter.py
-
5apps/hadis/views_admin.py
@ -0,0 +1,81 @@ |
|||
# backend/apps/hadis/management/commands/seed_edition_researchers.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, BookResearcher |
|||
|
|||
class Command(BaseCommand): |
|||
help = 'Assigns two researchers to each BookEdition: one random text-only, one linked to a random existing BookAuthor.' |
|||
|
|||
def handle(self, *args, **options): |
|||
self.stdout.write(self.style.WARNING("Starting to seed edition researchers...")) |
|||
|
|||
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 researchers |
|||
self.stdout.write("Deleting existing BookResearchers...") |
|||
BookResearcher.objects.all().delete() |
|||
|
|||
random_researcher_names = [ |
|||
{"fa": "دکتر سید علی موسوی", "en": "Dr. Seyed Ali Mousavi"}, |
|||
{"fa": "استاد محمد کریمی", "en": "Professor Mohammad Karimi"}, |
|||
{"fa": "دکتر رضا علوی", "en": "Dr. Reza Alavi"}, |
|||
{"fa": "پژوهشگر حمید احمدی", "en": "Researcher Hamid Ahmadi"}, |
|||
{"fa": "دکتر مریم رضایی", "en": "Dr. Maryam Rezaei"}, |
|||
{"fa": "استاد فاطمه حسینی", "en": "Professor Fatemeh Hosseini"}, |
|||
{"fa": "دکتر جعفر صادقی", "en": "Dr. Jafar Sadeghi"}, |
|||
] |
|||
|
|||
text_only_count = 0 |
|||
linked_author_count = 0 |
|||
|
|||
with transaction.atomic(): |
|||
for edition in editions: |
|||
# 1. First researcher: Text-only (Random Name & Slug) |
|||
name_data = random.choice(random_researcher_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 BookResearcher.objects.filter(slug=slug_val).exists(): |
|||
slug_val = f"{base_slug}-{counter}" |
|||
counter += 1 |
|||
|
|||
BookResearcher.objects.create( |
|||
book_edition=edition, |
|||
name=name_field, |
|||
slug=slug_val, |
|||
author=None |
|||
) |
|||
text_only_count += 1 |
|||
|
|||
# 2. Second researcher: Linked to a random BookAuthor |
|||
random_author = random.choice(authors) |
|||
BookResearcher.objects.create( |
|||
book_edition=edition, |
|||
author=random_author |
|||
) |
|||
linked_author_count += 1 |
|||
|
|||
self.stdout.write( |
|||
self.style.SUCCESS( |
|||
f"Successfully seeded researchers for {len(editions)} BookEditions!\n" |
|||
f"- Created {text_only_count} text-only researchers.\n" |
|||
f"- Created {linked_author_count} researchers linked to existing authors." |
|||
) |
|||
) |
|||
@ -0,0 +1,51 @@ |
|||
# Generated by Django 5.2.12 on 2026-07-07 16:02 |
|||
|
|||
import django.db.models.deletion |
|||
from django.db import migrations, models |
|||
|
|||
def copy_m2m_to_fk(apps, schema_editor): |
|||
BookReference = apps.get_model('hadis', 'BookReference') |
|||
for book in BookReference.objects.all(): |
|||
# Get first author from many-to-many relationship |
|||
# The relationship is still accessible as 'authors' on BookReference in this state |
|||
first_author = book.authors.first() if hasattr(book, 'authors') else None |
|||
if first_author: |
|||
book.author = first_author |
|||
book.save() |
|||
|
|||
class Migration(migrations.Migration): |
|||
|
|||
dependencies = [ |
|||
('hadis', '0029_booktype_sect'), |
|||
] |
|||
|
|||
operations = [ |
|||
migrations.AddField( |
|||
model_name='bookreference', |
|||
name='author', |
|||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='book_references', to='hadis.bookauthor', verbose_name='Author'), |
|||
), |
|||
migrations.RunPython(copy_m2m_to_fk, reverse_code=migrations.RunPython.noop), |
|||
migrations.RemoveField( |
|||
model_name='bookauthor', |
|||
name='book_references', |
|||
), |
|||
migrations.RemoveField( |
|||
model_name='bookreference', |
|||
name='researcher', |
|||
), |
|||
migrations.CreateModel( |
|||
name='BookResearcher', |
|||
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(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='researcher_profiles', to='hadis.bookauthor', verbose_name='Author')), |
|||
('book_edition', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='researchers', to='hadis.bookedition', verbose_name='Book Edition')), |
|||
], |
|||
options={ |
|||
'verbose_name': 'Book Researcher', |
|||
'verbose_name_plural': 'Book Researchers', |
|||
}, |
|||
), |
|||
] |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue