8 changed files with 254 additions and 14 deletions
-
82apps/hadis/migrations/0072_mawlastatus_transmitters_mawla_statuses.py
-
41apps/hadis/models/transmitter.py
-
37apps/hadis/serializers/hadis.py
-
70apps/hadis/serializers/serializers_admin.py
-
3apps/hadis/urls.py
-
6apps/hadis/views/category.py
-
6apps/hadis/views/transmitter.py
-
23apps/hadis/views_admin.py
@ -0,0 +1,82 @@ |
|||||
|
# Generated by Django 4.2.30 on 2026-10-31 16:15 |
||||
|
|
||||
|
from django.db import migrations, models |
||||
|
from django.utils.text import slugify |
||||
|
|
||||
|
|
||||
|
def migrate_freed_slave_of_to_mawla_status(apps, schema_editor): |
||||
|
Transmitters = apps.get_model('hadis', 'Transmitters') |
||||
|
MawlaStatus = apps.get_model('hadis', 'MawlaStatus') |
||||
|
|
||||
|
def _extract_primary_text(title_list): |
||||
|
if not title_list or not isinstance(title_list, list): |
||||
|
return None |
||||
|
for lang in ('en', 'ru', 'ar', 'fa', 'tr'): |
||||
|
for item in title_list: |
||||
|
if isinstance(item, dict) and item.get('language_code') == lang: |
||||
|
txt = (item.get('text') or item.get('title') or '').strip() |
||||
|
if txt: |
||||
|
return txt |
||||
|
for item in title_list: |
||||
|
if isinstance(item, dict): |
||||
|
txt = (item.get('text') or item.get('title') or '').strip() |
||||
|
if txt: |
||||
|
return txt |
||||
|
return None |
||||
|
|
||||
|
cache = {} |
||||
|
for ms in MawlaStatus.objects.all(): |
||||
|
primary = _extract_primary_text(ms.title) |
||||
|
if primary: |
||||
|
cache[primary.lower()] = ms |
||||
|
|
||||
|
for tx in Transmitters.objects.all(): |
||||
|
raw = tx.freed_slave_of |
||||
|
if not raw or not isinstance(raw, list): |
||||
|
continue |
||||
|
primary = _extract_primary_text(raw) |
||||
|
if not primary: |
||||
|
continue |
||||
|
key = primary.lower() |
||||
|
ms_obj = cache.get(key) |
||||
|
if not ms_obj: |
||||
|
base_slug = slugify(primary, allow_unicode=True)[:240] or 'mawla' |
||||
|
slug = base_slug |
||||
|
counter = 1 |
||||
|
while MawlaStatus.objects.filter(slug=slug).exists(): |
||||
|
slug = f"{base_slug}-{counter}" |
||||
|
counter += 1 |
||||
|
ms_obj = MawlaStatus.objects.create(title=raw, slug=slug) |
||||
|
cache[key] = ms_obj |
||||
|
tx.mawla_statuses.add(ms_obj) |
||||
|
|
||||
|
|
||||
|
class Migration(migrations.Migration): |
||||
|
|
||||
|
dependencies = [ |
||||
|
('hadis', '0071_quranayah_transliteration'), |
||||
|
] |
||||
|
|
||||
|
operations = [ |
||||
|
migrations.CreateModel( |
||||
|
name='MawlaStatus', |
||||
|
fields=[ |
||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), |
||||
|
('title', models.JSONField(default=list, verbose_name='Title')), |
||||
|
('slug', models.SlugField(allow_unicode=True, blank=True, max_length=255, null=True, verbose_name='slug')), |
||||
|
('created_at', models.DateTimeField(auto_now_add=True, verbose_name='created at')), |
||||
|
('updated_at', models.DateTimeField(auto_now=True, verbose_name='updated at')), |
||||
|
], |
||||
|
options={ |
||||
|
'verbose_name': 'Mawla Status', |
||||
|
'verbose_name_plural': 'Mawla Statuses', |
||||
|
'ordering': ('-created_at', '-id'), |
||||
|
}, |
||||
|
), |
||||
|
migrations.AddField( |
||||
|
model_name='transmitters', |
||||
|
name='mawla_statuses', |
||||
|
field=models.ManyToManyField(blank=True, related_name='transmitters', to='hadis.mawlastatus', verbose_name='Mawla Statuses (M2M)'), |
||||
|
), |
||||
|
migrations.RunPython(migrate_freed_slave_of_to_mawla_status, migrations.RunPython.noop), |
||||
|
] |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue