From 23c4135d27a420cc5aacbe9464507f9c6200f4d6 Mon Sep 17 00:00:00 2001 From: Yoan Le Clanche Date: Thu, 4 Oct 2018 09:43:37 +0200 Subject: [PATCH] appointments optimization --- teleforma/models/appointment.py | 35 +++++++++++++++++---------------- teleforma/views/appointment.py | 6 +++--- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/teleforma/models/appointment.py b/teleforma/models/appointment.py index 5d26fdb8..f9dde8a8 100644 --- a/teleforma/models/appointment.py +++ b/teleforma/models/appointment.py @@ -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: diff --git a/teleforma/views/appointment.py b/teleforma/views/appointment.py index fe8bb912..083080a9 100644 --- a/teleforma/views/appointment.py +++ b/teleforma/views/appointment.py @@ -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' -- 2.39.5