Skip to content

Commit

Permalink
Validate that a blog post has the minimum required fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
mblayman committed Jul 21, 2015
1 parent 238587b commit 73565fc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
16 changes: 15 additions & 1 deletion handroll/extensions/blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def on_frontmatter_loaded(self, source_file, frontmatter):
"""Record any new blog posts."""
if not self._is_post(frontmatter):
return
# TODO: Validate that the post has the required fields.
self._validate_post(source_file, frontmatter)
post = BlogPost(
date=frontmatter['date'],
source_file=source_file,
Expand Down Expand Up @@ -114,6 +114,20 @@ def _is_post(self, frontmatter):
'{blog_value}').format(blog_value=is_post))
return is_post

def _validate_post(self, source_file, frontmatter):
"""Validate that the post contains all the required fields."""
required = set([
'date',
'title',
])
fields = set(frontmatter.keys())
missing = required - fields
if missing:
raise AbortError(_(
'The blog post, {filename}, '
'is missing required fields: {missing_fields}'.format(
filename=source_file, missing_fields=', '.join(missing))))

def _generate_atom_feed(self, director, blog_posts):
"""Generate the atom feed."""
logger.info(_('Generating Atom XML feed ...'))
Expand Down
12 changes: 8 additions & 4 deletions handroll/locale/handroll.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: handroll 1.6\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2015-07-13 20:29-0400\n"
"POT-Creation-Date: 2015-07-21 11:36-0400\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down Expand Up @@ -171,15 +171,19 @@ msgstr ""
msgid "Invalid blog frontmatter (expects True or False): {blog_value}"
msgstr ""

#: handroll/extensions/blog.py:119
#: handroll/extensions/blog.py:129
msgid "The blog post, {filename}, is missing required fields: {missing_fields}"
msgstr ""

#: handroll/extensions/blog.py:133
msgid "Generating Atom XML feed ..."
msgstr ""

#: handroll/extensions/blog.py:127
#: handroll/extensions/blog.py:141
msgid "Generating blog list page ..."
msgstr ""

#: handroll/extensions/blog.py:144
#: handroll/extensions/blog.py:158
msgid "The blog extension requires the {option} option."
msgstr ""

Expand Down
30 changes: 30 additions & 0 deletions handroll/tests/test_blog_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,36 @@ def test_same_post_does_not_signal_generation(self):
extension.on_frontmatter_loaded('thundercats.md', frontmatter)
self.assertFalse(extension._should_generate)

def test_post_requires_date(self):
extension = self._make_preprocessed_one()
frontmatter = self._make_blog_post_frontmatter()
del frontmatter['date']
try:
extension.on_frontmatter_loaded('thundercats.md', frontmatter)
self.fail()
except AbortError as ae:
self.assertTrue('date' in str(ae))

def test_post_requires_title(self):
extension = self._make_preprocessed_one()
frontmatter = self._make_blog_post_frontmatter()
del frontmatter['title']
try:
extension.on_frontmatter_loaded('thundercats.md', frontmatter)
self.fail()
except AbortError as ae:
self.assertTrue('title' in str(ae))

def test_post_requires_error_includes_post_filename(self):
extension = self._make_preprocessed_one()
frontmatter = self._make_blog_post_frontmatter()
del frontmatter['title']
try:
extension.on_frontmatter_loaded('thundercats.md', frontmatter)
self.fail()
except AbortError as ae:
self.assertTrue('thundercats.md' in str(ae))


class TestBlogBuilder(TestCase):

Expand Down

0 comments on commit 73565fc

Please sign in to comment.