Projet

Général

Profil

0003-agendas-exceptional-custody-periods-should-override-.patch

Valentin Deniaud, 29 mars 2022 17:20

Télécharger (4,86 ko)

Voir les différences:

Subject: [PATCH 3/4] agendas: exceptional custody periods should override
 holiday rules (#62801)

 chrono/agendas/models.py    | 25 +++++++++++++++++++------
 tests/api/test_datetimes.py | 20 ++++++++++++++++++++
 tests/test_agendas.py       | 16 ++++++++++++++++
 3 files changed, 55 insertions(+), 6 deletions(-)
chrono/agendas/models.py
896 896
            date_start__lte=OuterRef('start_datetime'),
897 897
            date_end__gt=OuterRef('start_datetime'),
898 898
        )
899
        custody_periods = all_periods.filter(guardian__user_external_id=guardian_external_id)
900
        excluded_periods = all_periods.exclude(guardian__user_external_id=guardian_external_id)
899
        holiday_periods = all_periods.filter(holiday_rule__isnull=False)
900
        exceptional_periods = all_periods.filter(holiday_rule__isnull=True)
901 901
        qs = qs.annotate(
902
            in_custody_period=Exists(custody_periods),
903
            in_excluded_period=Exists(excluded_periods),
902
            in_holiday_period=Exists(holiday_periods.filter(guardian__user_external_id=guardian_external_id)),
903
            in_excluded_holiday_period=Exists(
904
                holiday_periods.exclude(guardian__user_external_id=guardian_external_id)
905
            ),
906
            in_exceptional_period=Exists(
907
                exceptional_periods.filter(guardian__user_external_id=guardian_external_id)
908
            ),
909
            in_excluded_exceptional_period=Exists(
910
                exceptional_periods.exclude(guardian__user_external_id=guardian_external_id)
911
            ),
904 912
        )
905 913

  
906
        return qs.filter((rules_lookup | Q(in_custody_period=True)) & Q(in_excluded_period=False))
914
        rules_lookup = (rules_lookup | Q(in_holiday_period=True)) & Q(in_excluded_holiday_period=False)
915
        return qs.filter(
916
            (rules_lookup | Q(in_exceptional_period=True)) & Q(in_excluded_exceptional_period=False)
917
        )
907 918

  
908 919
    @staticmethod
909 920
    def prefetch_recurring_events(qs):
......
3117 3128
    def get_custody_slots(self, min_date, max_date):
3118 3129
        slots = set()
3119 3130

  
3120
        periods = self.periods.filter(date_start__lt=max_date, date_end__gt=min_date)
3131
        periods = self.periods.filter(date_start__lt=max_date, date_end__gt=min_date).order_by(
3132
            '-holiday_rule'
3133
        )
3121 3134
        for period in periods:
3122 3135
            date = period.date_start
3123 3136
            while date < period.date_end and date < max_date:
tests/api/test_datetimes.py
2731 2731
        params={'subscribed': 'all', 'user_external_id': 'child_id', 'guardian_external_id': 'mother_id'},
2732 2732
    )
2733 2733
    assert len(resp.json['data']) == 0
2734

  
2735
    # check exceptional custody periods take precedence over holiday rules
2736
    SharedCustodyPeriod.objects.create(
2737
        agenda=agenda,
2738
        guardian=mother,
2739
        date_start=datetime.date(2021, 12, 22),
2740
        date_end=datetime.date(2021, 12, 30),
2741
    )
2742

  
2743
    resp = app.get(
2744
        '/api/agendas/datetimes/',
2745
        params={'subscribed': 'all', 'user_external_id': 'child_id', 'guardian_external_id': 'father_id'},
2746
    )
2747
    assert len(resp.json['data']) == 0
2748

  
2749
    resp = app.get(
2750
        '/api/agendas/datetimes/',
2751
        params={'subscribed': 'all', 'user_external_id': 'child_id', 'guardian_external_id': 'mother_id'},
2752
    )
2753
    assert len(resp.json['data']) == 1
tests/test_agendas.py
3292 3292
    assert all(name == 'John Doe' for name in guardians[:21])
3293 3293
    assert all(name == 'Jane Doe' for name in guardians[21:28])
3294 3294
    assert all(name == 'John Doe' for name in guardians[28:])
3295

  
3296
    # check exceptional custody periods take precedence over holiday rules
3297
    SharedCustodyPeriod.objects.create(
3298
        agenda=agenda,
3299
        guardian=mother,
3300
        date_start=datetime.date(year=2021, month=12, day=27),
3301
        date_end=datetime.date(year=2021, month=12, day=29),
3302
    )
3303
    slots = agenda.get_custody_slots(date_start, date_start + datetime.timedelta(days=30))
3304
    slots = [(x.date.strftime('%d/%m'), x.guardian.name) for x in slots]
3305
    assert slots[13:17] == [
3306
        ('26/12', 'John Doe'),
3307
        ('27/12', 'Jane Doe'),
3308
        ('28/12', 'Jane Doe'),
3309
        ('29/12', 'John Doe'),
3310
    ]
3295
-