Skip to content

Commit

Permalink
New endpoint - all collection versions unpaginated
Browse files Browse the repository at this point in the history
  • Loading branch information
fao89 committed Jan 27, 2021
1 parent f353ca0 commit 0db524e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES/7941.feature
@@ -0,0 +1,2 @@
Introduces a new ``v3/metadata/collection_versions/`` endpoint returning all collections versions
unpaginated.
4 changes: 2 additions & 2 deletions pulp_ansible/app/galaxy/v3/serializers.py
Expand Up @@ -182,15 +182,15 @@ def get_artifact(self, obj):
"""
Get atrifact summary.
"""
return ArtifactRefSerializer(self.context["content_artifact"]).data
return ArtifactRefSerializer(obj.contentartifact_set.get()).data

def get_download_url(self, obj) -> str:
"""
Get artifact download URL.
"""
host = settings.ANSIBLE_CONTENT_HOSTNAME.strip("/")
distro_base_path = self.context["path"]
filename_path = self.context["content_artifact"].relative_path.lstrip("/")
filename_path = obj.contentartifact_set.get().relative_path.lstrip("/")
download_url = f"{host}/{distro_base_path}/{filename_path}"
return download_url

Expand Down
42 changes: 38 additions & 4 deletions pulp_ansible/app/galaxy/v3/views.py
Expand Up @@ -17,7 +17,7 @@
from rest_framework import viewsets

from pulpcore.plugin.exceptions import DigestValidationError
from pulpcore.plugin.models import PulpTemporaryFile, Content, ContentArtifact
from pulpcore.plugin.models import PulpTemporaryFile, Content
from pulpcore.plugin.serializers import AsyncOperationResponseSerializer

from pulp_ansible.app.galaxy.v3.exceptions import ExceptionHandlerMixin
Expand Down Expand Up @@ -90,7 +90,9 @@ def get_queryset(self):
"""
distro_content = self.get_distro_content(self.kwargs["path"])

collections = CollectionVersion.objects.select_related("collection").filter(
collections = CollectionVersion.objects.select_related(
"content_ptr__contentartifact"
).filter(
pk__in=distro_content, namespace=self.kwargs["namespace"], name=self.kwargs["name"]
)
return collections
Expand All @@ -100,10 +102,8 @@ def retrieve(self, request, *args, **kwargs):
Returns a CollectionVersion object.
"""
instance = self.get_object()
artifact = ContentArtifact.objects.get(content=instance)

context = self.get_serializer_context()
context["content_artifact"] = artifact

serializer = self.get_serializer_class()(instance, context=context)

Expand Down Expand Up @@ -328,6 +328,40 @@ def list(self, request, *args, **kwargs):
return Response(serializer.data)


class UnpaginatedCollectionVersionViewSet(CollectionVersionViewSet):
"""Unpaginated ViewSet for CollectionVersions."""

pagination_class = None

def get_queryset(self):
"""
Returns a CollectionVersions queryset for specified distribution.
"""
distro_content = self.get_distro_content(self.kwargs["path"])

return CollectionVersion.objects.select_related("content_ptr__contentartifact").filter(
pk__in=distro_content
)

def list(self, request, *args, **kwargs):
"""
Returns paginated CollectionVersions list.
"""
queryset = self.filter_queryset(self.get_queryset())
queryset = sorted(
queryset, key=lambda obj: semantic_version.Version(obj.version), reverse=True
)

context = self.get_serializer_context()
page = self.paginate_queryset(queryset)
if page is not None:
serializer = CollectionVersionSerializer(page, many=True, context=context)
return self.get_paginated_response(serializer.data)

serializer = CollectionVersionSerializer(queryset, many=True, context=context)
return Response(serializer.data)


class CollectionVersionDocsViewSet(
CollectionVersionRetrieveMixin,
ExceptionHandlerMixin,
Expand Down
5 changes: 5 additions & 0 deletions pulp_ansible/app/urls.py
Expand Up @@ -73,6 +73,11 @@
views_v3.CollectionImportViewSet.as_view({"get": "retrieve"}),
name="collection-imports-detail",
),
path(
"metadata/collection_versions/",
views_v3.UnpaginatedCollectionVersionViewSet.as_view({"get": "list"}),
name="metadata-collection-versions-list",
),
]

urlpatterns = [
Expand Down

0 comments on commit 0db524e

Please sign in to comment.