# Generated by Django 3.0.9 on 2020-08-26 16:13

from django.db import migrations
from django.db.models.signals import post_save, pre_save
from edc_utils import DisableSignals


def convert_investigations_to_clinical_review(apps, schema_editor):
    try:
        investigations_model_cls = apps.get_model("inte_subject.investigations")
    except LookupError:
        pass
    else:
        clinical_review_model_cls = apps.get_model("inte_subject.clinicalreview")
        for obj in investigations_model_cls.objects.all():
            opts = dict(
                created=obj.created,
                modified=obj.modified,
                user_created=obj.user_created,
                user_modified=obj.user_modified,
                hostname_created=obj.hostname_created,
                hostname_modified=obj.hostname_modified,
                device_created=obj.device_created,
                device_modified=obj.device_modified,
                consent_model=obj.consent_model,
                consent_version=obj.consent_version,
                site_id=obj.site_id,
                subject_visit_id=obj.subject_visit_id,
                report_datetime=obj.report_datetime,
                hiv_tested=obj.hiv_tested,
                hypertension_tested=obj.hypertension_tested,
                diabetes_tested=obj.diabetes_tested,
                crf_status=obj.crf_status,
                crf_status_comments=obj.crf_status_comments,
            )
            clinical_review = clinical_review_model_cls()
            for k, v in opts.items():
                setattr(clinical_review, k, v)
            with DisableSignals(disabled_signals=[pre_save]):
                clinical_review.save()


class Migration(migrations.Migration):

    dependencies = [
        ("inte_subject", "0040_auto_20200826_1549"),
    ]

    operations = [migrations.RunPython(convert_investigations_to_clinical_review)]
