Skip to content
Snippets Groups Projects
Commit 80e2d4dc authored by Piotr Gawron's avatar Piotr Gawron
Browse files

custom fields can be unique

parent 8467b16c
No related branches found
No related tags found
1 merge request!265Resolve "configurable study fields"
......@@ -84,6 +84,25 @@ class StudySubjectForm(ModelForm):
if test_result is None and test_date is not None:
self.initial[test_result_column_name] = "Inc"
def clean(self):
cleaned_data = super(StudySubjectForm, self).clean()
subject_id = -1
if getattr(self, 'instance', None) is not None:
subject_id = getattr(self, 'instance', None).id
for field_type in CustomStudySubjectField.objects.filter(study=self.study):
if field_type.unique:
field_id = get_study_subject_field_id(field_type)
value = cleaned_data[field_id]
if value is not None and value != "":
count = StudySubject.objects.filter(customstudysubjectvalue__study_subject_field=field_type,
customstudysubjectvalue__value=value,
study=self.study) \
.exclude(id=subject_id) \
.count()
if count > 0:
self.add_error(field_id, "Value must be unique within the study")
return cleaned_data
class StudySubjectAddForm(StudySubjectForm):
class Meta:
......@@ -205,9 +224,11 @@ class StudySubjectEditForm(StudySubjectForm):
prepare_study_subject_fields(fields=self.fields, study=self.study)
def clean(self):
validate_subject_nd_number(self, self.cleaned_data)
validate_subject_mpower_number(self, self.cleaned_data)
validate_subject_resign_reason(self, self.cleaned_data)
cleaned_data = super(StudySubjectEditForm, self).clean()
validate_subject_nd_number(self, cleaned_data)
validate_subject_mpower_number(self, cleaned_data)
validate_subject_resign_reason(self, cleaned_data)
return cleaned_data
def save(self, commit=True) -> StudySubject:
for field_type in CustomStudySubjectField.objects.filter(study=self.study):
......
......@@ -172,3 +172,17 @@ class StudySubjectAddFormTests(LoggedInWithWorkerTestCase):
form = StudySubjectAddForm(user=self.user, study=self.study)
self.assertTrue(form.fields[get_study_subject_field_id(field)].disabled)
def test_form_with_unique_custom_field(self):
field = CustomStudySubjectField.objects.create(study=get_test_study(), default_value="xyz", unique=True)
study_subject = create_study_subject()
study_subject.set_custom_data_value(field, "bla")
self.sample_data[get_study_subject_field_id(field)] = "bla2"
form = StudySubjectAddForm(data=self.sample_data, user=self.user, study=self.study)
self.assertTrue(form.is_valid())
self.sample_data[get_study_subject_field_id(field)] = "bla"
form = StudySubjectAddForm(data=self.sample_data, user=self.user, study=self.study)
self.assertFalse(form.is_valid())
......@@ -85,6 +85,20 @@ class StudySubjectEditFormTests(LoggedInWithWorkerTestCase):
def test_form_with_readonly_custom_field(self):
field = CustomStudySubjectField.objects.create(study=get_test_study(), default_value="xyz", readonly=True)
form = StudySubjectEditForm(user=self.user)
form = StudySubjectEditForm(instance=self.study_subject)
self.assertTrue(form.fields[get_study_subject_field_id(field)].disabled)
def test_form_with_unique_custom_field(self):
field = CustomStudySubjectField.objects.create(study=get_test_study(), default_value="xyz", unique=True)
study_subject2 = create_study_subject()
study_subject2.set_custom_data_value(field, "bla")
self.sample_data[get_study_subject_field_id(field)] = "bla2"
form = StudySubjectEditForm(self.sample_data, instance=self.study_subject)
self.assertTrue(form.is_valid())
self.sample_data[get_study_subject_field_id(field)] = "bla"
form = StudySubjectEditForm(self.sample_data, instance=self.study_subject)
self.assertFalse(form.is_valid())
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment