Skip to content

Commit 0dde286

Browse files
committed
Pagination Updated
Signed-off-by: Rishi Garg <rishigarg2503@gmail.com>
1 parent 9910bef commit 0dde286

4 files changed

Lines changed: 108 additions & 43 deletions

File tree

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
1-
<nav class="pagination is-centered is-small" aria-label="pagination">
2-
{% if page_obj.has_previous %}
3-
<a href="?page={{ page_obj.previous_page_number }}&search={{ search|urlencode }}" class="pagination-previous">Previous</a>
4-
{% else %}
5-
<a class="pagination-previous" disabled>Previous</a>
6-
{% endif %}
7-
8-
{% if page_obj.has_next %}
9-
<a href="?page={{ page_obj.next_page_number }}&search={{ search|urlencode }}" class="pagination-next">Next</a>
10-
{% else %}
11-
<a class="pagination-next" disabled>Next</a>
12-
{% endif %}
13-
14-
<ul class="pagination-list">
15-
{% if page_obj.number != 1%}
16-
<li>
17-
<a href="?page=1&search={{ search|urlencode }}" class="pagination-link" aria-label="Goto page 1">1</a>
18-
</li>
19-
{% if page_obj.number > 2 %}
20-
<li>
21-
<span class="pagination-ellipsis">&hellip;</span>
22-
</li>
23-
{% endif %}
1+
{% if is_paginated %}
2+
<nav class="pagination is-centered" role="navigation" aria-label="pagination">
3+
{% if page_obj.has_previous %}
4+
<a href="?page={{ page_obj.previous_page_number }}&search={{ search|urlencode }}&page_size={{ page_size }}" class="pagination-previous">Previous</a>
5+
{% else %}
6+
<a class="pagination-previous" disabled>Previous</a>
247
{% endif %}
25-
<li>
26-
<a class="pagination-link is-current" aria-label="Page {{ page_obj.number }}" aria-current="page">{{ page_obj.number }}</a>
27-
</li>
28-
{% if page_obj.number != page_obj.paginator.num_pages %}
29-
{% if page_obj.next_page_number != page_obj.paginator.num_pages %}
30-
<li>
31-
<span class="pagination-ellipsis">&hellip;</span>
32-
</li>
33-
{% endif %}
34-
<li>
35-
<a href="?page={{ page_obj.paginator.num_pages }}&search={{ search|urlencode }}" class="pagination-link" aria-label="Goto page {{ page_obj.paginator.num_pages }}">{{ page_obj.paginator.num_pages }}</a>
36-
</li>
8+
9+
{% if page_obj.has_next %}
10+
<a href="?page={{ page_obj.next_page_number }}&search={{ search|urlencode }}&page_size={{ page_size }}" class="pagination-next">Next</a>
11+
{% else %}
12+
<a class="pagination-next" disabled>Next</a>
3713
{% endif %}
38-
</ul>
39-
</nav>
14+
15+
<ul class="pagination-list">
16+
{% if page_obj.number > 1 %}
17+
<li><a href="?page=1&search={{ search|urlencode }}&page_size={{ page_size }}" class="pagination-link" aria-label="Goto page 1">1</a></li>
18+
{% if page_obj.number > 4 %}
19+
<li><span class="pagination-ellipsis">&hellip;</span></li>
20+
{% endif %}
21+
{% endif %}
22+
23+
{% for i in page_obj.paginator.page_range %}
24+
{% if i > 1 and i < page_obj.paginator.num_pages %}
25+
{% if i >= page_obj.number|add:"-3" and i <= page_obj.number|add:"3" %}
26+
{% if page_obj.number == i %}
27+
<li><a class="pagination-link is-current" aria-label="Page {{ i }}" aria-current="page">{{ i }}</a></li>
28+
{% else %}
29+
<li><a href="?page={{ i }}&search={{ search|urlencode }}&page_size={{ page_size }}" class="pagination-link" aria-label="Goto page {{ i }}">{{ i }}</a></li>
30+
{% endif %}
31+
{% endif %}
32+
{% endif %}
33+
{% endfor %}
34+
35+
{% if page_obj.number < page_obj.paginator.num_pages %}
36+
{% if page_obj.number < page_obj.paginator.num_pages|add:"-3" %}
37+
<li><span class="pagination-ellipsis">&hellip;</span></li>
38+
{% endif %}
39+
<li><a href="?page={{ page_obj.paginator.num_pages }}&search={{ search|urlencode }}&page_size={{ page_size }}" class="pagination-link" aria-label="Goto page {{ page_obj.paginator.num_pages }}">{{ page_obj.paginator.num_pages }}</a></li>
40+
{% endif %}
41+
</ul>
42+
</nav>
43+
{% endif %}

