Skip to content

Commit

Permalink
Add custom SemanticVersionOrderingFilter for version sorting
Browse files Browse the repository at this point in the history
fixes: #1516
  • Loading branch information
jerabekjiri committed Jul 17, 2023
1 parent f2904f4 commit 03d0761
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES/1516.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed version ordering in CollectionVersionSearchFilter
16 changes: 12 additions & 4 deletions pulp_ansible/app/galaxy/v3/filters.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from django.contrib.postgres.search import SearchQuery
from django.db.models import fields as db_fields
from django.db.models import Q
from django.db.models.expressions import F, Func
from django.db.models.expressions import F, Func, RawSQL
from django_filters import (
filters,
FilterSet,
OrderingFilter,
)
import semantic_version
from rest_framework.exceptions import ValidationError
Expand All @@ -17,6 +16,16 @@
from pulp_ansible.app import models


class SemanticVersionOrderingFilter(filters.OrderingFilter):
def filter(self, qs, value):
if value is not None and any(v in ["version", "-version"] for v in value):
return qs.annotate(
version_compare=RawSQL("string_to_array(version, '.')::int[]", [])
).order_by(f"{value[0]}_compare")

return super().filter(qs, value)


class CollectionVersionSearchFilter(FilterSet):
"""A custom filterset for cross-repo search."""

Expand Down Expand Up @@ -49,7 +58,7 @@ class CollectionVersionSearchFilter(FilterSet):
keywords = filters.CharFilter(field_name="q", method="filter_by_q")
repository_label = LabelFilter(label_field_name="repository__pulp_labels")

order_by = OrderingFilter(
order_by = SemanticVersionOrderingFilter(
choices=(
("pulp_created", "by CV created"),
("-pulp_created", "by CV created (descending)"),
Expand All @@ -64,7 +73,6 @@ class CollectionVersionSearchFilter(FilterSet):
"collection_version__pulp_created": "pulp_created",
"collection_version__namespace": "namespace",
"collection_version__name": "name",
"collection_version__version": "version",
},
)

Expand Down

0 comments on commit 03d0761

Please sign in to comment.