2 # Copyright (C) 2014-2016 Jessica Tallon & Matt Molyneaux
4 # This file is part of Inboxen.
6 # Inboxen is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # Inboxen is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Affero General Public License for more details.
16 # You should have received a copy of the GNU Affero General Public License
17 # along with Inboxen. If not, see <http://www.gnu.org/licenses/>.
20 from datetime
import timedelta
22 from django
.conf
import settings
23 from django
.contrib
.auth
.models
import update_last_login
24 from django
.utils
import timezone
26 SESSION_HALF_COOKIE_AGE
= timedelta(seconds
=settings
.SESSION_COOKIE_AGE
/ 2)
29 class ExtendSessionMiddleware
:
30 """Extends the expiry of sessions for logged in users"""
31 def __init__(self
, get_response
):
32 self
.get_response
= get_response
34 def __call__(self
, request
):
35 if request
.user
.is_authenticated
:
36 # this is ugly, but django doesn't make it easy
37 session_obj
= request
.session
._get
_session
_from
_db
()
38 if session_obj
is not None:
39 cookie_time_left
= session_obj
.expire_date
- timezone
.now()
40 if cookie_time_left
<= SESSION_HALF_COOKIE_AGE
:
42 request
.session
.cycle_key()
43 update_last_login(None, request
.user
)
44 return self
.get_response(request
)