Skip to content

Commit

Permalink
Merge pull request #184 from nexB/do-not-process-empty-filter-by-chec…
Browse files Browse the repository at this point in the history
…ksum-request

Don't process empty request in filter_by_checksums
  • Loading branch information
JonoYang authored Sep 25, 2023
2 parents 8428cb8 + b939bc7 commit fe893cb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
13 changes: 10 additions & 3 deletions packagedb/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,21 @@ def filter_by_checksums(self, request, *args, **kwargs):
}
return Response(response_data)

q = Q()
if not data:
response_data = {
'status': 'No values provided'
}
return Response(response_data)

lookups = Q()
for field, value in data.items():
value = value or []
# We create this intermediate dictionary so we can modify the field
# name to have __in at the end
d = {f'{field}__in': value}
q |= Q(**d)
lookups |= Q(**d)

qs = Resource.objects.filter(q)
qs = Resource.objects.filter(lookups)
paginated_qs = self.paginate_queryset(qs)
serializer = ResourceAPISerializer(paginated_qs, many=True, context={'request': request})
return self.get_paginated_response(serializer.data)
Expand Down
8 changes: 4 additions & 4 deletions packagedb/api_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
class PageSizePagination(PageNumberPagination):
"""
Adds the page_size parameter. Default results per page is 10.
A page_size parameter can be provided, limited to 100 results per page max.
A page_size parameter can be provided, limited to 20 results per page max.
For example:
http://api.example.org/accounts/?page=4&page_size=100
http://api.example.org/accounts/?page=4&page_size=20
"""
page_size = 100
max_page_size = 100
page_size = 20
max_page_size = 20
page_size_query_param = 'page_size'
2 changes: 1 addition & 1 deletion purldb/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@
),
'DEFAULT_PAGINATION_CLASS': 'packagedb.api_custom.PageSizePagination',
# Limit the load on the Database returning a small number of records by default. https://github.com/nexB/vulnerablecode/issues/819
"PAGE_SIZE": 100,
"PAGE_SIZE": 20,
}

if not PURLDB_REQUIRE_AUTHENTICATION:
Expand Down

0 comments on commit fe893cb

Please sign in to comment.