]> git.parisson.com Git - teleforma.git/commitdiff
appointments optimization
authorYoan Le Clanche <yoan@ellington.pilotsystems.net>
Thu, 4 Oct 2018 07:43:37 +0000 (09:43 +0200)
committerYoan Le Clanche <yoan@ellington.pilotsystems.net>
Thu, 4 Oct 2018 07:43:37 +0000 (09:43 +0200)
teleforma/models/appointment.py
teleforma/views/appointment.py

index 5d26fdb826c6bef0707fdc646a0d02d2d97a34e8..f9dde8a85fb37013e4eb3da12872f50e8536c714 100644 (file)
@@ -46,6 +46,7 @@ class AppointmentPeriod(Model):
         """
         return self.start <= datetime.date.today() <= self.end and self.enable_appointment
 
+    @cached_property
     def days(self):
         days = {}
         delay = self.book_delay
@@ -67,18 +68,6 @@ class AppointmentPeriod(Model):
         # print days
         return sorted(days.values(), key=lambda d:d['date'])
 
-    def nb_jurys_to_show(self, date):
-        min = 100
-        for day in self.days():
-            if day['date'] != date:
-                continue
-            for groupslot in day['slots']:
-                for slot in groupslot.slots:
-                    for i, jury in enumerate(slot['jurys']):
-                        if jury['available']:
-                            if i < min:
-                                min = i
-        return 1 + min
 
     @staticmethod
     def work_day_between(start, end):
@@ -172,13 +161,25 @@ class AppointmentSlot(Model):
         return self.jurys.count()
     get_nb_jury.short_description = "Nombre de jurys"
 
-
+    @property
     def get_visible_jurys(self):
-        return self.jurys.order_by('id')[:self.appointment_period.nb_jurys_to_show(self.date)]
+        return self.jurys.order_by('id')[:self.nb_jurys_to_show]
 
-    @cached_property
+    @property
     def get_nb_of_visible_jurys(self):
-        return self.appointment_period.nb_jurys_to_show(self.date)
+        return self.nb_jurys_to_show
+
+    @cached_property
+    def nb_jurys_to_show(self):
+        min = 100
+
+        for groupslot in AppointmentSlot.objects.filter(date=self.date).all():
+            for slot in groupslot.slots:
+                for i, jury in enumerate(slot['jurys']):
+                    if jury['available']:
+                        if i < min:
+                            min = i
+        return 1 + min
 
     @cached_property
     def slots(self):
@@ -215,7 +216,7 @@ class AppointmentSlot(Model):
         # print res
         return res
 
-    @property
+    @cached_property
     def has_available_slot(self):
         """ is this day has any slot available"""
         for slot in self.slots:
index fe8bb912b2ecb1eca0b44334cd33fd5ce225671d..083080a9f844a1e0c3e40136aad6f5cd715b0d84 100644 (file)
@@ -40,7 +40,7 @@ class Appointments(View):
         for ap_period in AppointmentPeriod.objects.filter(periods__id=period_id).order_by('id'):
             if ap_period.is_open:
                 ap_periods.append({
-                    'days':ap_period.days(),
+                    'days':ap_period.days,
                     'name': ap_period.name,
                     'appointment':ap_period.get_appointment(user)
                 })
@@ -62,7 +62,7 @@ class Appointments(View):
             delay = slot.appointment_period.book_delay
             return u"Vous devez réserver au moins %d jours ouvrés à l'avance" % delay
         # Check if this jury is open
-        jurys = slot.get_visible_jurys()
+        jurys = slot.get_visible_jurys
         if not jury_id in [ j.id for j in jurys ]:
             return u"Ce jury n'est pas ouvert"
         # Check if this slot is empty
@@ -124,7 +124,7 @@ class Appointments(View):
                  'student': ap.student,
                  'main_text': ap.appointment_period.appointment_mail_text }
         # DEBUG
-        # data['mto'] = "gael@pilotsystems.net"
+        data['mto'] = "yoanl@pilotsystems.net"
         # data['mto'] = "dorothee.lavalle@pre-barreau.com"
 
         subject_template = 'teleforma/messages/email_appointment_sujet.txt'