Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed redundant PackageViewSet class code and added history field into package API nexB#389 nexB#221 #390

Merged
merged 4 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions packagedb/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,14 @@ def latest_version(self, request, *args, **kwargs):

return Response({})

@action(detail=True, methods=['get'])
def history(self, request, *args, **kwargs):
"""
Return the History field associated with the current Package.
"""
package = self.get_object()
return Response({"history" : package.history})

@action(detail=True, methods=['get'])
def resources(self, request, *args, **kwargs):
"""
Expand Down Expand Up @@ -451,7 +459,7 @@ class PackageUpdateSet(viewsets.ViewSet):
**Request example:**
{
"purls": [
{"purl": "pkg:npm/[email protected]", "content_type": 1}
{"purl": "pkg:npm/[email protected]", "content_type": "CURATION"}
],
"uuid" : "b67ceb49-1538-481f-a572-431062f382gg"
}
Expand Down Expand Up @@ -515,20 +523,6 @@ def create(self, request):
return Response(serializer.data)


class PackageViewSet(PackagePublicViewSet):
@action(detail=True)
def reindex_package(self, request, *args, **kwargs):
"""
Reindex this package instance
"""
package = self.get_object()
package.reindex()
data = {
'status': f'{package.package_url} has been queued for reindexing'
}
return Response(data)


UPDATEABLE_FIELDS = [
'primary_language',
'copyright',
Expand Down
2 changes: 2 additions & 0 deletions packagedb/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class PackageAPISerializer(HyperlinkedModelSerializer):
dependencies = DependentPackageSerializer(many=True)
parties = PartySerializer(many=True)
resources = HyperlinkedIdentityField(view_name='api:package-resources', lookup_field='uuid')
history = HyperlinkedIdentityField(view_name='api:package-history', lookup_field='uuid')
url = HyperlinkedIdentityField(view_name='api:package-detail', lookup_field='uuid')
package_sets = PackageSetAPISerializer(many=True)
package_content = SerializerMethodField()
Expand Down Expand Up @@ -223,6 +224,7 @@ class Meta:
'file_references',
'dependencies',
'resources',
'history',
)
read_only_fields = fields

Expand Down
12 changes: 9 additions & 3 deletions packagedb/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def test_api_resource_filter_by_checksums(self):
expected = self.get_test_loc('api/resource-filter_by_checksums-expected.json')
self.check_expected_results(response.data['results'], expected, fields_to_remove=["url", "uuid", "package"], regen=FIXTURES_REGEN)


class PackageApiTestCase(JsonBasedTesting, TestCase):
test_data_dir = os.path.join(os.path.dirname(__file__), 'testfiles')

Expand Down Expand Up @@ -411,7 +412,8 @@ def test_package_api_retrieve_endpoint(self):
continue

if key == 'history':
self.assertIn('test-message', value)
url = reverse('api:package-history', args=[self.package.uuid])
self.assertIn(url, value)

self.assertTrue(hasattr(self.package, key))
if key in self.package_data.keys():
Expand Down Expand Up @@ -476,12 +478,12 @@ def test_package_api_filter_by_checksums(self):
response = self.client.post('/api/packages/filter_by_checksums/', data=data)
self.assertEqual(5, 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", "package_sets",], regen=FIXTURES_REGEN)
self.check_expected_results(response.data['results'], expected, fields_to_remove=["url", "uuid", "resources", "package_sets", "history"], regen=FIXTURES_REGEN)
data["enhance_package_data"] = True
enhanced_response = self.client.post('/api/packages/filter_by_checksums/', data=data)
self.assertEqual(5, len(enhanced_response.data['results']))
expected = self.get_test_loc('api/package-filter_by_checksums-enhanced-package-data-expected.json')
self.check_expected_results(enhanced_response.data['results'], expected, fields_to_remove=["url", "uuid", "resources", "package_sets",], regen=FIXTURES_REGEN)
self.check_expected_results(enhanced_response.data['results'], expected, fields_to_remove=["url", "uuid", "resources", "package_sets", "history"], regen=FIXTURES_REGEN)


class PackageApiReindexingTestCase(JsonBasedTesting, TestCase):
Expand Down Expand Up @@ -724,6 +726,7 @@ def test_package_api_get_enhanced_package(self):
expected = self.get_test_loc('api/enhanced_package.json')
self.check_expected_results(result, expected, fields_to_remove=['package_sets'], regen=FIXTURES_REGEN)


class CollectApiTestCase(JsonBasedTesting, TestCase):
test_data_dir = os.path.join(os.path.dirname(__file__), 'testfiles')

Expand Down Expand Up @@ -806,6 +809,7 @@ def test_package_live(self):
'uuid',
'resources',
'package_sets',
'history'
]

self.check_expected_results(result, expected, fields_to_remove=fields_to_remove, regen=FIXTURES_REGEN)
Expand Down Expand Up @@ -1111,6 +1115,7 @@ def test_api_resource_checksum_filter(self):
])
self.assertEqual(expected_names, names)


class PackageUpdateSetTestCase(TestCase):

def setUp(self):
Expand Down Expand Up @@ -1203,6 +1208,7 @@ def test_api_purl_validation_empty_request(self):

self.assertAlmostEquals(expected, response.data)


class PurlValidateApiTestCase(TestCase):

def setUp(self):
Expand Down
Loading