From 03a738149bdb2c561c7caea818edcb7bc3552302 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 9 Jul 2020 14:50:36 +0200
Subject: [PATCH 5/5] manager: default view for events agenda (#44457)

---
 .../migrations/0051_events_default_view.py    | 24 +++++++++++++
 chrono/agendas/models.py                      |  8 +++++
 chrono/manager/forms.py                       | 12 ++++++-
 chrono/manager/views.py                       | 35 ++++++++++---------
 tests/test_manager.py                         | 22 +++++++++---
 5 files changed, 79 insertions(+), 22 deletions(-)
 create mode 100644 chrono/agendas/migrations/0051_events_default_view.py

diff --git a/chrono/agendas/migrations/0051_events_default_view.py b/chrono/agendas/migrations/0051_events_default_view.py
new file mode 100644
index 0000000..eed16cc
--- /dev/null
+++ b/chrono/agendas/migrations/0051_events_default_view.py
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('agendas', '0050_event_slug'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='agenda',
+            name='events_default_view',
+            field=models.CharField(
+                choices=[('month', 'Month view'), ('opened_events', 'Opened events view')],
+                default='month',
+                max_length=20,
+                verbose_name='Default view',
+            ),
+        ),
+    ]
diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py
index 4c8d582..6ed4e8e 100644
--- a/chrono/agendas/models.py
+++ b/chrono/agendas/models.py
@@ -54,6 +54,11 @@ AGENDA_KINDS = (
     ('virtual', _('Virtual')),
 )
 
+AGENDA_EVENTS_VIEWS = (
+    ('month', _('Month view')),
+    ('opened_events', _('Opened events view')),
+)
+
 
 def is_midnight(dtime):
     dtime = localtime(dtime)
@@ -138,6 +143,9 @@ class Agenda(models.Model):
         on_delete=models.SET_NULL,
     )
     resources = models.ManyToManyField('Resource')
+    events_default_view = models.CharField(
+        _('Default view'), max_length=20, choices=AGENDA_EVENTS_VIEWS, default='month'
+    )
 
     class Meta:
         ordering = ['label']
diff --git a/chrono/manager/forms.py b/chrono/manager/forms.py
index 94ac2d5..3420cf8 100644
--- a/chrono/manager/forms.py
+++ b/chrono/manager/forms.py
@@ -60,13 +60,23 @@ class AgendaAddForm(forms.ModelForm):
 class AgendaEditForm(AgendaAddForm):
     class Meta:
         model = Agenda
-        fields = ['label', 'slug', 'edit_role', 'view_role', 'minimal_booking_delay', 'maximal_booking_delay']
+        fields = [
+            'label',
+            'slug',
+            'edit_role',
+            'view_role',
+            'minimal_booking_delay',
+            'maximal_booking_delay',
+            'events_default_view',
+        ]
 
     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
+        if kwargs['instance'].kind != 'events':
+            del self.fields['events_default_view']
 
 
 class ResourceAddForm(forms.ModelForm):
diff --git a/chrono/manager/views.py b/chrono/manager/views.py
index 24605fd..22c49de 100644
--- a/chrono/manager/views.py
+++ b/chrono/manager/views.py
@@ -613,21 +613,6 @@ agenda_delete = AgendaDeleteView.as_view()
 
 class AgendaView(ViewableAgendaMixin, View):
     def get(self, request, *args, **kwargs):
-        if self.agenda.kind == 'virtual':
-            real_agendas = [
-                (agenda, agenda.can_be_viewed(request.user)) for agenda in self.agenda.real_agendas.all()
-            ]
-            return TemplateResponse(
-                request=request,
-                template='chrono/manager_virtual_agenda_view.html',
-                context={
-                    'real_agendas': real_agendas,
-                    'agenda': self.agenda,
-                    'object': self.agenda,
-                    'user_can_manage': self.agenda.can_be_managed(self.request.user),
-                },
-            )
-
         if self.agenda.kind == 'meetings':
             # redirect to today view
             today = datetime.date.today()
@@ -640,7 +625,25 @@ class AgendaView(ViewableAgendaMixin, View):
             )
 
         if self.agenda.kind == 'events':
-            return redirect('chrono-manager-agenda-month-redirect-view', pk=self.agenda.pk)
+            # redirect to month or opened events view
+            if self.agenda.events_default_view == 'month':
+                return redirect('chrono-manager-agenda-month-redirect-view', pk=self.agenda.pk)
+            return redirect('chrono-manager-agenda-opened-events-view', pk=self.agenda.pk)
+
+        # virtual agenda
+        real_agendas = [
+            (agenda, agenda.can_be_viewed(request.user)) for agenda in self.agenda.real_agendas.all()
+        ]
+        return TemplateResponse(
+            request=request,
+            template='chrono/manager_virtual_agenda_view.html',
+            context={
+                'real_agendas': real_agendas,
+                'agenda': self.agenda,
+                'object': self.agenda,
+                'user_can_manage': self.agenda.can_be_managed(self.request.user),
+            },
+        )
 
 
 agenda_view = AgendaView.as_view()
diff --git a/tests/test_manager.py b/tests/test_manager.py
index 0435444..67f064b 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -157,6 +157,11 @@ def test_events_agenda_redirect(app, admin_user):
     resp = app.get('/manage/agendas/%s/' % agenda.pk, status=302)
     assert resp.location.endswith('/manage/agendas/%s/month/' % agenda.pk)
 
+    agenda.events_default_view = 'opened_events'
+    agenda.save()
+    resp = app.get('/manage/agendas/%s/' % agenda.pk, status=302)
+    assert resp.location.endswith('/manage/agendas/%s/events/opened/' % agenda.pk)
+
 
 @freezegun.freeze_time('2020-07-12')
 def test_events_agenda_month_redirect(app, admin_user):
@@ -711,20 +716,27 @@ def test_add_agenda_as_manager(app, manager_user):
 
 
 def test_options_agenda(app, admin_user):
-    agenda = Agenda(label=u'Foo bar')
-    agenda.save()
+    agenda_events = Agenda.objects.create(label=u'Foo bar', kind='events')
+    agenda_meetings = Agenda.objects.create(label=u'Foo bar', kind='meetings')
+    agenda_virtual = Agenda.objects.create(label=u'Foo bar', kind='virtual')
+
     app = login(app)
-    resp = app.get('/manage/agendas/%s/settings' % agenda.pk)
-    resp = resp.click('Options')
+    resp = app.get('/manage/agendas/%s/edit' % agenda_events.pk)
     assert resp.form['label'].value == 'Foo bar'
     resp.form['label'] = 'Foo baz'
+    assert 'events_default_view' in resp.context['form'].fields
     resp = resp.form.submit()
-    assert resp.location.endswith('/manage/agendas/%s/settings' % agenda.id)
+    assert resp.location.endswith('/manage/agendas/%s/settings' % agenda_events.pk)
     resp = resp.follow()
     assert 'has_resources' not in resp.context
     assert 'Foo baz' in resp.text
     assert '<h2>Settings' in resp.text
 
+    resp = app.get('/manage/agendas/%s/edit' % agenda_meetings.pk)
+    assert 'events_default_view' not in resp.context['form'].fields
+    resp = app.get('/manage/agendas/%s/edit' % agenda_virtual.pk)
+    assert 'events_default_view' not in resp.context['form'].fields
+
 
 def test_options_agenda_cant_unset_delays(app, admin_user):
     agenda = Agenda.objects.create(label=u'Foo bar')
-- 
2.20.1

