Skip to content

Commit 8da8a21

Browse files
committed
Moved the conversion thing into the ConfigAttribute.
1 parent 6dccf77 commit 8da8a21

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

flask/app.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141
_logger_lock = Lock()
4242

4343

44+
def _make_timedelta(value):
45+
if not isinstance(value, timedelta):
46+
return timedelta(seconds=value)
47+
return value
48+
49+
4450
def setupmethod(f):
4551
"""Wraps a method so that it performs a check in debug mode if the
4652
first request was already handled.
@@ -177,6 +183,16 @@ class Flask(_PackageBoundObject):
177183
#: `SESSION_COOKIE_NAME` configuration key. Defaults to ``'session'``
178184
session_cookie_name = ConfigAttribute('SESSION_COOKIE_NAME')
179185

186+
#: A :class:`~datetime.timedelta` which is used to set the expiration
187+
#: date of a permanent session. The default is 31 days which makes a
188+
#: permanent session survive for roughly one month.
189+
#:
190+
#: This attribute can also be configured from the config with the
191+
#: `PERMANENT_SESSION_LIFETIME` configuration key. Defaults to
192+
#: ``timedelta(days=31)``
193+
permanent_session_lifetime = ConfigAttribute('PERMANENT_SESSION_LIFETIME',
194+
get_converter=_make_timedelta)
195+
180196
#: Enable this if you want to use the X-Sendfile feature. Keep in
181197
#: mind that the server has to support this. This only affects files
182198
#: sent with the :func:`send_file` method.
@@ -486,32 +502,6 @@ def preserve_context_on_exception(self):
486502
return rv
487503
return self.debug
488504

489-
def _get_permanent_session_lifetime(self):
490-
"""A :class:`~datetime.timedelta` which is used to set the expiration
491-
date of a permanent session. The default is 31 days which makes a
492-
permanent session survive for roughly one month.
493-
494-
This attribute can also be configured from the config with the
495-
`PERMANENT_SESSION_LIFETIME` configuration key. Defaults to
496-
``timedelta(days=31)``.
497-
498-
If you want to have this value as seconds you can use ``total_seconds()``::
499-
500-
app.permanent_session_lifetime.total_seconds()
501-
502-
Note that the config key can be a timedelta object or number of seconds
503-
as integer since Flask 0.8.
504-
"""
505-
rv = self.config['PERMANENT_SESSION_LIFETIME']
506-
if not isinstance(rv, timedelta):
507-
return timedelta(seconds=rv)
508-
return rv
509-
def _set_permanent_session_lifetime(self, value):
510-
self.config['PERMANENT_SESSION_LIFETIME'] = value
511-
permanent_session_lifetime = property(_get_permanent_session_lifetime,
512-
_set_permanent_session_lifetime)
513-
del _get_permanent_session_lifetime, _set_permanent_session_lifetime
514-
515505
@property
516506
def logger(self):
517507
"""A :class:`logging.Logger` object for this application. The

flask/config.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@
2121
class ConfigAttribute(object):
2222
"""Makes an attribute forward to the config"""
2323

24-
def __init__(self, name):
24+
def __init__(self, name, get_converter=None):
2525
self.__name__ = name
26+
self.get_converter = get_converter
2627

2728
def __get__(self, obj, type=None):
2829
if obj is None:
2930
return self
30-
return obj.config[self.__name__]
31+
rv = obj.config[self.__name__]
32+
if self.get_converter is not None:
33+
rv = self.get_converter(rv)
34+
return rv
3135

3236
def __set__(self, obj, value):
3337
obj.config[self.__name__] = value

flask/testsuite/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ def test_config_missing(self):
7878
self.assert_(0, 'expected config')
7979
self.assert_(not app.config.from_pyfile('missing.cfg', silent=True))
8080

81+
def test_session_lifetime(self):
82+
app = flask.Flask(__name__)
83+
app.config['PERMANENT_SESSION_LIFETIME'] = 42
84+
self.assert_equal(app.permanent_session_lifetime.total_seconds(), 42)
85+
8186

8287
class InstanceTestCase(FlaskTestCase):
8388

0 commit comments

Comments
 (0)