Projet

Général

Profil

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

Valentin Deniaud, 28 janvier 2021 12:38

Télécharger (5,43 ko)

Voir les différences:

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(-)
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))
507 516

  
508 517
        if self.minimal_booking_delay:
509
            min_start = localtime(now() + datetime.timedelta(days=self.minimal_booking_delay)).replace(
518
            min_booking_time = localtime(now() + datetime.timedelta(days=self.minimal_booking_delay)).replace(
510 519
                hour=0, minute=0
511 520
            )
521
            min_start = max(min_booking_time, min_start) if min_start else min_booking_time
522

  
523
        if min_start:
512 524
            if prefetched_queryset:
513 525
                entries = [e for e in entries if e.start_datetime >= min_start]
514 526
            else:
515 527
                entries = entries.filter(start_datetime__gte=min_start)
528

  
516 529
        if self.maximal_booking_delay:
517
            max_start = localtime(now() + datetime.timedelta(days=self.maximal_booking_delay)).replace(
530
            max_booking_time = localtime(now() + datetime.timedelta(days=self.maximal_booking_delay)).replace(
518 531
                hour=0, minute=0
519 532
            )
533
            max_start = min(max_booking_time, max_start) if max_start else max_booking_time
534

  
535
        if max_start:
520 536
            if prefetched_queryset:
521 537
                entries = [e for e in entries if e.start_datetime < max_start]
522 538
            else:
523 539
                entries = entries.filter(start_datetime__lt=max_start)
540

  
541
        if annotate_queryset and not prefetched_queryset:
542
            entries = Event.annotate_queryset(entries)
543

  
524 544
        return entries
525 545

  
526 546
    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(
......
504 504
        if agenda.kind != 'events':
505 505
            raise Http404('agenda found, but it was not an events agenda')
506 506

  
507
        entries = Event.annotate_queryset(agenda.get_open_events())
507
        date_start, date_end = request.GET.get('date_start'), request.GET.get('date_end')
508
        if date_start:
509
            date_start = make_aware(datetime.datetime.combine(parse_date(date_start), datetime.time(0, 0)))
510
        if date_end:
511
            date_end = make_aware(datetime.datetime.combine(parse_date(date_end), datetime.time(0, 0)))
508 512

  
509
        if 'date_start' in request.GET:
510
            entries = entries.filter(
511
                start_datetime__gte=make_aware(
512
                    datetime.datetime.combine(parse_date(request.GET['date_start']), datetime.time(0, 0))
513
                )
514
            )
515

  
516
        if 'date_end' in request.GET:
517
            entries = entries.filter(
518
                start_datetime__lt=make_aware(
519
                    datetime.datetime.combine(parse_date(request.GET['date_end']), datetime.time(0, 0))
520
                )
521
            )
513
        entries = agenda.get_open_events(annotate_queryset=True, min_start=date_start, max_start=date_end)
522 514

  
523 515
        response = {
524 516
            'data': [get_event_detail(request, x, agenda=agenda) for x in entries],
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
-