Projet

Général

Profil

0005-api-add-prefetching-to-recurring-events-endpoints-57.patch

Valentin Deniaud, 21 octobre 2021 18:07

Télécharger (4,64 ko)

Voir les différences:

Subject: [PATCH 5/5] api: add prefetching to recurring events endpoints
 (#57957)

 chrono/agendas/models.py    | 10 +++++-----
 chrono/api/views.py         | 18 ++++++++++++------
 tests/api/test_datetimes.py |  2 +-
 tests/api/test_fillslot.py  |  2 +-
 4 files changed, 19 insertions(+), 13 deletions(-)
chrono/agendas/models.py
744 744
        return entries
745 745

  
746 746
    def get_open_recurring_events(self):
747
        return self.event_set.filter(
748
            Q(publication_datetime__isnull=True) | Q(publication_datetime__lte=now()),
749
            recurrence_days__isnull=False,
750
            recurrence_end_date__gt=localtime(now()).date(),
751
        )
747
        return [
748
            e
749
            for e in self.prefetched_recurring_events
750
            if e.recurrence_end_date and e.recurrence_end_date > localtime(now()).date()
751
        ]
752 752

  
753 753
    def add_event_recurrences(
754 754
        self,
chrono/api/views.py
1068 1068

  
1069 1069
        agenda_slugs = get_agendas_from_request(request)
1070 1070
        agendas = get_objects_from_slugs(agenda_slugs, qs=Agenda.objects.filter(kind='events'))
1071
        agendas = Agenda.prefetch_recurring_events(agendas)
1071 1072

  
1072 1073
        events = []
1073 1074
        for agenda in agendas:
......
1596 1597
        agenda_slugs = get_agendas_from_request(request)
1597 1598
        agendas = get_objects_from_slugs(agenda_slugs, qs=Agenda.objects.filter(kind='events'))
1598 1599

  
1599
        context = {'allowed_agenda_slugs': agenda_slugs, 'agendas': agendas}
1600
        context = {'allowed_agenda_slugs': agenda_slugs, 'agendas': Agenda.prefetch_recurring_events(agendas)}
1600 1601
        serializer = self.serializer_class(data=request.data, partial=True, context=context)
1601 1602
        if not serializer.is_valid():
1602 1603
            raise APIError(
......
1607 1608
            )
1608 1609
        payload = serializer.validated_data
1609 1610
        user_external_id = payload['user_external_id']
1611
        agendas = Agenda.prefetch_events_and_exceptions(agendas, user_external_id=user_external_id)
1610 1612

  
1611 1613
        event_filter = Q()
1612 1614
        for agenda_slug, days_by_event in payload['slots'].items():
......
1622 1624
        if end_datetime:
1623 1625
            events_to_book = events_to_book.filter(start_datetime__lte=end_datetime)
1624 1626

  
1625
        events_to_unbook = list(
1626
            agenda.event_set.filter(booking__user_external_id=user_external_id, primary_event__isnull=False)
1627
            .exclude(pk__in=events_to_book)
1628
            .values_list('pk', flat=True)
1629
        )
1627
        events_to_book_ids = set(events_to_book.values_list('pk', flat=True))
1628
        events_to_unbook = [
1629
            e.pk
1630
            for agenda in agendas
1631
            for e in agenda.prefetched_events
1632
            if (e.user_places_count or e.user_waiting_places_count)
1633
            and e.primary_event_id
1634
            and e.pk not in events_to_book_ids
1635
        ]
1630 1636
        events_to_book = events_to_book.exclude(booking__user_external_id=user_external_id)
1631 1637

  
1632 1638
        full_events = list(events_to_book.filter(full=True))
tests/api/test_datetimes.py
1428 1428
    with CaptureQueriesContext(connection) as ctx:
1429 1429
        resp = app.get('/api/agendas/recurring-events/?agendas=%s' % ','.join(str(i) for i in range(20)))
1430 1430
        assert len(resp.json['data']) == 40
1431
        assert len(ctx.captured_queries) == 23
1431
        assert len(ctx.captured_queries) == 3
1432 1432

  
1433 1433

  
1434 1434
@pytest.mark.freeze_time('2021-05-06 14:00')
tests/api/test_fillslot.py
2405 2405
            params={'slots': events_to_book, 'user_external_id': 'user'},
2406 2406
        )
2407 2407
        assert resp.json['booking_count'] == 180
2408
        assert len(ctx.captured_queries) == 36
2408
        assert len(ctx.captured_queries) == 16
2409 2409

  
2410 2410

  
2411 2411
@pytest.mark.freeze_time('2021-09-06 12:00')
2412
-