diff --git a/smash/web/tests/view/test_notifications.py b/smash/web/tests/view/test_notifications.py index e84952c33c2207af6c1c65c0e505da4768e0d876..1be70d94ce1c722e4c45f68a55de6aecb2eb8605 100644 --- a/smash/web/tests/view/test_notifications.py +++ b/smash/web/tests/view/test_notifications.py @@ -4,10 +4,10 @@ import logging from django.contrib.auth.models import AnonymousUser from web.models import Appointment, Location, AppointmentTypeLink, Study, Visit -from web.models.constants import GLOBAL_STUDY_ID +from web.models.constants import GLOBAL_STUDY_ID, VOUCHER_STATUS_USED from web.tests import LoggedInTestCase from web.tests.functions import create_appointment, create_location, create_worker, create_appointment_type, \ - create_empty_notification_parameters, create_study_subject, create_visit + create_empty_notification_parameters, create_study_subject, create_visit, create_voucher from web.views.notifications import \ get_approaching_visits_for_mail_contact, \ get_approaching_visits_for_mail_contact_count, \ @@ -236,6 +236,24 @@ class NotificationViewTests(LoggedInTestCase): notification = get_subject_with_no_visit_notifications_count(self.user) self.assertEquals(original_notification.count + 1, notification.count) + def test_get_subject_with_no_visit_notifications_count_with_new_voucher(self): + original_notification = get_subject_with_no_visit_notifications_count(self.user) + study_subject = create_study_subject() + create_voucher(study_subject) + + notification = get_subject_with_no_visit_notifications_count(self.user) + self.assertEquals(original_notification.count, notification.count) + + def test_get_subject_with_no_visit_notifications_count_with_used_voucher(self): + original_notification = get_subject_with_no_visit_notifications_count(self.user) + study_subject = create_study_subject() + voucher = create_voucher(study_subject) + voucher.status = VOUCHER_STATUS_USED + voucher.save() + + notification = get_subject_with_no_visit_notifications_count(self.user) + self.assertEquals(original_notification.count + 1, notification.count) + def test_get_subject_with_no_visit_notifications_count_2(self): original_notification = get_subject_with_no_visit_notifications_count(self.user) subject = create_study_subject() diff --git a/smash/web/views/notifications.py b/smash/web/views/notifications.py index 2ef58548948192fb1b4fb4d606858c8eba81a85c..5a42783a6a7233b6b0fcf22b66ec0f8e083301a2 100644 --- a/smash/web/views/notifications.py +++ b/smash/web/views/notifications.py @@ -6,7 +6,7 @@ from django.db.models import Count, Case, When, Q, F from django.utils import timezone from web.models import Study -from web.models.constants import GLOBAL_STUDY_ID +from web.models.constants import GLOBAL_STUDY_ID, VOUCHER_STATUS_NEW from ..models import Worker, StudySubject, Visit, Appointment, Location, MissingSubject, InconsistentSubject @@ -145,14 +145,15 @@ def get_notifications(the_user): def get_subjects_with_no_visit(user): - result = StudySubject.objects.annotate(my_count=Count(Case(When(visit__is_finished=False, then=1)))).filter( + result = StudySubject.objects.annotate( + unfinished_visit_count=Count(Case(When(visit__is_finished=False, then=1)))).filter( subject__dead=False, resigned=False, - my_count=0, + unfinished_visit_count=0, default_location__in=get_filter_locations(user), postponed=False, datetime_contact_reminder__isnull=True, - ) + ).exclude(vouchers__status=VOUCHER_STATUS_NEW) return result @@ -242,13 +243,14 @@ def waiting_for_appointment(visit): required_types = visit.appointment_types.all() appointment_types = [] for appointment in visit.appointment_set.all(): - for type in appointment.appointment_types.all(): + for appointment_type in appointment.appointment_types.all(): if (appointment.status in [Appointment.APPOINTMENT_STATUS_FINISHED, - Appointment.APPOINTMENT_STATUS_SCHEDULED]) and (not (type in appointment_types)): - appointment_types.append(type) + Appointment.APPOINTMENT_STATUS_SCHEDULED]) and ( + not (appointment_type in appointment_types)): + appointment_types.append(appointment_type) result = False - for type in required_types: - if not (type in appointment_types): + for appointment_type in required_types: + if not (appointment_type in appointment_types): result = True return result @@ -266,8 +268,9 @@ def get_active_visits_without_appointments(user): # performed types_in_system_count=Count(Case(When( Q(appointment__status=Appointment.APPOINTMENT_STATUS_SCHEDULED) | - Q(appointment__status=Appointment.APPOINTMENT_STATUS_FINISHED) - , then="appointment__appointment_types")), distinct=True)).annotate( + Q(appointment__status=Appointment.APPOINTMENT_STATUS_FINISHED), + then="appointment__appointment_types")), + distinct=True)).annotate( # types_expected_in_system_count annotation counts how many different appointment types should be performed in # the visit types_expected_in_system_count=Count('appointment_types', distinct=True))