mirror of https://github.com/snachodog/mybuddy.git
Adds rolling session refresh middleware (#130)
This commit is contained in:
parent
9944de9bee
commit
1d9a7d4215
|
@ -1,5 +1,8 @@
|
|||
import time
|
||||
|
||||
import pytz
|
||||
|
||||
from django.conf import settings
|
||||
from django.utils import timezone
|
||||
|
||||
|
||||
|
@ -19,3 +22,24 @@ class UserTimezoneMiddleware:
|
|||
except pytz.UnknownTimeZoneError:
|
||||
pass
|
||||
return self.get_response(request)
|
||||
|
||||
class RollingSessionMiddleware:
|
||||
"""
|
||||
Periodically resets the session expiry.
|
||||
"""
|
||||
def __init__(self, get_response):
|
||||
self.get_response = get_response
|
||||
|
||||
def __call__(self, request):
|
||||
session_refresh = request.session.get('session_refresh')
|
||||
if session_refresh:
|
||||
try:
|
||||
delta = int(time.time()) - session_refresh
|
||||
except:
|
||||
delta = settings.ROLLING_SESSION_REFRESH + 1
|
||||
if delta > settings.ROLLING_SESSION_REFRESH:
|
||||
request.session['session_refresh'] = int(time.time())
|
||||
request.session.set_expiry(settings.SESSION_COOKIE_AGE)
|
||||
else:
|
||||
request.session['session_refresh'] = int(time.time())
|
||||
return self.get_response(request)
|
||||
|
|
|
@ -58,6 +58,7 @@ MIDDLEWARE = [
|
|||
'django.middleware.security.SecurityMiddleware',
|
||||
'whitenoise.middleware.WhiteNoiseMiddleware',
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'babybuddy.middleware.RollingSessionMiddleware',
|
||||
'django.middleware.locale.LocaleMiddleware',
|
||||
'babybuddy.middleware.UserTimezoneMiddleware',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
|
@ -223,6 +224,10 @@ IMPORT_EXPORT_IMPORT_PERMISSION_CODE = 'add'
|
|||
IMPORT_EXPORT_EXPORT_PERMISSION_CODE = 'change'
|
||||
IMPORT_EXPORT_USE_TRANSACTIONS = True
|
||||
|
||||
# Rolling session refreshes
|
||||
# How often to refresh the session
|
||||
ROLLING_SESSION_REFRESH = 86400
|
||||
|
||||
# Baby Buddy configuration
|
||||
# See README.md#configuration for details about these settings.
|
||||
|
||||
|
|
Loading…
Reference in New Issue