From ba4f20b971c236f0ca3601cb5529dede36f43809 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Fri, 22 May 2020 14:18:41 +0200
Subject: [PATCH] agenda: dont check for bookings on exception creation
 (#42336)

---
 chrono/agendas/models.py | 16 ----------
 tests/test_manager.py    | 69 ----------------------------------------
 2 files changed, 85 deletions(-)

diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py
index 65c86d3..99fed9a 100644
--- a/chrono/agendas/models.py
+++ b/chrono/agendas/models.py
@@ -1146,22 +1146,6 @@ class TimePeriodException(models.Model):
 
         return exc_repr
 
-    def clean(self):
-        super(TimePeriodException, self).clean()
-        if self.has_booking_within_time_slot():
-            raise ValidationError(_('One or several bookings exists within this time slot.'))
-
-    def has_booking_within_time_slot(self):
-        if not (self.start_datetime and self.end_datetime):
-            # incomplete time period, can't tell
-            return False
-        for event in Event.objects.filter(
-            desk=self.desk, booking__isnull=False, booking__cancellation_datetime__isnull=True
-        ):
-            if self.start_datetime <= event.start_datetime < self.end_datetime:
-                return True
-        return False
-
     @classmethod
     def import_json(cls, data):
         def import_datetime(s):
diff --git a/tests/test_manager.py b/tests/test_manager.py
index 0e9ff77..20b9f74 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -1090,75 +1090,6 @@ def test_meetings_agenda_add_time_period_exception(app, admin_user):
     assert 'Exception 2' in resp.text
 
 
-def test_meetings_agenda_add_time_period_exception_when_booking_exists(app, admin_user):
-    agenda = Agenda.objects.create(label='Foo bar', kind='meetings')
-    desk = Desk.objects.create(agenda=agenda, label='Desk A')
-    MeetingType(agenda=agenda, label='Blah').save()
-    TimePeriod.objects.create(
-        weekday=1, desk=desk, start_time=datetime.time(10, 0), end_time=datetime.time(12, 0)
-    )
-    event = Event.objects.create(
-        agenda=agenda, places=1, desk=desk, start_datetime=make_aware(datetime.datetime(2017, 5, 22, 10, 30))
-    )
-    Booking.objects.create(event=event)
-    login(app)
-    resp = app.get('/manage/agendas/%d/' % agenda.pk).follow()
-    resp = resp.click('Settings')
-    resp = resp.click('Add a time period exception')
-    resp = resp.form.submit()  # submit empty form
-    # fields should be marked with errors
-    assert resp.text.count('This field is required.') == 2
-    # try again with data in fields
-    resp.form['start_datetime$date'] = '2017-05-22'
-    resp.form['start_datetime$time'] = '08:00'
-    resp.form['end_datetime$date'] = '2017-05-26'
-    resp.form['end_datetime$time'] = '17:30'
-    resp = resp.form.submit()
-    assert 'One or several bookings exists within this time slot.' in resp.text
-    assert TimePeriodException.objects.count() == 0
-
-    # check it's possible to add an exception on another desk
-    desk = Desk.objects.create(agenda=agenda, label='Desk B')
-    TimePeriod.objects.create(
-        weekday=1, desk=desk, start_time=datetime.time(10, 0), end_time=datetime.time(12, 0)
-    )
-    resp = app.get('/manage/agendas/%d/' % agenda.pk).follow()
-    resp = resp.click('Settings')
-    resp = resp.click('Add a time period exception', href='desk/%s/' % desk.id)
-    resp.form['start_datetime$date'] = '2017-05-22'
-    resp.form['start_datetime$time'] = '08:00'
-    resp.form['end_datetime$date'] = '2017-05-26'
-    resp.form['end_datetime$time'] = '17:30'
-    resp = resp.form.submit()
-    assert TimePeriodException.objects.count() == 1
-
-
-def test_meetings_agenda_add_time_period_exception_when_cancelled_booking_exists(app, admin_user):
-    agenda = Agenda.objects.create(label='Foo bar', kind='meetings')
-    desk = Desk.objects.create(agenda=agenda, label='Desk A')
-    MeetingType(agenda=agenda, label='Blah').save()
-    TimePeriod.objects.create(
-        weekday=1, desk=desk, start_time=datetime.time(10, 0), end_time=datetime.time(12, 0)
-    )
-    event = Event.objects.create(
-        agenda=agenda, places=1, start_datetime=make_aware(datetime.datetime(2017, 5, 22, 10, 30))
-    )
-    Booking.objects.create(
-        event=event, cancellation_datetime=make_aware(datetime.datetime(2017, 5, 20, 10, 30))
-    )
-    login(app)
-    resp = app.get('/manage/agendas/%d/' % agenda.pk).follow()
-    resp = resp.click('Settings')
-    resp = resp.click('Add a time period exception')
-    resp.form['start_datetime$date'] = '2017-05-22'
-    resp.form['start_datetime$time'] = '08:00'
-    resp.form['end_datetime$date'] = '2017-05-26'
-    resp.form['end_datetime$time'] = '17:30'
-    resp = resp.form.submit()
-    assert 'One or several bookings exists within this time slot.' not in resp.text
-    assert TimePeriodException.objects.count() == 1
-
-
 def test_meetings_agenda_add_invalid_time_period_exception():
     form = TimePeriodExceptionForm(
         data={
-- 
2.20.1

