-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! ✨(backend) allow to filter member on team access endpoint
- Loading branch information
Showing
2 changed files
with
41 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Filters for viewsets.""" | ||
|
||
from django.contrib.postgres.search import TrigramSimilarity | ||
from django.db.models import Func, Max, Value | ||
from django.db.models.functions import Coalesce | ||
|
||
SIMILARITY_THRESHOLD = 0.04 | ||
|
||
|
||
def get_user_queryset(queryset, search_field, query): | ||
""" | ||
Filters user queryset by case-insensitive and accent-insensitive trigram similarity | ||
""" | ||
|
||
similarity = Max( | ||
TrigramSimilarity( | ||
Coalesce(Func(f"{search_field}__email", function="unaccent"), Value("")), | ||
Func(Value(query), function="unaccent"), | ||
) | ||
+ TrigramSimilarity( | ||
Coalesce(Func(f"{search_field}__name", function="unaccent"), Value("")), | ||
Func(Value(query), function="unaccent"), | ||
) | ||
) | ||
queryset = ( | ||
queryset.annotate(similarity=similarity) | ||
.filter(similarity__gte=SIMILARITY_THRESHOLD) | ||
.order_by("-similarity") | ||
) | ||
return queryset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters