Skip to content

Commit

Permalink
Merge ae36840 into 905b886
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarosław Wygoda committed Jan 16, 2018
2 parents 905b886 + ae36840 commit c708a6f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
10 changes: 10 additions & 0 deletions bakery/management/commands/build.py
Expand Up @@ -199,14 +199,22 @@ def build_static(self, *args, **options):
# and favicon.ico files down to the root so they will work
# on the live website.
robots_src = path.join(target_dir, 'robots.txt')
if getattr(settings, 'GZIP_SUFFIX', False):
robots_src += '.gz'
if self.fs.exists(robots_src):
robots_target = path.join(self.build_dir, 'robots.txt')
if getattr(settings, 'GZIP_SUFFIX', False):
robots_target += '.gz'
logger.debug("Copying {}{} to {}{}".format(self.fs_name, robots_src, self.fs_name, robots_target))
self.fs.copy(robots_src, robots_target)

favicon_src = path.join(target_dir, 'favicon.ico')
if getattr(settings, 'GZIP_SUFFIX', False):
favicon_src += '.gz'
if self.fs.exists(favicon_src):
favicon_target = path.join(self.build_dir, 'favicon.ico')
if getattr(settings, 'GZIP_SUFFIX', False):
favicon_target += '.gz'
logger.debug("Copying {}{} to {}{}".format(self.fs_name, favicon_src, self.fs_name, favicon_target))
self.fs.copy(favicon_src, favicon_target)

Expand Down Expand Up @@ -255,6 +263,8 @@ def copytree_and_gzip(self, source_dir, target_dir):
source_path = os.path.join(dirpath, f)
rel_path = os.path.relpath(dirpath, source_dir)
target_path = os.path.join(target_dir, rel_path, f)
if getattr(settings, 'GZIP_SUFFIX', False):
target_path += '.gz'
# Add it to our list to build
build_list.append((source_path, target_path))

Expand Down
42 changes: 42 additions & 0 deletions bakery/tests/__init__.py
Expand Up @@ -143,6 +143,8 @@ def test_template_view(self):
v.build_method
v.build()
build_path = os.path.join(settings.BUILD_DIR, 'foo.html')
if getattr(settings, 'GZIP_SUFFIX', False):
build_path += '.gz'
self.assertTrue(os.path.exists(build_path))
os.remove(build_path)
v = views.BuildableTemplateView(
Expand All @@ -152,6 +154,8 @@ def test_template_view(self):
v.build_method
v.build()
build_path = os.path.join(settings.BUILD_DIR, 'foo', 'bar.html')
if getattr(settings, 'GZIP_SUFFIX', False):
build_path += '.gz'
self.assertTrue(os.path.exists(build_path))
os.remove(build_path)

Expand All @@ -164,6 +168,8 @@ def test_list_view(self):
v.build_method
v.build_queryset()
build_path = os.path.join(settings.BUILD_DIR, 'foo.html')
if getattr(settings, 'GZIP_SUFFIX', False):
build_path += '.gz'
self.assertTrue(os.path.exists(build_path))
os.remove(build_path)
v = views.BuildableListView(
Expand All @@ -174,6 +180,8 @@ def test_list_view(self):
v.build_method
v.build_queryset()
build_path = os.path.join(settings.BUILD_DIR, 'foo', 'bar.html')
if getattr(settings, 'GZIP_SUFFIX', False):
build_path += '.gz'
self.assertTrue(os.path.exists(build_path))
os.remove(build_path)

Expand All @@ -191,6 +199,8 @@ def test_detail_view(self):
o.get_absolute_url().lstrip('/'),
'index.html',
)
if getattr(settings, 'GZIP_SUFFIX', False):
build_path += '.gz'
self.assertTrue(os.path.exists(build_path))
v.unbuild_object(o)
self.assertTrue(v.kwargs['slug'] == v.kwargs['this_slug'])
Expand All @@ -204,6 +214,8 @@ def test_index_view(self):
v.build_method
v.build_queryset()
build_path = os.path.join(settings.BUILD_DIR, v.build_path)
if getattr(settings, 'GZIP_SUFFIX', False):
build_path += '.gz'
self.assertTrue(os.path.exists(build_path))

def test_year_view(self):
Expand All @@ -218,6 +230,8 @@ def test_year_view(self):
'%s' % y,
'index.html'
)
if getattr(settings, 'GZIP_SUFFIX', False):
build_path += '.gz'
self.assertTrue(os.path.exists(build_path))

