From 849a0cc18961bbe77751648b8f60c280fa4e9b53 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 24 Sep 2020 17:40:30 +0100 Subject: [PATCH 1/5] Implement optional default for overide_tag --- docs/overview.md | 13 +++++++++++++ pattern_library/monkey_utils.py | 11 ++++++++--- .../atoms/tags_test_atom/tags_test_atom.html | 4 ++++ .../atoms/tags_test_atom/tags_test_atom.yaml | 7 +++++++ tests/templatetags/test_tags.py | 9 ++++++++- tests/tests/test_tags.py | 11 +++++++++++ 6 files changed, 51 insertions(+), 4 deletions(-) diff --git a/docs/overview.md b/docs/overview.md index b3648dab..2a2b4a0f 100644 --- a/docs/overview.md +++ b/docs/overview.md @@ -134,6 +134,19 @@ The override process has two parts: 1. Override your template tag with a mock implementation 2. Define fake result for your tag in a `yaml` file + +### Providing a default value for template tags +To provide a default for a template tag, you need to provide a keyword argument default when overriding your tag. + +``` +from pattern_library.monkey_utils import override_tag + +override_tag(register, 'a_tag_name', default="https://potato.com") +``` + +This default is used for any tag that's not passed it's own context allowing specificity for certain elements that need it but stops the tags breaking when they're not structural to the component. + + ### When do I need to override a template tag? Ideally your pattern library should be independent, so it doesn't fail when diff --git a/pattern_library/monkey_utils.py b/pattern_library/monkey_utils.py index 3e854215..7fa28acb 100644 --- a/pattern_library/monkey_utils.py +++ b/pattern_library/monkey_utils.py @@ -9,7 +9,7 @@ logger = logging.getLogger(__name__) -def override_tag(register, name): +def override_tag(register, name, default=None): """ An utility that helps you override original tags for use in your pattern library. @@ -53,7 +53,7 @@ def node_render(context): request = context.get('request') result = render_pattern(request, template_name, allow_non_patterns=True) tag_overridden = True - + # TODO: Allow objects with the __str__ method # In some cases we must return an object that can # be rendered as a string `{{ result }}` @@ -76,8 +76,13 @@ def node_render(context): # Render result instead of the tag return result else: + if default: + # Render provided default; + # if no stub data supplied. + return default + logger.warning( - 'No stub data defined for the "%s" tag in the "%s" template', + 'No default or stub data defined for the "%s" tag in the "%s" template', tag_name, current_template_name ) diff --git a/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.html b/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.html index 09258c7d..05486276 100644 --- a/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.html +++ b/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.html @@ -3,3 +3,7 @@ SAND{% error_tag empty_string %}WICH SAND{% error_tag none %}WICH SAND{% error_tag zero %}WICH + +POTA{% default_tag page.url %}TO +POTA{% default_tag page.child.url %}TO +POTA{% default_tag None %}TO \ No newline at end of file diff --git a/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.yaml b/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.yaml index 0fe3edf6..71c4b638 100644 --- a/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.yaml +++ b/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.yaml @@ -6,3 +6,10 @@ tags: raw: null zero: raw: 0 + default_tag: + page.url: + raw: "example" + page.child.url: + raw: "another_example" + # None supplied by default value. + \ No newline at end of file diff --git a/tests/templatetags/test_tags.py b/tests/templatetags/test_tags.py index 581edb9d..8b210ecb 100644 --- a/tests/templatetags/test_tags.py +++ b/tests/templatetags/test_tags.py @@ -4,11 +4,18 @@ register = template.Library() - +# Basic template tag @register.simple_tag def error_tag(arg=None): "Just raise an exception, never do anything" raise Exception("error_tag raised an exception") +# Template tag to to test setting a default. +@register.simple_tag() +def default_tag(arg=None): + "Just raise an exception, never do anything" + raise Exception("default_tag raised an exception") + +override_tag(register, 'default_tag', default="https://potato.com") override_tag(register, 'error_tag') diff --git a/tests/tests/test_tags.py b/tests/tests/test_tags.py index 8b0904a0..a9fd138e 100644 --- a/tests/tests/test_tags.py +++ b/tests/tests/test_tags.py @@ -12,3 +12,14 @@ def test_falsey_raw_values_for_tag_output(self): self.assertContains(response, "SANDWICH") self.assertContains(response, "SANDNoneWICH") self.assertContains(response, "SAND0WICH") + + def test_default_override(self): + response = self.client.get(reverse( + 'pattern_library:render_pattern', + kwargs={'pattern_template_name': 'patterns/atoms/tags_test_atom/tags_test_atom.html'}, + )) + + self.assertContains(response, "POTAexampleTO") + self.assertContains(response, "POTAanother_exampleTO") + self.assertContains(response, "POTAhttps://potato.comTO") + From 8b150074284dea37c719eb91e061a649ef92b2b3 Mon Sep 17 00:00:00 2001 From: William Date: Mon, 19 Oct 2020 16:39:29 +0100 Subject: [PATCH 2/5] WIP: Update from internal review --- docs/overview.md | 3 +-- pattern_library/monkey_utils.py | 14 +++++++------- .../atoms/tags_test_atom/tags_test_atom.html | 10 +++++++--- .../atoms/tags_test_atom/tags_test_atom.yaml | 9 +++++++-- tests/templatetags/test_tags.py | 16 +++++++++++++--- tests/tests/test_tags.py | 14 +++++++++++--- 6 files changed, 46 insertions(+), 20 deletions(-) diff --git a/docs/overview.md b/docs/overview.md index 2a2b4a0f..fb593150 100644 --- a/docs/overview.md +++ b/docs/overview.md @@ -144,8 +144,7 @@ from pattern_library.monkey_utils import override_tag override_tag(register, 'a_tag_name', default="https://potato.com") ``` -This default is used for any tag that's not passed it's own context allowing specificity for certain elements that need it but stops the tags breaking when they're not structural to the component. - +This default is used for any tag that's not passed its own context, allowing specificity for those elements that need it while preventing the tag from breaking when it's not structural to the component. ### When do I need to override a template tag? diff --git a/pattern_library/monkey_utils.py b/pattern_library/monkey_utils.py index 7fa28acb..1b13b26f 100644 --- a/pattern_library/monkey_utils.py +++ b/pattern_library/monkey_utils.py @@ -7,9 +7,10 @@ ) logger = logging.getLogger(__name__) +UNSPECIFIED = object() -def override_tag(register, name, default=None): +def override_tag(register, name, default_html=None): """ An utility that helps you override original tags for use in your pattern library. @@ -53,7 +54,7 @@ def node_render(context): request = context.get('request') result = render_pattern(request, template_name, allow_non_patterns=True) tag_overridden = True - + # TODO: Allow objects with the __str__ method # In some cases we must return an object that can # be rendered as a string `{{ result }}` @@ -75,12 +76,11 @@ def node_render(context): # Render result instead of the tag return result + elif default_html is not UNSPECIFIED: + # Render provided default; + # if no stub data supplied. + return default_html else: - if default: - # Render provided default; - # if no stub data supplied. - return default - logger.warning( 'No default or stub data defined for the "%s" tag in the "%s" template', tag_name, current_template_name diff --git a/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.html b/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.html index 05486276..3035a7e1 100644 --- a/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.html +++ b/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.html @@ -4,6 +4,10 @@ SAND{% error_tag none %}WICH SAND{% error_tag zero %}WICH -POTA{% default_tag page.url %}TO -POTA{% default_tag page.child.url %}TO -POTA{% default_tag None %}TO \ No newline at end of file +POTA{% default_html_tag page.url %}TO +POTA{% default_html_tag page.child.url %}TO +POTA{% default_html_tag None %}TO + +POTA{% default_html_tag page.url %}TO1 +POTA{% default_html_tag page.child.url %}TO2 +POTA{% default_html_tag %}TO3 \ No newline at end of file diff --git a/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.yaml b/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.yaml index 71c4b638..91bc0b39 100644 --- a/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.yaml +++ b/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.yaml @@ -6,10 +6,15 @@ tags: raw: null zero: raw: 0 - default_tag: + default_html_tag: page.url: raw: "example" page.child.url: raw: "another_example" - # None supplied by default value. + # None handled by default html value. + default_html_tag_falsey: + page.url: + raw: "None" + page.child.url: + raw: "False" \ No newline at end of file diff --git a/tests/templatetags/test_tags.py b/tests/templatetags/test_tags.py index 8b210ecb..e069621a 100644 --- a/tests/templatetags/test_tags.py +++ b/tests/templatetags/test_tags.py @@ -4,18 +4,28 @@ register = template.Library() + # Basic template tag @register.simple_tag def error_tag(arg=None): "Just raise an exception, never do anything" raise Exception("error_tag raised an exception") -# Template tag to to test setting a default. + +# Template tag to to test setting a default html value. +@register.simple_tag() +def default_html_tag(arg=None): + "Just raise an exception, never do anything" + raise Exception("default_tag raised an exception") + + +# Template tag to to test setting a default html value that is falsey. @register.simple_tag() -def default_tag(arg=None): +def default_html_tag_falsey(arg=None): "Just raise an exception, never do anything" raise Exception("default_tag raised an exception") -override_tag(register, 'default_tag', default="https://potato.com") override_tag(register, 'error_tag') +override_tag(register, 'default_html_tag', default_html="https://potato.com") +override_tag(register, 'default_html_tag_falsey', default_html="None") diff --git a/tests/tests/test_tags.py b/tests/tests/test_tags.py index a9fd138e..3f5964ee 100644 --- a/tests/tests/test_tags.py +++ b/tests/tests/test_tags.py @@ -3,7 +3,7 @@ from .utils import reverse -class TagsTesCase(SimpleTestCase): +class TagsTestCase(SimpleTestCase): def test_falsey_raw_values_for_tag_output(self): response = self.client.get(reverse( 'pattern_library:render_pattern', @@ -12,8 +12,8 @@ def test_falsey_raw_values_for_tag_output(self): self.assertContains(response, "SANDWICH") self.assertContains(response, "SANDNoneWICH") self.assertContains(response, "SAND0WICH") - - def test_default_override(self): + + def test_default_html_override(self): response = self.client.get(reverse( 'pattern_library:render_pattern', kwargs={'pattern_template_name': 'patterns/atoms/tags_test_atom/tags_test_atom.html'}, @@ -23,3 +23,11 @@ def test_default_override(self): self.assertContains(response, "POTAanother_exampleTO") self.assertContains(response, "POTAhttps://potato.comTO") + def test_falsey_default_html_overide(self): + response = self.client.get(reverse( + 'pattern_library:render_pattern', + kwargs={'pattern_template_name': 'patterns/atoms/tags_test_atom/tags_test_atom.html'}, + )) + self.assertContains(response, "POTATO1") + self.assertContains(response, "POTATO2") + self.assertContains(response, "POTATO3") From 12dc71e9906dbceecee8afc4c49666cb6e317cd2 Mon Sep 17 00:00:00 2001 From: William Date: Wed, 28 Oct 2020 12:37:27 +0000 Subject: [PATCH 3/5] Updates tests --- .../patterns/atoms/tags_test_atom/tags_test_atom.html | 6 +++--- .../patterns/atoms/tags_test_atom/tags_test_atom.yaml | 10 ++++++---- tests/templatetags/test_tags.py | 2 +- tests/tests/test_tags.py | 4 ++-- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.html b/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.html index 3035a7e1..c7945bc8 100644 --- a/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.html +++ b/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.html @@ -8,6 +8,6 @@ POTA{% default_html_tag page.child.url %}TO POTA{% default_html_tag None %}TO -POTA{% default_html_tag page.url %}TO1 -POTA{% default_html_tag page.child.url %}TO2 -POTA{% default_html_tag %}TO3 \ No newline at end of file +POTA{% default_html_tag_falsey empty_string %}TO1 +POTA{% default_html_tag_falsey none %}TO2 +POTA{% default_html_tag_falsey zero %}TO3 \ No newline at end of file diff --git a/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.yaml b/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.yaml index 91bc0b39..5f65be91 100644 --- a/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.yaml +++ b/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.yaml @@ -13,8 +13,10 @@ tags: raw: "another_example" # None handled by default html value. default_html_tag_falsey: - page.url: - raw: "None" - page.child.url: - raw: "False" + empty_string: + raw: '' + none: + raw: null + zero: + raw: 0 \ No newline at end of file diff --git a/tests/templatetags/test_tags.py b/tests/templatetags/test_tags.py index e069621a..9fedee03 100644 --- a/tests/templatetags/test_tags.py +++ b/tests/templatetags/test_tags.py @@ -28,4 +28,4 @@ def default_html_tag_falsey(arg=None): override_tag(register, 'error_tag') override_tag(register, 'default_html_tag', default_html="https://potato.com") -override_tag(register, 'default_html_tag_falsey', default_html="None") +override_tag(register, 'default_html_tag_falsey', default_html=None) diff --git a/tests/tests/test_tags.py b/tests/tests/test_tags.py index 3f5964ee..78ba1193 100644 --- a/tests/tests/test_tags.py +++ b/tests/tests/test_tags.py @@ -29,5 +29,5 @@ def test_falsey_default_html_overide(self): kwargs={'pattern_template_name': 'patterns/atoms/tags_test_atom/tags_test_atom.html'}, )) self.assertContains(response, "POTATO1") - self.assertContains(response, "POTATO2") - self.assertContains(response, "POTATO3") + self.assertContains(response, "POTANoneTO2") + self.assertContains(response, "POTA0TO3") From 021300c0d877d304f4cbf92f74ad0b8c32ec94f0 Mon Sep 17 00:00:00 2001 From: William Date: Wed, 28 Oct 2020 13:04:53 +0000 Subject: [PATCH 4/5] Update Docs --- docs/overview.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/overview.md b/docs/overview.md index fb593150..a92d77ec 100644 --- a/docs/overview.md +++ b/docs/overview.md @@ -146,6 +146,9 @@ override_tag(register, 'a_tag_name', default="https://potato.com") This default is used for any tag that's not passed its own context, allowing specificity for those elements that need it while preventing the tag from breaking when it's not structural to the component. +#### limitation +Currently the default override is limited to direct references only i.e. {% a_tag_name page.url %} and not {% a_tag_name page.url as variable_name %} + ### When do I need to override a template tag? Ideally your pattern library should be independent, so it doesn't fail when From 8647dab55823fec14befb56d86027c52f04b8800 Mon Sep 17 00:00:00 2001 From: William Date: Wed, 28 Oct 2020 14:19:14 +0000 Subject: [PATCH 5/5] Tweak documentation from review --- docs/overview.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/docs/overview.md b/docs/overview.md index a92d77ec..617237cf 100644 --- a/docs/overview.md +++ b/docs/overview.md @@ -136,19 +136,18 @@ The override process has two parts: ### Providing a default value for template tags -To provide a default for a template tag, you need to provide a keyword argument default when overriding your tag. +To provide a default for a template tag, you need to provide a keyword argument default_html when overriding your tag. ``` from pattern_library.monkey_utils import override_tag -override_tag(register, 'a_tag_name', default="https://potato.com") +override_tag(register, 'a_tag_name', default_html="https://potato.com") ``` This default is used for any tag that's not passed its own context, allowing specificity for those elements that need it while preventing the tag from breaking when it's not structural to the component. -#### limitation -Currently the default override is limited to direct references only i.e. {% a_tag_name page.url %} and not {% a_tag_name page.url as variable_name %} - +#### Limitation +Currently this feature only supports providing a default for the output of the tag, this does not support modifying context in templates such as {% an_example_tag page.url as example_variable %}. ### When do I need to override a template tag? Ideally your pattern library should be independent, so it doesn't fail when