Skip to content
This repository has been archived by the owner on Jun 20, 2018. It is now read-only.

Add ifnotswitch template tag #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions docs/usage/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

Expand All @@ -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!
Expand Down
22 changes: 18 additions & 4 deletions gargoyle/templatetags/gargoyle_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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{tag}'.format(tag=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
Expand Down
115 changes: 115 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='[email protected]')
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)
Expand Down