def test_month_view(self):
Expand All @@ -233,6 +247,8 @@ def test_month_view(self):
month,
'index.html'
)
if getattr(settings, 'GZIP_SUFFIX', False):
build_path += '.gz'
self.assertTrue(os.path.exists(build_path))

def test_day_view(self):
Expand All @@ -253,6 +269,8 @@ def test_day_view(self):
day,
'index.html'
)
if getattr(settings, 'GZIP_SUFFIX', False):
build_path += '.gz'
self.assertTrue(os.path.exists(build_path))

def test_redirect_view(self):
Expand All @@ -267,20 +285,26 @@ def test_redirect_view(self):
settings.BUILD_DIR,
"detail/badurl.html"
)
if getattr(settings, 'GZIP_SUFFIX', False):
build_path += '.gz'
self.assertTrue(os.path.exists(build_path))

def test_404_view(self):
v = views.Buildable404View()
v.build_method
v.build()
build_path = os.path.join(settings.BUILD_DIR, '404.html')
if getattr(settings, 'GZIP_SUFFIX', False):
build_path += '.gz'
self.assertTrue(os.path.exists(build_path))
os.remove(build_path)

def test_json_view(self):
v = MockJSONView()
v.build()
build_path = os.path.join(settings.BUILD_DIR, 'jsonview.json')
if getattr(settings, 'GZIP_SUFFIX', False):
build_path += '.gz'
self.assertTrue(os.path.exists(build_path))
self.assertEqual(
json.loads(open(build_path, 'rb').read().decode()),
Expand All @@ -293,6 +317,8 @@ def test_rss_feed(self):
f.build_method
f.build_queryset()
build_path = os.path.join(settings.BUILD_DIR, 'feed.xml')
if getattr(settings, 'GZIP_SUFFIX', False):
build_path += '.gz'
self.assertTrue(os.path.exists(build_path))
os.remove(build_path)

Expand All @@ -316,14 +342,20 @@ def test_build_cmd(self):
'static',
'foo.bar'
)
if getattr(settings, 'GZIP_SUFFIX', False):
foobar_path += '.gz'
self.assertTrue(os.path.exists(foobar_path))
self.assertEqual(
open(foobar_path, 'rb').read().strip(),
six.b('Hello tests')
)
robots_path = os.path.join(settings.BUILD_DIR, 'robots.txt')
if getattr(settings, 'GZIP_SUFFIX', False):
robots_path += '.gz'
self.assertTrue(os.path.exists(robots_path))
favicon_path = os.path.join(settings.BUILD_DIR, 'favicon.ico')
if getattr(settings, 'GZIP_SUFFIX', False):
favicon_path += '.gz'
self.assertTrue(os.path.exists(favicon_path))

def test_unbuild_cmd(self):
Expand All @@ -339,6 +371,16 @@ def test_gzipped(self):
self.test_404_view()
self.test_build_cmd()

def test_gzipped_with_suffix(self):
with self.settings(BAKERY_GZIP=True, GZIP_SUFFIX=True):
six.print_("testing gzipped files with suffix")
self.test_models()
self.test_template_view()
self.test_list_view()
self.test_detail_view()
self.test_404_view()
self.test_build_cmd()

def test_buildserver_cmd(self):
pass

Expand Down
3 changes: 3 additions & 0 deletions bakery/views/base.py
Expand Up @@ -107,6 +107,9 @@ def gzip_file(self, target_path, html):
"""
logger.debug("Gzipping to {}{}".format(self.fs_name, target_path))

if getattr(settings, 'GZIP_SUFFIX', False):
target_path += '.gz'

# Write GZIP data to an in-memory buffer
data_buffer = six.BytesIO()
kwargs = dict(
Expand Down
7 changes: 7 additions & 0 deletions docs/settingsvariables.rst
Expand Up @@ -188,6 +188,13 @@ GZIP_CONTENT_TYPES
"text/xml"
)
GZIP_SUFFIX
------------------

.. envvar:: GZIP_SUFFIX

Opt in to add '.gz' suffix to your gzipped files. Useful when using nginx with `http_gzip_static_module <http://nginx.org/en/docs/http/ngx_http_gzip_static_module.html>`_.

DEFAULT_ACL
---------------
.. envvar:: DEFAULT_ACL
Expand Down

0 comments on commit c708a6f

Please sign in to comment.