Skip to content

Commit

Permalink
Fix reset SessioTokenFilter on /api/iniconfig request when QDJANGO_PR…
Browse files Browse the repository at this point in the history
…J_CACHE = True
  • Loading branch information
wlorenzetti committed Nov 15, 2024
1 parent aafff46 commit f777048
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
4 changes: 4 additions & 0 deletions g3w-admin/core/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.contrib.auth import REDIRECT_FIELD_NAME
from guardian.exceptions import GuardianError
from guardian.utils import get_40x_or_None, get_anonymous_user
from qdjango.utils.session import reset_filtertoken

import logging

Expand Down Expand Up @@ -169,6 +170,9 @@ def _wrapped_view(request, *args, **kwargs):
else:
user_pk = request.user.pk

# Reset sessiontokenfilter
reset_filtertoken(request)

key = f"{key_prefix}{'_'.join([str(kwargs[k]) for k in key_args] + [str(user_pk)])}"
logger.debug(f"[CACHING /api/config]: Key {key}")
response = cache.get(key)
Expand Down
16 changes: 2 additions & 14 deletions g3w-admin/qdjango/api/projects/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from qdjango.signals import load_qdjango_widget_layer
from qdjango.apps import get_qgs_project
from qdjango.utils.structure import QdjangoMetaLayer, datasourcearcgis2dict
from qdjango.utils.session import reset_filtertoken
from qdjango.api.layers.serializers import FilterLayerSavedSerializer
from core.utils.structure import mapLayerAttributes
from core.configs import *
Expand Down Expand Up @@ -224,19 +225,6 @@ def get_metadata(self, instance, qgs_project):

return metadata

def reset_filtertoken(self):
"""Check session token filter ad delete it"""

try:
if settings.SESSION_COOKIE_NAME in self.request.COOKIES:
stf = SessionTokenFilter.objects.get(
sessionid=self.request.COOKIES[settings.SESSION_COOKIE_NAME])
stf.delete()
except AttributeError:
return None
except SessionTokenFilter.DoesNotExist:
return None

def layer_is_empty(self, layer):
"""
Check if a vector layer is empty (not data)
Expand Down Expand Up @@ -614,7 +602,7 @@ def readLeaf(layer, container):
ret['messages'] = self.get_messages(instance)

# reset tokenfilter by session
self.reset_filtertoken()
reset_filtertoken(self.request)

# add edit url if user has grant
if hasattr(self.request, 'user') and self.request.user.has_perm('qdjango.change_project', instance):
Expand Down
31 changes: 31 additions & 0 deletions g3w-admin/qdjango/utils/session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# coding=utf-8
"""" Utility function for session
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the Mozilla Public License 2.0.
"""

__author__ = '[email protected]'
__date__ = '2024-11-15'
__copyright__ = 'Copyright 2015 - 2024, Gis3w'
__license__ = 'MPL 2.0'

from django.conf import settings
from qdjango.models import SessionTokenFilter

def reset_filtertoken(request):
"""
Check session token filter ad delete it
:param request: Django request object
"""

try:
if settings.SESSION_COOKIE_NAME in request.COOKIES:
stf = SessionTokenFilter.objects.get(
sessionid=request.COOKIES[settings.SESSION_COOKIE_NAME])
stf.delete()
except AttributeError:
return None
except SessionTokenFilter.DoesNotExist:
return None
4 changes: 2 additions & 2 deletions g3w-admin/qdjango/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ def _get_sessiontokenfilter():
"""
Get or create an instance of SessionTokeFilter model
:return: A tuple with the instance of SessioneTokeFilter model and a boolean state for created or retreive
:return: A tuple with the instance of SessionTokeFilter model and a boolean state for created or retreive
:rtype: tuple
"""
s, created = SessionTokenFilter.objects.get_or_create(
Expand Down Expand Up @@ -545,7 +545,7 @@ def _check_url_params_name_fid(mode):

s, created = _get_sessiontokenfilter()

# Check if a recordo for surrent layer is present for update or create it
# Check if a record for current layer is present for update or create it
s.stf_layers.update_or_create(
layer=fls.layer,
defaults={
Expand Down

0 comments on commit f777048

Please sign in to comment.