diff --git a/tom_common/templates/bootstrap4_overrides/pagination.html b/tom_common/templates/bootstrap4_overrides/pagination.html new file mode 100644 index 00000000..6874f5a3 --- /dev/null +++ b/tom_common/templates/bootstrap4_overrides/pagination.html @@ -0,0 +1,42 @@ +{% load bootstrap4 %} +{% with bpurl=bootstrap_pagination_url|default:"" %} +
+ + {% if show_pagination_info %} + ( {{ start_index }}-{{ end_index }} of {{ total_count }} ) + {% endif %} +
+{% endwith %} diff --git a/tom_common/templatetags/bootstrap4_overrides.py b/tom_common/templatetags/bootstrap4_overrides.py new file mode 100644 index 00000000..86d0f2f9 --- /dev/null +++ b/tom_common/templatetags/bootstrap4_overrides.py @@ -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 diff --git a/tom_common/tests.py b/tom_common/tests.py index bf048999..1bc8dc29 100644 --- a/tom_common/tests.py +++ b/tom_common/tests.py @@ -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): @@ -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 diff --git a/tom_setup/templates/tom_setup/settings.tmpl b/tom_setup/templates/tom_setup/settings.tmpl index 5c4b4464..f24c27d6 100644 --- a/tom_setup/templates/tom_setup/settings.tmpl +++ b/tom_setup/templates/tom_setup/settings.tmpl @@ -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: