From c00ce9bfd1bf58640dbe1e2761c01a88e004be3b Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Tue, 17 Apr 2018 10:54:09 +0200
Subject: [PATCH] when filtering appointments that should be included in email
 request we need to remove duplicates

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

diff --git a/smash/web/tests/view/test_kit_request.py b/smash/web/tests/view/test_kit_request.py
index f0d199b6..e1c4a1e7 100644
--- a/smash/web/tests/view/test_kit_request.py
+++ b/smash/web/tests/view/test_kit_request.py
@@ -96,6 +96,29 @@ class ViewFunctionsTests(LoggedInTestCase):
         self.assertEqual(appointment1, result['appointments'][1])
         self.assertEqual(appointment2, result['appointments'][2])
 
+    def test_kit_requests_for_appointment_with_two_types(self):
+        item = Item.objects.create(disposable=True, name="item 1")
+        appointment_type = create_appointment_type()
+        appointment_type.required_equipment.add(item)
+        appointment_type.save()
+
+        item = Item.objects.create(disposable=True, name="item 2")
+        appointment_type2 = create_appointment_type()
+        appointment_type2.required_equipment.add(item)
+        appointment_type2.save()
+
+        visit = create_visit()
+
+        appointment1 = create_appointment(visit)
+        appointment1.datetime_when = get_today_midnight_date() + datetime.timedelta(days=3)
+        appointment1.save()
+        AppointmentTypeLink.objects.create(appointment=appointment1, appointment_type=appointment_type)
+        AppointmentTypeLink.objects.create(appointment=appointment1, appointment_type=appointment_type2)
+
+        result = get_kit_requests(self.user)
+
+        self.assertEqual(1, len(result["appointments"]))
+
     def test_kit_requests_send_email(self):
         item_name = "Test item to be ordered"
         item = Item.objects.create(disposable=True, name=item_name)
diff --git a/smash/web/views/kit.py b/smash/web/views/kit.py
index a20d266f..7c887566 100644
--- a/smash/web/views/kit.py
+++ b/smash/web/views/kit.py
@@ -45,7 +45,7 @@ def get_kit_requests(user, start_date=None, end_date=None):
         datetime_when__gt=start_date,
         location__in=get_filter_locations(user),
         status=Appointment.APPOINTMENT_STATUS_SCHEDULED,
-    ).order_by('datetime_when')
+    ).distinct().order_by('datetime_when')
 
     if end_date is not None:
         appointments = appointments.filter(datetime_when__lt=end_date)
-- 
GitLab