-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pagination Updated - Items per page option added #1625
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
# VulnerableCode is a trademark of nexB Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. | ||
# See https://github.com/aboutcode-org/vulnerablecode for support or download. | ||
# See https://github.com/nexB/vulnerablecode for support or download. | ||
# See https://aboutcode.org for more information about nexB OSS projects. | ||
# | ||
|
||
|
@@ -12,26 +12,87 @@ | |
|
||
from vulnerabilities.models import ApiUser | ||
|
||
from .models import * | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid * imports. |
||
|
||
class PackageSearchForm(forms.Form): | ||
|
||
class PaginationForm(forms.Form): | ||
"""Form to handle page size selection across the application.""" | ||
|
||
PAGE_CHOICES = [ | ||
("20", "20 per page"), | ||
("50", "50 per page"), | ||
("100", "100 per page"), | ||
] | ||
|
||
page_size = forms.ChoiceField( | ||
choices=PAGE_CHOICES, | ||
initial="20", | ||
required=False, | ||
widget=forms.Select( | ||
attrs={ | ||
"class": "select is-small", | ||
"onchange": "handlePageSizeChange(this.value)", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid inline JavaScript. |
||
"id": "page-size-select", | ||
} | ||
), | ||
) | ||
|
||
|
||
class BaseSearchForm(forms.Form): | ||
"""Base form for implementing search functionality.""" | ||
|
||
search = forms.CharField(required=True) | ||
|
||
def clean_search(self): | ||
return self.cleaned_data.get("search", "") | ||
Comment on lines
+46
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the purpose of this? |
||
|
||
def get_queryset(self, query=None): | ||
""" | ||
Get queryset with search/filter/ordering applied. | ||
Args: | ||
query (str, optional): Direct query for testing | ||
""" | ||
if query is not None: | ||
return self._search(query) | ||
|
||
if not self.is_valid(): | ||
return self.model.objects.none() | ||
|
||
return self._search(self.clean_search()) | ||
Comment on lines
+49
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the need for this logic? |
||
|
||
|
||
class PackageSearchForm(BaseSearchForm): | ||
model = Package | ||
search = forms.CharField( | ||
required=True, | ||
widget=forms.TextInput( | ||
attrs={"placeholder": "Package name, purl or purl fragment"}, | ||
), | ||
) | ||
|
||
def _search(self, query): | ||
"""Execute package-specific search logic.""" | ||
return ( | ||
self.model.objects.search(query) | ||
.with_vulnerability_counts() | ||
.prefetch_related() | ||
.order_by("package_url") | ||
) | ||
Comment on lines
+73
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not following the Django forms conventions. |
||
|
||
class VulnerabilitySearchForm(forms.Form): | ||
|
||
class VulnerabilitySearchForm(BaseSearchForm): | ||
model = Vulnerability | ||
search = forms.CharField( | ||
required=True, | ||
widget=forms.TextInput( | ||
attrs={"placeholder": "Vulnerability id or alias such as CVE or GHSA"} | ||
), | ||
) | ||
|
||
def _search(self, query): | ||
"""Execute vulnerability-specific search logic.""" | ||
return self.model.objects.search(query=query).with_package_counts() | ||
Comment on lines
+92
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above. |
||
|
||
|
||
class ApiUserCreationForm(forms.ModelForm): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,56 @@ | ||
<nav class="pagination is-centered is-small" aria-label="pagination"> | ||
{% if page_obj.has_previous %} | ||
<a href="?page={{ page_obj.previous_page_number }}&search={{ search|urlencode }}" class="pagination-previous">Previous</a> | ||
{% else %} | ||
<a class="pagination-previous" disabled>Previous</a> | ||
{% endif %} | ||
|
||
{% if page_obj.has_next %} | ||
<a href="?page={{ page_obj.next_page_number }}&search={{ search|urlencode }}" class="pagination-next">Next</a> | ||
{% else %} | ||
<a class="pagination-next" disabled>Next</a> | ||
{% endif %} | ||
|
||
<ul class="pagination-list"> | ||
{% if page_obj.number != 1%} | ||
<li> | ||
<a href="?page=1&search={{ search|urlencode }}" class="pagination-link" aria-label="Goto page 1">1</a> | ||
</li> | ||
{% if page_obj.number > 2 %} | ||
<li> | ||
<span class="pagination-ellipsis">…</span> | ||
</li> | ||
{% endif %} | ||
{% if is_paginated %} | ||
<nav class="pagination is-centered" role="navigation" aria-label="pagination"> | ||
{% if page_obj.has_previous %} | ||
<a href="?page={{ page_obj.previous_page_number }}&search={{ search|urlencode }}&page_size={{ page_obj.paginator.per_page }}" | ||
class="pagination-previous">Previous</a> | ||
{% else %} | ||
Comment on lines
+4
to
+5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if other filters are active, those are lost when using the pagination? |
||
<span class="pagination-previous" disabled>Previous</span> | ||
{% endif %} | ||
<li> | ||
<a class="pagination-link is-current" aria-label="Page {{ page_obj.number }}" aria-current="page">{{ page_obj.number }}</a> | ||
</li> | ||
{% if page_obj.number != page_obj.paginator.num_pages %} | ||
{% if page_obj.next_page_number != page_obj.paginator.num_pages %} | ||
<li> | ||
<span class="pagination-ellipsis">…</span> | ||
</li> | ||
{% endif %} | ||
<li> | ||
<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> | ||
</li> | ||
|
||
{% if page_obj.has_next %} | ||
<a href="?page={{ page_obj.next_page_number }}&search={{ search|urlencode }}&page_size={{ page_obj.paginator.per_page }}" | ||
class="pagination-next">Next</a> | ||
{% else %} | ||
<span class="pagination-next" disabled>Next</span> | ||
{% endif %} | ||
</ul> | ||
</nav> | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All the complexity of those templates should likely be handled in Python. |
||
<ul class="pagination-list"> | ||
{% if page_obj.number > 1 %} | ||
<li> | ||
<a href="?page=1&search={{ search|urlencode }}&page_size={{ page_obj.paginator.per_page }}" | ||
class="pagination-link" aria-label="Page 1">1</a> | ||
</li> | ||
{% if page_obj.number > 4 %} | ||
<li><span class="pagination-ellipsis">…</span></li> | ||
{% endif %} | ||
{% endif %} | ||
|
||
{% for i in page_obj.paginator.page_range %} | ||
{% if i > 1 and i < page_obj.paginator.num_pages %} | ||
{% if i >= page_obj.number|add:"-3" and i <= page_obj.number|add:"3" %} | ||
<li> | ||
{% if page_obj.number == i %} | ||
<span class="pagination-link is-current" aria-current="page">{{ i }}</span> | ||
{% else %} | ||
<a href="?page={{ i }}&search={{ search|urlencode }}&page_size={{ page_obj.paginator.per_page }}" | ||
class="pagination-link" aria-label="Goto page {{ i }}">{{ i }}</a> | ||
{% endif %} | ||
</li> | ||
{% endif %} | ||
{% endif %} | ||
{% endfor %} | ||
|
||
{% if page_obj.number < page_obj.paginator.num_pages %} | ||
{% if page_obj.number < page_obj.paginator.num_pages|add:"-3" %} | ||
<li><span class="pagination-ellipsis">…</span></li> | ||
{% endif %} | ||
<li> | ||
<a href="?page={{ page_obj.paginator.num_pages }}&search={{ search|urlencode }}&page_size={{ page_obj.paginator.per_page }}" | ||
class="pagination-link" aria-label="Goto page {{ page_obj.paginator.num_pages }}"> | ||
{{ page_obj.paginator.num_pages }} | ||
</a> | ||
</li> | ||
{% endif %} | ||
</ul> | ||
</nav> | ||
{% endif %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unwanted changes