Skip to content

Commit

Permalink
Do not include the pagination in the filter query string #563 (#635)
Browse files Browse the repository at this point in the history
* Do not include the pagination in the filter query string #563

Signed-off-by: Thomas Druez <[email protected]>

---------

Signed-off-by: Thomas Druez <[email protected]>
  • Loading branch information
tdruez authored Mar 14, 2023
1 parent b3d4564 commit 5707dae
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ v33.0.0 (unreleased)
with current Project/CodebaseResource path scheme.
https://github.com/nexB/scancode.io/pull/624

- Add ``SCANCODEIO_PAGINATE_BY`` setting to customize the number of items displayed per
page for each object type.
https://github.com/nexB/scancode.io/issues/563

v32.0.1 (2023-02-20)
--------------------

Expand Down
8 changes: 8 additions & 0 deletions docs/scancodeio-settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ A valid policies file is required to enable compliance-related features.
Check out the :ref:`tutorial_license_policies` tutorial for in-depth coverage of
this feature.

SCANCODEIO_PAGINATE_BY
----------------------

The number of objects display per page for each object type can be customized with the
following setting::

SCANCODEIO_PAGINATE_BY=project=30,error=50,resource=100,package=100,dependency=100

SCANCODEIO_REST_API_PAGE_SIZE
-----------------------------

Expand Down
13 changes: 13 additions & 0 deletions scancodeio/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@
# Default to 24 hours.
SCANCODEIO_TASK_TIMEOUT = env.int("SCANCODEIO_TASK_TIMEOUT", default=86400)

# List views pagination, controls the number of items displayed per page.
# Syntax in .env: SCANCODEIO_PAGINATE_BY=project=10,project_error=10
SCANCODEIO_PAGINATE_BY = env.dict(
"SCANCODEIO_PAGINATE_BY",
default={
"project": 20,
"error": 50,
"resource": 100,
"package": 100,
"dependency": 100,
},
)

# Default limit for "most common" entries in QuerySets.
SCANCODEIO_MOST_COMMON_LIMIT = env.int("SCANCODEIO_MOST_COMMON_LIMIT", default=7)

Expand Down
4 changes: 4 additions & 0 deletions scanpipe/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ def render_option(self, name, selected_choices, option_value, option_label):
data[name] = option_value
selected = data == self.data or option_value in selected_choices

# Do not include the pagination in the filter query string.
data.pop(PAGE_VAR, None)

css_class = str(self.extra_css_class)
if selected:
css_class += " is-active"
Expand Down Expand Up @@ -224,6 +227,7 @@ class ProjectFilterSet(FilterSetUtilsMixin, django_filters.FilterSet):
class Meta:
model = Project
fields = ["is_archived"]
exclude = ["page"]

def __init__(self, data=None, *args, **kwargs):
"""
Expand Down
19 changes: 19 additions & 0 deletions scanpipe/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ def test_scanpipe_views_project_list_state_of_filters_in_search_form(self):
expected = '<input type="hidden" name="status" value="failed">'
self.assertContains(response, expected, html=True)

@mock.patch("scanpipe.views.ProjectListView.get_paginate_by")
def test_scanpipe_views_project_list_filters_exclude_page(self, mock_paginate_by):
url = reverse("project_list")
# Create another project to enable pagination
Project.objects.create(name="project2")
mock_paginate_by.return_value = 1

data = {"page": "2"}
response = self.client.get(url, data=data)

expected = '<a class="is-black-link" href="?page=2&amp;sort=name">Name</a>'
self.assertContains(response, expected)
expected = '<li><a href="?status=" class="dropdown-item is-active">All</a></li>'
self.assertContains(response, expected)
expected = '<a href="?pipeline=" class="dropdown-item is-active">All</a>'
self.assertContains(response, expected)
expected = '<a href="?sort=" class="dropdown-item is-active">Newest</a>'
self.assertContains(response, expected)

def test_scanpipe_views_project_details_is_archived(self):
url = self.project1.get_absolute_url()
expected1 = "WARNING: This project is archived and read-only."
Expand Down
10 changes: 5 additions & 5 deletions scanpipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ class ProjectListView(
filterset_class = ProjectFilterSet
template_name = "scanpipe/project_list.html"
prefetch_related = ["runs"]
paginate_by = 20
paginate_by = settings.SCANCODEIO_PAGINATE_BY.get("project", 20)
table_columns = [
"name",
{
Expand Down Expand Up @@ -847,7 +847,7 @@ class CodebaseResourceListView(
model = CodebaseResource
filterset_class = ResourceFilterSet
template_name = "scanpipe/resource_list.html"
paginate_by = 100
paginate_by = settings.SCANCODEIO_PAGINATE_BY.get("resource", 100)
prefetch_related = ["discovered_packages"]
table_columns = [
"path",
Expand Down Expand Up @@ -884,7 +884,7 @@ class DiscoveredPackageListView(
model = DiscoveredPackage
filterset_class = PackageFilterSet
template_name = "scanpipe/package_list.html"
paginate_by = 100
paginate_by = settings.SCANCODEIO_PAGINATE_BY.get("package", 100)
prefetch_related = ["codebase_resources"]
table_columns = [
"package_url",
Expand All @@ -906,7 +906,7 @@ class DiscoveredDependencyListView(
model = DiscoveredDependency
filterset_class = DependencyFilterSet
template_name = "scanpipe/dependency_list.html"
paginate_by = 100
paginate_by = settings.SCANCODEIO_PAGINATE_BY.get("dependency", 100)
prefetch_related = ["for_package", "datafile_resource"]
table_columns = [
"package_url",
Expand All @@ -932,7 +932,7 @@ class ProjectErrorListView(
model = ProjectError
filterset_class = ErrorFilterSet
template_name = "scanpipe/error_list.html"
paginate_by = 50
paginate_by = settings.SCANCODEIO_PAGINATE_BY.get("error", 50)
table_columns = [
"model",
"message",
Expand Down

0 comments on commit 5707dae

Please sign in to comment.