Contribution for #4 breaks existing tests. Slight change to fix this and test S3_USE_CACHE_CONTROL. Finally, it's preferable to take app as argument for functions that are involved in one-time asset upload, so made that modification too.

This commit is contained in:
Edd Robinson
2013-03-10 15:47:25 +00:00
parent a9d136264c
commit 23d3f4ada8
2 changed files with 19 additions and 15 deletions
+8 -10
View File
@@ -85,7 +85,8 @@ def _static_folder_path(static_url, static_folder, static_asset):
# Now bolt the static url path and the relative asset location together
return u'%s/%s' % (static_url.rstrip('/'), rel_asset.lstrip('/'))
def _write_files(static_url_loc, static_folder, files, bucket, ex_keys=None):
def _write_files(app, static_url_loc, static_folder, files, bucket,
ex_keys=None):
""" Writes all the files inside a static folder to S3. """
for file_path in files:
asset_loc = _path_to_relative_url(file_path)
@@ -94,19 +95,16 @@ def _write_files(static_url_loc, static_folder, files, bucket, ex_keys=None):
if ex_keys and key_name in ex_keys:
logger.debug("%s excluded from upload" % key_name)
else:
app = current_app
k = Key(bucket=bucket, name=key_name)
if (app.config['S3_USE_CACHE_CONTROL']
and 'S3_CACHE_CONTROL' in app.config):
k.metadata.update({
'Cache-Control': app.config['S3_CACHE_CONTROL']
})
if (app.config['S3_USE_CACHE_CONTROL'] and
'S3_CACHE_CONTROL' in app.config):
k.set_metadata('Cache-Control', app.config['S3_CACHE_CONTROL'])
k.set_contents_from_filename(file_path)
k.make_public()
def _upload_files(files_, bucket):
def _upload_files(app, files_, bucket):
for (static_folder, static_url), names in files_.iteritems():
_write_files(static_url, static_folder, names, bucket)
_write_files(app, static_url, static_folder, names, bucket)
def create_all(app, user=None, password=None, bucket_name=None,
location='', include_hidden=False):
@@ -163,7 +161,7 @@ def create_all(app, user=None, password=None, bucket_name=None,
bucket.make_public(recursive=True)
except S3CreateError as e:
raise e
_upload_files(all_files, bucket)
_upload_files(app, all_files, bucket)
class FlaskS3(object):
+11 -5
View File
@@ -3,6 +3,7 @@ import ntpath
from mock import Mock, patch, call
from flask import Flask, render_template_string, Blueprint
from boto.s3.key import Key
import flask_s3
from flask_s3 import FlaskS3
@@ -28,7 +29,7 @@ class FlaskStaticTest(unittest.TestCase):
""" Tests configuration vars exist. """
FlaskS3(self.app)
defaults = ('S3_USE_HTTPS', 'USE_S3', 'USE_S3_DEBUG',
'S3_BUCKET_DOMAIN')
'S3_BUCKET_DOMAIN', 'S3_USE_CACHE_CONTROL')
for default in defaults:
self.assertIn(default, self.app.config)
@@ -107,7 +108,11 @@ class UrlTests(unittest.TestCase):
class S3Tests(unittest.TestCase):
def setUp(self):
self.app = Mock(spec=Flask)
self.app = Flask(__name__)
self.app.testing = True
self.app.config['S3_BUCKET_NAME'] = 'foo'
self.app.config['S3_USE_CACHE_CONTROL'] = True
self.app.config['S3_CACHE_CONTROL'] = 'cache instruction'
def test__bp_static_url(self):
""" Tests test__bp_static_url """
@@ -198,10 +203,11 @@ class S3Tests(unittest.TestCase):
assets = ['/home/z/bar.css', '/home/z/foo.css']
exclude = ['/foo/static/foo.css', '/foo/static/foo/bar.css']
# we expect foo.css to be excluded and not uploaded
expected = [call(bucket=None, name=u'/foo/static/bar.css'),
expected = [call(bucket=None, name=u'/foo/static/bar.css'),
call().set_metadata('Cache-Control', 'cache instruction'),
call().set_contents_from_filename('/home/z/bar.css')]
flask_s3._write_files(static_url_loc, static_folder, assets, None,
exclude)
flask_s3._write_files(self.app, static_url_loc, static_folder, assets,
None, exclude)
self.assertLessEqual(expected, key_mock.mock_calls)
def test_static_folder_path(self):