From 20cb72ebd4ddb0be9223a285c345f7c4f0b215f9 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Wed, 6 Jan 2021 12:26:45 +0100 Subject: [PATCH 2/5] agendas: make returning a queryset in get_open_events useless (#41663) --- chrono/agendas/models.py | 26 +++++++++++++++++++++++--- chrono/api/views.py | 22 +++++++--------------- chrono/manager/views.py | 2 +- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py index a570bbb..52f4818 100644 --- a/chrono/agendas/models.py +++ b/chrono/agendas/models.py @@ -491,7 +491,14 @@ class Agenda(models.Model): for weektime_interval in IntervalSet.simple(*time_period_interval) - closed_hours_by_days: yield SharedTimePeriod.from_weektime_interval(weektime_interval, desks=desks) - def get_open_events(self, prefetched_queryset=False): + def get_open_events( + self, + prefetched_queryset=False, + annotate_queryset=False, + include_full=True, + min_start=None, + max_start=None, + ): assert self.kind == 'events' if prefetched_queryset: @@ -504,23 +511,36 @@ class Agenda(models.Model): entries = entries.filter( Q(publication_date__isnull=True) | Q(publication_date__lte=localtime(now()).date()) ) + if not include_full: + entries = entries.filter(Q(full=False) | Q(primary_event__isnull=False)) if self.minimal_booking_delay: - min_start = localtime(now() + datetime.timedelta(days=self.minimal_booking_delay)).replace( + min_booking_time = localtime(now() + datetime.timedelta(days=self.minimal_booking_delay)).replace( hour=0, minute=0 ) + min_start = max(min_booking_time, min_start) if min_start else min_booking_time + + if min_start: if prefetched_queryset: entries = [e for e in entries if e.start_datetime >= min_start] else: entries = entries.filter(start_datetime__gte=min_start) + if self.maximal_booking_delay: - max_start = localtime(now() + datetime.timedelta(days=self.maximal_booking_delay)).replace( + max_booking_time = localtime(now() + datetime.timedelta(days=self.maximal_booking_delay)).replace( hour=0, minute=0 ) + max_start = min(max_booking_time, max_start) if max_start else max_booking_time + + if max_start: if prefetched_queryset: entries = [e for e in entries if e.start_datetime < max_start] else: entries = entries.filter(start_datetime__lt=max_start) + + if annotate_queryset and not prefetched_queryset: + entries = Event.annotate_queryset(entries) + return entries def get_booking_form_url(self): diff --git a/chrono/api/views.py b/chrono/api/views.py index f061764..e805160 100644 --- a/chrono/api/views.py +++ b/chrono/api/views.py @@ -326,7 +326,7 @@ def get_agenda_detail(request, agenda, check_events=False): ) } if check_events: - agenda_detail['opened_events_available'] = agenda.get_open_events().filter(full=False).exists() + agenda_detail['opened_events_available'] = bool(agenda.get_open_events(include_full=False)) elif agenda.accept_meetings(): agenda_detail['api'] = { 'meetings_url': request.build_absolute_uri( @@ -504,21 +504,13 @@ class Datetimes(APIView): if agenda.kind != 'events': raise Http404('agenda found, but it was not an events agenda') - entries = Event.annotate_queryset(agenda.get_open_events()) + date_start, date_end = request.GET.get('date_start'), request.GET.get('date_end') + if date_start: + date_start = make_aware(datetime.datetime.combine(parse_date(date_start), datetime.time(0, 0))) + if date_end: + date_end = make_aware(datetime.datetime.combine(parse_date(date_end), datetime.time(0, 0))) - if 'date_start' in request.GET: - entries = entries.filter( - start_datetime__gte=make_aware( - datetime.datetime.combine(parse_date(request.GET['date_start']), datetime.time(0, 0)) - ) - ) - - if 'date_end' in request.GET: - entries = entries.filter( - start_datetime__lt=make_aware( - datetime.datetime.combine(parse_date(request.GET['date_end']), datetime.time(0, 0)) - ) - ) + entries = agenda.get_open_events(annotate_queryset=True, min_start=date_start, max_start=date_end) response = { 'data': [get_event_detail(request, x, agenda=agenda) for x in entries], diff --git a/chrono/manager/views.py b/chrono/manager/views.py index 7c71c9d..53061ec 100644 --- a/chrono/manager/views.py +++ b/chrono/manager/views.py @@ -1226,7 +1226,7 @@ class AgendaOpenEventsView(ViewableAgendaMixin, DetailView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['user_can_manage'] = self.agenda.can_be_managed(self.request.user) - context['open_events'] = Event.annotate_queryset(self.agenda.get_open_events()) + context['open_events'] = self.agenda.get_open_events(annotate_queryset=True) return context -- 2.20.1