Add last_modified arg for send_file
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Enhancement: Add last_modified arg of type DateTime to send_file. Fixes pallets/flask#1321
authored by Prachi Shirish Khadke · committed by David Lord · parent 64a37bb · Browse files · 2 files·+22 added−4 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+22 added−4 removed
- tests:+12 added−1 removed
- other:+10 added−3 removed
Provenance
- Commit
af515cc- Tree
9f0238d- Author
Prachi Shirish Khadke <pskhadke@us.ibm.com>- Committer
David Lord <davidism@gmail.com>- Parent
64a37bb
2 files changed+22 added−4 removed
modified:flask/helpers.py+10 added-3 removed
37 unmodified lines38 39 from .signals import message_flashed40 from .globals import session, _request_ctx_stack, _app_ctx_stack, \41 current_app, request42-from ._compat import string_types, text_type42+from ._compat import string_types, text_type, PY243 44 45 # sentinel46 _missing = object()381 unmodified lines428 429 430 def send_file(filename_or_fp, mimetype=None, as_attachment=False,431 attachment_filename=None, add_etags=True,432- cache_timeout=None, conditional=False):432+ cache_timeout=None, conditional=False, last_modified=None):433 """Sends the contents of a file to the client. This will use the434 most efficient method available and configured. By default it will435 try to use the WSGI server's file_wrapper support. Alternatively436 you can set the application's :attr:`~Flask.use_x_sendfile` attribute45 unmodified lines482 :param cache_timeout: the timeout in seconds for the headers. When ``None``483 (default), this value is set by484 :meth:`~Flask.get_send_file_max_age` of485 :data:`~flask.current_app`.486+ :param last_modified: the Datetime object representing timestamp for when487+ the input file was last modified.488 """489 mtime = None490 if isinstance(filename_or_fp, string_types):491 filename = filename_or_fp37 unmodified lines529 direct_passthrough=True)530 531 # if we know the file modification date, we can store it as532 # the time of the last modification.531- if mtime is not None:533+ if last_modified is not None:534+ if PY2:535+ rv.last_modified = int(last_modified.strftime("%s"))536+ else:537+ rv.last_modified = last_modified.timestamp()538+ elif mtime is not None:539 rv.last_modified = int(mtime)540 541 rv.cache_control.public = True542 if cache_timeout is None:386 unmodified linesmodified:tests/test_helpers.py+12 added-1 removed
16 unmodified lines17 from logging import StreamHandler18 from werkzeug.exceptions import BadRequest, NotFound19 from werkzeug.http import parse_cache_control_header, parse_options_header20 from werkzeug.http import http_date21-from flask._compat import StringIO, text_type21+from flask._compat import StringIO, text_type, PY222 23 24 def has_encoding(name):25 try:322 unmodified lines348 os.path.join(app.root_path, 'static/index.html')349 assert rv.mimetype == 'text/html'350 rv.close()351 352+ def test_send_file_last_modified(self):353+ app = flask.Flask(__name__)354+ with app.test_request_context():355+ dtm = datetime.datetime.now()356+ rv = flask.send_file('static/index.html', last_modified=dtm)357+ if PY2:358+ assert rv.last_modified == int(dtm.strftime("%s"))359+ else:360+ assert rv.last_modified == dtm.timestamp()361+ rv.close()362+363 def test_send_file_object(self):364 app = flask.Flask(__name__)365 366 with app.test_request_context():411 unmodified lines