Projet

Général

Profil

0002-agendas-make-returning-a-queryset-in-get_open_events.patch

Valentin Deniaud, 12 janvier 2021 16:33

Télécharger (5,63 ko)

Voir les différences:

Subject: [PATCH 2/5] agendas: make returning a queryset in get_open_events
 useless (#41663)

 chrono/agendas/models.py | 36 ++++++++++++++++++++++++++----------
 chrono/api/views.py      | 22 +++++++---------------
 chrono/manager/views.py  |  2 +-
 3 files changed, 34 insertions(+), 26 deletions(-)
chrono/agendas/models.py
491 491
                for weektime_interval in IntervalSet.simple(*time_period_interval) - closed_hours_by_days:
492 492
                    yield SharedTimePeriod.from_weektime_interval(weektime_interval, desks=desks)
493 493

  
494
    def get_open_events(self, prefetched_queryset=False):
494
    def get_open_events(
495
        self,
496
        prefetched_queryset=False,
497
        annotate_queryset=False,
498
        include_full=True,
499
        min_start=None,
500
        max_start=None,
501
    ):
495 502
        assert self.kind == 'events'
496 503

  
497 504
        if prefetched_queryset:
......
504 511
            entries = entries.filter(
505 512
                Q(publication_date__isnull=True) | Q(publication_date__lte=localtime(now()).date())
506 513
            )
514
            if not include_full:
515
                entries = entries.filter(Q(full=False) | Q(primary_event__isnull=False))
516

  
517
        min_booking_time = localtime(now() + datetime.timedelta(days=self.minimal_booking_delay)).replace(
518
            hour=0, minute=0
519
        )
520
        min_start = max(min_start, min_booking_time) if min_start else min_booking_time
521
        if prefetched_queryset:
522
            entries = [e for e in entries if e.start_datetime >= min_start]
523
        else:
524
            entries = entries.filter(start_datetime__gte=min_start)
507 525

  
508
        if self.minimal_booking_delay:
509
            min_start = localtime(now() + datetime.timedelta(days=self.minimal_booking_delay)).replace(
510
                hour=0, minute=0
511
            )
512
            if prefetched_queryset:
513
                entries = [e for e in entries if e.start_datetime >= min_start]
514
            else:
515
                entries = entries.filter(start_datetime__gte=min_start)
516 526
        if self.maximal_booking_delay:
517
            max_start = localtime(now() + datetime.timedelta(days=self.maximal_booking_delay)).replace(
527
            max_booking_time = localtime(now() + datetime.timedelta(days=self.maximal_booking_delay)).replace(
518 528
                hour=0, minute=0
519 529
            )
530
            max_start = min(max_start, max_booking_time) if max_start else max_booking_time
531
        if max_start:
520 532
            if prefetched_queryset:
521 533
                entries = [e for e in entries if e.start_datetime < max_start]
522 534
            else:
523 535
                entries = entries.filter(start_datetime__lt=max_start)
536

  
537
        if annotate_queryset:
538
            entries = Event.annotate_queryset(entries)
539

  
524 540
        return entries
525 541

  
526 542
    def get_booking_form_url(self):
chrono/api/views.py
326 326
            )
327 327
        }
328 328
        if check_events:
329
            agenda_detail['opened_events_available'] = agenda.get_open_events().filter(full=False).exists()
329
            agenda_detail['opened_events_available'] = bool(agenda.get_open_events(include_full=False))
330 330
    elif agenda.accept_meetings():
331 331
        agenda_detail['api'] = {
332 332
            'meetings_url': request.build_absolute_uri(
......
486 486
        if agenda.kind != 'events':
487 487
            raise Http404('agenda found, but it was not an events agenda')
488 488

  
489
        entries = Event.annotate_queryset(agenda.get_open_events())
489
        date_start, date_end = request.GET.get('date_start'), request.GET.get('date_end')
490
        if date_start:
491
            date_start = make_aware(datetime.datetime.combine(parse_date(date_start), datetime.time(0, 0)))
492
        if date_end:
493
            date_end = make_aware(datetime.datetime.combine(parse_date(date_end), datetime.time(0, 0)))
490 494

  
491
        if 'date_start' in request.GET:
492
            entries = entries.filter(
493
                start_datetime__gte=make_aware(
494
                    datetime.datetime.combine(parse_date(request.GET['date_start']), datetime.time(0, 0))
495
                )
496
            )
497

  
498
        if 'date_end' in request.GET:
499
            entries = entries.filter(
500
                start_datetime__lt=make_aware(
501
                    datetime.datetime.combine(parse_date(request.GET['date_end']), datetime.time(0, 0))
502
                )
503
            )
495
        entries = agenda.get_open_events(annotate_queryset=True, min_start=date_start, max_start=date_end)
504 496

  
505 497
        response = {'data': [get_event_detail(request, x, agenda=agenda) for x in entries]}
506 498
        return Response(response)
chrono/manager/views.py
1226 1226
    def get_context_data(self, **kwargs):
1227 1227
        context = super().get_context_data(**kwargs)
1228 1228
        context['user_can_manage'] = self.agenda.can_be_managed(self.request.user)
1229
        context['open_events'] = Event.annotate_queryset(self.agenda.get_open_events())
1229
        context['open_events'] = self.agenda.get_open_events(annotate_queryset=True)
1230 1230
        return context
1231 1231

  
1232 1232

  
1233
-