From 98262d672b149703b7ffe5447c251c9fcf1422ff Mon Sep 17 00:00:00 2001 From: Lauren Qurashi <46787754+LaurenQurashi@users.noreply.github.com> Date: Fri, 15 Sep 2023 10:41:41 +0100 Subject: [PATCH] TP2000-959 measure list pagination fix (#1026) * Fix pagination and refactor template * Add comment * Increase per page number * Refactor context --- measures/jinja2/measures/list.jinja | 117 +++++++++++++++++++++++++++- measures/views.py | 34 +++++++- 2 files changed, 148 insertions(+), 3 deletions(-) diff --git a/measures/jinja2/measures/list.jinja b/measures/jinja2/measures/list.jinja index 40085e0ef..3c79c71f5 100644 --- a/measures/jinja2/measures/list.jinja +++ b/measures/jinja2/measures/list.jinja @@ -1,4 +1,119 @@ +{% extends "layouts/layout.jinja" %} + +{% from "components/button/macro.njk" import govukButton %} +{% from "components/input/macro.njk" import govukInput %} +{% from "components/table/macro.njk" import govukTable %} + {% set form_url = "measure-ui-list" %} {% set list_include = "includes/measures/list.jinja" %} -{%- include "layouts/list_vertical.jinja" -%} +{% set page_title = "Find and edit " ~ object_list.model._meta.verbose_name_plural %} + + +{% block content %} +

{{ page_title }}

+ + +{% endblock %} diff --git a/measures/views.py b/measures/views.py index 848938a88..71531e6ec 100644 --- a/measures/views.py +++ b/measures/views.py @@ -27,6 +27,7 @@ from common.forms import unprefix_formset_data from common.models import TrackedModel +from common.pagination import build_pagination_list from common.serializers import AutoCompleteSerializer from common.util import TaricDateRange from common.validators import UpdateType @@ -150,10 +151,39 @@ def get_form_kwargs(self): def paginator(self): filterset_class = self.get_filterset_class() self.filterset = self.get_filterset(filterset_class) - return MeasurePaginator(self.filterset.qs, per_page=20) + return MeasurePaginator(self.filterset.qs, per_page=40) def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) + # References to page or pagination in the template were heavily increasing load time. By setting everything we need in the context, + # we can reduce load time + page = self.paginator.get_page(self.request.GET.get("page", 1)) + context = {} + context.update( + { + "filter": kwargs["filter"], + "form": self.get_form(), + "view": self, + "is_paginated": True, + "results_count": self.paginator.count, + "results_limit_breached": self.paginator.limit_breached, + "page_count": self.paginator.num_pages, + "has_other_pages": page.has_other_pages(), + "has_previous_page": page.has_previous(), + "has_next_page": page.has_next(), + "page_number": page.number, + "list_items_count": self.paginator.per_page, + "object_list": page.object_list, + "page_links": build_pagination_list( + page.number, + page.paginator.num_pages, + ), + }, + ) + if context["has_previous_page"]: + context["prev_page_number"] = page.previous_page_number() + if context["has_next_page"]: + context["next_page_number"] = page.next_page_number() + measure_selections = [ SelectableObjectsForm.object_id_from_field_name(name) for name in self.measure_selections