Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cookies expires #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions torndsession/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
class SessionManager(object):

SESSION_ID = 'msid'
DEFAULT_SESSION_LIFETIME = 1200 # seconds
DEFAULT_SESSION_LIFETIME = 1200 # seconds

def __init__(self, handler):
self.handler = handler
Expand All @@ -37,16 +37,30 @@ def __init__(self, handler):
self._expires = self._default_session_lifetime
self._is_dirty = True
self.__init_session_driver()
self.__init_session_object() # initialize session object
self.__init_session_object() # initialize session object

def __init_session_object(self):
cookiename = self.settings.get('sid_name', self.SESSION_ID)
session_id = self.handler.get_cookie(cookiename)

cookie_config = self.settings.get("cookie_config")
if cookie_config:
expires = cookie_config.get("expires")
expires_days = cookie_config.get("expires_days")
if expires_days is not None and not expires:
self._expires = datetime.utcnow() + timedelta(days=expires_days)

if expires and isinstance(expires, int):
self._expires = datetime.utcnow() + timedelta(seconds=expires)

if self._expires:
cookie_config["expires"] = self._expires

if not session_id:
session_id = self._generate_session_id(30)
self.handler.set_cookie(cookiename,
session_id,
**self.__session_settings())
**cookie_config)
self._is_dirty = True
self.session = {}
else:
Expand All @@ -56,14 +70,7 @@ def __init_session_object(self):
self._is_dirty = True
else:
self._is_dirty = False
cookie_config = self.settings.get("cookie_config")
if cookie_config:
expires = cookie_config.get("expires")
expires_days = cookie_config.get("expires_days")
if expires_days is not None and not expires:
expires = datetime.utcnow() + timedelta(days=expires_days)
if expires and isinstance(expires, datetime):
self._expires = expires

self._expires = self._expires if self._expires else self._default_session_lifetime
self._id = session_id

Expand Down Expand Up @@ -234,12 +241,6 @@ def expires(self):
self.__init_session_object()
return self._expires

def __session_settings(self):
session_settings = self.settings.get('cookie_config', {})
session_settings.setdefault('expires', None)
session_settings.setdefault('expires_days', None)
return session_settings


class SessionMixin(object):

Expand Down