Skip to content

Commit

Permalink
Update to use specialized serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
dralley committed Feb 5, 2019
1 parent f0ffa1e commit 4376e5c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 40 deletions.
26 changes: 1 addition & 25 deletions pulp_ansible/app/models.py
Expand Up @@ -2,7 +2,7 @@

from django.db import models

from pulpcore.plugin.models import Content, ContentArtifact, Remote
from pulpcore.plugin.models import Content, Remote


log = getLogger(__name__)
Expand Down Expand Up @@ -35,30 +35,6 @@ class AnsibleRoleVersion(Content):
version = models.CharField(max_length=128)
role = models.ForeignKey(AnsibleRole, on_delete=models.PROTECT, related_name='versions')

@property
def artifact(self):
"""
Return the artifact id (there is only one for this content type).
"""
return self._artifacts.get().pk

@artifact.setter
def artifact(self, artifact):
"""
Set the artifact for this Ansible Role version.
"""
if self.pk:
ca = ContentArtifact(
artifact=artifact,
content=self,
relative_path="{namespace}/{name}/{version}.tar.gz".format(
namespace=self.role.namespace,
name=self.role.name,
version=self.version
)
)
ca.save()

@property
def relative_path(self):
"""
Expand Down
22 changes: 10 additions & 12 deletions pulp_ansible/app/serializers.py
@@ -1,13 +1,17 @@
from rest_framework import serializers

from pulpcore.plugin.serializers import ContentSerializer, IdentityField, NestedIdentityField, \
RelatedField, RemoteSerializer
from pulpcore.plugin.models import Artifact
from pulpcore.plugin.serializers import (
IdentityField,
NestedIdentityField,
NoArtifactContentSerializer,
RemoteSerializer,
SingleArtifactContentSerializer,
)

from .models import AnsibleRemote, AnsibleRole, AnsibleRoleVersion


class AnsibleRoleSerializer(ContentSerializer):
class AnsibleRoleSerializer(NoArtifactContentSerializer):
"""
A serializer for Ansible Roles.
"""
Expand All @@ -30,7 +34,7 @@ class Meta:
model = AnsibleRole


class AnsibleRoleVersionSerializer(ContentSerializer):
class AnsibleRoleVersionSerializer(SingleArtifactContentSerializer):
"""
A serializer for Ansible Role versions.
"""
Expand All @@ -40,16 +44,10 @@ class AnsibleRoleVersionSerializer(ContentSerializer):
parent_lookup_kwargs={'role_pk': 'role__pk'},
)

artifact = RelatedField(
view_name='artifacts-detail',
help_text="Artifact file representing the physical content",
queryset=Artifact.objects.all()
)

version = serializers.CharField()

class Meta:
fields = ('_href', 'version', 'artifact')
fields = SingleArtifactContentSerializer.Meta.fields + ('version',)
model = AnsibleRoleVersion


Expand Down
18 changes: 16 additions & 2 deletions pulp_ansible/app/viewsets.py
Expand Up @@ -4,7 +4,7 @@
from rest_framework import mixins, status
from rest_framework.response import Response

from pulpcore.plugin.models import Artifact, RepositoryVersion, Publication
from pulpcore.plugin.models import Artifact, ContentArtifact, RepositoryVersion, Publication
from pulpcore.plugin.serializers import (
AsyncOperationResponseSerializer,
RepositoryPublishURLSerializer,
Expand Down Expand Up @@ -69,7 +69,21 @@ def create(self, request):
# TODO: we should probably remove create() from ContentViewSet
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)

artifact = serializer.validated_data.pop('_artifact')
content = serializer.save()

if content.pk:
ContentArtifact.objects.create(
artifact=artifact,
content=content,
relative_path="{namespace}/{name}/{version}.tar.gz".format(
namespace=content.role.namespace,
name=content.role.name,
version=content.version
)
)

headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

Expand Down
2 changes: 1 addition & 1 deletion pulp_ansible/tests/functional/utils.py
Expand Up @@ -67,7 +67,7 @@ def gen_ansible_content_attrs(artifact):
:returns: A semi-random dict for use in creating a content unit.
"""
# FIXME: add content specific metadata here
return {'artifact': artifact['_href']}
return {'_artifact': artifact['_href']}


def populate_pulp(cfg, url=ANSIBLE_FIXTURE_URL):
Expand Down

0 comments on commit 4376e5c

Please sign in to comment.