From 9361f3f2f01db60d99289418765ca2b590df6d2e Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Thu, 30 Jul 2020 15:07:30 +0200 Subject: [PATCH] Aliases should load really faster --- apps/api/viewsets.py | 12 ++++++++---- apps/note/api/views.py | 6 ++---- apps/permission/fixtures/initial.json | 4 ++-- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/apps/api/viewsets.py b/apps/api/viewsets.py index f7532beb..6e0cb6b8 100644 --- a/apps/api/viewsets.py +++ b/apps/api/viewsets.py @@ -14,9 +14,11 @@ class ReadProtectedModelViewSet(viewsets.ModelViewSet): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - model = ContentType.objects.get_for_model(self.serializer_class.Meta.model).model_class() + self.model = ContentType.objects.get_for_model(self.serializer_class.Meta.model).model_class() + + def get_queryset(self): user = get_current_authenticated_user() - self.queryset = model.objects.filter(PermissionBackend.filter_queryset(user, model, "view")) + return self.model.objects.filter(PermissionBackend.filter_queryset(user, self.model, "view")) class ReadOnlyProtectedModelViewSet(viewsets.ReadOnlyModelViewSet): @@ -26,6 +28,8 @@ class ReadOnlyProtectedModelViewSet(viewsets.ReadOnlyModelViewSet): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - model = ContentType.objects.get_for_model(self.serializer_class.Meta.model).model_class() + self.model = ContentType.objects.get_for_model(self.serializer_class.Meta.model).model_class() + + def get_queryset(self): user = get_current_authenticated_user() - self.queryset = model.objects.filter(PermissionBackend.filter_queryset(user, model, "view")) + return self.model.objects.filter(PermissionBackend.filter_queryset(user, self.model, "view")) diff --git a/apps/note/api/views.py b/apps/note/api/views.py index 22470461..8a1616ce 100644 --- a/apps/note/api/views.py +++ b/apps/note/api/views.py @@ -106,10 +106,8 @@ class ConsumerViewSet(ReadOnlyProtectedModelViewSet): queryset = super().get_queryset() alias = self.request.query_params.get("alias", ".*") - queryset = queryset.filter( - Q(name__regex="^" + alias) - | Q(normalized_name__regex="^" + Alias.normalize(alias)) - | Q(normalized_name__regex="^" + alias.lower())).order_by('name') + queryset = queryset.filter(normalized_name__iregex="^" + Alias.normalize(alias))\ + .order_by('name').prefetch_related('note') return queryset diff --git a/apps/permission/fixtures/initial.json b/apps/permission/fixtures/initial.json index 0391c095..c95776c5 100644 --- a/apps/permission/fixtures/initial.json +++ b/apps/permission/fixtures/initial.json @@ -111,7 +111,7 @@ "note", "alias" ], - "query": "[\"AND\", [\"OR\", {\"note__in\": [\"NoteUser\", \"objects\", [\"filter\", {\"user__memberships__club__name\": \"Kfet\"}], [\"all\"]]}, {\"note__in\": [\"NoteClub\", \"objects\", [\"all\"]]}], {\"note__is_active\": true}]", + "query": "[\"AND\", [\"OR\", {\"note__noteuser__user__memberships__club__name\": \"Kfet\", \"note__noteuser__user__memberships__date_start__lte\": [\"today\"], \"note__noteuser__user__memberships__date_end__gte\": [\"today\"]}, {\"note__noteclub__isnull\": false}], {\"note__is_active\": true}]", "type": "view", "mask": 1, "field": "", @@ -319,7 +319,7 @@ "note", "note" ], - "query": "[\"OR\", {\"pk\": [\"club\", \"note\", \"pk\"]}, {\"pk__in\": [\"NoteUser\", \"objects\", [\"filter\", {\"user__memberships__club\": [\"club\"]}], [\"all\"]]}]", + "query": "[\"OR\", {\"pk\": [\"club\", \"note\", \"pk\"]}, {\"noteuser__user__memberships__club\": [\"club\"], \"noteuser__user__memberships__date_start__lte\": [\"today\"], \"noteuser__user__memberships__date_end__gte\": [\"today\"]}]", "type": "view", "mask": 2, "field": "",