Browse Source

pass value directly to last_modified

pull/1873/merge
David Lord 9 years ago
parent
commit
7c271401b2
  1. 14
      flask/helpers.py
  2. 17
      tests/test_helpers.py

14
flask/helpers.py

@ -483,8 +483,9 @@ 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 :param last_modified: set the ``Last-Modified`` header to this value,
the input file was last modified. a :class:`~datetime.datetime` or timestamp.
If a file was passed, this overrides its mtime.
""" """
mtime = None mtime = None
if isinstance(filename_or_fp, string_types): if isinstance(filename_or_fp, string_types):
@ -528,15 +529,10 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
rv = current_app.response_class(data, mimetype=mimetype, headers=headers, rv = current_app.response_class(data, mimetype=mimetype, headers=headers,
direct_passthrough=True) direct_passthrough=True)
# if we know the file modification date, we can store it as
# the time of the last modification.
if last_modified is not None: if last_modified is not None:
if PY2: rv.last_modified = last_modified
rv.last_modified = int(last_modified.strftime("%s"))
else:
rv.last_modified = last_modified.timestamp()
elif mtime is not None: elif mtime is not None:
rv.last_modified = int(mtime) rv.last_modified = mtime
rv.cache_control.public = True rv.cache_control.public = True
if cache_timeout is None: if cache_timeout is None:

17
tests/test_helpers.py

@ -351,14 +351,15 @@ class TestSendfile(object):
def test_send_file_last_modified(self): def test_send_file_last_modified(self):
app = flask.Flask(__name__) app = flask.Flask(__name__)
with app.test_request_context(): last_modified = datetime.datetime(1999, 1, 1)
dtm = datetime.datetime.now()
rv = flask.send_file('static/index.html', last_modified=dtm) @app.route('/')
if PY2: def index():
assert rv.last_modified == int(dtm.strftime("%s")) return flask.send_file(StringIO("party like it's"), last_modified=last_modified)
else:
assert rv.last_modified == dtm.timestamp() c = app.test_client()
rv.close() rv = c.get('/')
assert rv.last_modified == last_modified
def test_send_file_object(self): def test_send_file_object(self):
app = flask.Flask(__name__) app = flask.Flask(__name__)

Loading…
Cancel
Save