Skip to content

Commit

Permalink
Corrected Changes after Review - I
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 2bac2e0 commit ee09428
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 93 deletions.
40 changes: 35 additions & 5 deletions vulnerabilities/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,49 @@
# 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.
#

from django import forms
from django.core.validators import validate_email

from .models import *
from vulnerabilities.models import ApiUser


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 +54,7 @@ class PackageSearchForm(forms.Form):
)


class VulnerabilitySearchForm(forms.Form):

class VulnerabilitySearchForm(BaseSearchForm):
search = forms.CharField(
required=True,
widget=forms.TextInput(
Expand Down
91 changes: 54 additions & 37 deletions vulnerabilities/templates/includes/pagination.html
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">&hellip;</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 %}
<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">&hellip;</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>

<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">&hellip;</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">&hellip;</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 %}
22 changes: 14 additions & 8 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 @@ -18,6 +19,11 @@
<div>
{{ page_obj.paginator.count|intcomma }} results
</div>
<div class="is-flex is-justify-content-center mb-2">
<div class="select is-small">
{{ pagination_form.page_size }}
</div>
</div>
{% if is_paginated %}
{% include 'includes/pagination.html' with page_obj=page_obj %}
{% endif %}
Expand Down Expand Up @@ -57,28 +63,28 @@
{% for package in page_obj %}
<tr>
<td style="word-break: break-all;">
<a
href="{{ package.get_absolute_url }}?search={{ search }}"
target="_self">{{ package.purl }}</a>
<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 src="{% static 'js/pagination.js' %}"></script>
{% endblock %}

32 changes: 18 additions & 14 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 @@ -18,9 +19,14 @@
<div>
{{ page_obj.paginator.count|intcomma }} results
</div>
{% if is_paginated %}
{% include 'includes/pagination.html' with page_obj=page_obj %}
{% endif %}
<div class="is-flex is-justify-content-center mb-2">
<div class="select is-small">
{{ pagination_form.page_size }}
</div>
</div>
{% if is_paginated %}
{% include 'includes/pagination.html' with page_obj=page_obj %}
{% endif %}
</div>
</section>
</div>
Expand All @@ -40,10 +46,8 @@
{% 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>
<a href="{{ vulnerability.get_absolute_url }}?search={{ search }}"
target="_self">{{ vulnerability.vulnerability_id }}</a>
</td>
<td>
{% for alias in vulnerability.alias %}
Expand All @@ -62,20 +66,20 @@
</tr>
{% empty %}
<tr class="is-clipped-list">
<td colspan="3" style="word-break: break-all;">
No vulnerability found.
<td colspan="4" style="word-break: break-all;">
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 src="{% static 'js/pagination.js' %}"></script>
{% endblock %}

Loading

0 comments on commit ee09428

Please sign in to comment.