1- import datetime
1+ from datetime import timedelta , datetime
22import uuid
33
44from bson .tz_util import utc
@@ -49,12 +49,11 @@ class DBSession(db.Document):
4949
5050 self .cls = DBSession
5151
52- def get_expiration_time (self , app , session ):
52+ def get_expiration_time (self , app , session ) -> timedelta :
5353 if session .permanent :
5454 return app .permanent_session_lifetime
55- if "SESSION_TTL" in app .config :
56- return datetime .timedelta (** app .config ["SESSION_TTL" ])
57- return datetime .timedelta (days = 1 )
55+ # Fallback to 1 day session ttl, if SESSION_TTL not set.
56+ return timedelta (** app .config .get ("SESSION_TTL" , {"days" : 1 }))
5857
5958 def open_session (self , app , request ):
6059 sid = request .cookies .get (app .session_cookie_name )
@@ -67,7 +66,7 @@ def open_session(self, app, request):
6766 if not expiration .tzinfo :
6867 expiration = expiration .replace (tzinfo = utc )
6968
70- if expiration > datetime .datetime . utcnow ().replace (tzinfo = utc ):
69+ if expiration > datetime .utcnow ().replace (tzinfo = utc ):
7170 return MongoEngineSession (
7271 initial = stored_session .data , sid = stored_session .sid
7372 )
@@ -85,9 +84,9 @@ def save_session(self, app, session, response):
8584 response .delete_cookie (app .session_cookie_name , domain = domain )
8685 return
8786
88- expiration = datetime .datetime . utcnow ().replace (
89- tzinfo = utc
90- ) + self . get_expiration_time ( app , session )
87+ expiration = datetime .utcnow ().replace ( tzinfo = utc ) + self . get_expiration_time (
88+ app , session
89+ )
9190
9291 if session .modified :
9392 self .cls (sid = session .sid , data = session , expiration = expiration ).save ()
0 commit comments