Skip to content

Commit

Permalink
Merge pull request #150 from daviddavis/issue4827
Browse files Browse the repository at this point in the history
Adding checksum serializer for content
  • Loading branch information
daviddavis committed May 23, 2019
2 parents a3a4ef2 + 9728dee commit 92e1009
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 4 deletions.
1 change: 1 addition & 0 deletions pulpcore/app/serializers/__init__.py
Expand Up @@ -23,6 +23,7 @@
)
from .content import ( # noqa
ArtifactSerializer,
ContentChecksumSerializer,
NoArtifactContentSerializer,
SingleArtifactContentSerializer,
MultipleArtifactContentSerializer,
Expand Down
45 changes: 45 additions & 0 deletions pulpcore/app/serializers/content.py
Expand Up @@ -91,6 +91,51 @@ class Meta:
fields = BaseContentSerializer.Meta.fields + ('_artifacts',)


class ContentChecksumSerializer(serializers.Serializer):
"""
Provide a serializer with artifact checksum fields for single artifact content.
If you use this serializer, it's recommended that you prefetch artifacts:
Content.objects.prefetch_related("_artifacts").all()
"""

md5 = fields.ContentArtifactChecksumField(
help_text=_("The MD5 checksum if available."),
checksum='md5',
)

sha1 = fields.ContentArtifactChecksumField(
help_text=_("The SHA-1 checksum if available."),
checksum='sha1',
)

sha224 = fields.ContentArtifactChecksumField(
help_text=_("The SHA-224 checksum if available."),
checksum='sha224',
)

sha256 = fields.ContentArtifactChecksumField(
help_text=_("The SHA-256 checksum if available."),
checksum='sha256',
)

sha384 = fields.ContentArtifactChecksumField(
help_text=_("The SHA-384 checksum if available."),
checksum='sha384',
)

sha512 = fields.ContentArtifactChecksumField(
help_text=_("The SHA-512 checksum if available."),
checksum='sha512',
)

class Meta:
model = models.Artifact
fields = base.ModelSerializer.Meta.fields + ('md5', 'sha1', 'sha224', 'sha256', 'sha384',
'sha512')


class ArtifactSerializer(base.ModelSerializer):
_href = base.IdentityField(
view_name='artifacts-detail',
Expand Down
47 changes: 43 additions & 4 deletions pulpcore/app/serializers/fields.py
Expand Up @@ -49,15 +49,54 @@ def get_attribute(self, instance):
Returns:
A single Artifact model related to the instance of Content.
"""
try:
return instance._artifacts.get()
except models.Artifact.DoesNotExist:
# using get() and first() will query the db. count() and all() will use cached artifacts if
# they are prefetched.
if instance._artifacts.count() == 0:
return None
except models.Artifact.MultipleObjectsReturned:
if instance._artifacts.count() == 1:
return instance._artifacts.all()[0]
if instance._artifacts.count() > 1:
raise ValueError(_("SingleContentArtifactField should not be used in a context where "
"multiple artifacts for one content is possible."))


class ContentArtifactChecksumField(serializers.CharField):
"""
A serializer field for the artifact checksum Content model (single-artifact).
"""

def __init__(self, *args, **kwargs):
kwargs['read_only'] = True
self.checksum = kwargs.pop('checksum', 'md5')
super().__init__(*args, **kwargs)

def get_attribute(self, instance):
"""
Returns the field from the instance that should be serialized using this serializer field.
This serializer looks up the checksum for single artifact content
Args:
instance (:class:`pulpcore.app.models.Content`): An instance of Content being
serialized.
Returns:
A string of the checksum or None.
Raises:
:class:`rest_framework.exceptions.ValidationError`: When more than one Artifacts exist.
"""
# using get() and first() will query the db. count() and all() will use cached artifacts if
# they are prefetched.
if instance._artifacts.count() == 0:
return None
if instance._artifacts.count() == 1:
return getattr(instance._artifacts.all()[0], self.checksum)
if instance._artifacts.count() > 1:
raise ValueError(_("ContentArtifactChecksumField should not be used in a context where "
"multiple artifacts for one content is possible."))


class ContentArtifactsField(serializers.DictField):
"""
A serializer field for the '_artifacts' ManyToManyField on the Content model.
Expand Down

0 comments on commit 92e1009

Please sign in to comment.