From 5068f238ed2b164f38ddd5a9e8691814f589a9cc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 31 Oct 2019 14:36:36 +0100
Subject: [PATCH 2/2] time exceptions: paginated list of exceptions (#19033)

---
 .../templates/chrono/manager_agenda_settings.html    |  6 +++++-
 .../chrono/manager_time_period_exception_list.html   | 12 ++++++++----
 chrono/manager/urls.py                               |  2 ++
 chrono/manager/views.py                              | 12 ++++++++++++
 tests/test_manager.py                                |  7 ++++++-
 5 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/chrono/manager/templates/chrono/manager_agenda_settings.html b/chrono/manager/templates/chrono/manager_agenda_settings.html
index 561c843..f9f3839 100644
--- a/chrono/manager/templates/chrono/manager_agenda_settings.html
+++ b/chrono/manager/templates/chrono/manager_agenda_settings.html
@@ -3,7 +3,11 @@
 
 {% block breadcrumb %}
 {{ block.super }}
+{% if desk %}
+<a href="{% url 'chrono-manager-agenda-settings' desk.agenda_id %}">{% trans "Settings" %}</a>
+{% else %}
 <a href=".">{% trans "Settings" %}</a>
+{% endif %}
 {% endblock %}
 
 {% block appbar %}
@@ -144,7 +148,7 @@
                         {% if user_can_manage %}<a rel="popup" class="delete" href="{% url 'chrono-manager-time-period-exception-delete' pk=exception.id %}">{% trans "remove" %}</a>{% endif %}
                     {% endfor %}
                     {% if not desk.are_all_exceptions_displayed %}
-                    <li><a class="timeperiod-exception-all" rel="popup" data-selector="div.timeperiod" href="{% url 'chrono-manager-time-period-exception-list' pk=desk.id %}">({% trans 'see all exceptions' %})</a></li>
+                    <li><a class="timeperiod-exception-all" rel="popup" data-selector="div.timeperiod" href="{% url 'chrono-manager-time-period-exception-extract-list' pk=desk.id %}">({% trans 'see all exceptions' %})</a></li>
                     {% endif %}
                     <li><a class="add" rel="popup" href="{{add_time_period_exception_url}}">{% trans 'Add a time period exception' %}</a></li>
                 {% endif %}
diff --git a/chrono/manager/templates/chrono/manager_time_period_exception_list.html b/chrono/manager/templates/chrono/manager_time_period_exception_list.html
index d77e082..1c934f7 100644
--- a/chrono/manager/templates/chrono/manager_time_period_exception_list.html
+++ b/chrono/manager/templates/chrono/manager_time_period_exception_list.html
@@ -1,4 +1,4 @@
-{% extends "chrono/manager_desk_form.html" %}
+{% extends "chrono/manager_agenda_settings.html" %}
 {% load i18n %}
 
 {% block extrascripts %}
@@ -8,9 +8,7 @@
 
 {% block breadcrumb %}
 {{ block.super }}
-{% if desk %}
-<a href="{% url 'chrono-manager-agenda-view' pk=desk.agenda.id %}">{{desk.label}}</a>
-{% endif %}
+<a href=".">{% trans "All exceptions" %}</a>
 {% endblock %}
 
 {% block appbar %}
@@ -26,5 +24,11 @@
     </li>
     {% endfor %}
    </ul>
+
+    {% if not page_obj %}
+    <p><a href="{% url 'chrono-manager-time-period-exception-list' desk.pk %}">{% trans "Show more..." %}</a></p>
+    {% endif %}
+
+   {% include "gadjo/pagination.html" %}
 </div>
 {% endblock %}
diff --git a/chrono/manager/urls.py b/chrono/manager/urls.py
index 367278a..eba7a17 100644
--- a/chrono/manager/urls.py
+++ b/chrono/manager/urls.py
@@ -76,6 +76,8 @@ urlpatterns = [
             name='chrono-manager-time-period-exception-edit'),
         url(r'^time-period-exceptions/(?P<pk>\d+)/delete$', views.time_period_exception_delete,
             name='chrono-manager-time-period-exception-delete'),
+        url(r'^time-period-exceptions/(?P<pk>\d+)/exception-extract-list$', views.time_period_exception_extract_list,
+            name='chrono-manager-time-period-exception-extract-list'),
         url(r'^time-period-exceptions/(?P<pk>\d+)/exception-list$', views.time_period_exception_list,
             name='chrono-manager-time-period-exception-list'),
 
diff --git a/chrono/manager/views.py b/chrono/manager/views.py
index 402ec22..b425266 100644
--- a/chrono/manager/views.py
+++ b/chrono/manager/views.py
@@ -746,6 +746,7 @@ time_period_exception_edit = TimePeriodExceptionEditView.as_view()
 class TimePeriodExceptionListView(ManagedDeskMixin, ListView):
     template_name = 'chrono/manager_time_period_exception_list.html'
     model = TimePeriodException
+    paginate_by = 20
 
     def get_queryset(self):
         return self.model.objects.filter(desk=self.desk, end_datetime__gte=now())
@@ -759,6 +760,17 @@ class TimePeriodExceptionListView(ManagedDeskMixin, ListView):
 time_period_exception_list = TimePeriodExceptionListView.as_view()
 
 
+class TimePeriodExceptionExtractListView(TimePeriodExceptionListView):
+    paginate_by = None  # no pagination
+
+    def get_queryset(self):
+        # but limit queryset
+        return super(TimePeriodExceptionExtractListView, self).get_queryset()[:10]
+
+
+time_period_exception_extract_list = TimePeriodExceptionExtractListView.as_view()
+
+
 class TimePeriodExceptionDeleteView(ManagedDeskSubobjectMixin, DeleteView):
     template_name = 'chrono/manager_confirm_delete.html'
     model = TimePeriodException
diff --git a/tests/test_manager.py b/tests/test_manager.py
index 1437140..590384d 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -909,7 +909,7 @@ def test_meetings_agenda_add_time_period_exception(app, admin_user):
     assert TimePeriodException.objects.count() == 2
     assert 'Exception 1' in resp.text
     assert 'Exception 2' not in resp.text
-    resp = resp.click(href="/manage/time-period-exceptions/%d/exception-list" % agenda.desk_set.first().pk)
+    resp = resp.click(href="/manage/time-period-exceptions/%d/exception-extract-list" % agenda.desk_set.first().pk)
     assert 'Exception 1' in resp.text
     assert 'Exception 2' in resp.text
 
@@ -1040,6 +1040,11 @@ def test_exception_list(app, admin_user):
     assert '/manage/time-period-exceptions/%d/edit' % current_exception.pk in resp.text
     assert '/manage/time-period-exceptions/%d/edit' % future_exception.pk in resp.text
 
+    resp = resp.click(href="/manage/time-period-exceptions/%d/exception-extract-list" % desk.pk)
+    assert '/manage/time-period-exceptions/%d/edit' % past_exception.pk not in resp.text
+    assert '/manage/time-period-exceptions/%d/edit' % current_exception.pk in resp.text
+    assert '/manage/time-period-exceptions/%d/edit' % future_exception.pk in resp.text
+
     resp = resp.click(href="/manage/time-period-exceptions/%d/exception-list" % desk.pk)
     assert '/manage/time-period-exceptions/%d/edit' % past_exception.pk not in resp.text
     assert '/manage/time-period-exceptions/%d/edit' % current_exception.pk in resp.text
-- 
2.20.1

