Skip to content

Commit

Permalink
Review I Changes
Browse files Browse the repository at this point in the history
Signed-off-by: Rishi Garg <[email protected]>
  • Loading branch information
Rishi-garg03 committed Oct 29, 2024
1 parent 9bfcca5 commit c28765a
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 105 deletions.
40 changes: 36 additions & 4 deletions vulnerabilities/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#

Expand All @@ -12,9 +12,42 @@

from vulnerabilities.models import ApiUser

from .models import *

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)",
"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", "")


class PackageSearchForm(BaseSearchForm):
search = forms.CharField(
required=True,
widget=forms.TextInput(
Expand All @@ -23,8 +56,7 @@ class PackageSearchForm(forms.Form):
)


class VulnerabilitySearchForm(forms.Form):

class VulnerabilitySearchForm(BaseSearchForm):
search = forms.CharField(
required=True,
widget=forms.TextInput(
Expand Down
35 changes: 24 additions & 11 deletions vulnerabilities/templates/includes/pagination.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
{% 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_size }}" class="pagination-previous">Previous</a>
<a href="?page={{ page_obj.previous_page_number }}&search={{ search|urlencode }}&page_size={{ page_obj.paginator.per_page }}"
class="pagination-previous">Previous</a>
{% else %}
<a class="pagination-previous" disabled>Previous</a>
<span class="pagination-previous" disabled>Previous</span>
{% endif %}

{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}&search={{ search|urlencode }}&page_size={{ page_size }}" class="pagination-next">Next</a>
<a href="?page={{ page_obj.next_page_number }}&search={{ search|urlencode }}&page_size={{ page_obj.paginator.per_page }}"
class="pagination-next">Next</a>
{% else %}
<a class="pagination-next" disabled>Next</a>
<span class="pagination-next" disabled>Next</span>
{% endif %}

<ul class="pagination-list">
{% if page_obj.number > 1 %}
<li><a href="?page=1&search={{ search|urlencode }}&page_size={{ page_size }}" class="pagination-link" aria-label="Goto page 1">1</a></li>
<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">&hellip;</span></li>
{% endif %}
Expand All @@ -23,11 +28,14 @@
{% 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" %}
{% if page_obj.number == i %}
<li><a class="pagination-link is-current" aria-label="Page {{ i }}" aria-current="page">{{ i }}</a></li>
{% else %}
<li><a href="?page={{ i }}&search={{ search|urlencode }}&page_size={{ page_size }}" class="pagination-link" aria-label="Goto page {{ i }}">{{ i }}</a></li>
{% endif %}
<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 %}
Expand All @@ -36,7 +44,12 @@
{% if page_obj.number < page_obj.paginator.num_pages|add:"-3" %}
<li><span class="pagination-ellipsis">&hellip;</span></li>
{% endif %}
<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>
<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>
Expand Down
30 changes: 9 additions & 21 deletions vulnerabilities/templates/packages.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends "base.html" %}
{% load static %}
{% load humanize %}
{% load widget_tweaks %}

Expand All @@ -20,12 +21,7 @@
</div>
<div class="is-flex is-justify-content-center mb-2">
<div class="select is-small">
<select id="itemsPerPage" onchange="changeItemsPerPage(this.value)">
<option value="20" {% if page_obj.paginator.per_page == 20 %}selected{% endif %}>20 per page</option>
<option value="50" {% if page_obj.paginator.per_page == 50 %}selected{% endif %}>50 per page</option>
<option value="100" {% if page_obj.paginator.per_page == 100 %}selected{% endif %}>100 per page</option>
<option value="200" {% if page_obj.paginator.per_page == 200 %}selected{% endif %}>200 per page</option>
</select>
{{ pagination_form.page_size }}
</div>
</div>
{% if is_paginated %}
Expand Down Expand Up @@ -68,35 +64,27 @@
<tr>
<td style="word-break: break-all;">
<a
href="{{ package.get_absolute_url }}?search={{ search }}"
target="_self">{{ package.purl }}</a>
href="{{ package.get_absolute_url }}?search={{ search }}"
target="_self">{{ package.purl }}</a>
</td>
<td>{{ package.vulnerability_count }}</td>
<td>{{ package.patched_vulnerability_count }}</td>
</tr>
{% empty %}
<tr>
<td colspan="3" style="word-break: break-all;">
No Package found.
No Package found.
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>

{% if is_paginated %}
{% include 'includes/pagination.html' with page_obj=page_obj %}
{% endif %}

{% if is_paginated %}
{% include 'includes/pagination.html' with page_obj=page_obj %}
{% endif %}
</section>
{% endif %}
<script>
function changeItemsPerPage(pageSize) {
var urlParams = new URLSearchParams(window.location.search);
urlParams.set('page_size', pageSize);
window.location.search = urlParams.toString();
}

</script>
<script src="{% static 'js/pagination.js' %}"></script>
{% endblock %}
37 changes: 12 additions & 25 deletions vulnerabilities/templates/vulnerabilities.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends "base.html" %}
{% load static %}
{% load humanize %}
{% load widget_tweaks %}

Expand All @@ -20,17 +21,12 @@
</div>
<div class="is-flex is-justify-content-center mb-2">
<div class="select is-small">
<select id="itemsPerPage" onchange="changeItemsPerPage(this.value)">
<option value="20" {% if page_obj.paginator.per_page == 20 %}selected{% endif %}>20 per page</option>
<option value="50" {% if page_obj.paginator.per_page == 50 %}selected{% endif %}>50 per page</option>
<option value="100" {% if page_obj.paginator.per_page == 100 %}selected{% endif %}>100 per page</option>
<option value="200" {% if page_obj.paginator.per_page == 200 %}selected{% endif %}>200 per page</option>
</select>
{{ pagination_form.page_size }}
</div>
</div>
{% if is_paginated %}
{% include 'includes/pagination.html' with page_obj=page_obj %}
{% endif %}
{% include 'includes/pagination.html' with page_obj=page_obj %}
{% endif %}
</div>
</section>
</div>
Expand All @@ -50,9 +46,9 @@
{% for vulnerability in page_obj %}
<tr class="is-clipped-list">
<td style="word-break: break-all;">
<a
href="{{ vulnerability.get_absolute_url }}?search={{ search }}"
target="_self">{{ vulnerability.vulnerability_id }}
<a
href="{{ vulnerability.get_absolute_url }}?search={{ search }}"
target="_self">{{ vulnerability.vulnerability_id }}
</a>
</td>
<td>
Expand All @@ -73,27 +69,18 @@
{% empty %}
<tr class="is-clipped-list">
<td colspan="3" style="word-break: break-all;">
No vulnerability found.
No vulnerability found.
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>


{% if is_paginated %}
{% include 'includes/pagination.html' with page_obj=page_obj %}
{% endif %}
{% if is_paginated %}
{% include 'includes/pagination.html' with page_obj=page_obj %}
{% endif %}
</section>
{% endif %}
<script>
function changeItemsPerPage(pageSize) {
var urlParams = new URLSearchParams(window.location.search);
urlParams.set('page_size', pageSize);
window.location.search = urlParams.toString();
}

</script>

<script src="{% static 'js/pagination.js' %}"></script>
{% endblock %}
Loading

0 comments on commit c28765a

Please sign in to comment.