summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLauréline Guérin <zebuline@entrouvert.com>2022-05-20 21:17:15 (GMT)
committerLauréline Guérin <zebuline@entrouvert.com>2022-05-20 21:17:15 (GMT)
commit4508a505eccafea2f3561af54f6690aaefe162b2 (patch)
treeed97da95ba5a2b7e3e5afe4dbd162baafe3f36b1
parenta37ceab9cd0af775b2e3cd66f6ea2e9846384d72 (diff)
downloadpublik-django-templatetags-4508a505eccafea2f3561af54f6690aaefe162b2.zip
publik-django-templatetags-4508a505eccafea2f3561af54f6690aaefe162b2.tar.gz
publik-django-templatetags-4508a505eccafea2f3561af54f6690aaefe162b2.tar.bz2
publik: add |get template filter (#65540)v0.2wip/65540-get-filter
-rw-r--r--publik_django_templatetags/publik/templatetags/publik.py11
-rw-r--r--tests/test_publik.py54
2 files changed, 65 insertions, 0 deletions
diff --git a/publik_django_templatetags/publik/templatetags/publik.py b/publik_django_templatetags/publik/templatetags/publik.py
index d5a1617..97b7265 100644
--- a/publik_django_templatetags/publik/templatetags/publik.py
+++ b/publik_django_templatetags/publik/templatetags/publik.py
@@ -19,6 +19,17 @@ from django import template
register = template.Library()
+@register.filter(name='get')
+def get(obj, key):
+ try:
+ return obj.get(key)
+ except AttributeError:
+ try:
+ return obj[key]
+ except (IndexError, KeyError, TypeError):
+ return None
+
+
@register.filter
def getlist(mapping, key):
if mapping is None:
diff --git a/tests/test_publik.py b/tests/test_publik.py
new file mode 100644
index 0000000..c5bd16f
--- /dev/null
+++ b/tests/test_publik.py
@@ -0,0 +1,54 @@
+from django.template import Context, Template
+
+
+def test_get():
+ t = Template('{{ foo|get:"foo-bar" }}')
+ context = Context({'foo': {'foo-bar': 'hello'}})
+ assert t.render(context) == 'hello'
+ context = Context({'foo': {'bar-foo': 'hello'}})
+ assert t.render(context) == 'None'
+ context = Context({'foo': None})
+ assert t.render(context) == 'None'
+
+ t = Template('{{ foo|get:"foo-bar"|default:"" }}')
+ context = Context({'foo': {'rab': 'hello'}})
+ assert t.render(context) == ''
+
+ t = Template('{{ foo|get:key }}')
+ context = Context({'foo': {'foo-bar': 'hello'}, 'key': 'foo-bar'})
+ assert t.render(context) == 'hello'
+
+
+def test_getlist():
+ # nothing in context
+ t = Template('{% for v in values|getlist:"foo" %}{{ v }},{% endfor %}')
+ context = Context()
+ assert t.render(context) == ''
+ # non value
+ t = Template('{% for v in values|getlist:"foo" %}{{ v }},{% endfor %}')
+ context = Context({'values': None})
+ assert t.render(context) == ''
+ # not a list
+ t = Template('{% for v in values|getlist:"foo" %}{{ v }},{% endfor %}')
+ context = Context({'values': 'foo'})
+ assert t.render(context) == 'None,None,None,'
+ # not a list of dict
+ t = Template('{% for v in values|getlist:"foo" %}{{ v }},{% endfor %}')
+ context = Context({'values': ['foo']})
+ assert t.render(context) == 'None,'
+
+ t = Template('{% for v in values|getlist:"foo" %}{{ v }},{% endfor %}')
+ context = Context({'values': [{'foo': 'bar'}, {'foo': 'baz'}]})
+ assert t.render(context) == 'bar,baz,'
+ t = Template('{% for v in values|getlist:"unknown" %}{{ v }},{% endfor %}')
+ context = Context({'values': [{'foo': 'bar'}, {'foo': 'baz'}]})
+ assert t.render(context) == 'None,None,'
+ t = Template('{% for v in values|getlist:"k"|getlist:"v" %}{{ v }},{% endfor %}')
+ context = Context({'values': [{'k': {'v': 'bar'}}, {'k': {'v': 'baz'}}]})
+ assert t.render(context) == 'bar,baz,'
+ t = Template('{% for v in values|getlist:"k"|getlist:"unknown" %}{{ v }},{% endfor %}')
+ context = Context({'values': [{'k': {'v': 'bar'}}, {'k': {'v': 'baz'}}]})
+ assert t.render(context) == 'None,None,'
+ t = Template('{% for v in values|getlist:"k"|getlist:"v" %}{{ v }},{% endfor %}')
+ context = Context({'values': [{'k': None}, {'k': {'v': 'baz'}}]})
+ assert t.render(context) == 'None,baz,'