diff --git a/smash/web/forms.py b/smash/web/forms.py index b8866f178c2da1094f2633e42a432c1740ea2dec..7b6a7c149d4161a011786a7a90e3efc03704468f 100644 --- a/smash/web/forms.py +++ b/smash/web/forms.py @@ -25,7 +25,7 @@ def validate_subject_nd_number(self): if subject['nd_number']!="": subjects_from_db = Subject.objects.filter(nd_number=subject['nd_number']) if (len(subjects_from_db)>0): - if (subjects_from_db[0].screening_number!= subject['screening_number']): + if subjects_from_db[0].screening_number!= subject.get('screening_number',''): self.add_error('nd_number', "ND number already in use") @@ -42,31 +42,13 @@ class SubjectAddForm(ModelForm): def clean(self): subject = self.cleaned_data - subjects_from_db = Subject.objects.filter(screening_number=subject['screening_number']) + subjects_from_db = Subject.objects.filter(screening_number=subject.get('screening_number','')) if len(subjects_from_db)>0: self.add_error('screening_number', "Screening number already in use") validate_subject_nd_number(self) - def get_new_screening_number(self): - result = 1; - numbers = Subject.objects.values_list('screening_number') - for number_row in numbers: - try: - id = int(number_row[0]) - result = max(result, id+1) - except: - pass - return result - - - def clean_screening_number(self): - if self.cleaned_data['screening_number']=="": - self.cleaned_data['screening_number'] = self.get_new_screening_number() - - return self.cleaned_data['screening_number']; - class SubjectDetailForm(ModelForm): class Meta: model = Subject diff --git a/smash/web/models.py b/smash/web/models.py index a1041ee7e1217ec0bec5fa0f80fb3204218e360d..ba8f9e58062daaa42986546af9ed65a9a9e83dad 100644 --- a/smash/web/models.py +++ b/smash/web/models.py @@ -137,7 +137,6 @@ class Subject(models.Model): verbose_name='Country' ) screening_number = models.CharField(max_length=50, - blank=True, unique=True, verbose_name='Screening number' ) diff --git a/smash/web/tests/test_SubjectAddForm.py b/smash/web/tests/test_SubjectAddForm.py index bbe6a553652b01e679857d3bc38b288bb68cfd3c..4e78578c3a93d9f901802242cfaae538c2fee014 100644 --- a/smash/web/tests/test_SubjectAddForm.py +++ b/smash/web/tests/test_SubjectAddForm.py @@ -12,6 +12,7 @@ class SubjectAddFormTests(TestCase): 'sex' : Subject.SEX_CHOICES_MALE, 'type' : Subject.SUBJECT_TYPE_CHOICES_CONTROL, 'default_location' : location.id, + 'screening_number' : "123", 'country' : 'Luxembourg' } def test_validation(self): @@ -42,7 +43,17 @@ class SubjectAddFormTests(TestCase): self.assertTrue(form.is_valid()) form.save() + form_data['screening_number'] = "2" form2 = SubjectAddForm(data=form_data) validation_status = form2.is_valid() self.assertFalse(validation_status) self.assertTrue("nd_number" in form2.errors) + + def test_invalid_3(self): + form_data = self.sample_data + form_data['screening_number'] = "" + + form = SubjectAddForm(data=form_data) + validation_status = form.is_valid() + self.assertFalse(validation_status) + self.assertTrue("screening_number" in form.errors) diff --git a/smash/web/tests/test_VisitAddForm.py b/smash/web/tests/test_VisitAddForm.py index d292124c4fd6b89f8e83a01c0f8ac451655c7519..88945ca3b256a0e4c6dc31e2ed77d0dead68312d 100644 --- a/smash/web/tests/test_VisitAddForm.py +++ b/smash/web/tests/test_VisitAddForm.py @@ -9,14 +9,7 @@ from web.tests.functions import * class SubjectAddFormTests(TestCase): def setUp(self): location = get_test_location() - subject_data = {'first_name': 'name', - 'last_name': 'name', - 'sex' : Subject.SEX_CHOICES_MALE, - 'type' : Subject.SUBJECT_TYPE_CHOICES_CONTROL, - 'default_location' : location.id, - 'country' : 'Luxembourg', - } - self.subject = SubjectAddForm(data=subject_data).save() + self.subject = create_subject() self.sample_data = {'datetime_begin': "2017-01-01", 'datetime_end': "2017-02-02",