Skip to content

Commit

Permalink
Use new specialized serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
dralley committed Feb 1, 2019
1 parent a7dc791 commit d503518
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 71 deletions.
5 changes: 2 additions & 3 deletions docs/workflows/upload.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ Create ``rpm`` content from an Artifact

Create a content unit and point it to your artifact

``$ http POST http://localhost:8000/pulp/api/v3/content/rpm/packages/ relative_path=foo.rpm artifact="/pulp/api/v3/artifacts/1/" filename=foo-4.1-1.noarch.rpm``
``$ http POST http://localhost:8000/pulp/api/v3/content/rpm/packages/ relative_path=foo.rpm _artifact="/pulp/api/v3/artifacts/1/" filename=foo-4.1-1.noarch.rpm``

.. code:: json
{
"_href": "/pulp/api/v3/content/rpm/packages/36/",
"artifact": "/pulp/api/v3/artifacts/1/",
"_artifact": "/pulp/api/v3/artifacts/1/",
"relative_path": "foo.rpm",
"type": "rpm"
}
``$ export CONTENT_HREF=$(http :8000/pulp/api/v3/content/rpm/packages/ | jq -r '.results[] | select( .relative_path == "foo.rpm") | ._href')``
Expand Down
4 changes: 2 additions & 2 deletions docs/workflows/use_pulp_repo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ client tools to a repo distributed by Pulp.
Download ``foo.rpm`` from Pulp
------------------------------

``$ http GET http://localhost:8000/pulp/content/foo/foo.rpm``
``$ http GET http://localhost:8080/pulp/content/foo/foo.rpm``

Install a package from Pulp
---------------------------
Expand All @@ -19,7 +19,7 @@ Open /etc/yum.repos.d/foo.repo and add the following:
[foo]
name = foo
baseurl = http://localhost:8080/pulp/content/foo
baseurl = http://localhost:8080/pulp/content/foo/
gpgcheck = 0
Expand Down
20 changes: 1 addition & 19 deletions pulp_rpm/app/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from logging import getLogger

from django.db import models
from pulpcore.plugin.models import Content, ContentArtifact, Remote, Publisher
from pulpcore.plugin.models import Content, Remote, Publisher

from pulp_rpm.app.constants import (CHECKSUM_CHOICES, CREATEREPO_PACKAGE_ATTRS,
CREATEREPO_UPDATE_COLLECTION_ATTRS,
Expand Down Expand Up @@ -177,24 +177,6 @@ class Package(Content):
time_build = models.BigIntegerField(null=True)
time_file = models.BigIntegerField(null=True)

@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 FileContent.
"""
if self.pk:
ca = ContentArtifact(artifact=artifact,
content=self,
relative_path=self.filename)
ca.save()

@property
def filename(self):
"""
Expand Down
47 changes: 8 additions & 39 deletions pulp_rpm/app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,24 @@

from rest_framework import serializers

from pulpcore.plugin.models import Artifact, ContentArtifact
from pulpcore.plugin.serializers import (
ContentSerializer,
NoArtifactContentSerializer,
SingleArtifactContentSerializer,
RemoteSerializer,
RelatedField,
PublisherSerializer,
)

from pulp_rpm.app.models import Package, RpmRemote, RpmPublisher, UpdateRecord


class PackageSerializer(ContentSerializer):
class PackageSerializer(SingleArtifactContentSerializer):
"""
A Serializer for Package.
Add serializers for the new fields defined in Package and add those fields to the Meta class
keeping fields from the parent class as well. Provide help_text.
"""

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

name = serializers.CharField(
help_text=_("Name of the package"),
)
Expand Down Expand Up @@ -164,32 +157,8 @@ class PackageSerializer(ContentSerializer):
"file mtime in seconds since the epoch.")
)

def create(self, validated_data):
"""
Create a Package.
Overriding default create() to deal with artifact properly.
Args:
validated_data (dict): Data used to create the Package
Returns:
models.Package: The created Package
"""
artifact = validated_data.pop('artifact')

package = Package.objects.create(**validated_data)
ca = ContentArtifact(artifact=artifact,
content=package,
relative_path=package.filename)
ca.save()

return package

