diff --git a/docs/workflows/upload.rst b/docs/workflows/upload.rst index d7cb9d707..3c7a26139 100644 --- a/docs/workflows/upload.rst +++ b/docs/workflows/upload.rst @@ -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')`` diff --git a/docs/workflows/use_pulp_repo.rst b/docs/workflows/use_pulp_repo.rst index a42e7cd07..1e6c6e97d 100644 --- a/docs/workflows/use_pulp_repo.rst +++ b/docs/workflows/use_pulp_repo.rst @@ -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 --------------------------- @@ -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 diff --git a/pulp_rpm/app/models.py b/pulp_rpm/app/models.py index bf5883292..ea2a219a1 100644 --- a/pulp_rpm/app/models.py +++ b/pulp_rpm/app/models.py @@ -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, @@ -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): """ diff --git a/pulp_rpm/app/serializers.py b/pulp_rpm/app/serializers.py index 90bc0a762..b6d900e6e 100644 --- a/pulp_rpm/app/serializers.py +++ b/pulp_rpm/app/serializers.py @@ -2,18 +2,17 @@ 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. @@ -21,12 +20,6 @@ class PackageSerializer(ContentSerializer): 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"), ) @@ -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', @@ -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 @@ -236,7 +205,7 @@ class Meta: model = RpmPublisher -class UpdateRecordSerializer(ContentSerializer): +class UpdateRecordSerializer(NoArtifactContentSerializer): """ A Serializer for UpdateRecord. """ @@ -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', @@ -306,7 +275,7 @@ class MinimalUpdateRecordSerializer(UpdateRecordSerializer): """ class Meta: - fields = ContentSerializer.Meta.fields + ( + fields = NoArtifactContentSerializer.Meta.fields + ( 'id', 'title', 'severity', 'type' ) model = UpdateRecord diff --git a/pulp_rpm/app/viewsets.py b/pulp_rpm/app/viewsets.py index 967222aec..43ca308fd 100644 --- a/pulp_rpm/app/viewsets.py +++ b/pulp_rpm/app/viewsets.py @@ -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, @@ -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'] @@ -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) @@ -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) diff --git a/pulp_rpm/tests/functional/api/test_character_encoding.py b/pulp_rpm/tests/functional/api/test_character_encoding.py index 29c4968f9..f7bafcd6b 100644 --- a/pulp_rpm/tests/functional/api/test_character_encoding.py +++ b/pulp_rpm/tests/functional/api/test_character_encoding.py @@ -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()) diff --git a/pulp_rpm/tests/functional/api/test_crud_content_unit.py b/pulp_rpm/tests/functional/api/test_crud_content_unit.py index e3ec6050f..5504635fc 100644 --- a/pulp_rpm/tests/functional/api/test_crud_content_unit.py +++ b/pulp_rpm/tests/functional/api/test_crud_content_unit.py @@ -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 }) ) @@ -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 }