Skip to content

Commit

Permalink
Merge pull request aboutcode-org#830 from TG1999/fix_pagination
Browse files Browse the repository at this point in the history
Paginate initial listings to display a small number of or records
  • Loading branch information
TG1999 committed Aug 9, 2022
2 parents 2670b03 + 0c896aa commit b63d0c0
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ Version v30.0.0
- Package endpoint will give fixed packages of only those that
matches type, name, namespace, subpath and qualifiers of the package queried.

- Paginated initial listings to display a small number of records
and provided page per size with a maximum limit of 100 records per page.

Other:

- we dropped calver to use a plain semver.
Expand Down
4 changes: 0 additions & 4 deletions vulnerabilities/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ def filter_purl(self, queryset, name, value):
class PackageViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Package.objects.all()
serializer_class = PackageSerializer
paginate_by = 50
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = PackageFilterSet

Expand Down Expand Up @@ -252,7 +251,6 @@ def get_queryset(self):
)

serializer_class = VulnerabilitySerializer
paginate_by = 50
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = VulnerabilityFilterSet

Expand All @@ -270,7 +268,6 @@ class CPEViewSet(viewsets.ReadOnlyModelViewSet):
vulnerabilityreference__reference_id__startswith="cpe"
).distinct()
serializer_class = VulnerabilitySerializer
paginate_by = 50
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = CPEFilterSet

Expand All @@ -286,6 +283,5 @@ def filter_alias(self, queryset, name, value):
class AliasViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Vulnerability.objects.all()
serializer_class = VulnerabilitySerializer
paginate_by = 50
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = AliasFilterSet
6 changes: 6 additions & 0 deletions vulnerabilities/pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from rest_framework.pagination import PageNumberPagination


class SmallResultSetPagination(PageNumberPagination):
page_size_query_param = "page_size"
max_page_size = 100
36 changes: 36 additions & 0 deletions vulnerabilities/tests/test_fix_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from django.test import TestCase
from django.utils.http import int_to_base36
from packageurl import PackageURL
from rest_framework import status

from vulnerabilities.models import Alias
Expand Down Expand Up @@ -240,3 +241,38 @@ def test_api_status(self):
def test_api_response(self):
response = self.client.get("/api/alias?alias=CVE-9", format="json").data
self.assertEqual(response["count"], 1)


class BulkSearchAPI(TestCase):
def setUp(self):
packages = [
"pkg:nginx/nginx@0.6.18",
"pkg:nginx/nginx@1.20.0",
"pkg:nginx/nginx@1.21.0",
"pkg:nginx/nginx@1.20.1",
"pkg:nginx/nginx@1.9.5",
"pkg:nginx/nginx@1.17.2",
"pkg:nginx/nginx@1.17.3",
"pkg:nginx/nginx@1.16.1",
"pkg:nginx/nginx@1.15.5",
"pkg:nginx/nginx@1.15.6",
"pkg:nginx/nginx@1.14.1",
"pkg:nginx/nginx@1.0.7",
"pkg:nginx/nginx@1.0.15",
]
self.packages = packages
for package in packages:
purl = PackageURL.from_string(package)
attrs = {k: v for k, v in purl.to_dict().items() if v}
Package.objects.create(**attrs)

def test_api_response(self):
request_body = {
"purls": self.packages,
}
response = self.client.post(
"/api/packages/bulk_search",
data=request_body,
content_type="application/json",
).json()
assert len(response) == 13
5 changes: 3 additions & 2 deletions vulnerablecode/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
"django_filters.rest_framework.DjangoFilterBackend",
"rest_framework.filters.SearchFilter",
),
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
"PAGE_SIZE": 100,
"DEFAULT_PAGINATION_CLASS": "vulnerabilities.pagination.SmallResultSetPagination",
# Limit the load on the Database returning a small number of records by default. https://github.com/nexB/vulnerablecode/issues/819
"PAGE_SIZE": 10,
}

0 comments on commit b63d0c0

Please sign in to comment.