class Meta:
fields = tuple(set(ContentSerializer.Meta.fields) - {'_artifacts'}) + (
'artifact',
fields = SingleArtifactContentSerializer.Meta.fields + (
'name', 'epoch', 'version', 'release', 'arch', 'pkgId', 'checksum_type',
'summary', 'description', 'url', 'changelogs', 'files',
'requires', 'provides', 'conflicts', 'obsoletes',
Expand All @@ -210,7 +179,7 @@ class MinimalPackageSerializer(PackageSerializer):
"""

class Meta:
fields = ContentSerializer.Meta.fields + (
fields = SingleArtifactContentSerializer.Meta.fields + (
'name', 'epoch', 'version', 'release', 'arch', 'pkgId', 'checksum_type',
)
model = Package
Expand All @@ -236,7 +205,7 @@ class Meta:
model = RpmPublisher


class UpdateRecordSerializer(ContentSerializer):
class UpdateRecordSerializer(NoArtifactContentSerializer):
"""
A Serializer for UpdateRecord.
"""
Expand Down Expand Up @@ -291,7 +260,7 @@ class UpdateRecordSerializer(ContentSerializer):
)

class Meta:
fields = ContentSerializer.Meta.fields + (
fields = NoArtifactContentSerializer.Meta.fields + (
'id', 'updated_date', 'description', 'issued_date',
'fromstr', 'status', 'title', 'summary', 'version',
'type', 'severity', 'solution', 'release', 'rights',
Expand All @@ -306,7 +275,7 @@ class MinimalUpdateRecordSerializer(UpdateRecordSerializer):
"""

class Meta:
fields = ContentSerializer.Meta.fields + (
fields = NoArtifactContentSerializer.Meta.fields + (
'id', 'title', 'severity', 'type'
)
model = UpdateRecord
18 changes: 13 additions & 5 deletions pulp_rpm/app/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from rest_framework.decorators import detail_route
from rest_framework.response import Response

from pulpcore.plugin.models import Artifact, RepositoryVersion
from pulpcore.plugin.models import Artifact, ContentArtifact, RepositoryVersion
from pulpcore.plugin.tasking import enqueue_with_reservation
from pulpcore.plugin.serializers import (
AsyncOperationResponseSerializer,
Expand Down Expand Up @@ -79,9 +79,9 @@ def create(self, request):
Create a new Package from a request.
"""
try:
artifact = self.get_resource(request.data['artifact'], Artifact)
artifact = self.get_resource(request.data['_artifact'], Artifact)
except KeyError:
raise serializers.ValidationError(detail={'artifact': _('This field is required')})
raise serializers.ValidationError(detail={'_artifact': _('This field is required')})

try:
filename = request.data['filename']
Expand All @@ -96,11 +96,12 @@ def create(self, request):
package = Package.createrepo_to_dict(cr_pkginfo)

package['location_href'] = filename
package['artifact'] = request.data['artifact']

# TODO: Clean this up, maybe make a new function for the purpose of parsing it into
# a saveable format
new_pkg = {}
new_pkg['_artifact'] = request.data['_artifact']

for key, value in package.items():
if isinstance(value, list):
new_pkg[key] = json.dumps(value)
Expand All @@ -109,7 +110,14 @@ def create(self, request):

serializer = self.get_serializer(data=new_pkg)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
serializer.validated_data.pop('_artifact')
package = serializer.save()
if package.pk:
ContentArtifact.objects.create(
artifact=artifact,
content=package,
relative_path=package.filename
)

headers = self.get_success_headers(request.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
Expand Down
2 changes: 1 addition & 1 deletion pulp_rpm/tests/functional/api/test_character_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def do_test(self, url, filename):
files = {'file': utils.http_get(url)}
artifact = self.client.post(ARTIFACTS_PATH, files=files)
content_unit = self.client.post(RPM_CONTENT_PATH, {
'artifact': artifact['_href'],
'_artifact': artifact['_href'],
'filename': filename
})
repo = self.client.post(REPO_PATH, gen_repo())
Expand Down
4 changes: 2 additions & 2 deletions pulp_rpm/tests/functional/api/test_crud_content_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_01_create_content_unit(self):
"""Create content unit."""
self.content_unit.update(
self.client.post(RPM_CONTENT_PATH, {
'artifact': self.artifact['_href'],
'_artifact': self.artifact['_href'],
'filename': RPM_PACKAGE_FILENAME
})
)
Expand Down Expand Up @@ -136,7 +136,7 @@ def test_raise_error(self):
files = {'file': utils.http_get(RPM_UNSIGNED_URL)}
artifact = self.client.post(ARTIFACTS_PATH, files=files)
attrs = {
'artifact': artifact['_href'],
'_artifact': artifact['_href'],
'filename': RPM_PACKAGE_FILENAME
}

Expand Down

0 comments on commit d503518

Please sign in to comment.