From 741a3d1407473839b4d44e1db7b5d3a53f2e3bb6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Mon, 23 Dec 2019 10:23:42 +0100
Subject: [PATCH 4/4] manager: add direct links from event view to event
 options/delete (#20279)

---
 .../chrono/manager_confirm_event_delete.html  | 24 +++++++++++++++++++
 .../chrono/manager_event_detail.html          |  5 ++++
 .../templates/chrono/manager_event_form.html  |  5 ++--
 .../manager_events_agenda_settings.html       |  4 ++--
 chrono/manager/views.py                       | 17 ++++++++++++-
 tests/test_manager.py                         | 22 +++++++++++++++--
 6 files changed, 70 insertions(+), 7 deletions(-)
 create mode 100644 chrono/manager/templates/chrono/manager_confirm_event_delete.html

diff --git a/chrono/manager/templates/chrono/manager_confirm_event_delete.html b/chrono/manager/templates/chrono/manager_confirm_event_delete.html
new file mode 100644
index 0000000..4e3af09
--- /dev/null
+++ b/chrono/manager/templates/chrono/manager_confirm_event_delete.html
@@ -0,0 +1,24 @@
+{% extends "chrono/manager_home.html" %}
+{% load i18n %}
+
+{% block appbar %}
+<h2>{{ view.model.get_verbose_name }}</h2>
+{% endblock %}
+
+{% block content %}
+<form method="post">
+  {% csrf_token %}
+  <input type="hidden" name="next" value="{% firstof request.POST.next request.GET.next %}">
+  <p>
+  {% if cannot_delete %}
+  {% blocktrans %}This cannot be removed as there are bookings.{% endblocktrans %}
+  {% else %}
+  {% blocktrans %}Are you sure you want to delete this event?{% endblocktrans %}
+  {% endif %}
+  </p>
+  <div class="buttons">
+    <button class="delete-button" {% if cannot_delete %}disabled="disabled"{% endif %}>{% trans 'Delete' %}</button>
+    <a class="cancel" href="{{ view.get_success_url }}">{% trans 'Cancel' %}</a>
+  </div>
+</form>
+{% endblock %}
diff --git a/chrono/manager/templates/chrono/manager_event_detail.html b/chrono/manager/templates/chrono/manager_event_detail.html
index 88048e7..f533e8a 100644
--- a/chrono/manager/templates/chrono/manager_event_detail.html
+++ b/chrono/manager/templates/chrono/manager_event_detail.html
@@ -15,6 +15,11 @@
 {% if object.label %}<h2>{{ object.label }} — {{object.start_datetime|date:"DATETIME_FORMAT"}}</h2>
 {% else %}<h2>{{ object.start_datetime|date:"DATETIME_FORMAT"}}</h2>
 {% endif %}
+<span class="actions">
+{% if user_can_manage %}
+<a href="{% url 'chrono-manager-event-edit' pk=agenda.id event_pk=object.id %}">{% trans "Options" %}</a>
+{% endif %}
+</span>
 {% endblock %}
 
 {% block content %}
diff --git a/chrono/manager/templates/chrono/manager_event_form.html b/chrono/manager/templates/chrono/manager_event_form.html
index c005276..1d74945 100644
--- a/chrono/manager/templates/chrono/manager_event_form.html
+++ b/chrono/manager/templates/chrono/manager_event_form.html
@@ -17,7 +17,7 @@
 {% if object.id %}
 <h2>{% trans "Edit Event" %}</h2>
 <span class="actions">
-<a rel="popup" href="{% url 'chrono-manager-event-delete' pk=object.agenda.id event_pk=object.id %}">{% trans 'Delete' %}</a>
+<a rel="popup" href="{% url 'chrono-manager-event-delete' pk=object.agenda.id event_pk=object.id %}?next={{view.request.GET.next}}">{% trans 'Delete' %}</a>
 </span>
 {% else %}
 <h2>{% trans "New Event" %}</h2>
@@ -28,10 +28,11 @@
 
 <form method="post" enctype="multipart/form-data">
   {% csrf_token %}
+  <input type="hidden" name="next" value="{% firstof request.POST.next request.GET.next %}">
   {{ form.as_p }}
   <div class="buttons">
     <button class="submit-button">{% trans "Save" %}</button>
-    <a class="cancel" href="{% url 'chrono-manager-agenda-settings' pk=agenda.id %}">{% trans 'Cancel' %}</a>
+    <a class="cancel" href="{{ view.get_success_url }}">{% trans 'Cancel' %}</a>
   </div>
 </form>
 {% endblock %}
diff --git a/chrono/manager/templates/chrono/manager_events_agenda_settings.html b/chrono/manager/templates/chrono/manager_events_agenda_settings.html
index 7848d77..0896c1d 100644
--- a/chrono/manager/templates/chrono/manager_events_agenda_settings.html
+++ b/chrono/manager/templates/chrono/manager_events_agenda_settings.html
@@ -22,7 +22,7 @@
         {% elif event.waiting_list_places %}
           data-total="{{event.waiting_list_places}}" data-booked="{{event.waiting_list}}"
         {% endif %}
-        ><a rel="popup" href="{% if user_can_manage %}{% url 'chrono-manager-event-edit' pk=agenda.id event_pk=event.id %}{% else %}#{% endif %}">
+        ><a rel="popup" href="{% if user_can_manage %}{% url 'chrono-manager-event-edit' pk=agenda.id event_pk=event.id %}?next=settings{% else %}#{% endif %}">
         {% if event.label %}{{event.label}} / {% endif %}
         {{ event.start_datetime }}
         {% if event.full %}/ <span class="full">{% trans "full" %}</span>{% endif %}
@@ -41,7 +41,7 @@
         ({% trans "out of bookable period" %})
         {% endif %}
             </a>
-        {% if user_can_manage %}<a rel="popup" class="delete" href="{% url 'chrono-manager-event-delete' pk=agenda.id event_pk=event.id %}">{% trans "remove" %}</a>{% endif %}
+        {% if user_can_manage %}<a rel="popup" class="delete" href="{% url 'chrono-manager-event-delete' pk=agenda.id event_pk=event.id %}?next=settings">{% trans "remove" %}</a>{% endif %}
         <span class="occupation-bar"></span>
     </li>
     {% endfor %}
diff --git a/chrono/manager/views.py b/chrono/manager/views.py
index 34045ce..53ba996 100644
--- a/chrono/manager/views.py
+++ b/chrono/manager/views.py
@@ -690,6 +690,7 @@ class EventDetailView(ViewableAgendaMixin, DetailView):
 
     def get_context_data(self, **kwargs):
         context = super(EventDetailView, self).get_context_data(**kwargs)
+        context['user_can_manage'] = self.agenda.can_be_managed(self.request.user)
         event = self.get_object()
         context['booked'] = event.booking_set.filter(
             cancellation_datetime__isnull=True, in_waiting_list=False
@@ -709,12 +710,17 @@ class EventEditView(ManagedAgendaMixin, UpdateView):
     form_class = EventForm
     pk_url_kwarg = 'event_pk'
 
+    def get_success_url(self):
+        if self.request.GET.get('next') == 'settings' or self.request.POST.get('next') == 'settings':
+            return reverse('chrono-manager-agenda-settings', kwargs={'pk': self.agenda.id})
+        return reverse('chrono-manager-event-view', kwargs={'pk': self.agenda.id, 'event_pk': self.object.id})
+
 
 event_edit = EventEditView.as_view()
 
 
 class EventDeleteView(ManagedAgendaMixin, DeleteView):
-    template_name = 'chrono/manager_confirm_delete.html'
+    template_name = 'chrono/manager_confirm_event_delete.html'
     model = Event
     pk_url_kwarg = 'event_pk'
 
@@ -733,6 +739,15 @@ class EventDeleteView(ManagedAgendaMixin, DeleteView):
             raise PermissionDenied()
         return super(EventDeleteView, self).delete(request, *args, **kwargs)
 
+    def get_success_url(self):
+        if self.request.GET.get('next') == 'settings' or self.request.POST.get('next') == 'settings':
+            return reverse('chrono-manager-agenda-settings', kwargs={'pk': self.agenda.id})
+        day = self.object.start_datetime
+        return reverse(
+            'chrono-manager-agenda-month-view',
+            kwargs={'pk': self.agenda.id, 'year': day.year, 'month': day.month},
+        )
+
 
 event_delete = EventDeleteView.as_view()
 
diff --git a/tests/test_manager.py b/tests/test_manager.py
index fbc1a1b..a5709ff 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -519,7 +519,7 @@ def test_delete_busy_event(app, admin_user):
     resp = app.get('/manage/agendas/%s/settings' % agenda.id, status=200)
     resp = resp.click(href='/manage/agendas/%s/events/%s/edit' % (agenda.id, event.id))
     resp = resp.click('Delete')
-    assert 'Are you sure you want to delete this?' in resp.text
+    assert 'Are you sure you want to delete this event?' in resp.text
 
     booking = Booking(event=event)
     booking.save()
@@ -533,7 +533,7 @@ def test_delete_busy_event(app, admin_user):
     resp = app.get('/manage/agendas/%s/settings' % agenda.id, status=200)
     resp = resp.click(href='/manage/agendas/%s/events/%s/edit' % (agenda.id, event.id))
     resp = resp.click('Delete')
-    assert 'Are you sure you want to delete this?' in resp.text
+    assert 'Are you sure you want to delete this event?' in resp.text
 
     # suddenly the booking is no longer cancelled, but the admin clicks on the
     # delete button.
@@ -1883,6 +1883,24 @@ def test_agenda_view_event(app, manager_user):
     assert 'overbooked' in resp.text
 
 
+def test_agenda_view_edit_event(app, manager_user):
+    test_agenda_view_event(app, manager_user)
+    agenda = Agenda.objects.first()
+    resp = app.get('/manage/agendas/%s/2019/12/' % agenda.id, status=200)
+    resp = resp.click('xyz')
+    assert 'Options' not in resp.text
+
+    agenda.edit_role = manager_user.groups.all()[0]
+    agenda.save()
+    event_url = resp.request.url
+    resp = app.get(event_url)
+    assert 'Options' in resp.text
+    resp = resp.click('Options')
+    resp.form['start_datetime'] = agenda.event_set.first().start_datetime.strftime('%Y-%m-%d %H:%M')
+    resp = resp.form.submit(status=302).follow()
+    assert event_url == resp.request.url
+
+
 def test_import_agenda_as_manager(app, manager_user):
     # open /manage/ access to manager_user, and check agenda import is not
     # allowed.
-- 
2.24.1

