Made session loading in cached_db engine more DRY.
This commit is contained in:
parent
c5a2f48bed
commit
fff86cfa46
|
@ -2,13 +2,9 @@
|
||||||
Cached, database-backed sessions.
|
Cached, database-backed sessions.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import logging
|
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.sessions.backends.db import SessionStore as DBStore
|
from django.contrib.sessions.backends.db import SessionStore as DBStore
|
||||||
from django.core.cache import caches
|
from django.core.cache import caches
|
||||||
from django.core.exceptions import SuspiciousOperation
|
|
||||||
from django.utils import timezone
|
|
||||||
|
|
||||||
KEY_PREFIX = "django.contrib.sessions.cached_db"
|
KEY_PREFIX = "django.contrib.sessions.cached_db"
|
||||||
|
|
||||||
|
@ -36,20 +32,11 @@ class SessionStore(DBStore):
|
||||||
data = None
|
data = None
|
||||||
|
|
||||||
if data is None:
|
if data is None:
|
||||||
# Duplicate DBStore.load, because we need to keep track
|
s = self._get_session_from_db()
|
||||||
# of the expiry date to set it properly in the cache.
|
if s:
|
||||||
try:
|
|
||||||
s = self.model.objects.get(
|
|
||||||
session_key=self.session_key,
|
|
||||||
expire_date__gt=timezone.now()
|
|
||||||
)
|
|
||||||
data = self.decode(s.session_data)
|
data = self.decode(s.session_data)
|
||||||
self._cache.set(self.cache_key, data, self.get_expiry_age(expiry=s.expire_date))
|
self._cache.set(self.cache_key, data, self.get_expiry_age(expiry=s.expire_date))
|
||||||
except (self.model.DoesNotExist, SuspiciousOperation) as e:
|
else:
|
||||||
if isinstance(e, SuspiciousOperation):
|
|
||||||
logger = logging.getLogger('django.security.%s' % e.__class__.__name__)
|
|
||||||
logger.warning(str(e))
|
|
||||||
self._session_key = None
|
|
||||||
data = {}
|
data = {}
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
|
@ -27,19 +27,21 @@ class SessionStore(SessionBase):
|
||||||
def model(self):
|
def model(self):
|
||||||
return self.get_model_class()
|
return self.get_model_class()
|
||||||
|
|
||||||
def load(self):
|
def _get_session_from_db(self):
|
||||||
try:
|
try:
|
||||||
s = self.model.objects.get(
|
return self.model.objects.get(
|
||||||
session_key=self.session_key,
|
session_key=self.session_key,
|
||||||
expire_date__gt=timezone.now()
|
expire_date__gt=timezone.now()
|
||||||
)
|
)
|
||||||
return self.decode(s.session_data)
|
|
||||||
except (self.model.DoesNotExist, SuspiciousOperation) as e:
|
except (self.model.DoesNotExist, SuspiciousOperation) as e:
|
||||||
if isinstance(e, SuspiciousOperation):
|
if isinstance(e, SuspiciousOperation):
|
||||||
logger = logging.getLogger('django.security.%s' % e.__class__.__name__)
|
logger = logging.getLogger('django.security.%s' % e.__class__.__name__)
|
||||||
logger.warning(str(e))
|
logger.warning(str(e))
|
||||||
self._session_key = None
|
self._session_key = None
|
||||||
return {}
|
|
||||||
|
def load(self):
|
||||||
|
s = self._get_session_from_db()
|
||||||
|
return self.decode(s.session_data) if s else {}
|
||||||
|
|
||||||
def exists(self, session_key):
|
def exists(self, session_key):
|
||||||
return self.model.objects.filter(session_key=session_key).exists()
|
return self.model.objects.filter(session_key=session_key).exists()
|
||||||
|
|
Loading…
Reference in New Issue