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

Support modifying session's cookie_expires attribute on runtime. #138

Open
wants to merge 1 commit 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
25 changes: 18 additions & 7 deletions beaker/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def __init__(self, request, id=None, invalidate_corrupt=False,
self.timeout = timeout
self.save_atime = save_accessed_time
self.use_cookies = use_cookies
self.cookie_expires = cookie_expires
self._cookie_expires = cookie_expires

self._set_serializer(data_serializer)

Expand Down Expand Up @@ -244,7 +244,7 @@ def _set_cookie_values(self, expires=None):

def _set_cookie_expires(self, expires):
if expires is None:
expires = self.cookie_expires
expires = self._cookie_expires
if expires is False:
expires_date = datetime.fromtimestamp(0x7FFFFFFF)
elif isinstance(expires, timedelta):
Expand All @@ -253,8 +253,8 @@ def _set_cookie_expires(self, expires):
expires_date = expires
elif expires is not True:
raise ValueError("Invalid argument for cookie_expires: %s"
% repr(self.cookie_expires))
self.cookie_expires = expires
% repr(self._cookie_expires))
self._cookie_expires = expires
if not self.cookie or self.key not in self.cookie:
self.cookie[self.key] = self.id
if expires is True:
Expand All @@ -264,8 +264,8 @@ def _set_cookie_expires(self, expires):
expires_date.strftime("%a, %d-%b-%Y %H:%M:%S GMT")
return expires_date

def _update_cookie_out(self, set_cookie=True):
self._set_cookie_values()
def _update_cookie_out(self, set_cookie=True, cookie_expires=None):
self._set_cookie_values(expires=cookie_expires)
self.request['cookie_out'] = self.cookie[self.key].output(header='')
self.request['set_cookie'] = set_cookie

Expand All @@ -292,6 +292,17 @@ def _create_id(self, set_new=True):
def created(self):
return self['_creation_time']

def _set_cookie_expires_wrapper(self, cookie_expires):
if cookie_expires and isinstance(cookie_expires, int) and \
not isinstance(cookie_expires, bool):
cookie_expires = timedelta(seconds=cookie_expires)
self._update_cookie_out(cookie_expires=cookie_expires)

def _get_cookie_expires(self):
return self._cookie_expires

cookie_expires = property(_get_cookie_expires, _set_cookie_expires_wrapper)

def _set_domain(self, domain):
self['_domain'] = self._domain = domain
self._update_cookie_out()
Expand Down Expand Up @@ -566,7 +577,7 @@ def __init__(self, request, key='beaker.session.id', timeout=None,
self.key = key
self.timeout = timeout
self.save_atime = save_accessed_time
self.cookie_expires = cookie_expires
self._cookie_expires = cookie_expires
self.encrypt_key = encrypt_key
self.validate_key = validate_key
self.encrypt_nonce_size = get_nonce_size(encrypt_nonce_bits)
Expand Down