From 41b388a09fa3fba758fa945d0f9a97fb0aba4d5c Mon Sep 17 00:00:00 2001 From: Karol Duleba Date: Tue, 13 Jan 2015 09:02:38 +0000 Subject: [PATCH] Add ifnotswitch template tag --- docs/usage/index.rst | 10 ++- gargoyle/templatetags/gargoyle_tags.py | 22 ++++- tests/tests.py | 115 +++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 6 deletions(-) diff --git a/docs/usage/index.rst b/docs/usage/index.rst index dbee1a47..cd5b4dc5 100644 --- a/docs/usage/index.rst +++ b/docs/usage/index.rst @@ -51,7 +51,7 @@ to perform validation on your own custom objects:: ifswitch ~~~~~~~~ -If you prefer to use templatetags, Gargoyle provides a helper called ``ifswitch`` to give you easy conditional blocks based on active switches (for the request):: +If you prefer to use templatetags, Gargoyle provides a helpers called ``ifswitch`` and ``ifnotswitch`` to give you easy conditional blocks based on active or inactive switches (for the request):: {% load gargoyle_tags %} @@ -61,7 +61,13 @@ If you prefer to use templatetags, Gargoyle provides a helper called ``ifswitch` switch_name is not active :( {% endifswitch %} -``ifswitch`` can also be used with custom objects, like the ``gargoyle.is_active`` method:: + {% ifnotswitch other_switch_name %} + other_switch_name is not active! + {% else %} + other_switch_name is active! + {% endifnotswitch %} + +``ifswitch`` and ``ifnotswitch`` can also be used with custom objects, like the ``gargoyle.is_active`` method:: {% ifswitch "my switch name" user %} "my switch name" is active! diff --git a/gargoyle/templatetags/gargoyle_tags.py b/gargoyle/templatetags/gargoyle_tags.py index 44dcc3b6..c6421086 100644 --- a/gargoyle/templatetags/gargoyle_tags.py +++ b/gargoyle/templatetags/gargoyle_tags.py @@ -13,27 +13,41 @@ register = template.Library() -@register.tag -def ifswitch(parser, token): +def switch(parser, token): bits = token.split_contents() if len(bits) < 2: raise template.TemplateSyntaxError("%r tag requires an argument" % token.contents.split()[0]) name = bits[1] instances = bits[2:] + endtag = 'end{}'.format(bits[0]) - nodelist_true = parser.parse(('else', 'endifswitch')) + nodelist_true = parser.parse(('else', endtag)) token = parser.next_token() if token.contents == 'else': - nodelist_false = parser.parse(('endifswitch',)) + nodelist_false = parser.parse((endtag,)) parser.delete_first_token() else: nodelist_false = template.NodeList() + return nodelist_true, nodelist_false, name, instances + + +@register.tag +def ifswitch(parser, token): + nodelist_true, nodelist_false, name, instances = switch(parser, token) + return SwitchNode(nodelist_true, nodelist_false, name, instances) +@register.tag +def ifnotswitch(parser, token): + nodelist_true, nodelist_false, name, instances = switch(parser, token) + + return SwitchNode(nodelist_false, nodelist_true, name, instances) + + class SwitchNode(template.Node): def __init__(self, nodelist_true, nodelist_false, name, instances): self.nodelist_true = nodelist_true diff --git a/tests/tests.py b/tests/tests.py index 509f06e6..161dfac1 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -1037,6 +1037,121 @@ def test_with_custom_objects(self): self.assertTrue('hello world!' in rendered) +class NegatedTemplateTagTest(TestCase): + urls = 'tests.urls' + + def setUp(self): + self.user = User.objects.create(username='foo', email='foo@example.com') + self.gargoyle = SwitchManager(Switch, key='key', value='value', instances=True) + self.gargoyle.register(UserConditionSet(User)) + + def test_simple(self): + Switch.objects.create( + key='test', + status=GLOBAL, + ) + + template = Template(""" + {% load gargoyle_tags %} + {% ifnotswitch test %} + hello world! + {% endifnotswitch %} + """) + rendered = template.render(Context()) + + self.assertFalse('hello world!' in rendered) + + def test_else(self): + Switch.objects.create( + key='test', + status=DISABLED, + ) + + template = Template(""" + {% load gargoyle_tags %} + {% ifnotswitch test %} + hello world! + {% else %} + foo bar baz + {% endifnotswitch %} + """) + rendered = template.render(Context()) + + self.assertFalse('foo bar baz' in rendered) + self.assertTrue('hello world!' in rendered) + + def test_with_request(self): + condition_set = 'gargoyle.builtins.UserConditionSet(auth.user)' + + switch = Switch.objects.create( + key='test', + status=SELECTIVE, + ) + switch = self.gargoyle['test'] + + switch.add_condition( + condition_set=condition_set, + field_name='percent', + condition='0-50', + ) + + request = HttpRequest() + request.user = self.user + + template = Template(""" + {% load gargoyle_tags %} + {% ifnotswitch test %} + hello world! + {% else %} + foo bar baz + {% endifnotswitch %} + """) + rendered = template.render(Context({'request': request})) + + self.assertTrue('foo bar baz' in rendered) + self.assertFalse('hello world!' in rendered) + + def test_missing_name(self): + self.assertRaises(TemplateSyntaxError, Template, """ + {% load gargoyle_tags %} + {% ifnotswitch %} + hello world! + {% endifnotswitch %} + """) + + def test_with_custom_objects(self): + condition_set = 'gargoyle.builtins.UserConditionSet(auth.user)' + + switch = Switch.objects.create( + key='test', + status=SELECTIVE, + ) + switch = self.gargoyle['test'] + + switch.add_condition( + condition_set=condition_set, + field_name='percent', + condition='0-50', + ) + + request = HttpRequest() + request.user = self.user + + # Pass in request.user explicitly. + template = Template(""" + {% load gargoyle_tags %} + {% ifnotswitch test request.user %} + hello world! + {% else %} + foo bar baz + {% endifnotswitch %} + """) + rendered = template.render(Context({'request': request})) + + self.assertTrue('foo bar baz' in rendered) + self.assertFalse('hello world!' in rendered) + + class HostConditionSetTest(TestCase): def setUp(self): self.gargoyle = SwitchManager(Switch, key='key', value='value', instances=True, auto_create=True)