From 2af4c35b0c428802197243f89600281197b21a70 Mon Sep 17 00:00:00 2001
From: Carlos Vega <carlos.vega@uni.lu>
Date: Wed, 21 Nov 2018 11:24:51 +0100
Subject: [PATCH] changed order in code and controlled cases where the fields
 are not present

---
 smash/web/forms/study_forms.py | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/smash/web/forms/study_forms.py b/smash/web/forms/study_forms.py
index c0bb377f..a0c6b33f 100644
--- a/smash/web/forms/study_forms.py
+++ b/smash/web/forms/study_forms.py
@@ -20,26 +20,25 @@ class StudyEditForm(ModelForm):
         #check regex
         nd_number_study_subject_regex = cleaned_data.get('nd_number_study_subject_regex')
 
-        if StudySubject.check_nd_number_regex(nd_number_study_subject_regex) == False:
+        if nd_number_study_subject_regex is None or StudySubject.check_nd_number_regex(nd_number_study_subject_regex) == False:
             self.add_error('nd_number_study_subject_regex', 'Please enter a valid nd_number_study_subject_regex regex.')
 
         #check default_visit_duration_in_months
-        t = datetime.datetime.today()
-
         visit_duration_in_months = cleaned_data.get('default_visit_duration_in_months')
-        visit_duration = relativedelta(months=int(visit_duration_in_months))
-
         control_follow_up = cleaned_data.get('default_delta_time_for_control_follow_up')
         patient_follow_up = cleaned_data.get('default_delta_time_for_patient_follow_up')
         units = cleaned_data.get('default_delta_time_for_follow_up_units')
 
-        control_delta = relativedelta(**{units: control_follow_up})
-        patient_delta = relativedelta(**{units: patient_follow_up})
+        if None not in [visit_duration_in_months, control_follow_up, patient_follow_up, units]:
+            t = datetime.datetime.today()
+            visit_duration = relativedelta(months=int(visit_duration_in_months))
+            control_delta = relativedelta(**{units: control_follow_up})
+            patient_delta = relativedelta(**{units: patient_follow_up})
         
-        #relative time delta has no __cmp__ method, so we add them to a datetime
-        min_delta_time = min((t + control_delta), (t + patient_delta))
-        if (t+visit_duration) > min_delta_time:
-            self.add_error('default_visit_duration_in_months', 'Please enter a valid "duration of the visits". It must be shorter than the time difference between patient and control visits.')
+            #relative time delta has no __cmp__ method, so we add them to a datetime
+            min_delta_time = min((t + control_delta), (t + patient_delta))
+            if (t+visit_duration) > min_delta_time:
+                self.add_error('default_visit_duration_in_months', 'Please enter a valid "duration of the visits". It must be shorter than the time difference between patient and control visits.')
 
         return cleaned_data
 
-- 
GitLab