From d29e1d69d18e323c8b35e4639d253622d1014851 Mon Sep 17 00:00:00 2001 From: Alexandre Iooss Date: Thu, 3 Sep 2020 21:47:08 +0200 Subject: [PATCH] Format api viewsets --- apps/api/urls.py | 1 + apps/api/viewsets.py | 50 +++++++++++++++++++++++++------------------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/apps/api/urls.py b/apps/api/urls.py index 4addbdf1..7131c657 100644 --- a/apps/api/urls.py +++ b/apps/api/urls.py @@ -4,6 +4,7 @@ from django.conf import settings from django.conf.urls import url, include from rest_framework import routers + from .viewsets import ContentTypeViewSet, UserViewSet # Routers provide an easy way of automatically determining the URL conf. diff --git a/apps/api/viewsets.py b/apps/api/viewsets.py index f7d1e481..333ae5e3 100644 --- a/apps/api/viewsets.py +++ b/apps/api/viewsets.py @@ -64,28 +64,36 @@ class UserViewSet(ReadProtectedModelViewSet): if "search" in self.request.GET: pattern = self.request.GET["search"] - # We match first a user by its username, then if an alias is matched without normalization - # And finally if the normalized pattern matches a normalized alias. + # Filter with different rules + # We use union-all to keep each filter rule sorted in result queryset = queryset.filter( - username__iregex="^" + pattern).union( - queryset.filter( - Q(note__alias__name__iregex="^" + pattern) - & ~Q(username__iregex="^" + pattern)), all=True).union( - queryset.filter( - Q(note__alias__normalized_name__iregex="^" + Alias.normalize(pattern)) - & ~Q(note__alias__name__iregex="^" + pattern) - & ~Q(username__iregex="^" + pattern)), all=True).union( - queryset.filter( - Q(note__alias__normalized_name__iregex="^" + pattern.lower()) - & ~Q(note__alias__normalized_name__iregex="^" + Alias.normalize(pattern)) - & ~Q(note__alias__name__iregex="^" + pattern) - & ~Q(username__iregex="^" + pattern)), all=True).union( - queryset.filter( - (Q(last_name__iregex="^" + pattern) | Q(first_name__iregex="^" + pattern)) - & ~Q(note__alias__normalized_name__iregex="^" + pattern.lower()) - & ~Q(note__alias__normalized_name__iregex="^" + Alias.normalize(pattern)) - & ~Q(note__alias__name__iregex="^" + pattern) - & ~Q(username__iregex="^" + pattern)), all=True) + # Match without normalization + note__alias__name__iregex="^" + pattern + ).union( + queryset.filter( + # Match with normalization + Q(note__alias__normalized_name__iregex="^" + Alias.normalize(pattern)) + & ~Q(note__alias__name__iregex="^" + pattern) + ), + all=True, + ).union( + queryset.filter( + # Match on lower pattern + Q(note__alias__normalized_name__iregex="^" + pattern.lower()) + & ~Q(note__alias__normalized_name__iregex="^" + Alias.normalize(pattern)) + & ~Q(note__alias__name__iregex="^" + pattern) + ), + all=True, + ).union( + queryset.filter( + # Match on firstname or lastname + (Q(last_name__iregex="^" + pattern) | Q(first_name__iregex="^" + pattern)) + & ~Q(note__alias__normalized_name__iregex="^" + pattern.lower()) + & ~Q(note__alias__normalized_name__iregex="^" + Alias.normalize(pattern)) + & ~Q(note__alias__name__iregex="^" + pattern) + ), + all=True, + ) queryset = queryset if settings.DATABASES[queryset.db]["ENGINE"] == 'django.db.backends.postgresql' \ else queryset.order_by("username")