Skip to content

Commit f9d6325

Browse files
committed
fix pagination not working with filtering
Signed-off-by: Aayush Kumar <[email protected]>
1 parent c4d25dd commit f9d6325

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

scanpipe/filters.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,6 @@ def filter(self, qs, value):
522522

523523

524524
class ResourceFilterSet(FilterSetUtilsMixin, django_filters.FilterSet):
525-
526525
detected_license_expression = django_filters.ChoiceFilter(
527526
label="Detected license expression",
528527
choices=[

scanpipe/templates/scanpipe/includes/filter_sort.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<a
22
class="is-black-link"
3-
href="?path={{ path|urlencode }}&sort={% if column.is_sorted and column.sort_direction == '-' %}{{ column.field_name }}{% else %}-{{ column.field_name }}{% endif %}"
43
{% if is_htmx %}
4+
href="?path={{ path|urlencode }}&sort={% if column.is_sorted and column.sort_direction == '-' %}{{ column.field_name }}{% else %}-{{ column.field_name }}{% endif %}"
55
hx-get="{% url 'codebase_resource_table' project.slug %}"
66
hx-target="#right-pane"
77
hx-push-url="false"
88
hx-vals='{"path": "{{ path|escapejs }}", "sort": "{% if column.is_sorted and column.sort_direction == '-' %}{{ column.field_name }}{% else %}-{{ column.field_name }}{% endif %}"}'
99
onclick="return false;"
1010
style="cursor:pointer;"
11+
{% else %}
12+
href="?{{ column.sort_query }}"
1113
{% endif %}
1214
>
1315
{{ column.label }}

scanpipe/templates/scanpipe/panels/resource_table_panel.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,14 @@
9090
{% if is_paginated %}
9191
<nav class="pagination is-centered mt-4" role="navigation">
9292
{% if page_obj.has_previous %}
93-
<a class="pagination-previous" hx-get="{% url 'codebase_resource_table' project.slug %}?path={{ path }}&page={{ page_obj.previous_page_number }}" hx-target="#right-pane">Previous</a>
93+
<a class="pagination-previous"
94+
hx-get="{% url 'codebase_resource_table' project.slug %}?{% for key, value in request.GET.items %}{% if key != 'page' and value %}{{ key }}={{ value|urlencode }}&{% endif %}{% endfor %}page={{ page_obj.previous_page_number }}"
95+
hx-target="#right-pane">Previous</a>
9496
{% endif %}
9597
{% if page_obj.has_next %}
96-
<a class="pagination-next" hx-get="{% url 'codebase_resource_table' project.slug %}?path={{ path }}&page={{ page_obj.next_page_number }}" hx-target="#right-pane">Next page</a>
98+
<a class="pagination-next"
99+
hx-get="{% url 'codebase_resource_table' project.slug %}?{% for key, value in request.GET.items %}{% if key != 'page' and value %}{{ key }}={{ value|urlencode }}&{% endif %}{% endfor %}page={{ page_obj.next_page_number }}"
100+
hx-target="#right-pane">Next page</a>
97101
{% endif %}
98102
<ul class="pagination-list">
99103
<li><span class="pagination-ellipsis">Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}</span></li>

scanpipe/views.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,10 @@ def get_columns_data(self):
450450
column_data["sort_direction"] = sort_direction
451451

452452
query_dict = self.request.GET.copy()
453-
query_dict["sort"] = (
454-
f"{'' if sort_direction == '-' else '-'}{sort_name}"
455-
)
453+
if is_sorted and sort_direction == "":
454+
query_dict["sort"] = f"-{sort_name}"
455+
else:
456+
query_dict["sort"] = sort_name
456457
column_data["sort_query"] = query_dict.urlencode()
457458

458459
filter_fieldname = column_data.get("filter_fieldname")
@@ -693,6 +694,9 @@ def get_context_data(self, **kwargs):
693694
context["reset_form"] = ProjectResetForm()
694695
context["outputs_download_form"] = ProjectOutputDownloadForm()
695696
context["report_form"] = ProjectReportForm()
697+
context["request"] = (
698+
self.request
699+
) # Ensure request is available in template context
696700
return context
697701

698702
def get_queryset(self):

0 commit comments

Comments
 (0)