Only add sessions refresh data to existing sessions

Prior to change session were created for _every_ request (including API
requests).
This commit is contained in:
Christopher C. Wells 2021-08-05 06:34:08 -07:00
parent 207a5bc236
commit f29269869c
1 changed files with 12 additions and 11 deletions

View File

@ -26,21 +26,22 @@ class UserTimezoneMiddleware:
class RollingSessionMiddleware:
"""
Periodically resets the session expiry.
Periodically resets the session expiry for existing sessions.
"""
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 (ValueError, TypeError):
delta = settings.ROLLING_SESSION_REFRESH + 1
if delta > settings.ROLLING_SESSION_REFRESH:
if request.session.keys():
session_refresh = request.session.get('session_refresh')
if session_refresh:
try:
delta = int(time.time()) - session_refresh
except (ValueError, TypeError):
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())
request.session.set_expiry(settings.SESSION_COOKIE_AGE)
else:
request.session['session_refresh'] = int(time.time())
return self.get_response(request)