Skip to content

Commit 46f28fc

Browse files
committed
(Autocomplétion) Amélioration de la recherche d'utilisateurs
1 parent c011dbf commit 46f28fc

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

zds/member/api/views.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from django.utils.translation import gettext_lazy as _
55
from django.core.cache import cache
6+
from django.db.models import Case, When, IntegerField, Value
67
from django.db.models.signals import post_save, post_delete
78
from dry_rest_permissions.generics import DRYPermissions
89
from rest_framework import filters
@@ -81,12 +82,30 @@ class MemberListAPI(ListCreateAPIView, ProfileCreate, TokenGenerator):
8182
Profile resource to list and register.
8283
"""
8384

84-
filter_backends = (filters.SearchFilter,)
85-
search_fields = ("user__username",)
8685
list_key_func = PagingSearchListKeyConstructor()
8786

8887
def get_queryset(self):
89-
return Profile.objects.contactable_members()
88+
queryset = Profile.objects.contactable_members()
89+
search_param = self.request.query_params.get("search", None)
90+
91+
if search_param:
92+
queryset = (
93+
queryset.filter(user__username__icontains=search_param)
94+
.annotate(
95+
priority=Case(
96+
When(user__username=search_param, then=Value(1)),
97+
When(user__username__iexact=search_param, then=Value(2)),
98+
When(user__username__startswith=search_param, then=Value(3)),
99+
When(user__username__istartswith=search_param, then=Value(4)),
100+
When(user__username__contains=search_param, then=Value(5)),
101+
default=Value(6),
102+
output_field=IntegerField(),
103+
)
104+
)
105+
.order_by("priority", "user__username")
106+
)
107+
108+
return queryset
90109

91110
@etag(list_key_func)
92111
@cache_response(key_func=list_key_func)

0 commit comments

Comments
 (0)