Re-implement pagination using built-in Django's Paginator.
PATCH 1/1 Re-implement pagination using built-in Django's Paginator.
Signed-off-by Piotr Buliński <[email protected]>
--- 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
layerindex/templatetags/paginator.py new file +28 -0
@@ -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 %} [email protected]_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 %} [email protected] +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, + )
templates/layerindex/bulkchangesearch.html +1 -6
@@ -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 %}
templates/layerindex/classes.html +1 -6
@@ -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 %}
templates/layerindex/classicrecipes.html +1 -6
@@ -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 %}
templates/layerindex/distros.html +1 -6
@@ -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 %}
templates/layerindex/history.html +1 -6
@@ -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 %}
templates/layerindex/layers.html +1 -6
@@ -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>
templates/layerindex/machines.html +1 -6
@@ -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 %}
templates/layerindex/paginator.html new file +20 -0
@@ -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>
templates/layerindex/recipes.html +1 -6
@@ -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 %}
templates/layerindex/reviewlist.html +1 -6
@@ -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>
templates/layerindex/updatelist.html +1 -6
@@ -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 %}