class UploadSerializer(base.ModelSerializer): """Serializer for chunked uploads.""" viewname = 'uploads:upload-detail' _href = base.IdentityField(view_name='upload-detail', ) file = serializers.FileField(write_only=True, ) class Meta(ChunkedUploadSerializer.Meta): model = models.Upload fields = ('_href', 'file', 'offset', 'expires_at')
class UploadSerializer(base.ModelSerializer): """Serializer for chunked uploads.""" pulp_href = base.IdentityField(view_name='uploads-detail', ) size = serializers.IntegerField( help_text=_("The size of the upload in bytes.")) completed = serializers.DateTimeField( help_text=_("Timestamp when upload is committed."), read_only=True) class Meta: model = models.Upload fields = base.ModelSerializer.Meta.fields + ('size', 'completed')
class SigningServiceSerializer(base.ModelSerializer): """ A serializer for the model declaring a signing service. """ pulp_href = base.IdentityField(view_name="signing-services-detail") name = serializers.CharField(help_text=_("A unique name used to recognize a script.")) script = serializers.CharField( help_text=_("An absolute path to a script which is going to be used for the signing.") ) class Meta: model = models.SigningService fields = BaseContentSerializer.Meta.fields + ("name", "script")
class SigningServiceSerializer(base.ModelSerializer): """ A serializer for the model declaring a signing service. """ pulp_href = base.IdentityField(view_name="signing-services-detail") name = serializers.CharField(help_text=_("A unique name used to recognize a script.")) public_key = serializers.CharField( help_text=_("The value of a public key used for the repository verification.") ) pubkey_fingerprint = serializers.CharField(help_text=_("The fingerprint of the public key.")) script = serializers.CharField( help_text=_("An absolute path to a script which is going to be used for the signing.") ) class Meta: model = models.SigningService fields = BaseContentSerializer.Meta.fields + ( "name", "public_key", "pubkey_fingerprint", "script", )
class ArtifactSerializer(base.ModelSerializer): pulp_href = base.IdentityField( view_name='artifacts-detail', ) file = serializers.FileField( help_text=_("The stored file."), allow_empty_file=True, required=False ) size = serializers.IntegerField( help_text=_("The size of the file in bytes."), required=False ) md5 = serializers.CharField( help_text=_("The MD5 checksum of the file if available."), required=False, allow_null=True, ) sha1 = serializers.CharField( help_text=_("The SHA-1 checksum of the file if available."), required=False, allow_null=True, ) sha224 = serializers.CharField( help_text=_("The SHA-224 checksum of the file if available."), required=False, allow_null=True, ) sha256 = serializers.CharField( help_text=_("The SHA-256 checksum of the file if available."), required=False, allow_null=True, ) sha384 = serializers.CharField( help_text=_("The SHA-384 checksum of the file if available."), required=False, allow_null=True, ) sha512 = serializers.CharField( help_text=_("The SHA-512 checksum of the file if available."), required=False, allow_null=True, ) def validate(self, data): """ Validate file by size and by all checksums provided. Args: data (:class:`django.http.QueryDict`): QueryDict mapping Artifact model fields to their values Raises: :class:`rest_framework.exceptions.ValidationError`: When the expected file size or any of the checksums don't match their actual values. """ super().validate(data) if 'size' in data: if data['file'].size != int(data['size']): raise serializers.ValidationError(_("The size did not match actual size of file.")) else: data['size'] = data['file'].size for algorithm in hashlib.algorithms_guaranteed: if algorithm in models.Artifact.DIGEST_FIELDS: digest = data['file'].hashers[algorithm].hexdigest() if algorithm in data and digest != data[algorithm]: raise serializers.ValidationError(_("The %s checksum did not match.") % algorithm) else: data[algorithm] = digest if algorithm in UNIQUE_ALGORITHMS: validator = UniqueValidator(models.Artifact.objects.all(), message=_("{0} checksum must be " "unique.").format(algorithm)) validator.field_name = algorithm validator.instance = None validator(digest) return data class Meta: model = models.Artifact fields = base.ModelSerializer.Meta.fields + ('file', 'size', 'md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')
class ArtifactSerializer(base.ModelSerializer): _href = base.IdentityField(view_name='artifacts-detail', ) file = serializers.FileField(help_text=_("The stored file."), required=False) upload = serializers.HyperlinkedRelatedField( view_name="upload-detail", write_only=True, required=False, queryset=models.Upload.objects.filter(status=models.Upload.COMPLETE)) size = serializers.IntegerField( help_text=_("The size of the file in bytes."), required=False) md5 = serializers.CharField( help_text=_("The MD5 checksum of the file if available."), required=False, allow_blank=True) sha1 = serializers.CharField( help_text=_("The SHA-1 checksum of the file if available."), required=False, allow_blank=True) sha224 = serializers.CharField( help_text=_("The SHA-224 checksum of the file if available."), required=False, allow_blank=True) sha256 = serializers.CharField( help_text=_("The SHA-256 checksum of the file if available."), required=False, allow_blank=True) sha384 = serializers.CharField( help_text=_("The SHA-384 checksum of the file if available."), required=False, allow_blank=True) sha512 = serializers.CharField( help_text=_("The SHA-512 checksum of the file if available."), required=False, allow_blank=True) def validate(self, data): """ Validate file by size and by all checksums provided. Args: data (:class:`django.http.QueryDict`): QueryDict mapping Artifact model fields to their values Raises: :class:`rest_framework.exceptions.ValidationError`: When the expected file size or any of the checksums don't match their actual values. """ super().validate(data) if ('file' not in data and 'upload' not in data) or \ ('file' in data and 'upload' in data): raise serializers.ValidationError( _("Either 'file' or 'upload' parameter must be " "supplied but not both.")) if 'upload' in data: self.upload = data.pop('upload') data['file'] = files.PulpTemporaryUploadedFile.from_file( self.upload.file.file) if 'size' in data: if data['file'].size != int(data['size']): raise serializers.ValidationError( _("The size did not match actual size of file.")) else: data['size'] = data['file'].size for algorithm in hashlib.algorithms_guaranteed: if algorithm in models.Artifact.DIGEST_FIELDS: digest = data['file'].hashers[algorithm].hexdigest() if algorithm in data and digest != data[algorithm]: raise serializers.ValidationError( _("The %s checksum did not match.") % algorithm) else: data[algorithm] = digest if algorithm in UNIQUE_ALGORITHMS: validator = UniqueValidator( models.Artifact.objects.all(), message=_("{0} checksum must be " "unique.").format(algorithm)) validator.field_name = algorithm validator.instance = None validator(digest) return data def create(self, validated_data): """ Create the artifact and delete its associated upload (if there is one) Args: validated_data (dict): Data to save to the database """ artifact = super().create(validated_data) if hasattr(self, 'upload'): # creating an artifact will move the upload file so we need to delete the db record self.upload.delete() return artifact class Meta: model = models.Artifact fields = base.ModelSerializer.Meta.fields + ( 'file', 'size', 'md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'upload')
class ArtifactSerializer(base.ModelSerializer): pulp_href = base.IdentityField(view_name="artifacts-detail") file = serializers.FileField(help_text=_("The stored file."), allow_empty_file=True) size = serializers.IntegerField(help_text=_("The size of the file in bytes."), required=False) md5 = serializers.CharField( help_text=_("The MD5 checksum of the file if available."), required=False, allow_null=True ) sha1 = serializers.CharField( help_text=_("The SHA-1 checksum of the file if available."), required=False, allow_null=True, ) sha224 = serializers.CharField( help_text=_("The SHA-224 checksum of the file if available."), required=False, allow_null=True, ) sha256 = serializers.CharField( help_text=_("The SHA-256 checksum of the file if available."), required=False, allow_null=True, ) sha384 = serializers.CharField( help_text=_("The SHA-384 checksum of the file if available."), required=False, allow_null=True, ) sha512 = serializers.CharField( help_text=_("The SHA-512 checksum of the file if available."), required=False, allow_null=True, ) def validate(self, data): """ Validate file by size and by all checksums provided. Args: data (:class:`django.http.QueryDict`): QueryDict mapping Artifact model fields to their values Raises: :class:`rest_framework.exceptions.ValidationError`: When the expected file size or any of the checksums don't match their actual values. """ super().validate(data) if "size" in data: if data["file"].size != int(data["size"]): raise serializers.ValidationError(_("The size did not match actual size of file.")) else: data["size"] = data["file"].size bad_algs = [] for algorithm in models.Artifact.FORBIDDEN_DIGESTS: if algorithm in data: bad_algs.append(algorithm) if bad_algs: raise serializers.ValidationError( _(f"Checksum algorithms {bad_algs} forbidden for this Pulp instance.") ) for algorithm in hashlib.algorithms_guaranteed: if algorithm in models.Artifact.DIGEST_FIELDS: digest = data["file"].hashers[algorithm].hexdigest() if algorithm in data and digest != data[algorithm]: raise serializers.ValidationError( _("The %s checksum did not match.") % algorithm ) else: data[algorithm] = digest if algorithm in models.Artifact.RELIABLE_DIGEST_FIELDS: validator = UniqueValidator( models.Artifact.objects.all(), message=_("{0} checksum must be " "unique.").format(algorithm), ) validator.instance = None validator(digest, self.fields[algorithm]) return data class Meta: model = models.Artifact fields = base.ModelSerializer.Meta.fields + ( "file", "size", "md5", "sha1", "sha224", "sha256", "sha384", "sha512", )