Skip to content

Commit d8068aa

Browse files
committed
Review I Changes
Signed-off-by: Rishi Garg <rishigarg2503@gmail.com>
1 parent 9bfcca5 commit d8068aa

File tree

6 files changed

+207
-108
lines changed

6 files changed

+207
-108
lines changed

vulnerabilities/forms.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# VulnerableCode is a trademark of nexB Inc.
44
# SPDX-License-Identifier: Apache-2.0
55
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6-
# See https://github.com/aboutcode-org/vulnerablecode for support or download.
6+
# See https://github.com/nexB/vulnerablecode for support or download.
77
# See https://aboutcode.org for more information about nexB OSS projects.
88
#
99

@@ -12,9 +12,42 @@
1212

1313
from vulnerabilities.models import ApiUser
1414

15+
from .models import *
1516

16-
class PackageSearchForm(forms.Form):
1717

18+
class PaginationForm(forms.Form):
19+
"""Form to handle page size selection across the application."""
20+
21+
PAGE_CHOICES = [
22+
("20", "20 per page"),
23+
("50", "50 per page"),
24+
("100", "100 per page"),
25+
]
26+
27+
page_size = forms.ChoiceField(
28+
choices=PAGE_CHOICES,
29+
initial="20",
30+
required=False,
31+
widget=forms.Select(
32+
attrs={
33+
"class": "select is-small",
34+
"onchange": "handlePageSizeChange(this.value)",
35+
"id": "page-size-select",
36+
}
37+
),
38+
)
39+
40+
41+
class BaseSearchForm(forms.Form):
42+
"""Base form for implementing search functionality."""
43+
44+
search = forms.CharField(required=True)
45+
46+
def clean_search(self):
47+
return self.cleaned_data.get("search", "")
48+
49+
50+
class PackageSearchForm(BaseSearchForm):
1851
search = forms.CharField(
1952
required=True,
2053
widget=forms.TextInput(
@@ -23,8 +56,7 @@ class PackageSearchForm(forms.Form):
2356
)
2457

2558

26-
class VulnerabilitySearchForm(forms.Form):
27-
59+
class VulnerabilitySearchForm(BaseSearchForm):
2860
search = forms.CharField(
2961
required=True,
3062
widget=forms.TextInput(
@@ -78,4 +110,4 @@ def clean_username(self):
78110
return username
79111

80112
def save_m2m(self):
81-
pass
113+
pass

vulnerabilities/templates/includes/pagination.html

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
{% if is_paginated %}
22
<nav class="pagination is-centered" role="navigation" aria-label="pagination">
33
{% 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>
4+
<a href="?page={{ page_obj.previous_page_number }}&search={{ search|urlencode }}&page_size={{ page_obj.paginator.per_page }}"
5+
class="pagination-previous">Previous</a>
56
{% else %}
6-
<a class="pagination-previous" disabled>Previous</a>
7+
<span class="pagination-previous" disabled>Previous</span>
78
{% endif %}
89

910
{% 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+
<a href="?page={{ page_obj.next_page_number }}&search={{ search|urlencode }}&page_size={{ page_obj.paginator.per_page }}"
12+
class="pagination-next">Next</a>
1113
{% else %}
12-
<a class="pagination-next" disabled>Next</a>
14+
<span class="pagination-next" disabled>Next</span>
1315
{% endif %}
1416

1517
<ul class="pagination-list">
1618
{% 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>
19+
<li>
20+
<a href="?page=1&search={{ search|urlencode }}&page_size={{ page_obj.paginator.per_page }}"
21+
class="pagination-link" aria-label="Page 1">1</a>
22+
</li>
1823
{% if page_obj.number > 4 %}
1924
<li><span class="pagination-ellipsis">&hellip;</span></li>
2025
{% endif %}
@@ -23,11 +28,14 @@
2328
{% for i in page_obj.paginator.page_range %}
2429
{% if i > 1 and i < page_obj.paginator.num_pages %}
2530
{% 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+
<li>
32+
{% if page_obj.number == i %}
33+
<span class="pagination-link is-current" aria-current="page">{{ i }}</span>
34+
{% else %}
35+
<a href="?page={{ i }}&search={{ search|urlencode }}&page_size={{ page_obj.paginator.per_page }}"
36+
class="pagination-link" aria-label="Goto page {{ i }}">{{ i }}</a>
37+
{% endif %}
38+
</li>
3139
{% endif %}
3240
{% endif %}
3341
{% endfor %}
@@ -36,7 +44,12 @@
3644
{% if page_obj.number < page_obj.paginator.num_pages|add:"-3" %}
3745
<li><span class="pagination-ellipsis">&hellip;</span></li>
3846
{% 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>
47+
<li>
48+
<a href="?page={{ page_obj.paginator.num_pages }}&search={{ search|urlencode }}&page_size={{ page_obj.paginator.per_page }}"
49+
class="pagination-link" aria-label="Goto page {{ page_obj.paginator.num_pages }}">
50+
{{ page_obj.paginator.num_pages }}
51+
</a>
52+
</li>
4053
{% endif %}
4154
</ul>
4255
</nav>

vulnerabilities/templates/packages.html

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% extends "base.html" %}
2+
{% load static %}
23
{% load humanize %}
34
{% load widget_tweaks %}
45

@@ -20,12 +21,7 @@
2021
</div>
2122
<div class="is-flex is-justify-content-center mb-2">
2223
<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>
24+
{{ pagination_form.page_size }}
2925
</div>
3026
</div>
3127
{% if is_paginated %}
@@ -68,35 +64,27 @@
6864
<tr>
6965
<td style="word-break: break-all;">
7066
<a
71-
href="{{ package.get_absolute_url }}?search={{ search }}"
72-
target="_self">{{ package.purl }}</a>
67+
href="{{ package.get_absolute_url }}?search={{ search }}"
68+
target="_self">{{ package.purl }}</a>
7369
</td>
7470
<td>{{ package.vulnerability_count }}</td>
7571
<td>{{ package.patched_vulnerability_count }}</td>
7672
</tr>
7773
{% empty %}
7874
<tr>
7975
<td colspan="3" style="word-break: break-all;">
80-
No Package found.
76+
No Package found.
8177
</td>
8278
</tr>
8379
{% endfor %}
8480
</tbody>
8581
</table>
8682
</div>
8783

88-
{% if is_paginated %}
89-
{% include 'includes/pagination.html' with page_obj=page_obj %}
90-
{% endif %}
91-
84+
{% if is_paginated %}
85+
{% include 'includes/pagination.html' with page_obj=page_obj %}
86+
{% endif %}
9287
</section>
9388
{% endif %}
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>
89+
<script src="{% static 'js/pagination.js' %}"></script>
10290
{% endblock %}

vulnerabilities/templates/vulnerabilities.html

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% extends "base.html" %}
2+
{% load static %}
23
{% load humanize %}
34
{% load widget_tweaks %}
45

@@ -20,17 +21,12 @@
2021
</div>
2122
<div class="is-flex is-justify-content-center mb-2">
2223
<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>
24+
{{ pagination_form.page_size }}
2925
</div>
3026
</div>
3127
{% if is_paginated %}
32-
{% include 'includes/pagination.html' with page_obj=page_obj %}
33-
{% endif %}
28+
{% include 'includes/pagination.html' with page_obj=page_obj %}
29+
{% endif %}
3430
</div>
3531
</section>
3632
</div>
@@ -50,9 +46,9 @@
5046
{% for vulnerability in page_obj %}
5147
<tr class="is-clipped-list">
5248
<td style="word-break: break-all;">
53-
<a
54-
href="{{ vulnerability.get_absolute_url }}?search={{ search }}"
55-
target="_self">{{ vulnerability.vulnerability_id }}
49+
<a
50+
href="{{ vulnerability.get_absolute_url }}?search={{ search }}"
51+
target="_self">{{ vulnerability.vulnerability_id }}
5652
</a>
5753
</td>
5854
<td>
@@ -73,27 +69,18 @@
7369
{% empty %}
7470
<tr class="is-clipped-list">
7571
<td colspan="3" style="word-break: break-all;">
76-
No vulnerability found.
72+
No vulnerability found.
7773
</td>
7874
</tr>
7975
{% endfor %}
8076
</tbody>
8177
</table>
8278
</div>
8379

84-
85-
{% if is_paginated %}
86-
{% include 'includes/pagination.html' with page_obj=page_obj %}
87-
{% endif %}
80+
{% if is_paginated %}
81+
{% include 'includes/pagination.html' with page_obj=page_obj %}
82+
{% endif %}
8883
</section>
8984
{% endif %}
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-
85+
<script src="{% static 'js/pagination.js' %}"></script>
9986
{% endblock %}

0 commit comments

Comments
 (0)