diff --git a/app/access/mixins/permissions.py b/app/access/mixins/permissions.py index 6853e4db5..36404c800 100644 --- a/app/access/mixins/permissions.py +++ b/app/access/mixins/permissions.py @@ -264,6 +264,7 @@ def has_permission(self, request, view): return False + _app_settings: any = None def has_object_permission(self, request, view, obj): @@ -276,11 +277,15 @@ def has_object_permission(self, request, view, obj): object_organization: int = getattr(view.get_obj_organization( obj = obj ), 'id', None) - from settings.models.app_settings import AppSettings + if not self._app_settings: - app_settings = AppSettings.objects.get( - owner_organization = None - ) + from settings.models.app_settings import AppSettings + + app_settings = AppSettings.objects.get( + owner_organization = None + ) + + self._app_settings = app_settings if object_organization: diff --git a/app/access/models.py b/app/access/models.py index 4334065aa..6a8b4c605 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,11 +181,17 @@ def get_queryset(self): if request: - from settings.models.app_settings import AppSettings + app_settings = self._app_settings - app_settings = AppSettings.objects.prefetch_related('global_organization').get( - owner_organization = None - ) + if not self._app_settings: + + 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: @@ -196,7 +204,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: