From 30e2ef31e423cb7d94bcaaf7753e7a8d0dd1735f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Sun, 22 Dec 2019 16:13:08 +0100
Subject: [PATCH 2/4] manager: update event URIs to be under /agendas/<pk>/
 (#20279)

---
 chrono/agendas/models.py                      |  2 +-
 .../templates/chrono/manager_event_form.html  |  2 +-
 .../manager_events_agenda_settings.html       |  4 ++--
 chrono/manager/urls.py                        | 12 +++++++++--
 chrono/manager/views.py                       |  6 ++++--
 tests/test_manager.py                         | 20 +++++++++----------
 6 files changed, 28 insertions(+), 18 deletions(-)

diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py
index f6fbe6f..2b74555 100644
--- a/chrono/agendas/models.py
+++ b/chrono/agendas/models.py
@@ -369,7 +369,7 @@ class Event(models.Model):
         return self.start_datetime + datetime.timedelta(minutes=self.meeting_type.duration)
 
     def get_absolute_url(self):
-        return reverse('chrono-manager-event-edit', kwargs={'pk': self.id})
+        return reverse('chrono-manager-event-edit', kwargs={'pk': self.agenda.id, 'event_pk': self.id})
 
     @classmethod
     def import_json(cls, data):
diff --git a/chrono/manager/templates/chrono/manager_event_form.html b/chrono/manager/templates/chrono/manager_event_form.html
index 44f28cf..c005276 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.id %}">{% trans 'Delete' %}</a>
+<a rel="popup" href="{% url 'chrono-manager-event-delete' pk=object.agenda.id event_pk=object.id %}">{% trans 'Delete' %}</a>
 </span>
 {% else %}
 <h2>{% trans "New Event" %}</h2>
diff --git a/chrono/manager/templates/chrono/manager_events_agenda_settings.html b/chrono/manager/templates/chrono/manager_events_agenda_settings.html
index 779d450..7848d77 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=event.id %}{% else %}#{% endif %}">
+        ><a rel="popup" href="{% if user_can_manage %}{% url 'chrono-manager-event-edit' pk=agenda.id event_pk=event.id %}{% 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=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 %}">{% trans "remove" %}</a>{% endif %}
         <span class="occupation-bar"></span>
     </li>
     {% endfor %}
diff --git a/chrono/manager/urls.py b/chrono/manager/urls.py
index a163f0a..8cee6b8 100644
--- a/chrono/manager/urls.py
+++ b/chrono/manager/urls.py
@@ -43,8 +43,16 @@ urlpatterns = [
         views.agenda_import_events,
         name='chrono-manager-agenda-import-events',
     ),
