From c23f96c273e74ddf7e6237ad237e70423cceb3da Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Tue, 1 Dec 2020 14:42:49 +0100
Subject: [PATCH] year_od_diagnosis moved to custom field

---
 smash/web/forms/study_subject_forms.py                   | 1 -
 smash/web/migrations/0190_remove_study_related_fields.py | 8 +++++++-
 smash/web/migrations/0191_auto_20201201_1033.py          | 8 ++++++++
 smash/web/models/mail_template.py                        | 2 +-
 smash/web/models/study_columns.py                        | 4 ----
 smash/web/models/study_subject.py                        | 5 -----
 smash/web/tests/forms/test_StudySubjectAddForm.py        | 4 +---
 smash/web/tests/functions.py                             | 1 -
 8 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/smash/web/forms/study_subject_forms.py b/smash/web/forms/study_subject_forms.py
index eec3f8fb..648a0d71 100644
--- a/smash/web/forms/study_subject_forms.py
+++ b/smash/web/forms/study_subject_forms.py
@@ -360,7 +360,6 @@ def prepare_study_subject_fields(fields, study):
     prepare_field(fields, study.columns, 'flying_team')
     prepare_field(fields, study.columns, 'comments')
     prepare_field(fields, study.columns, 'referral')
-    prepare_field(fields, study.columns, 'year_of_diagnosis')
     prepare_field(fields, study.columns, 'information_sent')
     prepare_field(fields, study.columns, 'endpoint_reached')
     prepare_field(fields, study.columns, 'excluded')
diff --git a/smash/web/migrations/0190_remove_study_related_fields.py b/smash/web/migrations/0190_remove_study_related_fields.py
index 484f89e1..09bbf293 100644
--- a/smash/web/migrations/0190_remove_study_related_fields.py
+++ b/smash/web/migrations/0190_remove_study_related_fields.py
@@ -2,7 +2,8 @@
 from django.db import migrations
 
 # noinspection PyUnusedLocal
-from web.models.constants import CUSTOM_FIELD_TYPE_TEXT, GLOBAL_STUDY_ID, CUSTOM_FIELD_TYPE_BOOLEAN
+from web.models.constants import CUSTOM_FIELD_TYPE_TEXT, GLOBAL_STUDY_ID, CUSTOM_FIELD_TYPE_BOOLEAN, \
+    CUSTOM_FIELD_TYPE_INTEGER
 
 
 def create_custom_fields(apps, schema_editor):
@@ -29,6 +30,9 @@ def create_custom_fields(apps, schema_editor):
         brain_donation_agreement_field = CustomStudySubjectField.objects.create(name="Brain donation agreement",
                                                                                 type=CUSTOM_FIELD_TYPE_BOOLEAN,
                                                                                 study_id=GLOBAL_STUDY_ID)
+        year_of_diagnosis_field = CustomStudySubjectField.objects.create(name="Year of diagnosis",
+                                                                         type=CUSTOM_FIELD_TYPE_INTEGER,
+                                                                         study_id=GLOBAL_STUDY_ID)
 
         for subject in StudySubject.objects.all():
             CustomStudySubjectValue.objects.create(study_subject_field=mpower_field, value=subject.mpower_id,
@@ -43,6 +47,8 @@ def create_custom_fields(apps, schema_editor):
                                                    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)
+            CustomStudySubjectValue.objects.create(study_subject_field=year_of_diagnosis_field,
+                                                   value=subject.year_of_diagnosis, study_subject=subject)
 
 
 class Migration(migrations.Migration):
diff --git a/smash/web/migrations/0191_auto_20201201_1033.py b/smash/web/migrations/0191_auto_20201201_1033.py
index 8e35ca5c..ffe68a6d 100644
--- a/smash/web/migrations/0191_auto_20201201_1033.py
+++ b/smash/web/migrations/0191_auto_20201201_1033.py
@@ -34,6 +34,10 @@ class Migration(migrations.Migration):
             model_name='studycolumns',
             name='previously_in_study',
         ),
