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

counting of notification is done via django model query

parent 57d5b3ab
No related branches found
No related tags found
1 merge request!107Resolve "exceeded visits list & unfinished visits list"
...@@ -144,7 +144,7 @@ class NotificationViewTests(LoggedInTestCase): ...@@ -144,7 +144,7 @@ class NotificationViewTests(LoggedInTestCase):
visit.save() visit.save()
visits = get_unfinished_visits(self.user) visits = get_unfinished_visits(self.user)
self.assertEquals(3, len(visits)) self.assertEquals(3, visits.count())
# check sort order # check sort order
self.assertTrue(visits[0].datetime_begin < visits[1].datetime_begin) self.assertTrue(visits[0].datetime_begin < visits[1].datetime_begin)
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import datetime import datetime
from django.contrib.auth.models import User, AnonymousUser from django.contrib.auth.models import User, AnonymousUser
from django.db.models import Count, Case, When from django.db.models import Count, Case, When, Q, F
from django.utils import timezone from django.utils import timezone
from web.models import Study from web.models import Study
...@@ -71,7 +71,7 @@ def get_subject_with_no_visit_notifications_count(user): ...@@ -71,7 +71,7 @@ def get_subject_with_no_visit_notifications_count(user):
def get_visits_without_appointments_count(user): def get_visits_without_appointments_count(user):
notification = NotificationCount( notification = NotificationCount(
title="unfinished visits", title="unfinished visits",
count=len(get_unfinished_visits(user)), count=get_unfinished_visits(user).count(),
style="fa fa-user-times text-yellow", style="fa fa-user-times text-yellow",
type='web.views.unfinished_visits') type='web.views.unfinished_visits')
return notification return notification
...@@ -80,7 +80,7 @@ def get_visits_without_appointments_count(user): ...@@ -80,7 +80,7 @@ def get_visits_without_appointments_count(user):
def get_visits_with_missing_appointments_count(user): def get_visits_with_missing_appointments_count(user):
notification = NotificationCount( notification = NotificationCount(
title="visits with missing appointments", title="visits with missing appointments",
count=len(get_active_visits_with_missing_appointments(user)), count=get_active_visits_with_missing_appointments(user).count(),
style="fa fa-user-times text-yellow", style="fa fa-user-times text-yellow",
type='web.views.visits_with_missing_appointments') type='web.views.visits_with_missing_appointments')
return notification return notification
...@@ -169,19 +169,13 @@ def get_subjects_with_reminder(user): ...@@ -169,19 +169,13 @@ def get_subjects_with_reminder(user):
def get_active_visits_with_missing_appointments(user): def get_active_visits_with_missing_appointments(user):
result = [] visits_without_appointments = get_active_visits_without_appointments(user)
for visit in get_active_visits_without_appointments(user): return visits_without_appointments.filter(types_in_system_count__lt=F("types_expected_in_system_count"))
if waiting_for_appointment(visit):
result.append(visit)
return result
def get_unfinished_visits(user): def get_unfinished_visits(user):
result = [] visits_without_appointments = get_active_visits_without_appointments(user)
for visit in get_active_visits_without_appointments(user): return visits_without_appointments.filter(types_in_system_count__gte=F("types_expected_in_system_count"))
if not waiting_for_appointment(visit):
result.append(visit)
return result
def get_approaching_visits_without_appointments(user): def get_approaching_visits_without_appointments(user):
...@@ -259,7 +253,16 @@ def get_active_visits_without_appointments(user): ...@@ -259,7 +253,16 @@ def get_active_visits_without_appointments(user):
datetime_end__gt=today, datetime_end__gt=today,
is_finished=False, is_finished=False,
subject__default_location__in=get_filter_locations(user), subject__default_location__in=get_filter_locations(user),
my_count=0).order_by('datetime_begin') my_count=0).order_by('datetime_begin').annotate(
# types_in_system_count annotation counts how many different appointment types were scheduled or already
# 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(
# 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))
def get_filter_locations(user): def get_filter_locations(user):
......
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