Browse Source
feat(reference): add BookAuthorRutba model, admin, sync serializer, and migration
feat(reference): add BookAuthorRutba model, admin, sync serializer, and migration
- Introduce BookAuthorRutba model with title, slug, color, and description - Link BookAuthor to BookAuthorRutba via rutba_status foreign key - Update reference admin to manage BookAuthorRutba and assign rutba status - Include rutba_detail in BookAuthorDetailSerializer and BookAuthorSyncSerializer - Add migration 0054 to seed rutba records from existing authorsmaster
5 changed files with 204 additions and 7 deletions
-
25apps/hadis/admin/reference.py
-
76apps/hadis/migrations/0054_bookauthorrutba_opinionstatus_description_and_more.py
-
59apps/hadis/models/reference.py
-
47apps/hadis/serializers/reference_v2.py
-
4apps/hadis/views/reference_v2.py
@ -0,0 +1,76 @@ |
|||
# Generated by Django 4.2.30 on 2026-10-19 12:08 |
|||
|
|||
from django.db import migrations, models |
|||
import django.db.models.deletion |
|||
from django.utils.text import slugify |
|||
|
|||
|
|||
def migrate_existing_author_rutbas(apps, schema_editor): |
|||
BookAuthor = apps.get_model('hadis', 'BookAuthor') |
|||
BookAuthorRutba = apps.get_model('hadis', 'BookAuthorRutba') |
|||
|
|||
seen = {} |
|||
for author in BookAuthor.objects.exclude(rutba=[]): |
|||
if not author.rutba or not isinstance(author.rutba, list): |
|||
continue |
|||
first_text = "" |
|||
for entry in author.rutba: |
|||
if isinstance(entry, dict): |
|||
txt = (entry.get("text") or entry.get("title") or "").strip() |
|||
if txt: |
|||
first_text = txt |
|||
break |
|||
if not first_text: |
|||
continue |
|||
key = first_text.lower() |
|||
if key not in seen: |
|||
slug_val = slugify(first_text, allow_unicode=True) or f"rutba-{author.id}" |
|||
rutba_obj = BookAuthorRutba.objects.create( |
|||
title=author.rutba, |
|||
slug=slug_val, |
|||
color='green', |
|||
description=[], |
|||
) |
|||
seen[key] = rutba_obj |
|||
author.rutba_status = seen[key] |
|||
author.save(update_fields=['rutba_status']) |
|||
|
|||
|
|||
class Migration(migrations.Migration): |
|||
|
|||
dependencies = [ |
|||
('hadis', '0053_scholarlyopinion_and_transmitteropinion_opinion'), |
|||
] |
|||
|
|||
operations = [ |
|||
migrations.CreateModel( |
|||
name='BookAuthorRutba', |
|||
fields=[ |
|||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), |
|||
('color', models.CharField(choices=[('red', 'Red'), ('green', 'Green'), ('blue', 'Blue'), ('yellow', 'Yellow'), ('orange', 'Orange'), ('purple', 'Purple'), ('gray', 'Gray')], default='gray', max_length=20, verbose_name='color')), |
|||
('title', models.JSONField(default=list, verbose_name='Title')), |
|||
('slug', models.SlugField(allow_unicode=True, blank=True, max_length=255, null=True, verbose_name='slug')), |
|||
('description', models.JSONField(default=list, verbose_name='Description')), |
|||
], |
|||
options={ |
|||
'verbose_name': 'Author Rutba', |
|||
'verbose_name_plural': 'Author Rutbas', |
|||
}, |
|||
), |
|||
migrations.AddField( |
|||
model_name='opinionstatus', |
|||
name='description', |
|||
field=models.JSONField(default=list, verbose_name='Description'), |
|||
), |
|||
migrations.AddField( |
|||
model_name='transmitterreliability', |
|||
name='description', |
|||
field=models.JSONField(default=list, verbose_name='Description'), |
|||
), |
|||
migrations.AddField( |
|||
model_name='bookauthor', |
|||
name='rutba_status', |
|||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='authors', to='hadis.bookauthorrutba', verbose_name='Rutba Status'), |
|||
), |
|||
migrations.RunPython(migrate_existing_author_rutbas, migrations.RunPython.noop), |
|||
] |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue