mirror of https://gitlab.crans.org/bde/nk20
Format api viewsets
This commit is contained in:
parent
ff187581c9
commit
d29e1d69d1
|
@ -4,6 +4,7 @@
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.conf.urls import url, include
|
from django.conf.urls import url, include
|
||||||
from rest_framework import routers
|
from rest_framework import routers
|
||||||
|
|
||||||
from .viewsets import ContentTypeViewSet, UserViewSet
|
from .viewsets import ContentTypeViewSet, UserViewSet
|
||||||
|
|
||||||
# Routers provide an easy way of automatically determining the URL conf.
|
# Routers provide an easy way of automatically determining the URL conf.
|
||||||
|
|
|
@ -64,28 +64,36 @@ class UserViewSet(ReadProtectedModelViewSet):
|
||||||
if "search" in self.request.GET:
|
if "search" in self.request.GET:
|
||||||
pattern = self.request.GET["search"]
|
pattern = self.request.GET["search"]
|
||||||
|
|
||||||
# We match first a user by its username, then if an alias is matched without normalization
|
# Filter with different rules
|
||||||
# And finally if the normalized pattern matches a normalized alias.
|
# We use union-all to keep each filter rule sorted in result
|
||||||
queryset = queryset.filter(
|
queryset = queryset.filter(
|
||||||
username__iregex="^" + pattern).union(
|
# Match without normalization
|
||||||
queryset.filter(
|
note__alias__name__iregex="^" + pattern
|
||||||
Q(note__alias__name__iregex="^" + pattern)
|
).union(
|
||||||
& ~Q(username__iregex="^" + pattern)), all=True).union(
|
|
||||||
queryset.filter(
|
queryset.filter(
|
||||||
|
# Match with normalization
|
||||||
Q(note__alias__normalized_name__iregex="^" + Alias.normalize(pattern))
|
Q(note__alias__normalized_name__iregex="^" + Alias.normalize(pattern))
|
||||||
& ~Q(note__alias__name__iregex="^" + pattern)
|
& ~Q(note__alias__name__iregex="^" + pattern)
|
||||||
& ~Q(username__iregex="^" + pattern)), all=True).union(
|
),
|
||||||
|
all=True,
|
||||||
|
).union(
|
||||||
queryset.filter(
|
queryset.filter(
|
||||||
|
# Match on lower pattern
|
||||||
Q(note__alias__normalized_name__iregex="^" + pattern.lower())
|
Q(note__alias__normalized_name__iregex="^" + pattern.lower())
|
||||||
& ~Q(note__alias__normalized_name__iregex="^" + Alias.normalize(pattern))
|
& ~Q(note__alias__normalized_name__iregex="^" + Alias.normalize(pattern))
|
||||||
& ~Q(note__alias__name__iregex="^" + pattern)
|
& ~Q(note__alias__name__iregex="^" + pattern)
|
||||||
& ~Q(username__iregex="^" + pattern)), all=True).union(
|
),
|
||||||
|
all=True,
|
||||||
|
).union(
|
||||||
queryset.filter(
|
queryset.filter(
|
||||||
|
# Match on firstname or lastname
|
||||||
(Q(last_name__iregex="^" + pattern) | Q(first_name__iregex="^" + pattern))
|
(Q(last_name__iregex="^" + pattern) | Q(first_name__iregex="^" + pattern))
|
||||||
& ~Q(note__alias__normalized_name__iregex="^" + pattern.lower())
|
& ~Q(note__alias__normalized_name__iregex="^" + pattern.lower())
|
||||||
& ~Q(note__alias__normalized_name__iregex="^" + Alias.normalize(pattern))
|
& ~Q(note__alias__normalized_name__iregex="^" + Alias.normalize(pattern))
|
||||||
& ~Q(note__alias__name__iregex="^" + pattern)
|
& ~Q(note__alias__name__iregex="^" + pattern)
|
||||||
& ~Q(username__iregex="^" + pattern)), all=True)
|
),
|
||||||
|
all=True,
|
||||||
|
)
|
||||||
|
|
||||||
queryset = queryset if settings.DATABASES[queryset.db]["ENGINE"] == 'django.db.backends.postgresql' \
|
queryset = queryset if settings.DATABASES[queryset.db]["ENGINE"] == 'django.db.backends.postgresql' \
|
||||||
else queryset.order_by("username")
|
else queryset.order_by("username")
|
||||||
|
|
Loading…
Reference in New Issue