forked from orbit-oss/flask
support timedelta for SEND_FILE_MAX_AGE_DEFAULT config variable
This commit is contained in:
parent
1ac4156016
commit
d526932a09
5 changed files with 36 additions and 7 deletions
|
|
@ -130,7 +130,8 @@ The following configuration values are used internally by Flask:
|
||||||
``SEND_FILE_MAX_AGE_DEFAULT`` Default cache control max age to use with
|
``SEND_FILE_MAX_AGE_DEFAULT`` Default cache control max age to use with
|
||||||
:meth:`~flask.Flask.send_static_file` (the
|
:meth:`~flask.Flask.send_static_file` (the
|
||||||
default static file handler) and
|
default static file handler) and
|
||||||
:func:`~flask.send_file`, in
|
:func:`~flask.send_file`, as
|
||||||
|
:class:`datetime.timedelta` or as seconds.
|
||||||
seconds. Override this value on a per-file
|
seconds. Override this value on a per-file
|
||||||
basis using the
|
basis using the
|
||||||
:meth:`~flask.Flask.get_send_file_max_age`
|
:meth:`~flask.Flask.get_send_file_max_age`
|
||||||
|
|
|
||||||
12
flask/app.py
12
flask/app.py
|
|
@ -246,6 +246,16 @@ class Flask(_PackageBoundObject):
|
||||||
permanent_session_lifetime = ConfigAttribute('PERMANENT_SESSION_LIFETIME',
|
permanent_session_lifetime = ConfigAttribute('PERMANENT_SESSION_LIFETIME',
|
||||||
get_converter=_make_timedelta)
|
get_converter=_make_timedelta)
|
||||||
|
|
||||||
|
#: A :class:`~datetime.timedelta` which is used as default cache_timeout
|
||||||
|
#: for the :func:`send_file` functions. The default is 12 hours.
|
||||||
|
#:
|
||||||
|
#: This attribute can also be configured from the config with the
|
||||||
|
#: ``SEND_FILE_MAX_AGE_DEFAULT`` configuration key. This configuration
|
||||||
|
#: variable can also be set with an integer value used as seconds.
|
||||||
|
#: Defaults to ``timedelta(hours=12)``
|
||||||
|
send_file_max_age_default = ConfigAttribute('SEND_FILE_MAX_AGE_DEFAULT',
|
||||||
|
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.
|
||||||
|
|
@ -297,7 +307,7 @@ class Flask(_PackageBoundObject):
|
||||||
'SESSION_COOKIE_SECURE': False,
|
'SESSION_COOKIE_SECURE': False,
|
||||||
'SESSION_REFRESH_EACH_REQUEST': True,
|
'SESSION_REFRESH_EACH_REQUEST': True,
|
||||||
'MAX_CONTENT_LENGTH': None,
|
'MAX_CONTENT_LENGTH': None,
|
||||||
'SEND_FILE_MAX_AGE_DEFAULT': 12 * 60 * 60, # 12 hours
|
'SEND_FILE_MAX_AGE_DEFAULT': timedelta(hours=12),
|
||||||
'TRAP_BAD_REQUEST_ERRORS': False,
|
'TRAP_BAD_REQUEST_ERRORS': False,
|
||||||
'TRAP_HTTP_EXCEPTIONS': False,
|
'TRAP_HTTP_EXCEPTIONS': False,
|
||||||
'EXPLAIN_TEMPLATE_LOADING': False,
|
'EXPLAIN_TEMPLATE_LOADING': False,
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ import sys
|
||||||
import pkgutil
|
import pkgutil
|
||||||
import posixpath
|
import posixpath
|
||||||
import mimetypes
|
import mimetypes
|
||||||
|
from datetime import timedelta
|
||||||
from time import time
|
from time import time
|
||||||
from zlib import adler32
|
from zlib import adler32
|
||||||
from threading import RLock
|
from threading import RLock
|
||||||
|
|
@ -856,7 +857,7 @@ class _PackageBoundObject(object):
|
||||||
|
|
||||||
.. versionadded:: 0.9
|
.. versionadded:: 0.9
|
||||||
"""
|
"""
|
||||||
return current_app.config['SEND_FILE_MAX_AGE_DEFAULT']
|
return total_seconds(current_app.send_file_max_age_default)
|
||||||
|
|
||||||
def send_static_file(self, filename):
|
def send_static_file(self, filename):
|
||||||
"""Function used internally to send static files from the static
|
"""Function used internally to send static files from the static
|
||||||
|
|
@ -898,3 +899,14 @@ class _PackageBoundObject(object):
|
||||||
if mode not in ('r', 'rb'):
|
if mode not in ('r', 'rb'):
|
||||||
raise ValueError('Resources can only be opened for reading')
|
raise ValueError('Resources can only be opened for reading')
|
||||||
return open(os.path.join(self.root_path, resource), mode)
|
return open(os.path.join(self.root_path, resource), mode)
|
||||||
|
|
||||||
|
|
||||||
|
def total_seconds(td):
|
||||||
|
"""Returns the total seconds from a timedelta object.
|
||||||
|
|
||||||
|
:param timedelta td: the timedelta to be converted in seconds
|
||||||
|
|
||||||
|
:returns: number of seconds
|
||||||
|
:rtype: int
|
||||||
|
"""
|
||||||
|
return td.days * 60 * 60 * 24 + td.seconds
|
||||||
|
|
|
||||||
|
|
@ -17,14 +17,11 @@ from werkzeug.http import http_date, parse_date
|
||||||
from werkzeug.datastructures import CallbackDict
|
from werkzeug.datastructures import CallbackDict
|
||||||
from . import Markup, json
|
from . import Markup, json
|
||||||
from ._compat import iteritems, text_type
|
from ._compat import iteritems, text_type
|
||||||
|
from .helpers import total_seconds
|
||||||
|
|
||||||
from itsdangerous import URLSafeTimedSerializer, BadSignature
|
from itsdangerous import URLSafeTimedSerializer, BadSignature
|
||||||
|
|
||||||
|
|
||||||
def total_seconds(td):
|
|
||||||
return td.days * 60 * 60 * 24 + td.seconds
|
|
||||||
|
|
||||||
|
|
||||||
class SessionMixin(object):
|
class SessionMixin(object):
|
||||||
"""Expands a basic dictionary with an accessors that are expected
|
"""Expands a basic dictionary with an accessors that are expected
|
||||||
by Flask extensions and users for the session.
|
by Flask extensions and users for the session.
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
from datetime import timedelta
|
||||||
import flask
|
import flask
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -168,6 +169,14 @@ def test_session_lifetime():
|
||||||
assert app.permanent_session_lifetime.seconds == 42
|
assert app.permanent_session_lifetime.seconds == 42
|
||||||
|
|
||||||
|
|
||||||
|
def test_send_file_max_age():
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 3600
|
||||||
|
assert app.send_file_max_age_default.seconds == 3600
|
||||||
|
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = timedelta(hours=2)
|
||||||
|
assert app.send_file_max_age_default.seconds == 7200
|
||||||
|
|
||||||
|
|
||||||
def test_get_namespace():
|
def test_get_namespace():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
app.config['FOO_OPTION_1'] = 'foo option 1'
|
app.config['FOO_OPTION_1'] = 'foo option 1'
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue