From edbdc736968d404d35bbf62c4e78c0a6978651cf Mon Sep 17 00:00:00 2001
From: Carlos Vega <carlos.vega@uni.lu>
Date: Wed, 2 Jan 2019 15:22:42 +0100
Subject: [PATCH] Issue #282 create two different urls for event retreival. One
 with all kind of events (scheduled, cancelled, finished, etc.) and the
 previous version with only scheduled events

---
 smash/web/api_urls.py                 |  3 ++-
 smash/web/api_views/daily_planning.py | 38 ++++++++++++++++++++++-----
 2 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/smash/web/api_urls.py b/smash/web/api_urls.py
index 61777145..6d8aaccb 100644
--- a/smash/web/api_urls.py
+++ b/smash/web/api_urls.py
@@ -63,7 +63,8 @@ urlpatterns = [
         name='web.api.workers.daily_planning.availabilities$'),
 
     # daily planning events
-    url(r'^events/(?P<date>\d{4}-\d{2}-\d{2})/$', daily_planning.events, name='web.api.events'),
+    url(r'^events/(?P<date>\d{4}-\d{2}-\d{2})/include_all$', daily_planning.events, {'include_all': True}, name='web.api.events_all'),
+    url(r'^events/(?P<date>\d{4}-\d{2}-\d{2})/$', daily_planning.events, {'include_all': False}, name='web.api.events'),
     url(r'^availabilities/(?P<date>\d{4}-\d{2}-\d{2})/$', daily_planning.availabilities, name='web.api.availabilities'),
     url(r'^events_persist$', daily_planning.events_persist, name='web.api.events_persist'),
 
diff --git a/smash/web/api_views/daily_planning.py b/smash/web/api_views/daily_planning.py
index d7a64bfb..ef2bfbe3 100644
--- a/smash/web/api_views/daily_planning.py
+++ b/smash/web/api_views/daily_planning.py
@@ -203,10 +203,16 @@ def get_conflicts(worker, date):
     return result
 
 
-def get_generic_appointment_events(request, date):
+def get_generic_appointment_events(request, date, include_all=False):
     result = {}
 
-    appointments = Appointment.objects.filter(
+    if include_all:
+        appointments = Appointment.objects.filter(
+        datetime_when__date=date,
+        location__in=get_filter_locations(request.user),
+        visit__isnull=True).all()
+    else:
+        appointments = Appointment.objects.filter(
         datetime_when__date=date,
         location__in=get_filter_locations(request.user),
         status=Appointment.APPOINTMENT_STATUS_SCHEDULED,
@@ -233,6 +239,7 @@ def get_generic_appointment_events(request, date):
         event = {
             'title': appointment.title(),
             'short_title': appointment.title(),
+            'status': appointment.status,
             'duration': build_duration(appointment.length),
             'id': 'app-{}'.format(appointment.id),
             'appointment_id': appointment.id,
@@ -250,24 +257,40 @@ def get_generic_appointment_events(request, date):
     return result.values()
 
 
-def events(request, date):
-    appointments = Appointment.objects.filter(
+def events(request, date, include_all=False):
+    #fetch appointments
+    if include_all:
+        appointments = Appointment.objects.filter(
+        datetime_when__date=date,
+        location__in=get_filter_locations(request.user),
+        visit__isnull=False).all()
+    else:    
+        appointments = Appointment.objects.filter(
         datetime_when__date=date,
         location__in=get_filter_locations(request.user),
         status=Appointment.APPOINTMENT_STATUS_SCHEDULED,
         visit__isnull=False).all()
+    #store subjects
     subjects = {}
+    #for each appointment
     for i, appointment in enumerate(appointments):
+        #get its subject
         appointment_subject = appointment.visit.subject
+        #if there is a subject we add it to the subjects dict
         if appointment_subject.id not in subjects:
             # create subject
+            flag_set = set([language.image.url for language in appointment_subject.subject.languages.all() if language.image is not None])
+            default_language = appointment_subject.subject.default_written_communication_language
+            if default_language is not None and default_language.image is not None:
+                flag_set.add(default_language.image.url)
+
             subject = {
                 'name': unicode(appointment_subject),
                 'id': appointment_subject.id,
                 'appointment_id': appointment.id,
                 'color': RANDOM_COLORS[i],
                 'start': appointment.datetime_when.replace(tzinfo=None).strftime("%H:%M"),
-                'flags': [language.image.url for language in appointment_subject.subject.languages.all()],
+                'flags': list(flag_set),
                 # this indicates only location of the first appointment
                 # (there is small chance to have two appointments in two different places at the same day)
                 'location': str(appointment.location),
@@ -275,8 +298,10 @@ def events(request, date):
             }
             subjects[appointment_subject.id] = subject
 
+        #get appointment type link of the appointment
         links = AppointmentTypeLink.objects.filter(
             appointment=appointment).all()
+        #for the AppointmentTypeLink, get all the info and create the event object
         for j, link in enumerate(links):
             link_when = link.date_when
             link_end = None
@@ -287,6 +312,7 @@ def events(request, date):
                         minutes=link.appointment_type.default_duration)
             event = {
                 'title': link.appointment_type.description,
+                'status': link.appointment.status,
                 'short_title': link.appointment_type.code,
                 'duration': build_duration(link.appointment_type.default_duration),
                 'subject': unicode(appointment_subject),
@@ -303,7 +329,7 @@ def events(request, date):
             subject_events = subjects[appointment_subject.id]['events']
             subject_events.append(event)
 
-    generic_appointment_events = get_generic_appointment_events(request, date)
+    generic_appointment_events = get_generic_appointment_events(request, date, include_all=include_all)
 
     availabilities = []
     holidays = []
-- 
GitLab