Projet

Général

Profil

0001-manager-prevent-time-period-changes-when-bookings-ex.patch

Josué Kouka, 20 décembre 2017 19:08

Télécharger (5,76 ko)

Voir les différences:

Subject: [PATCH] manager: prevent time period changes when bookings exist
 (#20791)

 chrono/manager/forms.py | 30 ++++++++++++++++++++++++-
 tests/test_manager.py   | 58 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 87 insertions(+), 1 deletion(-)
chrono/manager/forms.py
19 19

  
20 20
from django import forms
21 21
from django.forms import ValidationError
22
from django.utils.timezone import localtime
22 23
from django.utils.translation import ugettext_lazy as _
23 24

  
24 25
from chrono.agendas.models import (Event, MeetingType, TimePeriod, Desk,
25
                                   TimePeriodException)
26
                                   TimePeriodException, Booking)
26 27

  
27 28
from . import widgets
28 29

  
......
76 77
        }
77 78
        exclude = []
78 79

  
80
    def clean(self):
81
        cleaned_data = super(TimePeriodForm, self).clean()
82
        if not self.changes_ok():
83
            raise ValidationError(_('One or several bookings exist within this time period.'))
84
        return cleaned_data
85

  
86
    def changes_ok(self):
87
        if not hasattr(self.instance, 'desk'):
88
            return True
89
        has_booking = self.has_booking_within()
90
        if self.cleaned_data['start_time'] > self.instance.start_time:
91
            return not(has_booking)
92
        if self.cleaned_data['end_time'] < self.instance.end_time:
93
            return not(has_booking)
94
        return True
95

  
96
    def has_booking_within(self):
97
        bookings = Booking.objects.filter(
98
            event__desk=self.instance.desk, cancellation_datetime__isnull=True).select_related('event')
99
        for booking in bookings:
100
            event_startdt = localtime(booking.event.start_datetime)
101
            if event_startdt.weekday() != self.instance.weekday:
102
                continue
103
            if self.instance.start_time <= event_startdt.time() < self.instance.end_time:
104
                return True
105
        return False
106

  
79 107

  
80 108
class NewDeskForm(forms.ModelForm):
81 109
    class Meta:
tests/test_manager.py
1127 1127
    mocked_get.side_effect = mocked_requests_http_ssl_error
1128 1128
    resp = resp.form.submit(status=200)
1129 1129
    assert 'Failed to retrieve remote calendar (SSL error).' in resp.content
1130

  
1131

  
1132
def test_prevent_time_period_changes_when_bookings_exist(app, admin_user):
1133
    agenda = Agenda.objects.create(label='Foo bar', kind='meetings')
1134
    desk = Desk.objects.create(agenda=agenda, label='Desk A')
1135
    MeetingType(agenda=agenda, label='Blah').save()
1136
    timeperiod = TimePeriod.objects.create(
1137
        weekday=1, desk=desk, start_time=datetime.time(8, 00), end_time=datetime.time(11, 0))
1138
    event = Event.objects.create(agenda=agenda, places=1, desk=desk,
1139
                                 start_datetime=datetime.datetime(2017, 5, 23, 8, 30))
1140
    Booking.objects.create(event=event)
1141
    login(app)
1142
    timeperiod_edit_link = "/manage/timeperiods/%d/edit" % timeperiod.pk
1143
    # extend time period start
1144
    resp = app.get('/manage/agendas/%d/' % agenda.pk)
1145
    resp = resp.click(href=timeperiod_edit_link)
1146
    resp.form['start_time'] = '7:30'
1147
    resp = resp.form.submit().follow()
1148
    assert resp.html.find('a', {'href': timeperiod_edit_link}).text.strip() == u'Tuesday / 7:30 a.m. \u2192 11 a.m.'
1149
    # shorten time period start
1150
    resp = app.get('/manage/agendas/%d/' % agenda.pk)
1151
    resp = resp.click(href=timeperiod_edit_link)
1152
    resp.form['start_time'] = '9:30'
1153
    resp = resp.form.submit()
1154
    error_message = resp.html.find('ul', {'class': 'errorlist'}).text.strip()
1155
    assert error_message == 'One or several bookings exist within this time period.'
1156
    # extend time period end
1157
    resp = app.get('/manage/agendas/%d/' % agenda.pk)
1158
    resp = resp.click(href=timeperiod_edit_link)
1159
    resp.form['end_time'] = '12:00'
1160
    resp = resp.form.submit().follow()
1161
    assert resp.html.find('a', {'href': timeperiod_edit_link}).text.strip() == u'Tuesday / 7:30 a.m. \u2192 noon'
1162
    # shorten time period end
1163
    event = Event.objects.create(agenda=agenda, places=1, desk=desk,
1164
                                 start_datetime=datetime.datetime(2017, 5, 23, 11, 30))
1165
    Booking.objects.create(event=event)
1166
    resp = app.get('/manage/agendas/%d/' % agenda.pk)
1167
    resp = resp.click(href=timeperiod_edit_link)
1168
    resp.form['end_time'] = '11:00'
1169
    resp = resp.form.submit()
1170
    error_message = resp.html.find('ul', {'class': 'errorlist'}).text.strip()
1171
    assert error_message == 'One or several bookings exist within this time period.'
1172
    # extend both time period start and end
1173
    resp = app.get('/manage/agendas/%d/' % agenda.pk)
1174
    resp = resp.click(href=timeperiod_edit_link)
1175
    resp.form['start_time'] = '7:00'
1176
    resp.form['end_time'] = '12:30'
1177
    resp = resp.form.submit().follow()
1178
    assert resp.html.find('a', {'href': timeperiod_edit_link}).text.strip() == u'Tuesday / 7 a.m. \u2192 12:30 p.m.'
1179
    # shorten both time period start and end
1180
    Booking.objects.create(event=event)
1181
    resp = app.get('/manage/agendas/%d/' % agenda.pk)
1182
    resp = resp.click(href=timeperiod_edit_link)
1183
    resp.form['start_time'] = '8:00'
1184
    resp.form['end_time'] = '11:00'
1185
    resp = resp.form.submit()
1186
    error_message = resp.html.find('ul', {'class': 'errorlist'}).text.strip()
1187
    assert error_message == 'One or several bookings exist within this time period.'
1130
-