Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
0190_remove_study_related_fields.py 3.53 KiB
# Generated by Django 3.1.3 on 2020-12-01 08:03
from django.db import migrations

# noinspection PyUnusedLocal
from web.models.constants import CUSTOM_FIELD_TYPE_TEXT, GLOBAL_STUDY_ID, CUSTOM_FIELD_TYPE_BOOLEAN


def create_custom_fields(apps, schema_editor):
    # noinspection PyPep8Naming
    StudySubject = apps.get_model("web", "StudySubject")
    if StudySubject.objects.all().count() > 0:
        # noinspection PyPep8Naming
        CustomStudySubjectField = apps.get_model("web", "CustomStudySubjectField")
        # noinspection PyPep8Naming
        CustomStudySubjectValue = apps.get_model("web", "CustomStudySubjectValue")

        mpower_field = CustomStudySubjectField.objects.create(name="MPower ID", type=CUSTOM_FIELD_TYPE_TEXT,
                                                              study_id=GLOBAL_STUDY_ID, unique=True)
        screening_field = CustomStudySubjectField.objects.create(name="Screening", type=CUSTOM_FIELD_TYPE_TEXT,
                                                                 study_id=GLOBAL_STUDY_ID)
        diagnosis_field = CustomStudySubjectField.objects.create(name="Diagnosis", type=CUSTOM_FIELD_TYPE_TEXT,
                                                                 study_id=GLOBAL_STUDY_ID)
        previously_in_study_field = CustomStudySubjectField.objects.create(name="Previously in PDP study",
                                                                           type=CUSTOM_FIELD_TYPE_BOOLEAN,
                                                                           study_id=GLOBAL_STUDY_ID)
        pd_in_family_field = CustomStudySubjectField.objects.create(name="PD in family",
                                                                    type=CUSTOM_FIELD_TYPE_BOOLEAN,
                                                                    study_id=GLOBAL_STUDY_ID)
        brain_donation_agreement_field = CustomStudySubjectField.objects.create(name="Brain donation agreement",
                                                                                type=CUSTOM_FIELD_TYPE_BOOLEAN,
                                                                                study_id=GLOBAL_STUDY_ID)

        for subject in StudySubject.objects.all():
            CustomStudySubjectValue.objects.create(study_subject_field=mpower_field, value=subject.mpower_id,
                                                   study_subject=subject)
            CustomStudySubjectValue.objects.create(study_subject_field=screening_field, value=subject.screening,
                                                   study_subject=subject)
            CustomStudySubjectValue.objects.create(study_subject_field=diagnosis_field, value=subject.diagnosis,
                                                   study_subject=subject)
            CustomStudySubjectValue.objects.create(study_subject_field=previously_in_study_field,
                                                   value=subject.previously_in_study, study_subject=subject)
            CustomStudySubjectValue.objects.create(study_subject_field=brain_donation_agreement_field,
                                                   value=subject.brain_donation_agreement, study_subject=subject)
            CustomStudySubjectValue.objects.create(study_subject_field=pd_in_family_field,
                                                   value=subject.pd_in_family, study_subject=subject)


class Migration(migrations.Migration):
    dependencies = [
        ('web', '0189_remove_studysubject_virus_test_1'),
    ]

    operations = [
        migrations.RunPython(create_custom_fields),
    ]