forked from orbit-oss/flask
Moved the conversion thing into the ConfigAttribute.
This commit is contained in:
parent
6dccf77546
commit
8da8a21b69
3 changed files with 27 additions and 28 deletions
42
flask/app.py
42
flask/app.py
|
|
@ -41,6 +41,12 @@ from .signals import request_started, request_finished, got_request_exception, \
|
||||||
_logger_lock = Lock()
|
_logger_lock = Lock()
|
||||||
|
|
||||||
|
|
||||||
|
def _make_timedelta(value):
|
||||||
|
if not isinstance(value, timedelta):
|
||||||
|
return timedelta(seconds=value)
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
def setupmethod(f):
|
def setupmethod(f):
|
||||||
"""Wraps a method so that it performs a check in debug mode if the
|
"""Wraps a method so that it performs a check in debug mode if the
|
||||||
first request was already handled.
|
first request was already handled.
|
||||||
|
|
@ -177,6 +183,16 @@ class Flask(_PackageBoundObject):
|
||||||
#: `SESSION_COOKIE_NAME` configuration key. Defaults to ``'session'``
|
#: `SESSION_COOKIE_NAME` configuration key. Defaults to ``'session'``
|
||||||
session_cookie_name = ConfigAttribute('SESSION_COOKIE_NAME')
|
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',
|
||||||
|
get_converter=_make_timedelta)
|
||||||
|
|
||||||
#: Enable this if you want to use the X-Sendfile feature. Keep in
|
#: 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
|
#: mind that the server has to support this. This only affects files
|
||||||
#: sent with the :func:`send_file` method.
|
#: sent with the :func:`send_file` method.
|
||||||
|
|
@ -486,32 +502,6 @@ class Flask(_PackageBoundObject):
|
||||||
return rv
|
return rv
|
||||||
return self.debug
|
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
|
@property
|
||||||
def logger(self):
|
def logger(self):
|
||||||
"""A :class:`logging.Logger` object for this application. The
|
"""A :class:`logging.Logger` object for this application. The
|
||||||
|
|
|
||||||
|
|
@ -21,13 +21,17 @@ from werkzeug.utils import import_string
|
||||||
class ConfigAttribute(object):
|
class ConfigAttribute(object):
|
||||||
"""Makes an attribute forward to the config"""
|
"""Makes an attribute forward to the config"""
|
||||||
|
|
||||||
def __init__(self, name):
|
def __init__(self, name, get_converter=None):
|
||||||
self.__name__ = name
|
self.__name__ = name
|
||||||
|
self.get_converter = get_converter
|
||||||
|
|
||||||
def __get__(self, obj, type=None):
|
def __get__(self, obj, type=None):
|
||||||
if obj is None:
|
if obj is None:
|
||||||
return self
|
return self
|
||||||
return obj.config[self.__name__]
|
rv = obj.config[self.__name__]
|
||||||
|
if self.get_converter is not None:
|
||||||
|
rv = self.get_converter(rv)
|
||||||
|
return rv
|
||||||
|
|
||||||
def __set__(self, obj, value):
|
def __set__(self, obj, value):
|
||||||
obj.config[self.__name__] = value
|
obj.config[self.__name__] = value
|
||||||
|
|
|
||||||
|
|
@ -78,6 +78,11 @@ class ConfigTestCase(FlaskTestCase):
|
||||||
self.assert_(0, 'expected config')
|
self.assert_(0, 'expected config')
|
||||||
self.assert_(not app.config.from_pyfile('missing.cfg', silent=True))
|
self.assert_(not app.config.from_pyfile('missing.cfg', silent=True))
|
||||||
|
|
||||||
|
def test_session_lifetime(self):
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
app.config['PERMANENT_SESSION_LIFETIME'] = 42
|
||||||
|
self.assert_equal(app.permanent_session_lifetime.total_seconds(), 42)
|
||||||
|
|
||||||
|
|
||||||
class InstanceTestCase(FlaskTestCase):
|
class InstanceTestCase(FlaskTestCase):
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue