# Generated by Django 4.2.5 on 2023-10-05 23:20
from django.db import migrations
from edc_list_data import PreloadData
from tqdm import tqdm

from intecomm_lists.list_data import list_data


def update_infosources(apps, schema_editor):
    PreloadData(list_data=list_data, apps=apps)
    old_model_cls = apps.get_model("edc_next_appointment.infosources")
    new_model_cls = apps.get_model("edc_appointment.infosources")
    crf_model_cls = apps.get_model("intecomm_subject.nextappointment")
    total = crf_model_cls.objects.all().count()
    for obj in tqdm(crf_model_cls.objects.all(), total=total):
        info_source = old_model_cls.objects.get(id=obj.info_source_id)
        info_source_new = new_model_cls.objects.get(name=info_source.name)
        obj.info_source_new = info_source_new
        obj.save_base(update_fields=["info_source_new"])


class Migration(migrations.Migration):
    dependencies = [
        ("intecomm_subject", "0118_historicalnextappointment_info_source_new_and_more"),
    ]

    operations = [migrations.RunPython(update_infosources)]
