Skip to content

Commit

Permalink
Address multiple v2 API issues. Fixes #1452 (#1453)
Browse files Browse the repository at this point in the history
* 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 425a4b9)
  • Loading branch information
sivel authored and patchback[bot] committed May 25, 2023
1 parent a3bd5e7 commit c147419
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES/1452.bugfix
@@ -0,0 +1 @@
Fixed several bugs in the galaxy v2 API related to generating URLs for various collection resources.
2 changes: 1 addition & 1 deletion pulp_ansible/app/galaxy/serializers.py
Expand Up @@ -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,
Expand Down
24 changes: 17 additions & 7 deletions pulp_ansible/app/galaxy/views.py
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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.
"""
Expand All @@ -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)
1 change: 1 addition & 0 deletions pulp_ansible/app/urls.py
Expand Up @@ -33,6 +33,7 @@
path(
"collections/<str:namespace>/<str:name>/versions/<str:version>/",
GalaxyCollectionVersionDetail.as_view(),
name="v2-collection-versions-detail",
),
path(
"collection-imports/<uuid:pk>/",
Expand Down

0 comments on commit c147419

Please sign in to comment.