From 4e8bb883b8a254a89f4bd325d3119757f4022fb0 Mon Sep 17 00:00:00 2001 From: Jon Date: Fri, 17 Jan 2025 01:30:37 +0930 Subject: [PATCH] refactor(access): cache app settings during model fetch ref: #469 #471 --- app/access/models.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/app/access/models.py b/app/access/models.py index 4334065aa..526df85ad 100644 --- a/app/access/models.py +++ b/app/access/models.py @@ -143,6 +143,8 @@ class TenancyManager(models.Manager): This manager specifically caters for the multi-tenancy features of Centurion ERP. """ + _app_settings: any = None + def get_queryset(self): """ Fetch the data @@ -179,15 +181,19 @@ def get_queryset(self): if request: - from settings.models.app_settings import AppSettings + if not self._app_settings: - app_settings = AppSettings.objects.prefetch_related('global_organization').get( - owner_organization = None - ) + from settings.models.app_settings import AppSettings + + app_settings = AppSettings.objects.prefetch_related('global_organization').get( + owner_organization = None + ) + + self._app_settings = app_settings - if app_settings.global_organization: + if self._app_settings.global_organization: - user_organizations += [ app_settings.global_organization.id ] + user_organizations += [ self._app_settings.global_organization.id ] # user = request.user._wrapped if hasattr(request.user,'_wrapped') else request.user @@ -196,7 +202,7 @@ def get_queryset(self): if user.is_authenticated: - for team_user in TeamUsers.objects.filter(user=user): + for team_user in TeamUsers.objects.filter(user=user).prefetch_related('team__organization'): if team_user.team.organization.name not in user_organizations: