From fe1548d160ff881f50410709547bbf895733ea10 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Tue, 6 Sep 2011 18:30:18 -0400 Subject: dont set explicit date header; we no longer need the request to be passed to FileResponse --- TODO.txt | 2 ++ pyramid/static.py | 17 +++++++---------- pyramid/tests/test_static.py | 20 ++++++++++---------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/TODO.txt b/TODO.txt index bdc3e036a..982af67a5 100644 --- a/TODO.txt +++ b/TODO.txt @@ -7,6 +7,8 @@ Should-Have - Consider adding exclog to all scaffolds to print tracebacks to the console while the debug toolbar is enabled. +- Add cache_max_age=3600 to add_static_view of all scaffolds. + Nice-to-Have ------------ diff --git a/pyramid/static.py b/pyramid/static.py index 93d399f54..d342fad9e 100644 --- a/pyramid/static.py +++ b/pyramid/static.py @@ -1,4 +1,4 @@ -from datetime import datetime, timedelta +from datetime import datetime from os.path import normcase, normpath, join, getmtime, getsize, isdir, exists from pkg_resources import resource_exists, resource_filename, resource_isdir import mimetypes @@ -30,18 +30,17 @@ class FileResponse(Response): """ Serves a static filelike object. """ - def __init__(self, path, expires): + def __init__(self, path, cache_max_age): super(FileResponse, self).__init__(conditional_response=True) - self.last_modified = datetime.fromtimestamp(getmtime(path), tz=UTC) - self.date = datetime.utcnow() + self.last_modified = getmtime(path) self.app_iter = open(path, 'rb') content_type = mimetypes.guess_type(path, strict=False)[0] if content_type is None: content_type = 'application/octet-stream' self.content_type = content_type self.content_length = getsize(path) - if expires is not None: - self.expires = self.date + expires + if cache_max_age is not None: + self.cache_expires = cache_max_age class static_view(object): """ An instance of this class is a callable which can act as a @@ -93,9 +92,7 @@ class static_view(object): # package_name is for bw compat; it is preferred to pass in a # package-relative path as root_dir # (e.g. ``anotherpackage:foo/static``). - if isinstance(cache_max_age, int): - cache_max_age = timedelta(seconds=cache_max_age) - self.expires = cache_max_age + self.cache_max_age = cache_max_age if package_name is None: package_name = caller_package().__name__ package_name, docroot = resolve_asset_spec(root_dir, package_name) @@ -141,7 +138,7 @@ class static_view(object): if not exists(filepath): return HTTPNotFound(request.url) - return self.FileResponse(filepath ,self.expires) + return self.FileResponse(filepath ,self.cache_max_age) def add_slash_redirect(self, request): url = request.path_url + '/' diff --git a/pyramid/tests/test_static.py b/pyramid/tests/test_static.py index 90bc91244..81f25b95d 100644 --- a/pyramid/tests/test_static.py +++ b/pyramid/tests/test_static.py @@ -28,7 +28,7 @@ class Test_static_view_use_subpath_False(unittest.TestCase): inst = self._makeOne('package:resource_name') self.assertEqual(inst.package_name, 'package') self.assertEqual(inst.docroot, 'resource_name') - self.assertEqual(inst.expires, datetime.timedelta(seconds=3600)) + self.assertEqual(inst.cache_max_age, 3600) self.assertEqual(inst.index, 'index.html') def test_call_adds_slash_path_info_empty(self): @@ -85,8 +85,8 @@ class Test_static_view_use_subpath_False(unittest.TestCase): header_names = [ x[0] for x in response.headerlist ] header_names.sort() self.assertEqual(header_names, - ['Content-Length', 'Content-Type', 'Date', 'Expires', - 'Last-Modified']) + ['Cache-Control', 'Content-Length', 'Content-Type', + 'Expires', 'Last-Modified']) def test_resource_is_file_with_no_cache_max_age(self): inst = self._makeOne('pyramid.tests:fixtures/static', @@ -95,12 +95,12 @@ class Test_static_view_use_subpath_False(unittest.TestCase): context = DummyContext() response = inst(context, request) self.assertTrue('static' in response.body) - self.assertEqual(len(response.headerlist), 4) + self.assertEqual(len(response.headerlist), 3) header_names = [ x[0] for x in response.headerlist ] header_names.sort() self.assertEqual( header_names, - ['Content-Length', 'Content-Type', 'Date', 'Last-Modified']) + ['Content-Length', 'Content-Type', 'Last-Modified']) def test_resource_notmodified(self): inst = self._makeOne('pyramid.tests:fixtures/static') @@ -148,7 +148,7 @@ class Test_static_view_use_subpath_True(unittest.TestCase): inst = self._makeOne('package:resource_name') self.assertEqual(inst.package_name, 'package') self.assertEqual(inst.docroot, 'resource_name') - self.assertEqual(inst.expires, datetime.timedelta(seconds=3600)) + self.assertEqual(inst.cache_max_age, 3600) self.assertEqual(inst.index, 'index.html') def test_call_adds_slash_path_info_empty(self): @@ -212,8 +212,8 @@ class Test_static_view_use_subpath_True(unittest.TestCase): header_names = [ x[0] for x in response.headerlist ] header_names.sort() self.assertEqual(header_names, - ['Content-Length', 'Content-Type', 'Date', 'Expires', - 'Last-Modified']) + ['Cache-Control', 'Content-Length', 'Content-Type', + 'Expires', 'Last-Modified']) def test_resource_is_file_with_no_cache_max_age(self): inst = self._makeOne('pyramid.tests:fixtures/static', @@ -223,12 +223,12 @@ class Test_static_view_use_subpath_True(unittest.TestCase): context = DummyContext() response = inst(context, request) self.assertTrue('static' in response.body) - self.assertEqual(len(response.headerlist), 4) + self.assertEqual(len(response.headerlist), 3) header_names = [ x[0] for x in response.headerlist ] header_names.sort() self.assertEqual( header_names, - ['Content-Length', 'Content-Type', 'Date', 'Last-Modified']) + ['Content-Length', 'Content-Type', 'Last-Modified']) def test_resource_notmodified(self): inst = self._makeOne('pyramid.tests:fixtures/static') -- cgit v1.2.3