+        migrations.RemoveField(
+            model_name='studycolumns',
+            name='year_of_diagnosis',
+        ),
 
 
         migrations.RemoveField(
@@ -60,4 +64,8 @@ class Migration(migrations.Migration):
             model_name='studysubject',
             name='previously_in_study',
         ),
+        migrations.RemoveField(
+            model_name='studysubject',
+            name='year_of_diagnosis',
+        ),
     ]
diff --git a/smash/web/models/mail_template.py b/smash/web/models/mail_template.py
index 753245da..dd12b2b2 100644
--- a/smash/web/models/mail_template.py
+++ b/smash/web/models/mail_template.py
@@ -338,7 +338,7 @@ class MailTemplate(models.Model):
                 "##S_ADDRESS##": study_subject.subject.address,
                 "##S_CITY##": study_subject.subject.city,
                 "##S_COUNTRY##": str(study_subject.subject.country),
-                "##S_DIAGNOSIS_YEAR##": str(study_subject.year_of_diagnosis),
+                "##S_DIAGNOSIS_YEAR##": study_subject.get_custom_field_value('Year of diagnosis'),
                 "##S_DATE_ADDED##": date_to_str(study_subject.date_added, DATE_FORMAT_SHORT),
                 "##S_DATE_BORN##": date_born,
                 "##S_DIAGNOSIS##": study_subject.get_custom_field_value('Diagnosis'),
diff --git a/smash/web/models/study_columns.py b/smash/web/models/study_columns.py
index bcf3e94b..8b0b946f 100644
--- a/smash/web/models/study_columns.py
+++ b/smash/web/models/study_columns.py
@@ -48,10 +48,6 @@ class StudyColumns(models.Model):
         default=True,
         verbose_name='Referred by'
     )
-    year_of_diagnosis = models.BooleanField(
-        default=True,
-        verbose_name='Year of diagnosis (YYYY)'
-    )
 
     information_sent = models.BooleanField(
         default=True,
diff --git a/smash/web/models/study_subject.py b/smash/web/models/study_subject.py
index e7289b76..58629a92 100644
--- a/smash/web/models/study_subject.py
+++ b/smash/web/models/study_subject.py
@@ -138,11 +138,6 @@ class StudySubject(models.Model):
                                            verbose_name='Voucher types'
                                            )
 
-    year_of_diagnosis = models.IntegerField(
-        null=True,
-        blank=True,
-        verbose_name='Year of diagnosis (YYYY)'
-    )
 
     information_sent = models.BooleanField(
         verbose_name='Information sent',
diff --git a/smash/web/tests/forms/test_StudySubjectAddForm.py b/smash/web/tests/forms/test_StudySubjectAddForm.py
index 2038c320..58cecf19 100644
--- a/smash/web/tests/forms/test_StudySubjectAddForm.py
+++ b/smash/web/tests/forms/test_StudySubjectAddForm.py
@@ -74,10 +74,8 @@ class StudySubjectAddFormTests(LoggedInWithWorkerTestCase):
         form_data['screening_number'] = "123"
 
         form = StudySubjectAddForm(data=form_data, user=self.user, study=self.study)
-        form.is_valid()
-        form.instance.subject_id = self.subject.id
         self.assertTrue(form.is_valid())
-        self.assertIsNone(form.fields['year_of_diagnosis'].initial)
+        form.instance.subject_id = self.subject.id
         form.save()
 
         form2 = StudySubjectAddForm(data=form_data, user=self.user, study=self.study)
diff --git a/smash/web/tests/functions.py b/smash/web/tests/functions.py
index ce977521..a4831422 100644
--- a/smash/web/tests/functions.py
+++ b/smash/web/tests/functions.py
@@ -53,7 +53,6 @@ def create_empty_study_columns():
         nd_number=False,
         comments=False,
         referral=False,
-        year_of_diagnosis=False,
         information_sent=False,
         resigned=False,
         endpoint_reached=False,
-- 
GitLab