From 6dccf775468ea0a849d3c1c3965e3a1db6a36a3f Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 25 Sep 2011 19:12:41 +0200 Subject: [PATCH] PERMANENT_SESSION_LIFETIME can now be an integer. This fixes #310 --- docs/api.rst | 7 +++++++ docs/config.rst | 2 ++ flask/app.py | 35 ++++++++++++++++++++++++++--------- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index 59b99295..13fcb659 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -217,6 +217,13 @@ implementation that Flask is using. .. autoclass:: SessionMixin :members: +.. admonition:: Notice + + The ``PERMANENT_SESSION_LIFETIME`` config key can also be an integer + starting with Flask 0.8. Either catch this down yourself or use + the :attr:`~flask.Flask.permanent_session_lifetime` attribute on the + app which converts the result to an integer automatically. + Test Client ----------- diff --git a/docs/config.rst b/docs/config.rst index 75ddf86d..32e267c3 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -86,6 +86,8 @@ The following configuration values are used internally by Flask: `False`. ``PERMANENT_SESSION_LIFETIME`` the lifetime of a permanent session as :class:`datetime.timedelta` object. + Starting with Flask 0.8 this can also be + an integer representing seconds. ``USE_X_SENDFILE`` enable/disable x-sendfile ``LOGGER_NAME`` the name of the logger ``SERVER_NAME`` the name and port number of the server. diff --git a/flask/app.py b/flask/app.py index 91ea3414..83721a6b 100644 --- a/flask/app.py +++ b/flask/app.py @@ -177,15 +177,6 @@ class Flask(_PackageBoundObject): #: `SESSION_COOKIE_NAME` configuration key. Defaults to ``'session'`` session_cookie_name = ConfigAttribute('SESSION_COOKIE_NAME') - #: A :class:`~datetime.timedelta` which is used to set the expiration - #: date of a permanent session. The default is 31 days which makes a - #: permanent session survive for roughly one month. - #: - #: This attribute can also be configured from the config with the - #: `PERMANENT_SESSION_LIFETIME` configuration key. Defaults to - #: ``timedelta(days=31)`` - permanent_session_lifetime = ConfigAttribute('PERMANENT_SESSION_LIFETIME') - #: Enable this if you want to use the X-Sendfile feature. Keep in #: mind that the server has to support this. This only affects files #: sent with the :func:`send_file` method. @@ -495,6 +486,32 @@ class Flask(_PackageBoundObject): return rv return self.debug + def _get_permanent_session_lifetime(self): + """A :class:`~datetime.timedelta` which is used to set the expiration + date of a permanent session. The default is 31 days which makes a + permanent session survive for roughly one month. + + This attribute can also be configured from the config with the + `PERMANENT_SESSION_LIFETIME` configuration key. Defaults to + ``timedelta(days=31)``. + + If you want to have this value as seconds you can use ``total_seconds()``:: + + app.permanent_session_lifetime.total_seconds() + + Note that the config key can be a timedelta object or number of seconds + as integer since Flask 0.8. + """ + rv = self.config['PERMANENT_SESSION_LIFETIME'] + if not isinstance(rv, timedelta): + return timedelta(seconds=rv) + return rv + def _set_permanent_session_lifetime(self, value): + self.config['PERMANENT_SESSION_LIFETIME'] = value + permanent_session_lifetime = property(_get_permanent_session_lifetime, + _set_permanent_session_lifetime) + del _get_permanent_session_lifetime, _set_permanent_session_lifetime + @property def logger(self): """A :class:`logging.Logger` object for this application. The