Moved the conversion thing into the ConfigAttribute.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 6dccf77 · Browse files · 3 files·+27 added−28 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+27 added−28 removed
- other:+27 added−28 removed
Provenance
- Commit
8da8a21- Tree
7cfb65f- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
6dccf77
3 files changed+27 added−28 removed
modified:flask/testsuite/config.py+5 added
76 unmodified lines77 else:78 self.assert_(0, 'expected config')79 self.assert_(not app.config.from_pyfile('missing.cfg', silent=True))80 81+ def test_session_lifetime(self):82+ app = flask.Flask(__name__)83+ app.config['PERMANENT_SESSION_LIFETIME'] = 4284+ self.assert_equal(app.permanent_session_lifetime.total_seconds(), 42)85 86+87 class InstanceTestCase(FlaskTestCase):88 89 def test_explicit_instance_paths(self):90 here = os.path.abspath(os.path.dirname(__file__))92 unmodified linesmodified:flask/app.py+16 added-26 removed
39 unmodified lines40 # a lock used for logger initialization41 _logger_lock = Lock()42 43 44+def _make_timedelta(value):45+ if not isinstance(value, timedelta):46+ return timedelta(seconds=value)47+ return value48+49+50 def setupmethod(f):51 """Wraps a method so that it performs a check in debug mode if the52 first request was already handled.53 """128 unmodified lines182 #: This attribute can also be configured from the config with the183 #: `SESSION_COOKIE_NAME` configuration key. Defaults to ``'session'``184 session_cookie_name = ConfigAttribute('SESSION_COOKIE_NAME')185 186+ #: A :class:`~datetime.timedelta` which is used to set the expiration187+ #: date of a permanent session. The default is 31 days which makes a188+ #: permanent session survive for roughly one month.189+ #:190+ #: This attribute can also be configured from the config with the191+ #: `PERMANENT_SESSION_LIFETIME` configuration key. Defaults to192+ #: ``timedelta(days=31)``193+ permanent_session_lifetime = ConfigAttribute('PERMANENT_SESSION_LIFETIME',194+ get_converter=_make_timedelta)195+196 #: Enable this if you want to use the X-Sendfile feature. Keep in197 #: mind that the server has to support this. This only affects files198 #: sent with the :func:`send_file` method.199 #:301 unmodified lines501 if rv is not None:502 return rv503 return self.debug504 489- def _get_permanent_session_lifetime(self):490- """A :class:`~datetime.timedelta` which is used to set the expiration491- date of a permanent session. The default is 31 days which makes a492- permanent session survive for roughly one month.493-494- This attribute can also be configured from the config with the495- `PERMANENT_SESSION_LIFETIME` configuration key. Defaults to496- ``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 seconds503- 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 rv509- def _set_permanent_session_lifetime(self, value):510- self.config['PERMANENT_SESSION_LIFETIME'] = value511- permanent_session_lifetime = property(_get_permanent_session_lifetime,512- _set_permanent_session_lifetime)513- del _get_permanent_session_lifetime, _set_permanent_session_lifetime514-505 @property506 def logger(self):507 """A :class:`logging.Logger` object for this application. The508 default configuration is to log to stderr if the application is1010 unmodified linesmodified:flask/config.py+6 added-2 removed
19 unmodified lines20 21 class ConfigAttribute(object):22 """Makes an attribute forward to the config"""23 24- def __init__(self, name):24+ def __init__(self, name, get_converter=None):25 self.__name__ = name26+ self.get_converter = get_converter27 28 def __get__(self, obj, type=None):29 if obj is None:30 return self30- 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 rv35 36 def __set__(self, obj, value):37 obj.config[self.__name__] = value38 131 unmodified lines