Merge pull request #1593 from timofurrer/master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
support timedelta for SEND_FILE_MAX_AGE_DEFAULT config variable
authored by David Lord · committed by David Lord · parents 3f6e8c6, d526932 · Browse files · 5 files·+36 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+36 added−7 removed
- tests:+9 added−0 removed
- other:+27 added−7 removed
Provenance
- Commit
84a12af- Tree
4d90a63- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent 1
3f6e8c6- Parent 2
d526932
5 files changed+36 added−7 removed
modified:docs/config.rst+2 added-1 removed
128 unmodified lines129 returning a 413 status code.130 ``SEND_FILE_MAX_AGE_DEFAULT`` Default cache control max age to use with131 :meth:`~flask.Flask.send_static_file` (the132 default static file handler) and133- :func:`~flask.send_file`, in133+ :func:`~flask.send_file`, as134+ :class:`datetime.timedelta` or as seconds.135 seconds. Override this value on a per-file136 basis using the137 :meth:`~flask.Flask.get_send_file_max_age`138 hook on :class:`~flask.Flask` or325 unmodified linesmodified:flask/app.py+11 added-1 removed
244 unmodified lines245 #: ``timedelta(days=31)``246 permanent_session_lifetime = ConfigAttribute('PERMANENT_SESSION_LIFETIME',247 get_converter=_make_timedelta)248 249+ #: A :class:`~datetime.timedelta` which is used as default cache_timeout250+ #: for the :func:`send_file` functions. The default is 12 hours.251+ #:252+ #: This attribute can also be configured from the config with the253+ #: ``SEND_FILE_MAX_AGE_DEFAULT`` configuration key. This configuration254+ #: variable can also be set with an integer value used as seconds.255+ #: Defaults to ``timedelta(hours=12)``256+ send_file_max_age_default = ConfigAttribute('SEND_FILE_MAX_AGE_DEFAULT',257+ get_converter=_make_timedelta)258+259 #: Enable this if you want to use the X-Sendfile feature. Keep in260 #: mind that the server has to support this. This only affects files261 #: sent with the :func:`send_file` method.262 #:43 unmodified lines306 'SESSION_COOKIE_HTTPONLY': True,307 'SESSION_COOKIE_SECURE': False,308 'SESSION_REFRESH_EACH_REQUEST': True,309 'MAX_CONTENT_LENGTH': None,300- 'SEND_FILE_MAX_AGE_DEFAULT': 12 * 60 * 60, # 12 hours310+ 'SEND_FILE_MAX_AGE_DEFAULT': timedelta(hours=12),311 'TRAP_BAD_REQUEST_ERRORS': False,312 'TRAP_HTTP_EXCEPTIONS': False,313 'EXPLAIN_TEMPLATE_LOADING': False,314 'PREFERRED_URL_SCHEME': 'http',1671 unmodified linesmodified:flask/helpers.py+13 added-1 removed
12 unmodified lines13 import sys14 import pkgutil15 import posixpath16 import mimetypes17+from datetime import timedelta18 from time import time19 from zlib import adler3220 from threading import RLock21 from werkzeug.routing import BuildError834 unmodified lines856 return flask.Flask.get_send_file_max_age(self, name)857 858 .. versionadded:: 0.9859 """859- return current_app.config['SEND_FILE_MAX_AGE_DEFAULT']860+ return total_seconds(current_app.send_file_max_age_default)861 862 def send_static_file(self, filename):863 """Function used internally to send static files from the static864 folder to the browser.33 unmodified lines898 """899 if mode not in ('r', 'rb'):900 raise ValueError('Resources can only be opened for reading')901 return open(os.path.join(self.root_path, resource), mode)902+903+904+def total_seconds(td):905+ """Returns the total seconds from a timedelta object.906+907+ :param timedelta td: the timedelta to be converted in seconds908+909+ :returns: number of seconds910+ :rtype: int911+ """912+ return td.days * 60 * 60 * 24 + td.secondsmodified:flask/sessions.py+1 added-4 removed
15 unmodified lines16 from werkzeug.http import http_date, parse_date17 from werkzeug.datastructures import CallbackDict18 from . import Markup, json19 from ._compat import iteritems, text_type20+from .helpers import total_seconds21 22 from itsdangerous import URLSafeTimedSerializer, BadSignature23 24 24-def total_seconds(td):25- return td.days * 60 * 60 * 24 + td.seconds26-27-25 class SessionMixin(object):26 """Expands a basic dictionary with an accessors that are expected27 by Flask extensions and users for the session.28 """334 unmodified linesmodified:tests/test_config.py+9 added
8 unmodified lines9 10 import pytest11 12 import os13+from datetime import timedelta14 import flask15 16 17 # config keys used for the TestConfig150 unmodified lines168 app.config['PERMANENT_SESSION_LIFETIME'] = 42169 assert app.permanent_session_lifetime.seconds == 42170 171 172+def test_send_file_max_age():173+ app = flask.Flask(__name__)174+ app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 3600175+ assert app.send_file_max_age_default.seconds == 3600176+ app.config['SEND_FILE_MAX_AGE_DEFAULT'] = timedelta(hours=2)177+ assert app.send_file_max_age_default.seconds == 7200178+179+180 def test_get_namespace():181 app = flask.Flask(__name__)182 app.config['FOO_OPTION_1'] = 'foo option 1'183 app.config['FOO_OPTION_2'] = 'foo option 2'18 unmodified lines