Expand docs on send_file option hook, #433.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Ron DuPlain · committed by Ron DuPlain · parent f4a31db · Browse files · 3 files·+21 added−5 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+21 added−5 removed
- other:+21 added−5 removed
Provenance
- Commit
146088d- Tree
bd4f47c- Author
Ron DuPlain <ron.duplain@gmail.com>- Committer
Ron DuPlain <ron.duplain@gmail.com>- Parent
f4a31db
3 files changed+21 added−5 removed
modified:flask/testsuite/helpers.py+1 added-3 removed
211 unmodified lines212 cc = parse_cache_control_header(rv.headers['Cache-Control'])213 self.assert_equal(cc.max_age, 12 * 60 * 60)214 # override get_static_file_options with some new values and check them215 class StaticFileApp(flask.Flask):216- def __init__(self):217- super(StaticFileApp, self).__init__(__name__)216 def get_static_file_options(self, filename):217 opts = super(StaticFileApp, self).get_static_file_options(filename)218 opts['cache_timeout'] = 10219 # this test catches explicit inclusion of the conditional220 # keyword arg in the guts221 opts['conditional'] = True222 return opts225- app = StaticFileApp()223+ app = StaticFileApp(__name__)224 with app.test_request_context():225 rv = app.send_static_file('index.html')226 cc = parse_cache_control_header(rv.headers['Cache-Control'])227 self.assert_equal(cc.max_age, 10)154 unmodified linesmodified:flask/helpers.py+16 added-2 removed
651 unmodified lines652 return FileSystemLoader(os.path.join(self.root_path,653 self.template_folder))654 655 def get_static_file_options(self, filename):656- """Function used internally to determine what keyword arguments657- to send to :func:`send_from_directory` for a specific file."""656+ """Provides keyword arguments to send to :func:`send_from_directory`.657+658+ This allows subclasses to change the behavior when sending files based659+ on the filename. For example, to set the cache timeout for .js files660+ to 60 seconds (note the options are keywords for :func:`send_file`)::661+662+ class MyFlask(flask.Flask):663+ def get_static_file_options(self, filename):664+ options = super(MyFlask, self).get_static_file_options(filename)665+ if filename.lower().endswith('.js'):666+ options['cache_timeout'] = 60667+ options['conditional'] = True668+ return options669+670+ .. versionaded:: 0.9671+ """672 return {}673 674 def send_static_file(self, filename):675 """Function used internally to send static files from the static34 unmodified linesmodified:CHANGES+4 added
46 unmodified lines47 single template name. On an iterable, the first template found is rendered.48 - View functions can now return a tuple with the first instance being an49 instance of :class:`flask.Response`. This allows for returning50 ``jsonify(error="error msg"), 400`` from a view function.51+- :class:`flask.Flask` now provides a `get_static_file_options` hook for52+ subclasses to override behavior of serving static files through Flask,53+ optionally by filename, which for example allows changing cache controls by54+ file extension.55 56 57 Version 0.8.158 -------------295 unmodified lines