diff mbox series

[layerindex-web] Re-implement pagination using built-in Django's Paginator.

Message ID 20260131143343.23359-1-piotr@qbee.io
State New
Headers show
Series [layerindex-web] Re-implement pagination using built-in Django's Paginator. | expand

Commit Message

Piotr Buliński Jan. 31, 2026, 2:33 p.m. UTC
Signed-off-by: Piotr Buliński <piotr@qbee.io>
---
 layerindex/templatetags/paginator.py       | 28 ++++++++++++++++++++++
 templates/layerindex/bulkchangesearch.html |  7 +-----
 templates/layerindex/classes.html          |  7 +-----
 templates/layerindex/classicrecipes.html   |  7 +-----
 templates/layerindex/distros.html          |  7 +-----
 templates/layerindex/history.html          |  7 +-----
 templates/layerindex/layers.html           |  7 +-----
 templates/layerindex/machines.html         |  7 +-----
 templates/layerindex/paginator.html        | 20 ++++++++++++++++
 templates/layerindex/recipes.html          |  7 +-----
 templates/layerindex/reviewlist.html       |  7 +-----
 templates/layerindex/updatelist.html       |  7 +-----
 12 files changed, 58 insertions(+), 60 deletions(-)
 create mode 100644 layerindex/templatetags/paginator.py
 create mode 100644 templates/layerindex/paginator.html
diff mbox series

Patch

diff --git a/layerindex/templatetags/paginator.py b/layerindex/templatetags/paginator.py
new file mode 100644
index 0000000..60ada6e
--- /dev/null
+++ b/layerindex/templatetags/paginator.py
@@ -0,0 +1,28 @@ 
+from django import template
+from django.http import HttpRequest
+from django.core.paginator import Paginator
+
+register = template.Library()
+
+
+# Return new page URL for the current view.
+# Usage: {% page_url request 2 %}
+@register.simple_tag
+def page_url(request: "HttpRequest", page: int) -> "str":
+    new_query = request.GET.copy()
+    new_query['page'] = f'{page}'
+    return '?' + new_query.urlencode()
+
+
+# Return elided page range for the current page object.
+# Usage: {% page_obj|elided_page_range  %}
+@register.filter
+def elided_page_range(page_obj: "Paginator.page") -> "list[int]":
+    if not isinstance(page_obj.paginator, Paginator):
+        return []
+
+    return page_obj.paginator.get_elided_page_range(
+        number=page_obj.number,
+        on_each_side=3,
+        on_ends=1,
+    )
\ No newline at end of file
diff --git a/templates/layerindex/bulkchangesearch.html b/templates/layerindex/bulkchangesearch.html
index e8d6bd8..fad7975 100644
--- a/templates/layerindex/bulkchangesearch.html
+++ b/templates/layerindex/bulkchangesearch.html
@@ -90,12 +90,7 @@ 
                     <input type="submit" class="btn btn-default" name="add_all" value="Add all"></input>
 
     {% if is_paginated %}
-    {% comment %}
-        {% load bootstrap_pagination %}
-        <div class="text-center">
-        {% bootstrap_paginate page_obj range=10 show_prev_next="false" show_first_last="true" %}
-        </div>
-    {% endcomment %}
+    {% include "layerindex/paginator.html" %}
     {% endif %}
 {% else %}
     {% if searched %}
diff --git a/templates/layerindex/classes.html b/templates/layerindex/classes.html
index e99761b..0ba93ad 100644
--- a/templates/layerindex/classes.html
+++ b/templates/layerindex/classes.html
@@ -65,12 +65,7 @@ 
                 </table>
 
     {% if is_paginated %}
-    {% comment %}
-        {% load bootstrap_pagination %}
-        <div class="text-center">
-        {% bootstrap_paginate page_obj range=10 show_prev_next="false" show_first_last="true" %}
-        </div>
-    {% endcomment %}
+    {% include "layerindex/paginator.html" %}
     {% endif %}
 {% else %}
     {% if search_keyword %}
diff --git a/templates/layerindex/classicrecipes.html b/templates/layerindex/classicrecipes.html
index cf911a5..c4fe13c 100644
--- a/templates/layerindex/classicrecipes.html
+++ b/templates/layerindex/classicrecipes.html
@@ -287,12 +287,7 @@ 
                 </table>
 
     {% if is_paginated %}
-    {% comment %}
-        {% load bootstrap_pagination %}
-        <div class="text-center">
-        {% bootstrap_paginate page_obj range=10 show_prev_next="false" show_first_last="true" %}
-        </div>
-    {% endcomment %}
+    {% include "layerindex/paginator.html" %}
     {% endif %}
 {% else %}
     {% if searched %}
diff --git a/templates/layerindex/distros.html b/templates/layerindex/distros.html
index 0c1b276..4cacd84 100644
--- a/templates/layerindex/distros.html
+++ b/templates/layerindex/distros.html
@@ -65,12 +65,7 @@ 
                 </table>
 
     {% if is_paginated %}
-    {% comment %}
-        {% load bootstrap_pagination %}
-        <div class="text-center">
-        {% bootstrap_paginate page_obj range=10 show_prev_next="false" show_first_last="true" %}
-        </div>
-    {% endcomment %}
+    {% include "layerindex/paginator.html" %}
     {% endif %}
 {% else %}
     {% if search_keyword %}
