templates: add dict support to |distance (#43296)

This commit is contained in:
Frédéric Péters 2020-05-26 08:22:23 +02:00
parent b8b9244fc8
commit 1ea7fc36dd
2 changed files with 18 additions and 6 deletions

View File

@ -540,6 +540,8 @@ def test_distance():
assert t.render({'coords': '48.1;2.1'}) == '13387.2'
t = Template('{{ c1|distance:c2|floatformat }}',)
assert t.render({'c1': '48;2', 'c2': '48.1;2.1'}) == '13387.2'
assert t.render({'c1': {'lat': '48', 'lon': '2'}, 'c2': {'lat': '48.1', 'lng': '2.1'}}) == '13387.2'
assert t.render({'c1': {'lat': '48', 'lng': '2'}, 'c2': {'lat': '48.1', 'lng': '2.1'}}) == '13387.2'
class MockFormData(object):
formdef = None

View File

@ -394,12 +394,22 @@ def get_latlon(obj):
return None, None
if hasattr(obj, 'get_value'):
obj = obj.get_value() # unlazy
if not obj or not isinstance(obj, six.string_types) or ';' not in obj:
return None, None
try:
return float(obj.split(';')[0]), float(obj.split(';')[1])
except ValueError:
return None, None
if isinstance(obj, dict) and 'lat' in obj and 'lon' in obj:
try:
return float(obj['lat']), float(obj['lon'])
except (TypeError, ValueError):
pass
if isinstance(obj, dict) and 'lat' in obj and 'lng' in obj:
try:
return float(obj['lat']), float(obj['lng'])
except (TypeError, ValueError):
pass
if isinstance(obj, six.string_types) and ';' in obj:
try:
return float(obj.split(';')[0]), float(obj.split(';')[1])
except ValueError:
pass
return None, None
@register.filter