Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #40 from max-k/master

Add an option to allow freeze to continue when a 404 error is returned
  • Loading branch information...
commit 508cb0ae8e74cff1411a5bb78dc386beb727dfec 2 parents 0f88d02 + e795c18
@SimonSapin authored
Showing with 56 additions and 3 deletions.
  1. +10 −0 docs/index.rst
  2. +13 −2 flask_frozen/__init__.py
  3. +33 −1 flask_frozen/tests.py
View
10 docs/index.rst
@@ -274,6 +274,16 @@ are accepted:
.. versionadded:: 0.12
+``FREEZER_IGNORE_404_NOT_FOUND``
+ If set to ``True`` (defaults False), Frozen-Flask won't stop freezing when
+ 404 error is returned by your application.
+ In this case, a warning will be printed on stdout and the static page will
+ be generated using your 404 error page handler or flask's default one.
+ This can be usefull during development phase if you have already referenced
+ pages wich aren't written yet.
+
+ .. versionadded:: 0.12
+
.. _mime-types:
Filenames and MIME types
View
15 flask_frozen/__init__.py
@@ -52,6 +52,8 @@ class MissingURLGeneratorWarning(Warning):
class MimetypeMismatchWarning(Warning):
pass
+class NotFoundWarning(Warning):
+ pass
class Freezer(object):
"""
@@ -100,6 +102,7 @@ def init_app(self, app):
'application/octet-stream')
app.config.setdefault('FREEZER_IGNORE_MIMETYPE_WARNINGS', False)
app.config.setdefault('FREEZER_RELATIVE_URLS', False)
+ app.config.setdefault('FREEZER_IGNORE_404_NOT_FOUND', False)
def register_generator(self, function):
"""Register a function as an URL generator.
@@ -259,9 +262,17 @@ def _build_one(self, url):
# The client follows redirects by itself
# Any other status code is probably an error
+ # except we explictly want 404 errors to be skipped
+ # (eg. while application is in development)
+ ignore_404 = self.app.config['FREEZER_IGNORE_404_NOT_FOUND']
if response.status_code != 200:
- raise ValueError('Unexpected status %r on URL %s' \
- % (response.status, url))
+ if response.status_code == 404 and ignore_404:
+ warnings.warn('Ignored %r on URL %s' % (response.status, url),
+ NotFoundWarning,
+ stacklevel=3)
+ else:
+ raise ValueError('Unexpected status %r on URL %s' \
+ % (response.status, url))
destination_path = self.urlpath_to_filepath(url)
filename = os.path.join(self.root, *destination_path.split('/'))
View
34 flask_frozen/tests.py
@@ -21,7 +21,7 @@
from warnings import catch_warnings
from flask_frozen import (Freezer, walk_directory,
- MissingURLGeneratorWarning, MimetypeMismatchWarning)
+ MissingURLGeneratorWarning, MimetypeMismatchWarning, NotFoundWarning)
from flask_frozen import test_app
try:
@@ -169,6 +169,15 @@ def make_app(self):
yield temp, app, freezer
@contextmanager
+ def make_app_with_404(self):
+ # Build an app with a link to a non-existent route
+ with self.make_app() as (temp, app, freezer):
+ @freezer.register_generator
+ def non_existent_url():
+ yield '/404/'
+ yield temp, app, freezer
+
+ @contextmanager
def built_app(self):
with self.make_app() as (temp, app, freezer):
urls = freezer.freeze()
@@ -271,6 +280,29 @@ def external_url():
else:
assert False, 'Expected ValueError'
+ def test_error_on_internal_404(self):
+ with self.make_app_with_404() as (temp, app, freezer):
+ # Test standard behaviour with 404 errors (freeze failure)
+ try:
+ freezer.freeze()
+ except ValueError as e:
+ error_msg = "Unexpected status '404 NOT FOUND' on URL /404/"
+ assert error_msg in e.args[0]
+ else:
+ assert False, 'Expected ValueError'
+
+ def test_warn_on_internal_404(self):
+ with self.make_app_with_404() as (temp, app, freezer):
+ # Enable 404 erros ignoring
+ app.config['FREEZER_IGNORE_404_NOT_FOUND'] = True
+ # Test warning with 404 errors when we choose to ignore them
+ with catch_warnings(record=True) as logged_warnings:
+ warnings.simplefilter("always")
+ freezer.freeze()
+ self.assertEqual(len(logged_warnings), 1)
+ self.assertEqual(logged_warnings[0].category,
+ NotFoundWarning)
+
def test_warn_on_missing_generator(self):
with self.make_app() as (temp, app, freezer):
# Add a new endpoint without URL generator
Please sign in to comment.
Something went wrong with that request. Please try again.