diff --git a/templates/layerindex/history.html b/templates/layerindex/history.html
index 9ae4431..b4a1f62 100644
--- a/templates/layerindex/history.html
+++ b/templates/layerindex/history.html
@@ -60,12 +60,7 @@ 
 </table>
 
     {% if is_paginated %}
-    {% comment %}
-        {% load bootstrap_pagination %}
-        <div class="text-center">
-        {% bootstrap_paginate page_obj range=10 show_prev_next="false" show_first_last="true" %}
-        </div>
-    {% endcomment %}
+    {% include "layerindex/paginator.html" %}
     {% endif %}
 
 
diff --git a/templates/layerindex/layers.html b/templates/layerindex/layers.html
index ae9befb..daae8cb 100644
--- a/templates/layerindex/layers.html
+++ b/templates/layerindex/layers.html
@@ -105,12 +105,7 @@ 
             </table>
 
     {% if is_paginated %}
-    {% comment %}
-        {% load bootstrap_pagination %}
-        <div class="text-center">
-        {% bootstrap_paginate page_obj range=10 show_prev_next="false" show_first_last="true" %}
-        </div>
-    {% endcomment %}
+    {% include "layerindex/paginator.html" %}
     {% endif %}
 {% else %}
     <p>No matching layers in database.</p>
diff --git a/templates/layerindex/machines.html b/templates/layerindex/machines.html
index 685506d..3b4971c 100644
--- a/templates/layerindex/machines.html
+++ b/templates/layerindex/machines.html
@@ -64,12 +64,7 @@ 
                 </table>
 
     {% if is_paginated %}
-    {% comment %}
-        {% load bootstrap_pagination %}
-        <div class="text-center">
-        {% bootstrap_paginate page_obj range=10 show_prev_next="false" show_first_last="true" %}
-        </div>
-    {% endcomment %}
+    {% include "layerindex/paginator.html" %}
     {% endif %}
 {% else %}
     {% if search_keyword %}
diff --git a/templates/layerindex/paginator.html b/templates/layerindex/paginator.html
new file mode 100644
index 0000000..99abff9
--- /dev/null
+++ b/templates/layerindex/paginator.html
@@ -0,0 +1,20 @@ 
+{% load paginator %}
+<nav aria-label="Pagination Navigation" class="text-center">
+    <ul class="pagination">
+        {% for i in page_obj|elided_page_range %}
+        {% if page_obj.number == i %}
+            <li class="page-item active">
+            <span aria-current="page">{{ i }}</span>
+            </li>
+        {% else %}
+            {% if i == page_obj.paginator.ELLIPSIS %}
+            <li class="pagination-ellipsis" aria-hidden="true"><span>{{ i }}</span></li>
+            {% else %}
+            <li class="page-item">
+                <a href="{% page_url request i %}" aria-label="Go to page {{ i }}">{{ i }}</a>
+            </li>
+            {% endif %}
+        {% endif %}
+        {% endfor %}
+    </ul>
+</nav>
\ No newline at end of file
diff --git a/templates/layerindex/recipes.html b/templates/layerindex/recipes.html
index 9c15e6c..cea7e2a 100644
--- a/templates/layerindex/recipes.html
+++ b/templates/layerindex/recipes.html
@@ -90,12 +90,7 @@ 
                 </table>
 
     {% if is_paginated %}
-    {% comment %}
-        {% load bootstrap_pagination %}
-        <div class="text-center">
-        {% bootstrap_paginate page_obj range=10 show_prev_next="false" show_first_last="true" %}
-        </div>
-    {% endcomment %}
+    {% include "layerindex/paginator.html" %}
     {% endif %}
 {% else %}
     {% if search_keyword %}
diff --git a/templates/layerindex/reviewlist.html b/templates/layerindex/reviewlist.html
index 18e0d78..c431621 100644
--- a/templates/layerindex/reviewlist.html
+++ b/templates/layerindex/reviewlist.html
@@ -69,12 +69,7 @@ 
     </div>
 
     {% if is_paginated %}
-    {% comment %}
-        {% load bootstrap_pagination %}
-        <div class="text-center">
-        {% bootstrap_paginate page_obj range=10 show_prev_next="false" show_first_last="true" %}
-        </div>
-    {% endcomment %}
+    {% include "layerindex/paginator.html" %}
     {% endif %}
 {% else %}
     <p>No unpublished layers to review.</p>
diff --git a/templates/layerindex/updatelist.html b/templates/layerindex/updatelist.html
index b68a860..55091a9 100644
--- a/templates/layerindex/updatelist.html
+++ b/templates/layerindex/updatelist.html
@@ -54,12 +54,7 @@ 
 </div>
 
 {% if is_paginated %}
-{% comment %}
-    {% load bootstrap_pagination %}
-    <div class="text-center">
-    {% bootstrap_paginate page_obj range=10 show_prev_next="false" show_first_last="true" %}
-    </div>
-{% endcomment %}
+{% include "layerindex/paginator.html" %}
 {% endif %}
 
 {% endautoescape %}