Add last_modified arg for send_file

Enhancement: Add last_modified arg of type DateTime to send_file.

Fixes pallets/flask#1321
This commit is contained in:
Prachi Shirish Khadke 2016-06-02 13:00:42 -07:00 committed by David Lord
parent 64a37bb9b7
commit af515cc7ea
2 changed files with 22 additions and 4 deletions

View file

@ -39,7 +39,7 @@ from jinja2 import FileSystemLoader
from .signals import message_flashed from .signals import message_flashed
from .globals import session, _request_ctx_stack, _app_ctx_stack, \ from .globals import session, _request_ctx_stack, _app_ctx_stack, \
current_app, request current_app, request
from ._compat import string_types, text_type from ._compat import string_types, text_type, PY2
# sentinel # sentinel
@ -429,7 +429,7 @@ def get_flashed_messages(with_categories=False, category_filter=[]):
def send_file(filename_or_fp, mimetype=None, as_attachment=False, def send_file(filename_or_fp, mimetype=None, as_attachment=False,
attachment_filename=None, add_etags=True, attachment_filename=None, add_etags=True,
cache_timeout=None, conditional=False): cache_timeout=None, conditional=False, last_modified=None):
"""Sends the contents of a file to the client. This will use the """Sends the contents of a file to the client. This will use the
most efficient method available and configured. By default it will most efficient method available and configured. By default it will
try to use the WSGI server's file_wrapper support. Alternatively try to use the WSGI server's file_wrapper support. Alternatively
@ -483,6 +483,8 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
(default), this value is set by (default), this value is set by
:meth:`~Flask.get_send_file_max_age` of :meth:`~Flask.get_send_file_max_age` of
:data:`~flask.current_app`. :data:`~flask.current_app`.
:param last_modified: the Datetime object representing timestamp for when
the input file was last modified.
""" """
mtime = None mtime = None
if isinstance(filename_or_fp, string_types): if isinstance(filename_or_fp, string_types):
@ -528,7 +530,12 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
# if we know the file modification date, we can store it as # if we know the file modification date, we can store it as
# the time of the last modification. # the time of the last modification.
if mtime is not None: if last_modified is not None:
if PY2:
rv.last_modified = int(last_modified.strftime("%s"))
else:
rv.last_modified = last_modified.timestamp()
elif mtime is not None:
rv.last_modified = int(mtime) rv.last_modified = int(mtime)
rv.cache_control.public = True rv.cache_control.public = True

View file

@ -18,7 +18,7 @@ from logging import StreamHandler
from werkzeug.exceptions import BadRequest, NotFound from werkzeug.exceptions import BadRequest, NotFound
from werkzeug.http import parse_cache_control_header, parse_options_header from werkzeug.http import parse_cache_control_header, parse_options_header
from werkzeug.http import http_date from werkzeug.http import http_date
from flask._compat import StringIO, text_type from flask._compat import StringIO, text_type, PY2
def has_encoding(name): def has_encoding(name):
@ -349,6 +349,17 @@ class TestSendfile(object):
assert rv.mimetype == 'text/html' assert rv.mimetype == 'text/html'
rv.close() rv.close()
def test_send_file_last_modified(self):
app = flask.Flask(__name__)
with app.test_request_context():
dtm = datetime.datetime.now()
rv = flask.send_file('static/index.html', last_modified=dtm)
if PY2:
assert rv.last_modified == int(dtm.strftime("%s"))
else:
assert rv.last_modified == dtm.timestamp()
rv.close()
def test_send_file_object(self): def test_send_file_object(self):
app = flask.Flask(__name__) app = flask.Flask(__name__)