templatetags: remove json_script as it's available in django (#65801)
gitea-wip/combo/pipeline/head There was a failure building this commit Details
gitea/combo/pipeline/head Something is wrong with the build of this commit Details

This commit is contained in:
Frédéric Péters 2022-05-31 10:55:57 +02:00
parent ebab8fdb4e
commit 2248f16cc9
2 changed files with 0 additions and 32 deletions

View File

@ -27,7 +27,6 @@ import django
from django import template
from django.core import signing
from django.core.exceptions import PermissionDenied
from django.core.serializers.json import DjangoJSONEncoder
from django.template import VariableDoesNotExist
try:
@ -43,8 +42,6 @@ from django.template import Template, TemplateSyntaxError, defaultfilters
from django.template.defaultfilters import stringfilter
from django.utils import dateparse
from django.utils.encoding import force_text
from django.utils.html import format_html
from django.utils.safestring import mark_safe
from django.utils.timezone import is_naive, make_aware
from combo.apps.dashboard.models import DashboardCell, Tile
@ -652,27 +649,6 @@ def abs_(value):
return decimal(abs(parse_decimal(value)))
_json_script_escapes = {
ord('>'): '\\u003E',
ord('<'): '\\u003C',
ord('&'): '\\u0026',
}
@register.filter(is_safe=True)
def json_script(value, element_id):
"""
Escape all the HTML/XML special characters with their unicode escapes, so
value is safe to be output anywhere except for inside a tag attribute. Wrap
the escaped JSON in a script tag.
--
This is a backport from Django 2.1; it should be removed once Combo bumps
its minimal dependency to 2.1.
"""
json_str = json.dumps(value, cls=DjangoJSONEncoder).translate(_json_script_escapes)
return format_html('<script id="{}" type="application/json">{}</script>', element_id, mark_safe(json_str))
@register.filter(is_safe=False)
def phonenumber_fr(value, separator=' '):
DROMS = ('262', '508', '590', '594', '596')

View File

@ -854,14 +854,6 @@ def test_abs_templatetag():
assert tmpl.render(Context({'value': None})) == '0'
def test_json_script():
tmpl = Template('{{ plop|json_script:"toto" }}')
assert (
tmpl.render(Context({'plop': {'a': 'b'}}))
== '<script id="toto" type="application/json">{"a": "b"}</script>'
)
def test_phonenumber_fr():
t = Template('{{ number|phonenumber_fr }}')
assert t.render(Context({'number': '01 23 45 67 89'})) == '01 23 45 67 89'