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.
109 lines
4.5 KiB
109 lines
4.5 KiB
# Generated by Django 4.2.30 on 2026-10-21 10:21
|
|
|
|
from django.db import migrations, models
|
|
from django.utils.text import slugify
|
|
|
|
|
|
def populate_professions_from_existing_records(apps, schema_editor):
|
|
TransmitterProfession = apps.get_model('hadis', 'TransmitterProfession')
|
|
Transmitters = apps.get_model('hadis', 'Transmitters')
|
|
|
|
profession_by_text = {}
|
|
for p in TransmitterProfession.objects.all():
|
|
if isinstance(p.title, list):
|
|
for entry in p.title:
|
|
if isinstance(entry, dict):
|
|
txt = (entry.get('text') or entry.get('title') or '').strip().lower()
|
|
if txt:
|
|
profession_by_text[txt] = p
|
|
|
|
def get_or_create_profession(json_list):
|
|
if not json_list or not isinstance(json_list, list):
|
|
return None
|
|
cleaned = []
|
|
lookup_keys = []
|
|
for entry in json_list:
|
|
if isinstance(entry, dict):
|
|
lang = (entry.get('language_code') or '').strip()
|
|
txt = (entry.get('text') or entry.get('title') or '').strip()
|
|
if lang and txt:
|
|
cleaned.append({'language_code': lang, 'text': txt, 'title': txt})
|
|
lookup_keys.append(txt.lower())
|
|
if not cleaned:
|
|
return None
|
|
|
|
for key in lookup_keys:
|
|
if key in profession_by_text:
|
|
existing = profession_by_text[key]
|
|
existing_langs = {
|
|
item.get('language_code'): item
|
|
for item in (existing.title or [])
|
|
if isinstance(item, dict) and item.get('language_code')
|
|
}
|
|
updated = False
|
|
for item in cleaned:
|
|
if item['language_code'] not in existing_langs:
|
|
existing_langs[item['language_code']] = item
|
|
updated = True
|
|
if updated:
|
|
existing.title = list(existing_langs.values())
|
|
existing.save(update_fields=['title'])
|
|
for k in lookup_keys:
|
|
profession_by_text[k] = existing
|
|
return existing
|
|
|
|
first_text = cleaned[0]['text']
|
|
slug_val = slugify(first_text, allow_unicode=True)[:240] or 'profession'
|
|
prof_obj = TransmitterProfession.objects.create(title=cleaned, slug=slug_val)
|
|
for k in lookup_keys:
|
|
profession_by_text[k] = prof_obj
|
|
return prof_obj
|
|
|
|
for tr in Transmitters.objects.all().iterator():
|
|
source_data = getattr(tr, 'occupation', None) or getattr(tr, 'profession', None)
|
|
if source_data and isinstance(source_data, list):
|
|
p_obj = get_or_create_profession(source_data)
|
|
if p_obj:
|
|
tr.professions_list.add(p_obj)
|
|
if not tr.profession:
|
|
tr.profession = p_obj.title
|
|
tr.save(update_fields=['profession'])
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
|
('hadis', '0063_relation_type_and_relations'),
|
|
]
|
|
|
|
operations = [
|
|
migrations.CreateModel(
|
|
name='TransmitterProfession',
|
|
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': 'Transmitter Profession',
|
|
'verbose_name_plural': 'Transmitter Professions',
|
|
'ordering': ('-created_at', '-id'),
|
|
},
|
|
),
|
|
migrations.AddField(
|
|
model_name='transmitters',
|
|
name='profession',
|
|
field=models.JSONField(blank=True, default=list, verbose_name='Profession'),
|
|
),
|
|
migrations.AddField(
|
|
model_name='transmitters',
|
|
name='professions_list',
|
|
field=models.ManyToManyField(blank=True, related_name='transmitters', to='hadis.transmitterprofession', verbose_name='Professions (M2M)'),
|
|
),
|
|
migrations.RunPython(
|
|
populate_professions_from_existing_records,
|
|
reverse_code=migrations.RunPython.noop,
|
|
),
|
|
]
|