-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix wrong version ordering in x-repo search #1517
Changes from 7 commits
03d0761
0694733
478463b
d99bc80
18d0f34
f95d445
23d08fa
46deeb5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fixed ordering by version in the ``/ansible/search/collection-versions/`` endpoint. |
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 fields as db_fields, Case, When, Value | ||
from django.db.models import Q | ||
from django.db.models.expressions import F, Func | ||
from django_filters import ( | ||
filters, | ||
FilterSet, | ||
OrderingFilter, | ||
) | ||
import semantic_version | ||
from rest_framework.exceptions import ValidationError | ||
|
@@ -17,6 +16,26 @@ | |
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): | ||
order = "-" if "-version" in value else "" | ||
|
||
return qs.annotate( | ||
prerelease=Case( | ||
When(collection_version__version_prerelease="", then=Value(None)), | ||
default="collection_version__version_prerelease", | ||
), | ||
).order_by( | ||
f"{order}collection_version__version_major", | ||
f"{order}collection_version__version_minor", | ||
f"{order}collection_version__version_patch", | ||
f"{order}prerelease", | ||
) | ||
|
||
return super().filter(qs, value) | ||
|
||
|
||
class CollectionVersionSearchFilter(FilterSet): | ||
"""A custom filterset for cross-repo search.""" | ||
|
||
|
@@ -49,7 +68,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)"), | ||
|
@@ -64,7 +83,6 @@ class CollectionVersionSearchFilter(FilterSet): | |
"collection_version__pulp_created": "pulp_created", | ||
"collection_version__namespace": "namespace", | ||
"collection_version__name": "name", | ||
"collection_version__version": "version", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this change doing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, since we have a custom There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right, now that i can see it on the large screen... |
||
}, | ||
) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like these happen to be alright in alphabetical order.