Skip to content
This repository has been archived by the owner on Dec 7, 2022. It is now read-only.
/ pulp Public archive

Commit

Permalink
Save ContentArtifacts in ContentSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Dellweg authored and daviddavis committed Feb 20, 2019
1 parent 704c44a commit d3fbf86
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
8 changes: 7 additions & 1 deletion pulpcore/app/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
validate_unknown_fields,
AsyncOperationResponseSerializer
)
from .fields import BaseURLField, ContentRelatedField, LatestVersionField # noqa
from .fields import ( # noqa
BaseURLField,
ContentRelatedField,
LatestVersionField,
SingleContentArtifactField,
relative_path_validator,
)
from .content import ( # noqa
ArtifactSerializer,
NoArtifactContentSerializer,
Expand Down
49 changes: 46 additions & 3 deletions pulpcore/app/serializers/content.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from gettext import gettext as _
import hashlib

from django.db import transaction
from rest_framework import serializers
from rest_framework.validators import UniqueValidator

Expand All @@ -23,17 +24,41 @@ class NoArtifactContentSerializer(BaseContentSerializer):

class Meta:
model = models.Content
fields = base.MasterModelSerializer.Meta.fields
fields = BaseContentSerializer.Meta.fields


class SingleArtifactContentSerializer(BaseContentSerializer):
_artifact = fields.SingleContentArtifactField(
help_text=_("Artifact file representing the physical content"),
)

_relative_path = serializers.CharField(
help_text=_("Path where the artifact is located relative to distributions base_path"),
validators=[fields.relative_path_validator],
write_only=True,
)

@transaction.atomic
def create(self, validated_data):
"""
Create the content and associate it with its Artifact.
Args:
validated_data (dict): Data to save to the database
"""
artifact = validated_data.pop('_artifact')
relative_path = validated_data.pop('_relative_path')
content = self.Meta.model.objects.create(**validated_data)
models.ContentArtifact.objects.create(
artifact=artifact,
content=content,
relative_path=relative_path,
)
return content

class Meta:
model = models.Content
fields = base.MasterModelSerializer.Meta.fields + ('_artifact',)
fields = BaseContentSerializer.Meta.fields + ('_artifact', '_relative_path')


class MultipleArtifactContentSerializer(BaseContentSerializer):
Expand All @@ -43,9 +68,27 @@ class MultipleArtifactContentSerializer(BaseContentSerializer):
"'/artifacts/1/'"),
)

@transaction.atomic
def create(self, validated_data):
"""
Create the content and associate it with all its Artifacts.
Args:
validated_data (dict): Data to save to the database
"""
_artifacts = validated_data.pop('_artifacts')
content = self.Meta.model.objects.create(**validated_data)
for relative_path, artifact in _artifacts.items():
models.ContentArtifact.objects.create(
artifact=artifact,
content=content,
relative_path=relative_path,
)
return content

class Meta:
model = models.Content
fields = base.MasterModelSerializer.Meta.fields + ('_artifacts',)
fields = BaseContentSerializer.Meta.fields + ('_artifacts',)


class ArtifactSerializer(base.ModelSerializer):
Expand Down
10 changes: 7 additions & 3 deletions pulpcore/app/serializers/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
from pulpcore.app.serializers import DetailRelatedField, RelatedField


def relative_path_validator(relative_path):
if os.path.isabs(relative_path):
raise serializers.ValidationError(_("Relative path can't start with '/'. "
"{0}").format(relative_path))


class ContentRelatedField(DetailRelatedField):
"""
Serializer Field for use when relating to Content Detail Models
Expand Down Expand Up @@ -76,9 +82,7 @@ def run_validation(self, data):
"""
ret = {}
for relative_path, url in data.items():
if os.path.isabs(relative_path):
raise serializers.ValidationError(_("Relative path can't start with '/'. "
"{0}").format(relative_path))
relative_path_validator(relative_path)
artifactfield = RelatedField(view_name='artifacts-detail',
queryset=models.Artifact.objects.all(),
source='*', initial=url)
Expand Down

0 comments on commit d3fbf86

Please sign in to comment.