|
3 | 3 |
|
4 | 4 | from django.utils.translation import gettext_lazy as _
|
5 | 5 | from django.core.cache import cache
|
| 6 | +from django.db.models import Case, When, IntegerField, Value |
6 | 7 | from django.db.models.signals import post_save, post_delete
|
7 | 8 | from dry_rest_permissions.generics import DRYPermissions
|
8 | 9 | from rest_framework import filters
|
@@ -81,12 +82,30 @@ class MemberListAPI(ListCreateAPIView, ProfileCreate, TokenGenerator):
|
81 | 82 | Profile resource to list and register.
|
82 | 83 | """
|
83 | 84 |
|
84 |
| - filter_backends = (filters.SearchFilter,) |
85 |
| - search_fields = ("user__username",) |
86 | 85 | list_key_func = PagingSearchListKeyConstructor()
|
87 | 86 |
|
88 | 87 | 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 |
90 | 109 |
|
91 | 110 | @etag(list_key_func)
|
92 | 111 | @cache_response(key_func=list_key_func)
|
|
0 commit comments