permission and search preference caching
This commit is contained in:
@ -1,7 +1,9 @@
|
|||||||
|
import inspect
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.contrib.auth.decorators import user_passes_test
|
from django.contrib.auth.decorators import user_passes_test
|
||||||
from django.core.cache import caches
|
from django.core.cache import cache
|
||||||
from django.core.exceptions import ValidationError, ObjectDoesNotExist
|
from django.core.exceptions import ValidationError, ObjectDoesNotExist
|
||||||
from django.http import HttpResponseRedirect
|
from django.http import HttpResponseRedirect
|
||||||
from django.urls import reverse, reverse_lazy
|
from django.urls import reverse, reverse_lazy
|
||||||
@ -41,13 +43,23 @@ def has_group_permission(user, groups):
|
|||||||
if not user.is_authenticated:
|
if not user.is_authenticated:
|
||||||
return False
|
return False
|
||||||
groups_allowed = get_allowed_groups(groups)
|
groups_allowed = get_allowed_groups(groups)
|
||||||
|
|
||||||
|
CACHE_KEY = hash((inspect.stack()[0][3], user, groups_allowed))
|
||||||
|
cached_result = cache.get(CACHE_KEY, default=None)
|
||||||
|
if cached_result is not None:
|
||||||
|
return cached_result
|
||||||
|
|
||||||
|
result = False
|
||||||
|
print('running check', user, groups_allowed)
|
||||||
if user.is_authenticated:
|
if user.is_authenticated:
|
||||||
if user_space := user.userspace_set.filter(active=True):
|
if user_space := user.userspace_set.filter(active=True):
|
||||||
if len(user_space) != 1:
|
if len(user_space) != 1:
|
||||||
return False # do not allow any group permission if more than one space is active, needs to be changed when simultaneous multi-space-tenancy is added
|
result = False # do not allow any group permission if more than one space is active, needs to be changed when simultaneous multi-space-tenancy is added
|
||||||
if bool(user_space.first().groups.filter(name__in=groups_allowed)):
|
if bool(user_space.first().groups.filter(name__in=groups_allowed)):
|
||||||
return True
|
result = True
|
||||||
return False
|
|
||||||
|
cache.set(CACHE_KEY, result, timeout=10)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
def is_object_owner(user, obj):
|
def is_object_owner(user, obj):
|
||||||
@ -106,7 +118,7 @@ def share_link_valid(recipe, share):
|
|||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
CACHE_KEY = f'recipe_share_{recipe.pk}_{share}'
|
CACHE_KEY = f'recipe_share_{recipe.pk}_{share}'
|
||||||
if c := caches['default'].get(CACHE_KEY, False):
|
if c := cache.get(CACHE_KEY, False):
|
||||||
return c
|
return c
|
||||||
|
|
||||||
if link := ShareLink.objects.filter(recipe=recipe, uuid=share, abuse_blocked=False).first():
|
if link := ShareLink.objects.filter(recipe=recipe, uuid=share, abuse_blocked=False).first():
|
||||||
@ -114,7 +126,7 @@ def share_link_valid(recipe, share):
|
|||||||
return False
|
return False
|
||||||
link.request_count += 1
|
link.request_count += 1
|
||||||
link.save()
|
link.save()
|
||||||
caches['default'].set(CACHE_KEY, True, timeout=3)
|
cache.set(CACHE_KEY, True, timeout=3)
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
except ValidationError:
|
except ValidationError:
|
||||||
|
@ -3,6 +3,7 @@ from collections import Counter
|
|||||||
from datetime import date, timedelta
|
from datetime import date, timedelta
|
||||||
|
|
||||||
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector, TrigramSimilarity
|
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector, TrigramSimilarity
|
||||||
|
from django.core.cache import cache
|
||||||
from django.core.cache import caches
|
from django.core.cache import caches
|
||||||
from django.db.models import (Avg, Case, Count, Exists, F, Func, Max, OuterRef, Q, Subquery, Value, When, FilteredRelation)
|
from django.db.models import (Avg, Case, Count, Exists, F, Func, Max, OuterRef, Q, Subquery, Value, When, FilteredRelation)
|
||||||
from django.db.models.functions import Coalesce, Lower, Substr
|
from django.db.models.functions import Coalesce, Lower, Substr
|
||||||
@ -35,7 +36,13 @@ class RecipeSearch():
|
|||||||
else:
|
else:
|
||||||
self._params = {**(params or {})}
|
self._params = {**(params or {})}
|
||||||
if self._request.user.is_authenticated:
|
if self._request.user.is_authenticated:
|
||||||
|
CACHE_KEY = f'search_pref_{request.user.id}'
|
||||||
|
cached_result = cache.get(CACHE_KEY, default=None)
|
||||||
|
if cached_result is not None:
|
||||||
|
self._search_prefs = cached_result
|
||||||
|
else:
|
||||||
self._search_prefs = request.user.searchpreference
|
self._search_prefs = request.user.searchpreference
|
||||||
|
cache.set(CACHE_KEY, self._search_prefs, timeout=10)
|
||||||
else:
|
else:
|
||||||
self._search_prefs = SearchPreference()
|
self._search_prefs = SearchPreference()
|
||||||
self._string = self._params.get('query').strip() if self._params.get('query', None) else None
|
self._string = self._params.get('query').strip() if self._params.get('query', None) else None
|
||||||
|
Reference in New Issue
Block a user