From df56eb65a622c73fd3de5bfa0172ba9b6478f70a Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Thu, 23 Mar 2017 16:27:14 +0100
Subject: [PATCH] list of approching visits for mail contact sorted by start
 date

---
 smash/web/tests/test_view_notifications.py | 23 ++++++++++++++++++++++
 smash/web/views/notifications.py           |  2 +-
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/smash/web/tests/test_view_notifications.py b/smash/web/tests/test_view_notifications.py
index 748414e9..a11d5202 100644
--- a/smash/web/tests/test_view_notifications.py
+++ b/smash/web/tests/test_view_notifications.py
@@ -8,6 +8,7 @@ from functions import create_subject
 from functions import create_visit
 from web.models import Appointment, Location
 from web.views.notifications import \
+    get_approaching_visits_for_mail_contact, \
     get_approaching_visits_for_mail_contact_count, \
     get_approaching_visits_without_appointments, \
     get_approaching_visits_without_appointments_count, \
@@ -283,6 +284,28 @@ class NotificationViewTests(TestCase):
         notification = get_approaching_visits_for_mail_contact_count(self.user)
         self.assertEquals(original_notification.count, notification.count)
 
+    def test_get_approaching_visits_for_mail_contact_order(self):
+        subject = create_subject()
+
+        visit = create_visit(subject)
+        visit.datetime_begin = get_today_midnight_date() + datetime.timedelta(days=110)
+        visit.save()
+
+        visit = create_visit(subject)
+        visit.datetime_begin = get_today_midnight_date() + datetime.timedelta(days=108)
+        visit.save()
+
+        visit = create_visit(subject)
+        visit.datetime_begin = get_today_midnight_date() + datetime.timedelta(days=112)
+        visit.save()
+
+        visits = get_approaching_visits_for_mail_contact(self.user)
+        self.assertEquals(3, visits.count())
+
+        # check sort order
+        self.assertTrue(visits[0].datetime_begin < visits[1].datetime_begin)
+        self.assertTrue(visits[1].datetime_begin < visits[2].datetime_begin)
+
     def test_get_subjects_with_reminder_count(self):
         original_without_visit_notification = get_subject_with_no_visit_notifications_count(self.user)
         original_notification = get_subjects_with_reminder_count(self.user)
diff --git a/smash/web/views/notifications.py b/smash/web/views/notifications.py
index 6f22dc9d..e881d908 100644
--- a/smash/web/views/notifications.py
+++ b/smash/web/views/notifications.py
@@ -177,7 +177,7 @@ def get_approaching_visits_for_mail_contact(user):
         is_finished=False,
         post_mail_sent=False,
         subject__default_location__in=get_filter_locations(user),
-        my_count=0)
+        my_count=0).order_by('datetime_begin')
 
 
 def get_exceeded_visits(user):
-- 
GitLab