From c14741916c3f83dcefb0bb724569fac1f9ba2b3f Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Mon, 22 May 2023 04:29:44 -0500 Subject: [PATCH] Address multiple v2 API issues. Fixes #1452 (#1453) * Populate a context for use in v2 serializers * Add name to route to satisfy v2 get_latest_version href * Properly build download_url using reverse (cherry picked from commit 425a4b9f303f114f22491bf1b4e1e11c6f1587fb) --- CHANGES/1452.bugfix | 1 + pulp_ansible/app/galaxy/serializers.py | 2 +- pulp_ansible/app/galaxy/views.py | 24 +++++++++++++++++------- pulp_ansible/app/urls.py | 1 + 4 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 CHANGES/1452.bugfix diff --git a/CHANGES/1452.bugfix b/CHANGES/1452.bugfix new file mode 100644 index 000000000..83f9e7f0d --- /dev/null +++ b/CHANGES/1452.bugfix @@ -0,0 +1 @@ +Fixed several bugs in the galaxy v2 API related to generating URLs for various collection resources. diff --git a/pulp_ansible/app/galaxy/serializers.py b/pulp_ansible/app/galaxy/serializers.py index f07413c47..71818ee91 100644 --- a/pulp_ansible/app/galaxy/serializers.py +++ b/pulp_ansible/app/galaxy/serializers.py @@ -108,7 +108,7 @@ def get_latest_version(self, obj): """ rv = obj.versions.filter(is_highest=True).first() href = reverse( - "collection-versions-detail", + "v2-collection-versions-detail", kwargs={ "path": self.context["path"], "namespace": obj.namespace, diff --git a/pulp_ansible/app/galaxy/views.py b/pulp_ansible/app/galaxy/views.py index ef553c5e7..d48130bb8 100644 --- a/pulp_ansible/app/galaxy/views.py +++ b/pulp_ansible/app/galaxy/views.py @@ -4,6 +4,7 @@ from django.shortcuts import get_object_or_404, HttpResponse from drf_spectacular.utils import extend_schema, extend_schema_view from rest_framework import generics, pagination, response, views +from rest_framework.reverse import reverse from pulpcore.plugin.models import PulpTemporaryFile from pulpcore.plugin.viewsets import OperationPostponedResponse @@ -149,7 +150,8 @@ def get(self, request, path=None, namespace=None, name=None): """ # This seems wrong, no repository scoping occurring collection = get_object_or_404(Collection, namespace=namespace, name=name) - return response.Response(GalaxyCollectionSerializer(collection).data) + context = self.get_serializer_context() + return response.Response(GalaxyCollectionSerializer(collection, context=context).data) class GalaxyCollectionView(DistributionMixin, UploadGalaxyCollectionMixin, generics.ListAPIView): @@ -214,7 +216,7 @@ def get_queryset(self): return versions -class GalaxyCollectionVersionDetail(DistributionMixin, views.APIView): +class GalaxyCollectionVersionDetail(DistributionMixin, generics.GenericAPIView): """ APIView for Galaxy Collections Detail view. """ @@ -234,12 +236,20 @@ def get(self, request, path, namespace, name, version): ContentArtifact, content__in=self._distro_content, relative_path=version.relative_path ) - download_url = "{content_hostname}/{base_path}/{relative_path}".format( - content_hostname=settings.ANSIBLE_CONTENT_HOSTNAME, - base_path=self.kwargs["path"], - relative_path=version.relative_path, + # Normally would just pass request to reverse and DRF would automatically build the + # absolute URI for us. However there's a weird bug where, because there's a kwarg + # in the URL for this view called "version" (the collection version), DRF gets + # confused and thinks we're using a versioning scheme + # (https://www.django-rest-framework.org/api-guide/versioning/) and attempts to insert + # the version into kwargs, which causes the reverse lookup to fail. + path = reverse( + settings.ANSIBLE_URL_NAMESPACE + "collection-artifact-download", + kwargs={"distro_base_path": self.kwargs["path"], "filename": version.relative_path}, ) + context = self.get_serializer_context() + request = context["request"] + download_url = request.build_absolute_uri(path) - data = GalaxyCollectionVersionSerializer(version).data + data = GalaxyCollectionVersionSerializer(version, context=context).data data["download_url"] = download_url return response.Response(data) diff --git a/pulp_ansible/app/urls.py b/pulp_ansible/app/urls.py index 2a4721872..ba569a7a4 100644 --- a/pulp_ansible/app/urls.py +++ b/pulp_ansible/app/urls.py @@ -33,6 +33,7 @@ path( "collections///versions//", GalaxyCollectionVersionDetail.as_view(), + name="v2-collection-versions-detail", ), path( "collection-imports//",