From 6dfda140bc30c1f54a795ce4f15712dc82ac43f3 Mon Sep 17 00:00:00 2001 From: matthewt Date: Mon, 18 Nov 2024 08:42:46 +0200 Subject: [PATCH 1/5] 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/5] 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/5] 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.""" From d763b44c4758e286dd1038d0aa0213eca4f70c03 Mon Sep 17 00:00:00 2001 From: matthewt Date: Tue, 7 Jan 2025 18:54:09 +0200 Subject: [PATCH 4/5] docs: adding documentation for template filters and tags for `flag_is_active`, `switch_is_active`, `sample_is_active` --- docs/usage/templates.rst | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/docs/usage/templates.rst b/docs/usage/templates.rst index bc32f060..c2576b96 100644 --- a/docs/usage/templates.rst +++ b/docs/usage/templates.rst @@ -67,6 +67,51 @@ Samples {% endsample %} +Django Template Filters and Tags +================================ +Django Waffle provides three new Django Template tags and filters, ``flag_is_active``, +``switch_is_active``, and ``sample_is_active``. Each of these tags and filters can be used in Django +template's if statement contitions to check if a flag, switch, or sample is active. + +This is useful for when a django template context variable and a flag, switch, or sample are required to +be active in order to render a certain part of the template. + +flag_is_active +-------------- + +:: + + {% flag_is_active "flag" request True as is_flag_active %} + {% if is_flag_active %} + flag_is_active on + {% else %} + flag_is_active off + {% endif %} + +switch_is_active +---------------- + +:: + + {% if "switch"|switch_is_active %} + switch_is_active on + {% else %} + switch_is_active off + {% endif %} + +sample_is_active +---------------- + +:: + + {% if "sample"|sample_is_active %} + sample_is_active on + {% else %} + sample_is_active off + {% endif %} + + + .. _templates-jinja: Jinja Templates From a188d4b94803429f2ac036d14dbf7a1101a5d954 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 7 Jan 2025 16:54:40 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/usage/templates.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/usage/templates.rst b/docs/usage/templates.rst index c2576b96..41842630 100644 --- a/docs/usage/templates.rst +++ b/docs/usage/templates.rst @@ -69,11 +69,11 @@ Samples Django Template Filters and Tags ================================ -Django Waffle provides three new Django Template tags and filters, ``flag_is_active``, +Django Waffle provides three new Django Template tags and filters, ``flag_is_active``, ``switch_is_active``, and ``sample_is_active``. Each of these tags and filters can be used in Django template's if statement contitions to check if a flag, switch, or sample is active. -This is useful for when a django template context variable and a flag, switch, or sample are required to +This is useful for when a django template context variable and a flag, switch, or sample are required to be active in order to render a certain part of the template. flag_is_active