# Generated by Django 5.0 on 2023-12-16 15:07
from django.apps import apps as django_apps
from django.db import migrations
from django.db.models import Count
from tqdm import tqdm


def update_site(apps, schema_editor):
    model_cls = django_apps.get_model("intecomm_screening.patientlogreportprinthistory")
    patient_log_model_cls = django_apps.get_model("intecomm_screening.patientlog")
    patient_log_identifiers = model_cls.objects.values("patient_log_identifier").annotate(
        count=Count("patient_log_identifier")
    )
    total = len(patient_log_identifiers)
    for data in tqdm(patient_log_identifiers, total=total):
        patient_log = patient_log_model_cls.objects.get(
            patient_log_identifier=data.get("patient_log_identifier")
        )
        model_cls.objects.filter(
            patient_log_identifier=data.get("patient_log_identifier")
        ).update(site=patient_log.site)


class Migration(migrations.Migration):
    dependencies = [
        ("intecomm_screening", "0066_alter_patientlogreportprinthistory_managers_and_more"),
    ]

    operations = [migrations.RunPython(update_site)]
