Skip to content

Commit

Permalink
refactor(access): cache app settings
Browse files Browse the repository at this point in the history
ref: #469 #471
  • Loading branch information
jon-nfc committed Jan 16, 2025
1 parent 033ac30 commit e4ecc07
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
13 changes: 9 additions & 4 deletions app/access/mixins/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ def has_permission(self, request, view):
return False


_app_settings: any = None

def has_object_permission(self, request, view, obj):

Expand All @@ -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:

Expand Down
18 changes: 13 additions & 5 deletions app/access/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:

Expand All @@ -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:
Expand Down

0 comments on commit e4ecc07

Please sign in to comment.