Skip to content

Commit

Permalink
Update PackageSet serializers #95
Browse files Browse the repository at this point in the history
    * Show hyperlinks to packages in package sets within a package in the Package API
    * Show package uids of packages in package sets within a Package in the Package metadata

Signed-off-by: Jono Yang <[email protected]>
  • Loading branch information
JonoYang committed Jul 19, 2023
1 parent 0f15caf commit 1e426a6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packagedb/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from packagedb.serializers import DependentPackageSerializer
from packagedb.serializers import ResourceAPISerializer
from packagedb.serializers import PackageAPISerializer
from packagedb.serializers import PackageSetSerializer
from packagedb.serializers import PackageSetAPISerializer
from packagedb.serializers import PartySerializer

class PackageResourcePurlFilter(Filter):
Expand Down Expand Up @@ -460,4 +460,4 @@ def _get_enhanced_package(package, packages):

class PackageSetViewSet(viewsets.ReadOnlyModelViewSet):
queryset = PackageSet.objects.prefetch_related('packages')
serializer_class = PackageSetSerializer
serializer_class = PackageSetAPISerializer
55 changes: 51 additions & 4 deletions packagedb/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
# See https://aboutcode.org for more information about nexB OSS projects.
#

from django.http import HttpRequest
from django.urls import reverse_lazy

from rest_framework.serializers import CharField
from rest_framework.serializers import HyperlinkedIdentityField
from rest_framework.serializers import HyperlinkedModelSerializer
Expand Down Expand Up @@ -130,11 +133,30 @@ class Meta:
)


class PackageSetSerializer(ModelSerializer):
class PackageInPackageSetAPISerializer(ModelSerializer):
"""
This serializes Package instances within a PackageSet that is within a
Package in the PackageAPISerializer
"""
class Meta:
model = Package
fields = (
'uuid',
)

def to_representation(self, instance):
reverse_uri = reverse_lazy('api:package-detail', kwargs={'uuid': instance.uuid})
request = self.context['request']
return request.build_absolute_uri(reverse_uri)


class PackageSetAPISerializer(ModelSerializer):
packages = PackageInPackageSetAPISerializer(many=True)
class Meta:
model = PackageSet
fields = (
'uuid',
'packages',
)


Expand All @@ -143,7 +165,7 @@ class PackageAPISerializer(HyperlinkedModelSerializer):
parties = PartySerializer(many=True)
resources = HyperlinkedIdentityField(view_name='api:package-resources', lookup_field='uuid')
url = HyperlinkedIdentityField(view_name='api:package-detail', lookup_field='uuid')
package_sets = PackageSetSerializer(many=True)
package_sets = PackageSetAPISerializer(many=True)
package_content = SerializerMethodField()

class Meta:
Expand Down Expand Up @@ -203,6 +225,31 @@ def get_package_content(self, obj):
return obj.get_package_content_display()


class PackageInPackageSetMetadataSerializer(ModelSerializer):
"""
This serializes Package instances within a PackageSet that is within a
Package in the PackageMetadataSerializer
"""
class Meta:
model = Package
fields = (
'uuid',
)

def to_representation(self, instance):
return instance.package_uid


class PackageSetMetadataSerializer(ModelSerializer):
packages = PackageInPackageSetMetadataSerializer(many=True)
class Meta:
model = PackageSet
fields = (
'uuid',
'packages',
)


class PackageMetadataSerializer(ModelSerializer):
"""
Serializes the metadata of a Package from the fields of the Package model
Expand All @@ -213,7 +260,7 @@ class PackageMetadataSerializer(ModelSerializer):
"""
dependencies = DependentPackageSerializer(many=True)
parties = PartySerializer(many=True)
package_sets = PackageSetSerializer(many=True)
package_sets = PackageSetMetadataSerializer(many=True)
package_content = SerializerMethodField()

class Meta:
Expand Down Expand Up @@ -268,7 +315,7 @@ def get_package_content(self, obj):
return obj.get_package_content_display()


class PackageSetSerializer(ModelSerializer):
class PackageSetAPISerializer(ModelSerializer):
packages = PackageAPISerializer(many=True)

class Meta:
Expand Down

0 comments on commit 1e426a6

Please sign in to comment.