Skip to content

Commit 08edd2a

Browse files
authored
Merge pull request #1551 from nationalarchives/FCL-242/fix-pagination
FCL-242 | fix pagination
2 parents ef9961c + 7d4b7c3 commit 08edd2a

File tree

5 files changed

+78
-21
lines changed

5 files changed

+78
-21
lines changed

.github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ jobs:
6565
target: ".env"
6666

6767
- name: Build the Stack
68-
run: docker-compose build django postgres
68+
run: docker compose build django postgres
6969

7070
- name: Run mypy
71-
run: docker-compose run django mypy ds_caselaw_editor_ui judgments
71+
run: docker compose run django mypy ds_caselaw_editor_ui judgments
7272

7373
# With no caching at all the entire ci process takes 4m 30s to complete!
7474
pytest:

ds_caselaw_editor_ui/sass/includes/_pagination.scss

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
}
2626
}
2727

28+
&-divider {
29+
display: inline-block;
30+
color: $color-white;
31+
width: 2rem;
32+
}
33+
2834
&-item {
2935
display: inline-block;
3036
min-height: 3.125rem;

ds_caselaw_editor_ui/templates/includes/pagination.html

+33-12
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,44 @@ <h3>Unpublished documents pagination</h3>
1515
</li>
1616
<li class="pagination__list-pages">
1717
<ol>
18-
<li class="pagination__list-item">
19-
<a class="pagination__page-link-current"
20-
href="{{ request.path }}?{% if query_string %}{{ query_string }}&{% endif %}page={{ paginator.current_page }}"
21-
aria-label="Current page, Page {{ paginator.current_page }}"
22-
aria-current="true">
23-
<span>Current page</span>{{ paginator.current_page }}
24-
</a>
25-
</li>
26-
{% for page in paginator.next_pages %}
18+
{% if paginator.show_first_page %}
2719
<li class="pagination__list-item">
2820
<a class="pagination__page-link"
29-
href="{{ request.path }}?{% if query_string %}{{ query_string }}&{% endif %}page={{ page }}"
30-
aria-label="Go to page {{ page }}">
31-
<span>Page</span>{{ page }}
21+
href="{{ request.path }}?{% if query_string %}{{ query_string }}&{% endif %}page=1"
22+
aria-label="Go to page 1">
23+
<span>Page</span>1
3224
</a>
3325
</li>
26+
{% endif %}
27+
{% if paginator.show_first_page_divider %}<li class="pagination__list-divider"></li>{% endif %}
28+
{% for page in paginator.page_range %}
29+
<li class="pagination__list-item">
30+
{% if page == paginator.current_page %}
31+
<a class="pagination__page-link-current"
32+
href="{{ request.path }}?{% if query_string %}{{ query_string }}&{% endif %}page={{ page }}"
33+
aria-label="Current page, Page {{ page }}"
34+
aria-current="true">
35+
<span>Current page</span>{{ page }}
36+
</a>
37+
{% else %}
38+
<a class="pagination__page-link"
39+
href="{{ request.path }}?{% if query_string %}{{ query_string }}&{% endif %}page={{ page }}"
40+
aria-label="Go to page {{ page }}">
41+
<span>Page</span>{{ page }}
42+
</a>
43+
{% endif %}
44+
</li>
3445
{% endfor %}
46+
{% if paginator.show_last_page_divider %}<li class="pagination__list-divider"></li>{% endif %}
47+
{% if paginator.show_last_page %}
48+
<li class="pagination__list-item">
49+
<a class="pagination__page-link"
50+
href="{{ request.path }}?{% if query_string %}{{ query_string }}&{% endif %}page={{ paginator.number_of_pages }}"
51+
aria-label="Go to page {{ paginator.number_of_pages }}">
52+
<span>Page</span>{{ paginator.number_of_pages }}
53+
</a>
54+
</li>
55+
{% endif %}
3556
</ol>
3657
</li>
3758
<li class="pagination__list-item">

judgments/tests/utils/test_utils.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -50,37 +50,49 @@ def test_invalidation(self, mockboto):
5050
class TestPaginator:
5151
def test_paginator_2500(self):
5252
expected_result = {
53+
"show_first_page": True,
54+
"show_first_page_divider": True,
55+
"show_last_page": True,
56+
"show_last_page_divider": True,
5357
"current_page": 10,
5458
"has_next_page": True,
5559
"has_prev_page": True,
5660
"next_page": 11,
5761
"prev_page": 9,
58-
"next_pages": [11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
62+
"page_range": [8, 9, 10, 11, 12],
5963
"number_of_pages": 250,
6064
}
6165
assert paginator(10, 2500) == expected_result
6266

6367
def test_paginator_25(self):
6468
# 25 items has 5 items on page 3.
6569
expected_result = {
70+
"show_first_page": False,
71+
"show_first_page_divider": False,
72+
"show_last_page": False,
73+
"show_last_page_divider": False,
6674
"current_page": 1,
6775
"has_next_page": True,
6876
"has_prev_page": False,
6977
"next_page": 2,
7078
"prev_page": 0,
71-
"next_pages": [2, 3],
79+
"page_range": [1, 2, 3],
7280
"number_of_pages": 3,
7381
}
7482
assert paginator(1, 25) == expected_result
7583

7684
def test_paginator_5(self):
7785
expected_result = {
86+
"show_first_page": False,
87+
"show_first_page_divider": False,
88+
"show_last_page": False,
89+
"show_last_page_divider": False,
7890
"current_page": 1,
7991
"has_next_page": False,
8092
"has_prev_page": False,
8193
"next_page": 2, # Note: remember to check has_next_page
8294
"prev_page": 0,
83-
"next_pages": [],
95+
"page_range": [1],
8496
"number_of_pages": 1,
8597
}
8698
assert paginator(1, 5) == expected_result

judgments/utils/paginator.py

+22-4
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,36 @@
44

55

66
def paginator(current_page, total):
7+
number_of_pagination_links = 5
78
size_per_page = RESULTS_PER_PAGE
89
number_of_pages = math.ceil(int(total) / size_per_page)
9-
next_pages = list(
10-
range(current_page + 1, min(current_page + 10, number_of_pages) + 1),
11-
)
10+
11+
half_range = number_of_pagination_links // 2
12+
13+
if number_of_pages <= number_of_pagination_links:
14+
page_range = list(range(1, number_of_pages + 1))
15+
elif current_page - half_range < 1:
16+
page_range = list(range(1, number_of_pagination_links + 1))
17+
elif current_page + half_range > number_of_pages:
18+
page_range = list(range(number_of_pages - number_of_pagination_links + 1, number_of_pages + 1))
19+
else:
20+
page_range = list(range(current_page - half_range, current_page + half_range + 1))
21+
22+
show_first_page = 1 not in page_range
23+
show_first_page_divider = number_of_pages > number_of_pagination_links and 2 not in page_range
24+
show_last_page = number_of_pages not in page_range
25+
show_last_page_divider = number_of_pages > number_of_pagination_links and number_of_pages - 1 not in page_range
1226

1327
return {
28+
"show_first_page": show_first_page,
29+
"show_first_page_divider": show_first_page_divider,
30+
"show_last_page": show_last_page,
31+
"show_last_page_divider": show_last_page_divider,
1432
"current_page": current_page,
1533
"has_next_page": current_page < number_of_pages,
1634
"next_page": current_page + 1,
1735
"has_prev_page": current_page > 1,
1836
"prev_page": current_page - 1,
19-
"next_pages": next_pages,
2037
"number_of_pages": number_of_pages,
38+
"page_range": page_range,
2139
}

0 commit comments

Comments
 (0)