Skip to content

Commit

Permalink
Fix is_highest query
Browse files Browse the repository at this point in the history
  • Loading branch information
fao89 committed Sep 21, 2020
1 parent 2dead70 commit 4daa91d
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions pulp_ansible/app/galaxy/v3/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from gettext import gettext as _
import semantic_version

from django.db.models import Q
from django.shortcuts import get_object_or_404
from django.utils.dateparse import parse_datetime
from drf_spectacular.utils import OpenApiParameter, extend_schema
Expand Down Expand Up @@ -113,17 +114,23 @@ def get_queryset(self):
Returns a CollectionVersions queryset for specified distribution.
"""
distro_content = self.get_distro_content(self.kwargs["path"])
query_params = {"pk__in": distro_content}

versions = CollectionVersion.objects.filter(pk__in=distro_content).values_list(
"version", flat=True
"collection_id",
"version",
)

if len(versions):
highest = sorted(versions, key=semantic_version.Version, reverse=True)[0]
query_params["version"] = highest
collection_versions = {}
for collection_id, version in versions:
value = collection_versions.get(str(collection_id))
if not value or semantic_version.Version(version) > semantic_version.Version(value):
collection_versions[str(collection_id)] = version

collections = CollectionVersion.objects.select_related("collection").filter(**query_params)
query_params = Q()
for collection_id, version in collection_versions.items():
query_params |= Q(collection_id=collection_id, version=version)

collections = CollectionVersion.objects.select_related("collection").filter(query_params)
return collections

def get_object(self):
Expand Down

0 comments on commit 4daa91d

Please sign in to comment.