Projet

Général

Profil

0001-templates-add-dict-support-to-distance-43296.patch

Frédéric Péters, 26 mai 2020 11:27

Télécharger (2,23 ko)

Voir les différences:

Subject: [PATCH] templates: add dict support to |distance (#43296)

 tests/test_templates.py           |  2 ++
 wcs/qommon/templatetags/qommon.py | 22 ++++++++++++++++------
 2 files changed, 18 insertions(+), 6 deletions(-)
tests/test_templates.py
535 535
    assert t.render({'coords': '48.1;2.1'}) == '13387.2'
536 536
    t = Template('{{ c1|distance:c2|floatformat }}',)
537 537
    assert t.render({'c1': '48;2', 'c2': '48.1;2.1'}) == '13387.2'
538
    assert t.render({'c1': {'lat': '48', 'lon': '2'}, 'c2': {'lat': '48.1', 'lng': '2.1'}}) == '13387.2'
539
    assert t.render({'c1': {'lat': '48', 'lng': '2'}, 'c2': {'lat': '48.1', 'lng': '2.1'}}) == '13387.2'
538 540

  
539 541
    class MockFormData(object):
540 542
        formdef = None
wcs/qommon/templatetags/qommon.py
388 388
        return None, None
389 389
    if hasattr(obj, 'get_value'):
390 390
        obj = obj.get_value()  # unlazy
391
    if not obj or not isinstance(obj, six.string_types) or ';' not in obj:
392
        return None, None
393
    try:
394
        return float(obj.split(';')[0]), float(obj.split(';')[1])
395
    except ValueError:
396
        return None, None
391
    if isinstance(obj, dict) and 'lat' in obj and 'lon' in obj:
392
        try:
393
            return float(obj['lat']), float(obj['lon'])
394
        except (TypeError, ValueError):
395
            pass
396
    if isinstance(obj, dict) and 'lat' in obj and 'lng' in obj:
397
        try:
398
            return float(obj['lat']), float(obj['lng'])
399
        except (TypeError, ValueError):
400
            pass
401
    if isinstance(obj, six.string_types) and ';' in obj:
402
        try:
403
            return float(obj.split(';')[0]), float(obj.split(';')[1])
404
        except ValueError:
405
            pass
406
    return None, None
397 407

  
398 408

  
399 409
@register.filter
400
-