vulnerabilities/templates/packages.html

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818
<div>
1919
{{ page_obj.paginator.count|intcomma }} results
2020
</div>
21+
<div class="is-flex is-justify-content-center mb-2">
22+
<div class="select is-small">
23+
<select id="itemsPerPage" onchange="changeItemsPerPage(this.value)">
24+
<option value="20" {% if page_obj.paginator.per_page == 20 %}selected{% endif %}>20 per page</option>
25+
<option value="50" {% if page_obj.paginator.per_page == 50 %}selected{% endif %}>50 per page</option>
26+
<option value="100" {% if page_obj.paginator.per_page == 100 %}selected{% endif %}>100 per page</option>
27+
<option value="200" {% if page_obj.paginator.per_page == 200 %}selected{% endif %}>200 per page</option>
28+
</select>
29+
</div>
30+
</div>
2131
{% if is_paginated %}
2232
{% include 'includes/pagination.html' with page_obj=page_obj %}
2333
{% endif %}
@@ -81,4 +91,12 @@
8191

8292
</section>
8393
{% endif %}
84-
{% endblock %}
94+
<script>
95+
function changeItemsPerPage(pageSize) {
96+
var urlParams = new URLSearchParams(window.location.search);
97+
urlParams.set('page_size', pageSize);
98+
window.location.search = urlParams.toString();
99+
}
100+
101+
</script>
102+
{% endblock %}

vulnerabilities/templates/vulnerabilities.html

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,17 @@
1818
<div>
1919
{{ page_obj.paginator.count|intcomma }} results
2020
</div>
21-
{% if is_paginated %}
21+
<div class="is-flex is-justify-content-center mb-2">
22+
<div class="select is-small">
23+
<select id="itemsPerPage" onchange="changeItemsPerPage(this.value)">
24+
<option value="20" {% if page_obj.paginator.per_page == 20 %}selected{% endif %}>20 per page</option>
25+
<option value="50" {% if page_obj.paginator.per_page == 50 %}selected{% endif %}>50 per page</option>
26+
<option value="100" {% if page_obj.paginator.per_page == 100 %}selected{% endif %}>100 per page</option>
27+
<option value="200" {% if page_obj.paginator.per_page == 200 %}selected{% endif %}>200 per page</option>
28+
</select>
29+
</div>
30+
</div>
31+
{% if is_paginated %}
2232
{% include 'includes/pagination.html' with page_obj=page_obj %}
2333
{% endif %}
2434
</div>
@@ -77,5 +87,13 @@
7787
{% endif %}
7888
</section>
7989
{% endif %}
80-
81-
{% endblock %}
90+
<script>
91+
function changeItemsPerPage(pageSize) {
92+
var urlParams = new URLSearchParams(window.location.search);
93+
urlParams.set('page_size', pageSize);
94+
window.location.search = urlParams.toString();
95+
}
96+
97+
</script>
98+
99+
{% endblock %}

vulnerabilities/views.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
from django.contrib import messages
1616
from django.core.exceptions import ValidationError
1717
from django.core.mail import send_mail
18+
from django.core.paginator import EmptyPage
19+
from django.core.paginator import PageNotAnInteger
20+
from django.core.paginator import Paginator
1821
from django.http.response import Http404
1922
from django.shortcuts import redirect
2023
from django.shortcuts import render
@@ -68,11 +71,16 @@ class PackageSearch(ListView):
6871
ordering = ["type", "namespace", "name", "version"]
6972
paginate_by = PAGE_SIZE
7073

74+
def get_paginate_by(self, queryset):
75+
page_size = self.request.GET.get("page_size", "")
76+
return int(page_size) if page_size.isdigit() else self.paginate_by
77+
7178
def get_context_data(self, **kwargs):
7279
context = super().get_context_data(**kwargs)
7380
request_query = self.request.GET
7481
context["package_search_form"] = PackageSearchForm(request_query)
7582
context["search"] = request_query.get("search")
83+
context["page_size"] = self.get_paginate_by(self.get_queryset())
7684
return context
7785

7886
def get_queryset(self, query=None):
@@ -96,17 +104,34 @@ class VulnerabilitySearch(ListView):
96104
ordering = ["vulnerability_id"]
97105
paginate_by = PAGE_SIZE
98106

107+
def get_paginate_by(self, queryset):
108+
page_size = self.request.GET.get("page_size", "")
109+
return int(page_size) if page_size.isdigit() else self.paginate_by
110+
99111
def get_context_data(self, **kwargs):
100112
context = super().get_context_data(**kwargs)
101113
request_query = self.request.GET
102114
context["vulnerability_search_form"] = VulnerabilitySearchForm(request_query)
103115
context["search"] = request_query.get("search")
116+
context["page_size"] = self.get_paginate_by(self.get_queryset())
104117
return context
105118

106-
def get_queryset(self, query=None):
107-
query = query or self.request.GET.get("search") or ""
119+
def get_queryset(self):
120+
query = self.request.GET.get("search") or ""
108121
return self.model.objects.search(query=query).with_package_counts()
109122

123+
def paginate_queryset(self, queryset, page_size):
124+
paginator = Paginator(queryset, page_size)
125+
page = self.request.GET.get("page", "1")
126+
try:
127+
page_number = int(page)
128+
page_obj = paginator.page(page_number)
129+
except (ValueError, PageNotAnInteger):
130+
page_obj = paginator.page(1)
131+
except EmptyPage:
132+
page_obj = paginator.page(paginator.num_pages)
133+
return (paginator, page_obj, page_obj.object_list, page_obj.has_other_pages())
134+
110135

111136
class PackageDetails(DetailView):
112137
model = models.Package

0 commit comments

Comments
 (0)