Skip to content

Commit

Permalink
Fix column sorting for tabs #95
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <tdruez@nexb.com>
  • Loading branch information
tdruez committed Aug 30, 2024
1 parent 6d8a21b commit 4ce5cf9
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 5 deletions.
5 changes: 4 additions & 1 deletion dje/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ def is_active(self):
)

def get_query_no_sort(self):
return remove_field_from_query_dict(self.data, "sort")
sort_field_name = "sort"
if self.form_prefix:
sort_field_name = f"{self.form_prefix}-{sort_field_name}"
return remove_field_from_query_dict(self.data, sort_field_name)

def get_filter_breadcrumb(self, field_name, data_field_name, value):
return {
Expand Down
2 changes: 2 additions & 0 deletions dje/templates/includes/object_list_table_header.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
{% if header.sort %}
{% with query_no_sort=filter.get_query_no_sort %}
{% if filter.form.sort.data and header.field_name in filter.form.sort.data.0 %}
{# The sort for this field is currently active #}
<a href="?{% if query_no_sort %}{{ query_no_sort }}&{% endif %}{% if tab_id %}{{ tab_id }}-{% endif %}sort={% if '-' not in filter.form.sort.data.0 %}-{% endif %}{{ header.field_name }}{% if tab_id %}#{{ tab_id }}{% endif %}" class="sort active" aria-label="Sort"><i class="fas fa-sort-{% if '-' not in filter.form.sort.data.0 %}up{% else %}down{% endif %}"></i></a>
{% else %}
{# The sort for this field is NOT active #}
<a href="?{% if query_no_sort %}{{ query_no_sort }}&{% endif %}{% if tab_id %}{{ tab_id }}-{% endif %}sort={{ header.field_name }}{% if tab_id %}#{{ tab_id }}{% endif %}" class="sort" aria-label="Sort"><i class="fas fa-sort"></i></a>
{% endif %}
{% endwith %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{% spaceless %}
{% include 'tabs/pagination.html' %}
<table class="table table-bordered table-hover table-md text-break">
{% include 'includes/object_list_table_header.html' %}
{% include 'includes/object_list_table_header.html' with filter=filterset %}
<tbody class="text-break">
{% for dependency in page_obj.object_list %}
<tr class="{% cycle 'odd' '' %}">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% load i18n %}
{% include 'tabs/pagination.html' %}
<table class="table table-bordered table-hover table-md text-break">
{% include 'includes/object_list_table_header.html' %}
{% include 'includes/object_list_table_header.html' with filter=filterset %}
<tbody>
{% for vulnerability in page_obj.object_list %}
<tr>
Expand Down
6 changes: 4 additions & 2 deletions product_portfolio/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,10 @@ def test_product_portfolio_detail_view_tab_vulnerability_view_filters(self):
self.client.login(username="nexb_user", password="secret")
url = self.product1.get_url("tab_vulnerabilities")
response = self.client.get(url)
expected = "?vulnerabilities-max_score=#vulnerabilities"
self.assertContains(response, expected)
self.assertContains(response, "?vulnerabilities-max_score=#vulnerabilities")
self.assertContains(response, "?vulnerabilities-sort=max_score#vulnerabilities")
response = self.client.get(url + "?vulnerabilities-sort=max_score#vulnerabilities")
self.assertContains(response, "?vulnerabilities-sort=-max_score#vulnerabilities")

@mock.patch("dejacode_toolkit.vulnerablecode.VulnerableCode.is_configured")
def test_product_portfolio_detail_view_tab_vulnerability_label(self, mock_is_configured):
Expand Down

0 comments on commit 4ce5cf9

Please sign in to comment.