Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pagination information and make display configurable via settings. #1159

Merged
merged 6 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions tom_common/templates/bootstrap4_overrides/pagination.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{% load bootstrap4 %}
{% with bpurl=bootstrap_pagination_url|default:"" %}
<div class="d-flex align-items-baseline">
<ul class="{{ pagination_css_classes }}">
<li class="prev page-item{% if current_page == 1 %} disabled{% endif %}">
<a class="page-link" href="{% if current_page == 1 %}#{% else %}{% bootstrap_url_replace_param bpurl parameter_name 1 %}{% endif %}">
&laquo;
</a>
</li>

{% if pages_back %}
<li class="page-item">
<a class="page-link" href="{% bootstrap_url_replace_param bpurl parameter_name pages_back %}">&hellip;</a>
</li>
{% endif %}

{% for p in pages_shown %}
<li class="page-item{% if current_page == p %} active{% endif %}">
<a class="page-link" href="{% if current_page == p %}#{% else %}{% bootstrap_url_replace_param bpurl parameter_name p %}{% endif %}">
{{ p }}
</a>
</li>
{% endfor %}

{% if pages_forward %}
<li class="page-item">
<a class="page-link" href="{% bootstrap_url_replace_param bpurl parameter_name pages_forward %}">&hellip;</a>
</li>
{% endif %}

<li class="last page-item{% if current_page == num_pages %} disabled{% endif %}">
<a class="page-link" href="{% if current_page == num_pages %}#{% else %}{% bootstrap_url_replace_param bpurl parameter_name num_pages %}{% endif %}">
&raquo;
</a>
</li>

</ul>
{% if show_pagination_info %}
<span class="ml-2 small"> ( {{ start_index }}-{{ end_index }} of {{ total_count }} )</span>
{% endif %}
</div>
{% endwith %}
36 changes: 36 additions & 0 deletions tom_common/templatetags/bootstrap4_overrides.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from typing import Any

from bootstrap4.templatetags.bootstrap4 import get_pagination_context, register
from django.conf import settings
from django.core.paginator import Page


@register.inclusion_tag("bootstrap4_overrides/pagination.html")
def bootstrap_pagination(page: Page, **kwargs) -> dict[str, Any]:
"""Render a Bootstrap pagination component with additional context. This function
extends the default pagination context to include the start index, end index, and
total count of items.

Parameters
----------
page : `Page`
The page of results to show.

Returns
-------
`dict`
The context for rendering the pagination component, including
pagination details and item counts.
"""
pagination_kwargs = kwargs.copy()
pagination_kwargs["page"] = page

# Get the default pagination context.
context = get_pagination_context(**pagination_kwargs)

# Add item counts to the context.
context["start_index"] = page.start_index()
context["end_index"] = page.end_index()
context["total_count"] = page.paginator.count
context["show_pagination_info"] = getattr(settings, "SHOW_PAGINATION_INFO", True)
return context
20 changes: 20 additions & 0 deletions tom_common/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
from django.contrib.auth.models import User
from django.urls import reverse
from django_comments.models import Comment
from django.core.paginator import Paginator

from tom_targets.tests.factories import SiderealTargetFactory
from tom_common.templatetags.tom_common_extras import verbose_name, multiplyby, truncate_value_for_display
from tom_common.templatetags.bootstrap4_overrides import bootstrap_pagination


class TestCommonViews(TestCase):
Expand All @@ -27,6 +29,24 @@ def test_index(self):
self.assertEqual(response.status_code, 200)


class TestBootstrap4Overrides(TestCase):
def setUp(self):
# Set up a dataset for pagination.
self.items = list(range(1, 101))
self.paginator = Paginator(self.items, 10)

def test_bootstrap_pagination(self):
# Get the first page.
page = self.paginator.page(1)
context = bootstrap_pagination(page)

# Assert the context contains the correct data.
self.assertEqual(context["start_index"], 1)
self.assertEqual(context["end_index"], 10)
self.assertEqual(context["total_count"], 100)
self.assertEqual(context["show_pagination_info"], True)


class TestCommonExtras(TestCase):
def setUp(self):
pass
Expand Down
4 changes: 4 additions & 0 deletions tom_setup/templates/tom_setup/settings.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ REST_FRAMEWORK = {
# 'plotly', 'plotly_white', 'plotly_dark', 'ggplot2', 'seaborn', 'simple_white', 'none'
PLOTLY_THEME = 'plotly_white'

# Setting for displaying pagination information (e.g., "(0-0 of 0)").
# Set this to False if you have a particularly large DB and paginated views are slow.
SHOW_PAGINATION_INFO = True

try:
from local_settings import * # noqa
except ImportError:
Expand Down