From 4b3f64d8db04e030c4dd586205f2c2ec50767337 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Mon, 25 Jan 2021 14:23:39 +0100
Subject: [PATCH 02/11] agendas: add desk_simple_management flag (#48924)

---
 .../agendas/migrations/0074_simple_desks.py   | 19 +++++++++++++++++++
 chrono/agendas/models.py                      |  1 +
 chrono/manager/forms.py                       | 17 +++++++++++++----
 chrono/manager/views.py                       |  8 --------
 tests/test_manager.py                         | 18 ++++++++++++++++++
 5 files changed, 51 insertions(+), 12 deletions(-)
 create mode 100644 chrono/agendas/migrations/0074_simple_desks.py

diff --git a/chrono/agendas/migrations/0074_simple_desks.py b/chrono/agendas/migrations/0074_simple_desks.py
new file mode 100644
index 0000000..2854dfd
--- /dev/null
+++ b/chrono/agendas/migrations/0074_simple_desks.py
@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('agendas', '0073_auto_20210125_1800'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='agenda',
+            name='desk_simple_management',
+            field=models.BooleanField(default=False),
+        ),
+    ]
diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py
index 8427816..2b86133 100644
--- a/chrono/agendas/models.py
+++ b/chrono/agendas/models.py
@@ -195,6 +195,7 @@ class Agenda(models.Model):
     booking_form_url = models.CharField(
         _('Booking form URL'), max_length=200, blank=True, validators=[django_template_validator]
     )
+    desk_simple_management = models.BooleanField(default=False)
 
     class Meta:
         ordering = ['label']
diff --git a/chrono/manager/forms.py b/chrono/manager/forms.py
index 057fc5e..675cf6a 100644
--- a/chrono/manager/forms.py
+++ b/chrono/manager/forms.py
@@ -54,10 +54,6 @@ from .widgets import SplitDateTimeField
 
 
 class AgendaAddForm(forms.ModelForm):
-    class Meta:
-        model = Agenda
-        fields = ['label', 'kind', 'category', 'edit_role', 'view_role']
-
     edit_role = forms.ModelChoiceField(
         label=_('Edit Role'), required=False, queryset=Group.objects.all().order_by('name')
     )
@@ -65,6 +61,19 @@ class AgendaAddForm(forms.ModelForm):
         label=_('View Role'), required=False, queryset=Group.objects.all().order_by('name')
     )
 
+    class Meta:
+        model = Agenda
+        fields = ['label', 'kind', 'category', 'edit_role', 'view_role']
+
+    def save(self, *args, **kwargs):
+        super().save()
+        if self.instance.kind == 'meetings':
+            default_desk = self.instance.desk_set.create(label=_('Desk 1'))
+            default_desk.import_timeperiod_exceptions_from_settings(enable=True)
+            self.instance.desk_simple_management = True
+            self.instance.save()
+        return self.instance
+
 
 class AgendaEditForm(forms.ModelForm):
     class Meta:
diff --git a/chrono/manager/views.py b/chrono/manager/views.py
index 371899f..1ba86d7 100644
--- a/chrono/manager/views.py
+++ b/chrono/manager/views.py
@@ -616,14 +616,6 @@ class AgendaAddView(CreateView):
             raise PermissionDenied()
         return super(AgendaAddView, self).dispatch(request, *args, **kwargs)
 
-    def form_valid(self, form):
-        model_form = super(AgendaAddView, self).form_valid(form)
-        if self.object.kind == 'meetings':
-            default_desk = Desk(agenda=self.object, label=_('Desk 1'))
-            default_desk.save()
-            default_desk.import_timeperiod_exceptions_from_settings(enable=True)
-        return model_form
-
     def get_success_url(self):
         return reverse('chrono-manager-agenda-settings', kwargs={'pk': self.object.id})
 
diff --git a/tests/test_manager.py b/tests/test_manager.py
index 248dc91..1ef3a6a 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -883,6 +883,24 @@ def test_add_agenda(app, admin_user):
     assert '<h2>Settings' in resp.text
     assert agenda.minimal_booking_delay == 1
     assert agenda.maximal_booking_delay == 56
+    assert agenda.kind == 'events'
+    assert agenda.desk_simple_management is False
+
+    resp = app.get('/manage/agendas/add/')
+    resp.form['label'] = 'Foo bar 2'
+    resp.form['kind'] = 'meetings'
+    resp = resp.form.submit()
+    agenda = Agenda.objects.latest('pk')
+    assert agenda.kind == 'meetings'
+    assert agenda.desk_simple_management is True
+
+    resp = app.get('/manage/agendas/add/')
+    resp.form['label'] = 'Foo bar 3'
+    resp.form['kind'] = 'virtual'
+    resp = resp.form.submit()
+    agenda = Agenda.objects.latest('pk')
+    assert agenda.kind == 'virtual'
+    assert agenda.desk_simple_management is False
 
 
 @override_settings(
-- 
2.29.2

