0001-misc-add-distance-template-tag-32262.patch
tests/test_templates.py | ||
---|---|---|
7 | 7 |
from quixote import cleanup |
8 | 8 |
from qommon.template import Template, TemplateError |
9 | 9 |
from wcs.conditions import Condition |
10 |
from wcs.variables import LazyFormData |
|
10 | 11 | |
11 | 12 |
from utilities import create_temporary_pub, clean_temporary_pub |
12 | 13 | |
... | ... | |
459 | 460 |
assert not any(set(token) & set('01IiOo') for token in tokens) |
460 | 461 |
t = Template('{% if token1|token_check:token2 %}ok{% endif %}') |
461 | 462 |
assert t.render({'token1': tokens[0] + ' ', 'token2': tokens[0].lower()}) == 'ok' |
463 | ||
464 |
def test_distance(): |
|
465 |
class MockFormData(object): |
|
466 |
formdef = None |
|
467 |
geolocations = {'base': {'lat': 48, 'lon': 2}} |
|
468 | ||
469 |
lazy_formdata = LazyFormData(MockFormData()) |
|
470 |
t = Template('{{ formdata|distance:coords|floatformat }}',) |
|
471 |
assert t.render({'formdata': lazy_formdata, 'coords': '48.1;2.1'}) == '13387.2' |
|
472 |
assert t.render({'formdata': lazy_formdata, 'coords': '49.1;3.1'}) == '146821.9' |
|
473 |
assert t.render({'formdata': lazy_formdata, 'coords': ''}) == '' |
|
474 | ||
475 |
MockFormData.geolocations = {} |
|
476 |
assert t.render({'formdata': lazy_formdata, 'coords': '49.1;3.1'}) == '' |
wcs/qommon/templatetags/qommon.py | ||
---|---|---|
23 | 23 |
import string |
24 | 24 |
import random |
25 | 25 | |
26 |
from pyproj import Geod |
|
27 | ||
26 | 28 |
from django import template |
27 | 29 |
from django.template import defaultfilters |
28 | 30 |
from django.utils import dateparse |
29 | 31 |
from django.utils.safestring import mark_safe |
30 | 32 |
from wcs.qommon import evalutils |
33 |
from wcs.qommon import form |
|
31 | 34 |
from wcs.qommon import tokens |
32 | 35 |
from wcs.qommon.admin.texts import TextsDirectory |
33 | 36 | |
... | ... | |
300 | 303 |
@register.filter |
301 | 304 |
def token_check(token1, token2): |
302 | 305 |
return token1.strip().upper() == token2.strip().upper() |
306 | ||
307 | ||
308 |
@register.filter |
|
309 |
def distance(obj, coords): |
|
310 |
geod = Geod(ellps='WGS84') |
|
311 |
if not coords: |
|
312 |
return None |
|
313 |
if not obj.geoloc or not 'base' in obj.geoloc: |
|
314 |
return None |
|
315 |
distance = geod.inv( |
|
316 |
obj.geoloc['base']['lon'], |
|
317 |
obj.geoloc['base']['lat'], |
|
318 |
float(coords.split(';')[1]), |
|
319 |
float(coords.split(';')[0]) |
|
320 |
)[2] |
|
321 |
return distance |
|
303 |
- |