Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: only prefetch related objects when required (cherry-pick #9476) #9510

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion authentik/core/api/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,18 @@ class UserAccountSerializer(PassiveSerializer):

pk = IntegerField(required=True)

queryset = Group.objects.all().select_related("parent").prefetch_related("users")
queryset = Group.objects.none()
serializer_class = GroupSerializer
search_fields = ["name", "is_superuser"]
filterset_class = GroupFilter
ordering = ["name"]

def get_queryset(self):
base_qs = Group.objects.all().select_related("parent").prefetch_related("roles")
if self.serializer_class(context={"request": self.request})._should_include_users:
base_qs = base_qs.prefetch_related("users")
return base_qs

@extend_schema(
parameters=[
OpenApiParameter("include_users", bool, default=True),
Expand Down
7 changes: 5 additions & 2 deletions authentik/core/api/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,11 @@ class UserViewSet(UsedByMixin, ModelViewSet):
search_fields = ["username", "name", "is_active", "email", "uuid"]
filterset_class = UsersFilter

def get_queryset(self): # pragma: no cover
return User.objects.all().exclude_anonymous().prefetch_related("ak_groups")
def get_queryset(self):
base_qs = User.objects.all().exclude_anonymous()
if self.serializer_class(context={"request": self.request})._should_include_groups:
base_qs = base_qs.prefetch_related("ak_groups")
return base_qs

@extend_schema(
parameters=[
Expand Down
9 changes: 8 additions & 1 deletion authentik/core/tests/test_groups_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from rest_framework.test import APITestCase

from authentik.core.models import Group, User
from authentik.core.tests.utils import create_test_user
from authentik.core.tests.utils import create_test_admin_user, create_test_user

Check warning on line 8 in authentik/core/tests/test_groups_api.py

View check run for this annotation

Codecov / codecov/patch

authentik/core/tests/test_groups_api.py#L8

Added line #L8 was not covered by tests
from authentik.lib.generators import generate_id


Expand All @@ -16,6 +16,13 @@
self.login_user = create_test_user()
self.user = User.objects.create(username="test-user")

def test_list_with_users(self):

Check warning on line 19 in authentik/core/tests/test_groups_api.py

View check run for this annotation

Codecov / codecov/patch

authentik/core/tests/test_groups_api.py#L19

Added line #L19 was not covered by tests
"""Test listing with users"""
admin = create_test_admin_user()
self.client.force_login(admin)
response = self.client.get(reverse("authentik_api:group-list"), {"include_users": "true"})
self.assertEqual(response.status_code, 200)

Check warning on line 24 in authentik/core/tests/test_groups_api.py

View check run for this annotation

Codecov / codecov/patch

authentik/core/tests/test_groups_api.py#L21-L24

Added lines #L21 - L24 were not covered by tests

def test_add_user(self):
"""Test add_user"""
group = Group.objects.create(name=generate_id())
Expand Down
6 changes: 6 additions & 0 deletions authentik/core/tests/test_users_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
)
self.assertEqual(response.status_code, 200)

def test_list_with_groups(self):

Check warning on line 44 in authentik/core/tests/test_users_api.py

View check run for this annotation

Codecov / codecov/patch

authentik/core/tests/test_users_api.py#L44

Added line #L44 was not covered by tests
"""Test listing with groups"""
self.client.force_login(self.admin)
response = self.client.get(reverse("authentik_api:user-list"), {"include_groups": "true"})
self.assertEqual(response.status_code, 200)

Check warning on line 48 in authentik/core/tests/test_users_api.py

View check run for this annotation

Codecov / codecov/patch

authentik/core/tests/test_users_api.py#L46-L48

Added lines #L46 - L48 were not covered by tests

def test_metrics(self):
"""Test user's metrics"""
self.client.force_login(self.admin)
Expand Down
2 changes: 0 additions & 2 deletions authentik/core/tests/test_users_avatars.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from authentik.core.models import User
from authentik.core.tests.utils import create_test_admin_user
from authentik.lib.config import CONFIG
from authentik.tenants.utils import get_current_tenant


Expand All @@ -25,7 +24,6 @@ def set_avatar_mode(self, mode: str):
tenant.avatars = mode
tenant.save()

@CONFIG.patch("avatars", "none")
def test_avatars_none(self):
"""Test avatars none"""
self.set_avatar_mode("none")
Expand Down
2 changes: 1 addition & 1 deletion authentik/events/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ def get_user(self, request: HttpRequest) -> User:
return user
user = getattr(request, "user", self.anonymous_user)
if not user.is_authenticated:
self._ensure_fallback_user()
return self.anonymous_user
return user

def connect(self, request: HttpRequest):
"""Connect signal for automatic logging"""
self._ensure_fallback_user()
if not hasattr(request, "request_id"):
return
post_save.connect(
Expand Down
Loading