Projet

Général

Profil

0001-manager-display-excluded-periods-for-virtual-agendas.patch

Valentin Deniaud, 18 mai 2022 17:29

Télécharger (6,54 ko)

Voir les différences:

Subject: [PATCH] manager: display excluded periods for virtual agendas in date
 views (#65074)

 chrono/agendas/models.py  | 13 +++++++++++++
 chrono/manager/views.py   | 17 +++++++++++++++--
 tests/manager/test_all.py | 40 +++++++++++++++++++++++++++++++++++++--
 3 files changed, 66 insertions(+), 4 deletions(-)
chrono/agendas/models.py
1022 1022

  
1023 1023
        return True
1024 1024

  
1025
    def get_exceptions_from_excluded_periods(self, date):
1026
        if not self.kind == 'virtual':
1027
            return []
1028

  
1029
        return [
1030
            TimePeriodException(
1031
                start_datetime=make_aware(datetime.datetime.combine(date, period.start_time)),
1032
                end_datetime=make_aware(datetime.datetime.combine(date, period.end_time)),
1033
            )
1034
            for period in self.excluded_timeperiods.all()
1035
            if period.weekday == date.weekday()
1036
        ]
1037

  
1025 1038

  
1026 1039
class VirtualMember(models.Model):
1027 1040
    """Trough model to link virtual agendas to their real agendas.
chrono/manager/views.py
1347 1347
    allow_empty = True
1348 1348
    allow_future = True
1349 1349

  
1350
    def set_agenda(self, **kwargs):
1351
        self.agenda = get_object_or_404(
1352
            Agenda.objects.prefetch_related('excluded_timeperiods'), pk=kwargs.get('pk')
1353
        )
1354

  
1350 1355
    def dispatch(self, request, *args, **kwargs):
1351 1356
        # specify 6am time to get the expected timezone on daylight saving time
1352 1357
        # days.
......
1494 1499
                        )
1495 1500

  
1496 1501
                    # and exceptions for this desk
1502
                    exceptions = (
1503
                        desk.prefetched_exceptions
1504
                        + self.agenda.get_exceptions_from_excluded_periods(current_date.date())
1505
                    )
1497 1506
                    info['exceptions'] = []
1498
                    for exception in desk.prefetched_exceptions:
1507
                    for exception in exceptions:
1499 1508
                        if exception.end_datetime < current_date:
1500 1509
                            continue
1501 1510
                        if exception.start_datetime > max_date:
......
1710 1719
                                'css_left': left,
1711 1720
                            }
1712 1721
                        )
1713
                    for exception in desk.prefetched_exceptions:
1722
                    exceptions = (
1723
                        desk.prefetched_exceptions
1724
                        + self.agenda.get_exceptions_from_excluded_periods(current_date.date())
1725
                    )
1726
                    for exception in exceptions:
1714 1727
                        if exception.end_datetime < current_date:
1715 1728
                            continue
1716 1729
                        if exception.start_datetime > max_date:
tests/manager/test_all.py
1940 1940
        resp = app.get(
1941 1941
            '/manage/agendas/%s/%d/%d/%d/' % (agenda.id, date.year, date.month, date.day), status=200
1942 1942
        )
1943
        assert len(ctx.captured_queries) == 15
1943
        assert len(ctx.captured_queries) == 16
1944 1944
    # day is displaying rows from 10am to 6pm,
1945 1945
    # opening hours, 10am to 1pm gives top: 300%
1946 1946
    # rest of the day, 1pm to 6(+1)pm gives 600%
......
1950 1950
    }
1951 1951
    assert resp.pyquery.find('.exception-hours span')[0].text == 'Exception for the afternoon'
1952 1952

  
1953
    # display excluded period
1954
    date += datetime.timedelta(days=7)
1955
    TimePeriod.objects.create(
1956
        agenda=agenda, weekday=today.weekday(), start_time=datetime.time(14, 0), end_time=datetime.time(23, 0)
1957
    )
1958
    resp = app.get('/manage/agendas/%s/%d/%d/%d/' % (agenda.id, date.year, date.month, date.day), status=200)
1959
    assert resp.pyquery.find('.exception-hours')[0].attrib == {
1960
        'class': 'exception-hours',
1961
        'style': 'height: 500%; top: 400%;',
1962
    }
1963

  
1964
    # check excluded period is only displayed on relevant weekday
1965
    date += datetime.timedelta(days=1)
1966
    TimePeriod.objects.create(
1967
        desk=desk1, weekday=date.weekday(), start_time=datetime.time(10, 0), end_time=datetime.time(18, 0)
1968
    )
1969
    resp = app.get('/manage/agendas/%s/%d/%d/%d/' % (agenda.id, date.year, date.month, date.day), status=200)
1970
    assert resp.text.count('<tr') == 9
1971
    assert 'exceptions-hours' not in resp.text
1972

  
1953 1973

  
1954 1974
def test_virtual_agenda_month_view(app, admin_user):
1955 1975
    agenda = Agenda.objects.create(label='Virtual', kind='virtual')
......
2038 2058
    )
2039 2059
    with CaptureQueriesContext(connection) as ctx:
2040 2060
        resp = app.get('/manage/agendas/%s/%s/%s/' % (agenda.id, today.year, today.month))
2041
        assert len(ctx.captured_queries) == 12
2061
        assert len(ctx.captured_queries) == 13
2062
    assert len(resp.pyquery.find('.exception-hours')) == 1
2042 2063
    assert resp.pyquery.find('.exception-hours')[0].attrib == {
2043 2064
        'class': 'exception-hours',
2044 2065
        'style': 'height:800.0%;top:0.0%;width:48.0%;left:1.0%;',
2045 2066
        'title': 'Exception for a December day',
2046 2067
    }
2047 2068

  
2069
    # display excluded period
2070
    TimePeriod.objects.create(
2071
        agenda=agenda, weekday=today.weekday(), start_time=datetime.time(13, 0), end_time=datetime.time(23, 0)
2072
    )
2073
    resp = app.get('/manage/agendas/%s/%s/%s/' % (agenda.id, today.year, today.month))
2074
    assert len(resp.pyquery.find('.exception-hours')) == 11  # five occurences on two desks
2075
    assert resp.pyquery.find('.exception-hours')[0].attrib == {
2076
        'class': 'exception-hours',
2077
        'style': 'height:500.0%;top:300.0%;width:48.0%;left:1.0%;',
2078
    }
2079
    assert resp.pyquery.find('.exception-hours')[1].attrib == {
2080
        'class': 'exception-hours',
2081
        'style': 'height:500.0%;top:300.0%;width:48.0%;left:50.0%;',
2082
    }
2083

  
2048 2084

  
2049 2085
def test_virtual_agenda_settings_empty(app, admin_user):
2050 2086
    agenda = Agenda.objects.create(label='My Virtual agenda', kind='virtual')
2051
-