From 6dfda140bc30c1f54a795ce4f15712dc82ac43f3 Mon Sep 17 00:00:00 2001 From: matthewt Date: Mon, 18 Nov 2024 08:42:46 +0200 Subject: [PATCH 1/3] waffle_tags: adding `switch_is_active`, `flag_is_active`, and `sample_is_active` --- waffle/templatetags/waffle_tags.py | 43 +++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/waffle/templatetags/waffle_tags.py b/waffle/templatetags/waffle_tags.py index 62f5bb7a..cbb5c3f2 100644 --- a/waffle/templatetags/waffle_tags.py +++ b/waffle/templatetags/waffle_tags.py @@ -1,7 +1,13 @@ +from typing import Optional from django import template +from django.http import HttpRequest from django.template.base import VariableDoesNotExist -from waffle import flag_is_active, sample_is_active, switch_is_active +from waffle import ( + flag_is_active as waffle_flag_is_active, + sample_is_active as waffle_sample_is_active, + switch_is_active as waffle_switch_is_active + ) from waffle.views import _generate_waffle_js @@ -60,17 +66,17 @@ def handle_token(cls, parser, token, kind, condition): @register.tag def flag(parser, token): - return WaffleNode.handle_token(parser, token, 'flag', flag_is_active) + return WaffleNode.handle_token(parser, token, 'flag', waffle_flag_is_active) @register.tag def switch(parser, token): - return WaffleNode.handle_token(parser, token, 'switch', lambda request, name: switch_is_active(name)) + return WaffleNode.handle_token(parser, token, 'switch', lambda request, name: waffle_switch_is_active(name)) @register.tag def sample(parser, token): - return WaffleNode.handle_token(parser, token, 'sample', lambda request, name: sample_is_active(name)) + return WaffleNode.handle_token(parser, token, 'sample', lambda request, name: waffle_sample_is_active(name)) class InlineWaffleJSNode(template.Node): @@ -81,3 +87,32 @@ def render(self, context): @register.tag def wafflejs(parser, token): return InlineWaffleJSNode() + + +@register.filter(name="switch_is_active") # type: ignore[misc] +def switch_is_active(switch_name: str) -> bool: + """ + This template filter works like the switch tag, but you can use it within + if statement conditions in Django templates. Example: `{% if "switch_name"|switch_is_active %}`. + """ + return waffle_switch_is_active(switch_name) + + +@register.simple_tag # type: ignore[misc] +def flag_is_active(flag_name: str, request: HttpRequest, read_only: bool = False) -> Optional[bool]: + """ + This template filter works like the flag tag, but you can use it within + if statement conditions in Django templates. Example: + `{% flag_is_active "flag_name" request True as is_flag_active %}` + `{% if is_flag_active %}`. + """ + return waffle_flag_is_active(request=request, flag_name=flag_name, read_only=read_only) + + +@register.filter(name="sample_is_active") # type: ignore[misc] +def sample_is_active(sample_name: str) -> bool: + """ + This template filter works like the sample tag, but you can use it within if + statement conditions in Django templates. Example: `{% if "sample_name"|sample_is_active %}`. + """ + return waffle_sample_is_active(sample_name) From 58153d8ac5e0757033d39f356d1e795a5c409c42 Mon Sep 17 00:00:00 2001 From: matthewt Date: Mon, 18 Nov 2024 08:44:36 +0200 Subject: [PATCH 2/3] templates: adding new template tag to django.html --- test_app/templates/django/django.html | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test_app/templates/django/django.html b/test_app/templates/django/django.html index 9a966df4..600cdbea 100644 --- a/test_app/templates/django/django.html +++ b/test_app/templates/django/django.html @@ -40,4 +40,23 @@ sample_var off {% endsample %} +{% flag_is_active "flag" request True as is_flag_active %} +{% if is_flag_active %} + flag_is_active on +{% else %} + flag_is_active off +{% endif %} + +{% if "switch"|switch_is_active %} + switch_is_active on +{% else %} + switch_is_active off +{% endif %} + +{% if "sample"|sample_is_active %} + sample_is_active on +{% else %} + sample_is_active off +{% endif %} + {% wafflejs %} From 30e57d3fc24ae585d4b2afadec0e2374bb297071 Mon Sep 17 00:00:00 2001 From: matthewt Date: Mon, 18 Nov 2024 08:44:55 +0200 Subject: [PATCH 3/3] waffle: adding template tag tests --- waffle/tests/test_templates.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/waffle/tests/test_templates.py b/waffle/tests/test_templates.py index a368a9c3..fad9a4d9 100644 --- a/waffle/tests/test_templates.py +++ b/waffle/tests/test_templates.py @@ -9,6 +9,7 @@ from test_app import views from waffle.middleware import WaffleMiddleware from waffle.tests.base import TestCase +from waffle.testutils import override_switch, override_flag, override_sample def get(): @@ -37,6 +38,26 @@ def test_django_tags(self): self.assertContains(response, 'switch_var off') self.assertContains(response, 'sample_var') self.assertContains(response, 'window.waffle =') + self.assertContains(response, 'flag_is_active off') + self.assertContains(response, 'switch_is_active off') + self.assertContains(response, 'sample_is_active off') + + @override_flag("flag", active=True) + @override_sample("sample", active=True) + @override_switch("switch", active=True) + def test_django_tags_enabled(self): + request = get() + response = process_request(request, views.flag_in_django) + self.assertContains(response, 'flag on') + self.assertContains(response, 'switch on') + self.assertContains(response, 'sample on') + self.assertNotContains(response, 'flag off') + self.assertNotContains(response, 'switch off') + self.assertNotContains(response, 'sample off') + self.assertContains(response, 'window.waffle =') + self.assertContains(response, 'flag_is_active on') + self.assertContains(response, 'switch_is_active on') + self.assertContains(response, 'sample_is_active on') def test_get_nodes_by_type(self): """WaffleNode.get_nodes_by_type() should find all child nodes."""