Skip to content
Myr

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

2 files+22 added−4 removed

modified:flask/helpers.py+10 added-3 removed
37 unmodified lines
3839from .signals import message_flashed40from .globals import session, _request_ctx_stack, _app_ctx_stack, \41 current_app, request42from ._compat import string_types, text_type42from ._compat import string_types, text_type, PY2434445# sentinel46_missing = object()
381 unmodified lines
428429430def 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` attribute
45 unmodified lines
482 :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_fp
37 unmodified lines
529 direct_passthrough=True)530531 # 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)540541 rv.cache_control.public = True542 if cache_timeout is None:
386 unmodified lines
modified:tests/test_helpers.py+12 added-1 removed
16 unmodified lines
17from logging import StreamHandler18from werkzeug.exceptions import BadRequest, NotFound19from werkzeug.http import parse_cache_control_header, parse_options_header20from werkzeug.http import http_date21from flask._compat import StringIO, text_type21from flask._compat import StringIO, text_type, PY2222324def has_encoding(name):25 try:
322 unmodified lines
348 os.path.join(app.root_path, 'static/index.html')349 assert rv.mimetype == 'text/html'350 rv.close()351352 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()362363 def test_send_file_object(self):364 app = flask.Flask(__name__)365366 with app.test_request_context():
411 unmodified lines