Skip to content

Commit

Permalink
migrate filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Williams authored and Samuel Williams committed May 25, 2017
1 parent 220dfce commit af074db
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
12 changes: 12 additions & 0 deletions dmutils/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,15 @@ def capitalize_first(maybe_text):
return [capitalize_first(item) for item in maybe_text]

return maybe_text


def question_references(data, get_question):
if not data:
return data
references = re.sub(
r"\[\[([^\]]+)\]\]", # anything that looks like [[nameOfQuestion]]
lambda question_id: str(get_question(question_id.group(1))['number']),
data
)

return data.__class__(references)
1 change: 1 addition & 0 deletions dmutils/flask_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def add_header(response):
application.add_template_filter(filters.format_links)
application.add_template_filter(filters.nbsp)
application.add_template_filter(filters.smartjoin)
application.add_template_filter(filters.question_references)
# Make select formats available in templates.
application.add_template_filter(formats.dateformat)
application.add_template_filter(formats.datetimeformat)
Expand Down
26 changes: 25 additions & 1 deletion tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from flask import Markup

from dmutils.filters import capitalize_first, format_links, nbsp, smartjoin
from dmutils.filters import capitalize_first, format_links, nbsp, smartjoin, question_references


def test_smartjoin_for_more_than_one_item():
Expand Down Expand Up @@ -118,3 +118,27 @@ def test_capitalize_first_for_non_strings():
assert capitalize_first([{'list': 'of'}, 'things']) == [{'list': 'of'}, 'Things']
assert capitalize_first({'this': 'thing'}) == {'this': 'thing'}
assert capitalize_first('https://www.example.com') == 'https://www.example.com'


class TestQuestionReferences(object):

def get_question_mock(self, id):
return {'number': 19}

def test_string_with_with_question_references(self):
assert question_references(
'Please see question [[otherQuestion]] for more info',
self.get_question_mock
) == 'Please see question 19 for more info'

def test_string_with_no_question_references(self):
assert question_references(
'What was the name of your first pet?',
self.get_question_mock
) == 'What was the name of your first pet?'

def test_string_with_broken_question_references(self):
assert question_references(
'Here’s ]][[ a [[string full of ] misused square brackets]',
self.get_question_mock
) == 'Here’s ]][[ a [[string full of ] misused square brackets]'

0 comments on commit af074db

Please sign in to comment.