From d87689a6eab63e64371315bff842be8d4e12c33e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 30 Sep 2021 08:52:26 +0200
Subject: [PATCH] wcs: pagination configuration for cards cell (#57322)

---
 combo/apps/wcs/forms.py                       |  2 +-
 combo/apps/wcs/migrations/0040_cards_limit.py | 18 ++++++++++++++++++
 combo/apps/wcs/models.py                      |  4 ++++
 combo/public/templates/combo/pagination.html  |  2 +-
 tests/test_wcs.py                             |  3 +++
 5 files changed, 27 insertions(+), 2 deletions(-)
 create mode 100644 combo/apps/wcs/migrations/0040_cards_limit.py

diff --git a/combo/apps/wcs/forms.py b/combo/apps/wcs/forms.py
index dcbc83af..55bfe6c9 100644
--- a/combo/apps/wcs/forms.py
+++ b/combo/apps/wcs/forms.py
@@ -48,7 +48,7 @@ class WcsCardsCellForm(forms.ModelForm):
 
     class Meta:
         model = WcsCardsCell
-        fields = ('carddef_reference', 'custom_title', 'only_for_user')
+        fields = ('carddef_reference', 'custom_title', 'limit', 'only_for_user')
 
     def __init__(self, *args, **kwargs):
         instance = kwargs['instance']
diff --git a/combo/apps/wcs/migrations/0040_cards_limit.py b/combo/apps/wcs/migrations/0040_cards_limit.py
new file mode 100644
index 00000000..5bc6016b
--- /dev/null
+++ b/combo/apps/wcs/migrations/0040_cards_limit.py
@@ -0,0 +1,18 @@
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('wcs', '0039_card_title_type'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='wcscardscell',
+            name='limit',
+            field=models.PositiveSmallIntegerField(
+                blank=True, null=True, verbose_name='Number of cards per page (default 10)'
+            ),
+        ),
+    ]
diff --git a/combo/apps/wcs/models.py b/combo/apps/wcs/models.py
index 336ea417..d468194d 100644
--- a/combo/apps/wcs/models.py
+++ b/combo/apps/wcs/models.py
@@ -780,6 +780,9 @@ class WcsCardsCell(CardMixin, WcsBlurpMixin, CellBase):
     custom_title = models.CharField(_('Custom Title'), max_length=150, blank=True)
     only_for_user = models.BooleanField(_('Limit to cards linked to the logged-in user'), default=False)
     without_user = models.BooleanField(_('Ignore the logged-in user'), default=False)
+    limit = models.PositiveSmallIntegerField(
+        _('Number of cards per page (default 10)'), null=True, blank=True
+    )
 
     default_template_name = 'combo/wcs/cards.html'
     variable_name = 'cards'
@@ -863,6 +866,7 @@ class WcsCardsCell(CardMixin, WcsBlurpMixin, CellBase):
 
     def get_cell_extra_context(self, context):
         extra_context = super().get_cell_extra_context(context)
+        extra_context['paginate_by'] = self.limit or 10
         extra_context['title'] = self.custom_title or self.cached_title
 
         pages_with_sub_slug = Page.objects.exclude(sub_slug='')
diff --git a/combo/public/templates/combo/pagination.html b/combo/public/templates/combo/pagination.html
index 02f33ce7..231c3391 100644
--- a/combo/public/templates/combo/pagination.html
+++ b/combo/public/templates/combo/pagination.html
@@ -1,5 +1,5 @@
 {% load static %}
-<div class="cell-items-pagination" data-paginate-by="10" hidden>
+<div class="cell-items-pagination" data-paginate-by="{{ paginate_by|default:10 }}" hidden>
   <button class="cell-items-pagination-prev" disabled>←</a>
   <button class="cell-items-pagination-next" disabled>→</a>
 </div>
diff --git a/tests/test_wcs.py b/tests/test_wcs.py
index 14b8f7f9..2e5eeeb1 100644
--- a/tests/test_wcs.py
+++ b/tests/test_wcs.py
@@ -1518,6 +1518,7 @@ def test_cards_cell_render(mock_send, context):
         '<a href="http://127.0.0.1:8999/backoffice/data/card_model_1/13/"><span class="card-title">cc</span></a>'
         in result
     )
+    assert 'data-paginate-by="10"' in result
 
     # create a page with the correct subslug
     page = Page.objects.create(slug='foo', title='Foo', sub_slug='(?P<card_model_1_id>[a-z0-9]+)')
@@ -1529,6 +1530,7 @@ def test_cards_cell_render(mock_send, context):
     assert '<a href="/foo/13"><span class="card-title">cc</span></a>' in result
 
     cell.carddef_reference = 'default:card_model_1:foo'
+    cell.limit = 42
     cell.save()
     page.sub_slug = 'card_model_1_id'
     page.save()
@@ -1538,6 +1540,7 @@ def test_cards_cell_render(mock_send, context):
     assert '<a href="/foo/11"><span class="card-title">aa</span></a>' in result
     assert '<a href="/foo/12"><span class="card-title">bb</span></a>' in result
     assert '<a href="/foo/13"><span class="card-title">cc</span></a>' not in result
+    assert 'data-paginate-by="42"' in result
 
     with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
         mock_json = mock.Mock(status_code=200)
-- 
2.33.0