-    url(r'^events/(?P<pk>\d+)/$', views.event_edit, name='chrono-manager-event-edit'),
-    url(r'^events/(?P<pk>\d+)/delete$', views.event_delete, name='chrono-manager-event-delete'),
+    url(
+        r'^agendas/(?P<pk>\d+)/events/(?P<event_pk>\d+)/edit$',
+        views.event_edit,
+        name='chrono-manager-event-edit',
+    ),
+    url(
+        r'^agendas/(?P<pk>\d+)/events/(?P<event_pk>\d+)/delete$',
+        views.event_delete,
+        name='chrono-manager-event-delete',
+    ),
     url(
         r'^agendas/(?P<pk>\d+)/add-meeting-type$',
         views.agenda_add_meeting_type,
diff --git a/chrono/manager/views.py b/chrono/manager/views.py
index 5abc456..92477ef 100644
--- a/chrono/manager/views.py
+++ b/chrono/manager/views.py
@@ -683,18 +683,20 @@ class AgendaImportEventsView(ManagedAgendaMixin, FormView):
 agenda_import_events = AgendaImportEventsView.as_view()
 
 
-class EventEditView(ManagedAgendaSubobjectMixin, UpdateView):
+class EventEditView(ManagedAgendaMixin, UpdateView):
     template_name = 'chrono/manager_event_form.html'
     model = Event
     form_class = EventForm
+    pk_url_kwarg = 'event_pk'
 
 
 event_edit = EventEditView.as_view()
 
 
-class EventDeleteView(ManagedAgendaSubobjectMixin, DeleteView):
+class EventDeleteView(ManagedAgendaMixin, DeleteView):
     template_name = 'chrono/manager_confirm_delete.html'
     model = Event
+    pk_url_kwarg = 'event_pk'
 
     def get_context_data(self, **kwargs):
         context = super(EventDeleteView, self).get_context_data(**kwargs)
diff --git a/tests/test_manager.py b/tests/test_manager.py
index f2b703e..000aae2 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -358,7 +358,7 @@ def test_add_event(app, admin_user):
     event = Event.objects.get(places=10)
     assert event.slug is None
     assert not "This agenda doesn't have any event yet." in resp.text
-    assert '/manage/events/%s/' % event.id in resp.text
+    assert '/manage/agendas/%s/events/%s/' % (agenda.id, event.id) in resp.text
     assert ('Feb. 15, %s, 5 p.m.' % year) in resp.text
     assert '10 places' in resp.text
 
@@ -404,7 +404,7 @@ def test_add_event_as_manager(app, manager_user):
     resp = resp.follow()
     event = Event.objects.get(places=10)
     assert not "This agenda doesn't have any event yet." in resp.text
-    assert '/manage/events/%s/' % event.id in resp.text
+    assert '/manage/agendas/%s/events/%s/edit' % (agenda.id, event.id) in resp.text
     assert 'Feb. 15, 2016, 5 p.m.' in resp.text
     assert '10 places' in resp.text
 
@@ -422,7 +422,7 @@ def test_edit_event(app, admin_user):
     resp.form['places'] = 20
     resp = resp.form.submit()
     resp = resp.follow()
-    assert '/manage/events/%s/' % event.id in resp.text
+    assert '/manage/agendas/%s/events/%s/edit' % (agenda.id, event.id) in resp.text
     assert 'Feb. 16, 2016, 5 p.m.' in resp.text
     assert '20 places' in resp.text
 
@@ -439,7 +439,7 @@ def test_edit_event_as_manager(app, manager_user):
     event = Event(start_datetime=make_aware(datetime.datetime(2016, 2, 15, 17, 0)), places=20, agenda=agenda)
     event.save()
     app = login(app, username='manager', password='manager')
-    resp = app.get('/manage/events/%s/' % event.id, status=403)
+    resp = app.get('/manage/agendas/%s/events/%s/edit' % (agenda.id, event.id), status=403)
 
     agenda.edit_role = manager_user.groups.all()[0]
     agenda.save()
@@ -450,7 +450,7 @@ def test_edit_event_as_manager(app, manager_user):
     resp.form['places'] = 20
     resp = resp.form.submit()
     resp = resp.follow()
-    assert '/manage/events/%s/' % event.id in resp.text
+    assert '/manage/agendas/%s/events/%s/edit' % (agenda.id, event.id) in resp.text
     assert 'Feb. 16, 2016, 5 p.m.' in resp.text
     assert '20 places' in resp.text
 
@@ -502,7 +502,7 @@ def test_delete_event(app, admin_user):
 
     app = login(app)
     resp = app.get('/manage/agendas/%s/settings' % agenda.id, status=200)
-    resp = resp.click(href=r'/manage/events/%s/$' % event.id)
+    resp = resp.click(href='/manage/agendas/%s/events/%s/edit' % (agenda.id, event.id))
     resp = resp.click('Delete')
     resp = resp.form.submit()
     assert resp.location.endswith('/manage/agendas/%s/settings' % agenda.id)
@@ -517,21 +517,21 @@ def test_delete_busy_event(app, admin_user):
 
     app = login(app)
     resp = app.get('/manage/agendas/%s/settings' % agenda.id, status=200)
-    resp = resp.click(href=r'/manage/events/%s/$' % event.id)
+    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
 
     booking = Booking(event=event)
     booking.save()
     resp = app.get('/manage/agendas/%s/settings' % agenda.id, status=200)
-    resp = resp.click(href=r'/manage/events/%s/$' % event.id)
+    resp = resp.click(href='/manage/agendas/%s/events/%s/edit' % (agenda.id, event.id))
     resp = resp.click('Delete')
     assert 'This cannot be removed' in resp.text
 
     booking.cancellation_datetime = now()
     booking.save()
     resp = app.get('/manage/agendas/%s/settings' % agenda.id, status=200)
-    resp = resp.click(href=r'/manage/events/%s/$' % event.id)
+    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
 
@@ -551,7 +551,7 @@ def test_delete_event_as_manager(app, manager_user):
 
     app = login(app, username='manager', password='manager')
     resp = app.get('/manage/agendas/%s/settings' % agenda.id, status=200)
-    resp = resp.click(href=r'/manage/events/%s/$' % event.id)
+    resp = resp.click(href='/manage/agendas/%s/events/%s/edit' % (agenda.id, event.id))
     resp = resp.click('Delete')
     resp = resp.form.submit()
     assert resp.location.endswith('/manage/agendas/%s/settings' % agenda.id)
-- 
2.24.1

