Skip to content

Commit 6ca25ee

Browse files
committed
Minor Changes to Get Workflow tests corrected
Signed-off-by: Rishi Garg <rishigarg2503@gmail.com>
1 parent 38b97ff commit 6ca25ee

File tree

6 files changed

+251
-92
lines changed

6 files changed

+251
-92
lines changed

vulnerabilities/forms.py

Lines changed: 64 additions & 3 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,26 +12,87 @@
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+
def get_queryset(self, query=None):
50+
"""
51+
Get queryset with search/filter/ordering applied.
52+
Args:
53+
query (str, optional): Direct query for testing
54+
"""
55+
if query is not None:
56+
return self._search(query)
57+
58+
if not self.is_valid():
59+
return self.model.objects.none()
60+
61+
return self._search(self.clean_search())
62+
63+
64+
class PackageSearchForm(BaseSearchForm):
65+
model = Package
1866
search = forms.CharField(
1967
required=True,
2068
widget=forms.TextInput(
2169
attrs={"placeholder": "Package name, purl or purl fragment"},
2270
),
2371
)
2472

73+
def _search(self, query):
74+
"""Execute package-specific search logic."""
75+
return (
76+
self.model.objects.search(query)
77+
.with_vulnerability_counts()
78+
.prefetch_related()
79+
.order_by("package_url")
80+
)
2581

26-
class VulnerabilitySearchForm(forms.Form):
2782

83+
class VulnerabilitySearchForm(BaseSearchForm):
84+
model = Vulnerability
2885
search = forms.CharField(
2986
required=True,
3087
widget=forms.TextInput(
3188
attrs={"placeholder": "Vulnerability id or alias such as CVE or GHSA"}
3289
),
3390
)
3491

92+
def _search(self, query):
93+
"""Execute vulnerability-specific search logic."""
94+
return self.model.objects.search(query=query).with_package_counts()
95+
3596

3697
class ApiUserCreationForm(forms.ModelForm):
3798
"""
Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,56 @@
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_obj.paginator.per_page }}"
5+
class="pagination-previous">Previous</a>
6+
{% else %}
7+
<span class="pagination-previous" disabled>Previous</span>
248
{% 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>
9+
10+
{% if page_obj.has_next %}
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>
13+
{% else %}
14+
<span class="pagination-next" disabled>Next</span>
3715
{% endif %}
38-
</ul>
39-
</nav>
16+
17+
<ul class="pagination-list">
18+
{% if page_obj.number > 1 %}
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>
23+
{% if page_obj.number > 4 %}
24+
<li><span class="pagination-ellipsis">&hellip;</span></li>
25+
{% endif %}
26+
{% endif %}
27+
28+
{% for i in page_obj.paginator.page_range %}
29+
{% if i > 1 and i < page_obj.paginator.num_pages %}
30+
{% if i >= page_obj.number|add:"-3" and i <= page_obj.number|add:"3" %}
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>
39+
{% endif %}
40+
{% endif %}
41+
{% endfor %}
42+
43+
{% if page_obj.number < page_obj.paginator.num_pages %}
44+
{% if page_obj.number < page_obj.paginator.num_pages|add:"-3" %}
45+
<li><span class="pagination-ellipsis">&hellip;</span></li>
46+
{% endif %}
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>
53+
{% endif %}
54+
</ul>
55+
</nav>
56+
{% endif %}

vulnerabilities/templates/packages.html

Lines changed: 14 additions & 8 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

@@ -18,6 +19,11 @@
1819
<div>
1920
{{ page_obj.paginator.count|intcomma }} results
2021
</div>
22+
<div class="is-flex is-justify-content-center mb-2">
23+
<div class="select is-small">
24+
{{ pagination_form.page_size }}
25+
</div>
26+
</div>
2127
{% if is_paginated %}
2228
{% include 'includes/pagination.html' with page_obj=page_obj %}
2329
{% endif %}
@@ -58,27 +64,27 @@
5864
<tr>
5965
<td style="word-break: break-all;">
6066
<a
61-
href="{{ package.get_absolute_url }}?search={{ search }}"
62-
target="_self">{{ package.purl }}</a>
67+
href="{{ package.get_absolute_url }}?search={{ search }}"
68+
target="_self">{{ package.purl }}</a>
6369
</td>
6470
<td>{{ package.vulnerability_count }}</td>
6571
<td>{{ package.patched_vulnerability_count }}</td>
6672
</tr>
6773
{% empty %}
6874
<tr>
6975
<td colspan="3" style="word-break: break-all;">
70-
No Package found.
76+
No Package found.
7177
</td>
7278
</tr>
7379
{% endfor %}
7480
</tbody>
7581
</table>
7682
</div>
7783

78-
{% if is_paginated %}
79-
{% include 'includes/pagination.html' with page_obj=page_obj %}
80-
{% endif %}
81-
84+
{% if is_paginated %}
85+
{% include 'includes/pagination.html' with page_obj=page_obj %}
86+
{% endif %}
8287
</section>
8388
{% endif %}
84-
{% endblock %}
89+
<script src="{% static 'js/pagination.js' %}"></script>
90+
{% endblock %}

vulnerabilities/templates/vulnerabilities.html

Lines changed: 18 additions & 13 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

@@ -18,9 +19,14 @@
1819
<div>
1920
{{ page_obj.paginator.count|intcomma }} results
2021
</div>
21-
{% if is_paginated %}
22-
{% include 'includes/pagination.html' with page_obj=page_obj %}
23-
{% endif %}
22+
<div class="is-flex is-justify-content-center mb-2">
23+
<div class="select is-small">
24+
{{ pagination_form.page_size }}
25+
</div>
26+
</div>
27+
{% if is_paginated %}
28+
{% include 'includes/pagination.html' with page_obj=page_obj %}
29+
{% endif %}
2430
</div>
2531
</section>
2632
</div>
@@ -40,9 +46,9 @@
4046
{% for vulnerability in page_obj %}
4147
<tr class="is-clipped-list">
4248
<td style="word-break: break-all;">
43-
<a
44-
href="{{ vulnerability.get_absolute_url }}?search={{ search }}"
45-
target="_self">{{ vulnerability.vulnerability_id }}
49+
<a
50+
href="{{ vulnerability.get_absolute_url }}?search={{ search }}"
51+
target="_self">{{ vulnerability.vulnerability_id }}
4652
</a>
4753
</td>
4854
<td>
@@ -63,19 +69,18 @@
6369
{% empty %}
6470
<tr class="is-clipped-list">
6571
<td colspan="3" style="word-break: break-all;">
66-
No vulnerability found.
72+
No vulnerability found.
6773
</td>
6874
</tr>
6975
{% endfor %}
7076
</tbody>
7177
</table>
7278
</div>
7379

74-
75-
{% if is_paginated %}
76-
{% include 'includes/pagination.html' with page_obj=page_obj %}
77-
{% endif %}
80+
{% if is_paginated %}
81+
{% include 'includes/pagination.html' with page_obj=page_obj %}
82+
{% endif %}
7883
</section>
7984
{% endif %}
80-
81-
{% endblock %}
85+
<script src="{% static 'js/pagination.js' %}"></script>
86+
{% endblock %}

0 commit comments

Comments
 (0)