diff --git a/smash/web/tests/view/test_export.py b/smash/web/tests/view/test_export.py index f2c7850f269e95d3ddc8f051d676f2971e9d5761..91e1462730506a790a59ad8885ce431c60210cdb 100644 --- a/smash/web/tests/view/test_export.py +++ b/smash/web/tests/view/test_export.py @@ -1,8 +1,10 @@ # coding=utf-8 from django.urls import reverse +from web.models import Appointment from web.tests import LoggedInTestCase -from web.tests.functions import create_subject, create_appointment +from web.tests.functions import create_subject, create_appointment, create_visit +from web.views.export import subject_to_row_for_fields, DROP_OUT_FIELD class TestExportView(LoggedInTestCase): @@ -25,3 +27,32 @@ class TestExportView(LoggedInTestCase): create_appointment() response = self.client.get(reverse('web.views.export_to_excel', kwargs={'data_type': "appointments"})) self.assertEqual(response.status_code, 200) + + def test_subject_to_row_for_fields_when_not_resigned(self): + subject = create_subject() + subject.resigned = False + subject.save() + + result = subject_to_row_for_fields(subject, [DROP_OUT_FIELD]) + self.assertFalse(result[0]) + + def test_subject_to_row_for_fields_when_resigned(self): + subject = create_subject() + subject.resigned = True + subject.save() + + result = subject_to_row_for_fields(subject, [DROP_OUT_FIELD]) + self.assertFalse(result[0]) + + def test_subject_to_row_for_fields_when_dropped_out(self): + subject = create_subject() + subject.resigned = True + subject.save() + + visit = create_visit(subject) + appointment = create_appointment(visit) + appointment.status = Appointment.APPOINTMENT_STATUS_FINISHED + appointment.save() + + result = subject_to_row_for_fields(subject, [DROP_OUT_FIELD]) + self.assertTrue(result[0]) diff --git a/smash/web/views/export.py b/smash/web/views/export.py index b1962c7e66b797f89b141da41266a584e470cdd4..5279b6b8f302574f0ffaf4f78cdb774256458f21 100644 --- a/smash/web/views/export.py +++ b/smash/web/views/export.py @@ -46,12 +46,18 @@ def export_to_excel(request, data_type="subjects"): return response +class CustomField: + def __init__(self, dictionary): + for k, v in dictionary.items(): + setattr(self, k, v) + + +DROP_OUT_FIELD = CustomField({'verbose_name': "DROP OUT", 'name': "custom-drop-out"}) + + def get_subjects_as_array(): result = [] - subject_fields = [] - for field in Subject._meta.fields: - if field.name != "ID": - subject_fields.append(field) + subject_fields = get_default_subject_fields() field_names = [] for field in subject_fields: @@ -61,14 +67,39 @@ def get_subjects_as_array(): subjects = Subject.objects.order_by('-last_name') for subject in subjects: - row = [] - for field in subject_fields: + row = subject_to_row_for_fields(subject, subject_fields) + result.append([unicode(s).replace("\n", ";").replace("\r", ";") for s in row]) + return result + + +def subject_to_row_for_fields(subject, subject_fields): + row = [] + for field in subject_fields: + if field == DROP_OUT_FIELD: + if not subject.resigned: + cell = False + else: + finished_appointments = Appointment.objects.filter(visit__subject=subject).filter( + status=Appointment.APPOINTMENT_STATUS_FINISHED).count() + if finished_appointments > 0: + cell = True + else: + cell = False + else: cell = getattr(subject, field.name) if cell is None: cell = "" - row.append(cell) - result.append([unicode(s).replace("\n", ";").replace("\r", ";") for s in row]) - return result + row.append(cell) + return row + + +def get_default_subject_fields(): + subject_fields = [] + for field in Subject._meta.fields: + if field.name != "ID": + subject_fields.append(field) + subject_fields.append(DROP_OUT_FIELD) + return subject_fields def get_appointments_as_array():