From c137bf507bb811dcfb95e5c4d97df76d9e7c4b68 Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Tue, 25 Feb 2020 08:38:25 +0100
Subject: [PATCH 4/8] manager: allow empty booking delays on virtual agendas
 (#40121)

---
 .../agendas/migrations/0039_booking_delays.py | 29 +++++++++++++++++++
 chrono/agendas/models.py                      | 11 +++++--
 chrono/manager/forms.py                       |  6 ++++
 tests/test_manager.py                         | 28 ++++++++++++++++++
 4 files changed, 72 insertions(+), 2 deletions(-)
 create mode 100644 chrono/agendas/migrations/0039_booking_delays.py

diff --git a/chrono/agendas/migrations/0039_booking_delays.py b/chrono/agendas/migrations/0039_booking_delays.py
new file mode 100644
index 0000000..213a511
--- /dev/null
+++ b/chrono/agendas/migrations/0039_booking_delays.py
@@ -0,0 +1,29 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.18 on 2020-02-25 07:33
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('agendas', '0038_start_virtual_agendas'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='agenda',
+            name='maximal_booking_delay',
+            field=models.PositiveIntegerField(
+                blank=True, default=None, null=True, verbose_name='Maximal booking delay (in days)'
+            ),
+        ),
+        migrations.AlterField(
+            model_name='agenda',
+            name='minimal_booking_delay',
+            field=models.PositiveIntegerField(
+                blank=True, default=None, null=True, verbose_name='Minimal booking delay (in days)'
+            ),
+        ),
+    ]
diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py
index 64aaf56..655dd22 100644
--- a/chrono/agendas/models.py
+++ b/chrono/agendas/models.py
@@ -74,9 +74,11 @@ class Agenda(models.Model):
     label = models.CharField(_('Label'), max_length=150)
     slug = models.SlugField(_('Identifier'), max_length=160, unique=True)
     kind = models.CharField(_('Kind'), max_length=20, choices=AGENDA_KINDS, default='events')
-    minimal_booking_delay = models.PositiveIntegerField(_('Minimal booking delay (in days)'), default=1)
+    minimal_booking_delay = models.PositiveIntegerField(
+        _('Minimal booking delay (in days)'), default=None, null=True, blank=True
+    )
     maximal_booking_delay = models.PositiveIntegerField(
-        _('Maximal booking delay (in days)'), default=56
+        _('Maximal booking delay (in days)'), default=None, null=True, blank=True
     )  # eight weeks
     real_agendas = models.ManyToManyField(
         'self',
@@ -113,6 +115,11 @@ class Agenda(models.Model):
     def save(self, *args, **kwargs):
         if not self.slug:
             self.slug = generate_slug(self)
+        if self.kind != 'virtual':
+            if self.minimal_booking_delay is None:
+                self.minimal_booking_delay = 1
+            if self.maximal_booking_delay is None:
+                self.maximal_booking_delay = 56
         super(Agenda, self).save(*args, **kwargs)
 
     def get_absolute_url(self):
diff --git a/chrono/manager/forms.py b/chrono/manager/forms.py
index 6ce6a91..43c8413 100644
--- a/chrono/manager/forms.py
+++ b/chrono/manager/forms.py
@@ -60,6 +60,12 @@ class AgendaEditForm(AgendaAddForm):
         model = Agenda
         fields = ['label', 'slug', 'edit_role', 'view_role', 'minimal_booking_delay', 'maximal_booking_delay']
 
+    def __init__(self, *args, **kwargs):
+        super(AgendaEditForm, self).__init__(*args, **kwargs)
+        if kwargs['instance'].kind != 'virtual':
+            self.fields['minimal_booking_delay'].required = True
+            self.fields['maximal_booking_delay'].required = True
+
 
 class NewEventForm(forms.ModelForm):
     class Meta:
diff --git a/tests/test_manager.py b/tests/test_manager.py
index f413167..687494f 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -188,6 +188,8 @@ def test_add_agenda(app, admin_user):
     resp = resp.follow()
     assert 'Foo bar' in resp.text
     assert '<h2>Settings' in resp.text
+    assert agenda.minimal_booking_delay == 1
+    assert agenda.maximal_booking_delay == 56
 
 
 def test_add_agenda_as_manager(app, manager_user):
@@ -218,6 +220,30 @@ def test_options_agenda(app, admin_user):
     assert '<h2>Settings' in resp.text
 
 
+def test_options_agenda_cant_unset_delays(app, admin_user):
+    agenda = Agenda.objects.create(label=u'Foo bar')
+    assert agenda.minimal_booking_delay == 1
+    app = login(app)
+    url = '/manage/agendas/%s/edit' % agenda.pk
+    resp = app.get(url)
+    resp.form['minimal_booking_delay'] = None
+    resp = resp.form.submit()
+    agenda = Agenda.objects.get(label=u'Foo bar')
+    assert agenda.minimal_booking_delay == 1
+
+
+def test_options_virtuale_agenda_can_unset_delays(app, admin_user):
+    agenda = Agenda.objects.create(label=u'Foo bar', kind='virtual', maximal_booking_delay=2)
+    assert agenda.maximal_booking_delay == 2
+    app = login(app)
+    url = '/manage/agendas/%s/edit' % agenda.pk
+    resp = app.get(url)
+    resp.form['maximal_booking_delay'] = None
+    resp = resp.form.submit()
+    agenda = Agenda.objects.get(label=u'Foo bar')
+    assert agenda.maximal_booking_delay is None
+
+
 def test_options_agenda_as_manager(app, manager_user):
     agenda = Agenda(label=u'Foo bar')
     agenda.view_role = manager_user.groups.all()[0]
@@ -2183,6 +2209,8 @@ def test_virtual_agenda_add(app, admin_user):
     resp = resp.form.submit()
     agenda = Agenda.objects.get(label='Virtual agenda')
     assert resp.location.endswith('/manage/agendas/%s/settings' % agenda.id)
+    assert agenda.minimal_booking_delay is None
+    assert agenda.maximal_booking_delay is None
 
 
 def test_virtual_agenda_baseview_empty(app, admin_user):
-- 
2.20.1

