From 76aff8544d899597e52b9095cb3e577c88203c30 Mon Sep 17 00:00:00 2001 From: Dan Avner Date: Thu, 23 Jan 2025 14:16:24 -0800 Subject: [PATCH] Add pagination information and make display configurable via settings. - Add SHOW_PAGINATION_INFO setting to toggle display of pagination info. - Update template and context overriding bootstrap4 templatetag. --- .../bootstrap4_overrides/pagination.html | 42 +++++++++++++++++++ .../templatetags/bootstrap4_overrides.py | 36 ++++++++++++++++ tom_common/tests.py | 20 +++++++++ tom_setup/templates/tom_setup/settings.tmpl | 3 ++ 4 files changed, 101 insertions(+) create mode 100644 tom_common/templates/bootstrap4_overrides/pagination.html create mode 100644 tom_common/templatetags/bootstrap4_overrides.py diff --git a/tom_common/templates/bootstrap4_overrides/pagination.html b/tom_common/templates/bootstrap4_overrides/pagination.html new file mode 100644 index 00000000..7fd7f5df --- /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 %} + Showing {{ start_index }}-{{ end_index }} of {{ total_count }} + {% endif %} +
+{% endwith %} \ No newline at end of file diff --git a/tom_common/templatetags/bootstrap4_overrides.py b/tom_common/templatetags/bootstrap4_overrides.py new file mode 100644 index 00000000..25cb096c --- /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 \ No newline at end of file diff --git a/tom_common/tests.py b/tom_common/tests.py index bf048999..a4322844 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, Page 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..3d57c4bc 100644 --- a/tom_setup/templates/tom_setup/settings.tmpl +++ b/tom_setup/templates/tom_setup/settings.tmpl @@ -344,6 +344,9 @@ REST_FRAMEWORK = { # 'plotly', 'plotly_white', 'plotly_dark', 'ggplot2', 'seaborn', 'simple_white', 'none' PLOTLY_THEME = 'plotly_white' +# Setting for displaying pagination information (e.g., "showing 0-0 of 0"). +SHOW_PAGINATION_INFO = True + try: from local_settings import * # noqa except ImportError: