diff --git a/packagedb/api.py b/packagedb/api.py index 43df7aa3..14150c13 100644 --- a/packagedb/api.py +++ b/packagedb/api.py @@ -522,6 +522,12 @@ def filter_by_checksums(self, request, *args, **kwargs): qs = Package.objects.filter(q) paginated_qs = self.paginate_queryset(qs) + get_enhanced_package_data = request.query_params.get('get_enhanced_package_data', False) + if get_enhanced_package_data: + enhanced_package_data = [get_enhanced_package(package=package) for package in qs] + enhanced_package_data = [package for package in enhanced_package_data if package] + paginated_response = self.get_paginated_response(enhanced_package_data) + return paginated_response serializer = PackageAPISerializer(paginated_qs, many=True, context={'request': request}) return self.get_paginated_response(serializer.data) diff --git a/packagedb/serializers.py b/packagedb/serializers.py index e744a6d2..2b8a9afd 100644 --- a/packagedb/serializers.py +++ b/packagedb/serializers.py @@ -161,15 +161,6 @@ class Meta: class PackageAPISerializer(HyperlinkedModelSerializer): - - def to_representation(self, instance): - data = super().to_representation(instance) - request = self.context["request"] - if hasattr(request, "query_params") and request.query_params.get("enhanced", False): - from packagedb.api import get_enhanced_package - data["enhanced_package"] = get_enhanced_package(instance) - return data - dependencies = DependentPackageSerializer(many=True) parties = PartySerializer(many=True) resources = HyperlinkedIdentityField(view_name='api:package-resources', lookup_field='uuid') diff --git a/packagedb/tests/test_api.py b/packagedb/tests/test_api.py index 8311e9e4..3418c2bc 100644 --- a/packagedb/tests/test_api.py +++ b/packagedb/tests/test_api.py @@ -265,6 +265,23 @@ def setUp(self): self.package3 = Package.objects.create(**self.package_data3) self.package3.refresh_from_db() + self.package_data4= { + 'type': 'jar', + 'namespace': 'sample', + 'name': 'Baz', + 'version': '90.123', + 'qualifiers': '', + 'subpath': '', + 'download_url': 'http://anothersample.com', + 'filename': 'Baz.zip', + 'sha1': 'testsha1-4', + 'md5': 'testmd5-3', + 'size': 100, + 'package_content': 5, + } + self.package4 = Package.objects.create(**self.package_data4) + self.package4.refresh_from_db() + self.test_url = 'http://testserver/api/packages/{}/' self.client = APIClient() @@ -272,7 +289,7 @@ def setUp(self): def test_package_api_list_endpoint(self): response = self.client.get('/api/packages/') self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(3, response.data.get('count')) + self.assertEqual(4, response.data.get('count')) def test_package_api_list_endpoint_filter(self): for key, value in self.package_data.items(): @@ -448,14 +465,17 @@ def test_package_api_filter_by_checksums(self): 'testsha1', 'testsha1-2', 'testsha1-3', + 'testsha1-4', ] data = { 'sha1': sha1s } + enhanced_response = self.client.post('/api/packages/filter_by_checksums/?get_enhanced_package_data=true', data=data) + self.assertEqual(1, len(enhanced_response.data['results'])) response = self.client.post('/api/packages/filter_by_checksums/', data=data) - self.assertEqual(3, response.data['count']) + self.assertEqual(4, response.data['count']) expected = self.get_test_loc('api/package-filter_by_checksums-expected.json') - self.check_expected_results(response.data['results'], expected, fields_to_remove=["url", "uuid", "resources"], regen=False) + self.check_expected_results(response.data['results'], expected, fields_to_remove=["url", "uuid", "resources"], regen=True) class PackageApiReindexingTestCase(JsonBasedTesting, TestCase): diff --git a/packagedb/tests/testfiles/api/package-filter_by_checksums-expected.json b/packagedb/tests/testfiles/api/package-filter_by_checksums-expected.json index a01c743b..7bd514ec 100644 --- a/packagedb/tests/testfiles/api/package-filter_by_checksums-expected.json +++ b/packagedb/tests/testfiles/api/package-filter_by_checksums-expected.json @@ -136,5 +136,51 @@ "datasource_id":null, "file_references":[], "dependencies":[] + }, + { + "filename":"Baz.zip", + "package_sets":[], + "package_content":"binary", + "purl":"pkg:jar/sample/baz@90.123", + "type":"jar", + "namespace":"sample", + "name":"baz", + "version":"90.123", + "qualifiers":"", + "subpath":"", + "primary_language":null, + "description":null, + "release_date":null, + "parties":[], + "keywords":[], + "homepage_url":null, + "download_url":"http://anothersample.com", + "bug_tracking_url":null, + "code_view_url":null, + "vcs_url":null, + "repository_homepage_url":null, + "repository_download_url":null, + "api_data_url":null, + "size":100, + "md5":"testmd5-3", + "sha1":"testsha1-4", + "sha256":null, + "sha512":null, + "copyright":null, + "holder":null, + "declared_license_expression":null, + "declared_license_expression_spdx":null, + "license_detections":[], + "other_license_expression":null, + "other_license_expression_spdx":null, + "other_license_detections":[], + "extracted_license_statement":null, + "notice_text":null, + "source_packages":[], + "extra_data":{}, + "package_uid":"pkg:jar/sample/baz@90.123?uuid=fixed-uid-done-for-testing-5642512d1758", + "datasource_id":null, + "file_references":[], + "dependencies":[] } ] \ No newline at end of file