Skip to content
Myr

Added _PackageBoundObject.get_static_file_options.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This method receives the name of a static file that is going to be served up and generates a dict of options to use when serving the file. The default set is empty so code will fall back to the existing behavior if the method is not overridden. I needed this method to adjust the cache control headers for .js files that one of my applications was statically serving. The default expiration is buried in an argument to send_file and is set to 12 hours. There was no good way to adjust this value previously.

authored by Dave Shawley · committed by Dave Shawley · parent 8d7ca29 · Browse files · 2 files+34 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+34 added−3 removed

  • other:+34 added−3 removed

2 files+34 added−3 removed

modified:flask/testsuite/helpers.py+25 added-1 removed
15 unmodified lines
16import unittest17from logging import StreamHandler18from StringIO import StringIO19from flask.testsuite import FlaskTestCase, catch_warnings, catch_stderr20from werkzeug.http import parse_options_header20from werkzeug.http import parse_cache_control_header, parse_options_header212223def has_encoding(name):24 try:
178 unmodified lines
203 value, options = parse_options_header(rv.headers['Content-Disposition'])204 self.assert_equal(value, 'attachment')205 self.assert_equal(options['filename'], 'index.txt')206207 def test_static_file(self):208 app = flask.Flask(__name__)209 # default cache timeout is 12 hours (hard-coded)210 with app.test_request_context():211 rv = app.send_static_file('index.html')212 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__)218 def get_static_file_options(self, filename):219 opts = super(StaticFileApp, self).get_static_file_options(filename)220 opts['cache_timeout'] = 10221 # this test catches explicit inclusion of the conditional222 # keyword arg in the guts223 opts['conditional'] = True224 return opts225 app = StaticFileApp()226 with app.test_request_context():227 rv = app.send_static_file('index.html')228 cc = parse_cache_control_header(rv.headers['Cache-Control'])229 self.assert_equal(cc.max_age, 10)230231232class LoggingTestCase(FlaskTestCase):233234 def test_logger_cache(self):235 app = flask.Flask(__name__)
148 unmodified lines
modified:flask/helpers.py+9 added-2 removed
493 unmodified lines
494 """495 filename = safe_join(directory, filename)496 if not os.path.isfile(filename):497 raise NotFound()498 options.setdefault('conditional', True)498 return send_file(filename, conditional=True, **options)499 return send_file(filename, **options)500501502def get_root_path(import_name):503 """Returns the path to a package or cwd if that cannot be found. This
147 unmodified lines
651 if self.template_folder is not None:652 return FileSystemLoader(os.path.join(self.root_path,653 self.template_folder))654655 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."""658 return {}659660 def send_static_file(self, filename):661 """Function used internally to send static files from the static662 folder to the browser.663664 .. versionadded:: 0.5665 """666 if not self.has_static_folder:667 raise RuntimeError('No static folder for this object')662 return send_from_directory(self.static_folder, filename)668 return send_from_directory(self.static_folder, filename,669 **self.get_static_file_options(filename))670671 def open_resource(self, resource, mode='rb'):672 """Opens a resource from the application's resource folder. To see673 how this works, consider the following folder structure::
22 unmodified lines