From cb84e6c83dc9ea0a4a2713b45bdd49811822ee4b Mon Sep 17 00:00:00 2001 From: Josue Kouka Date: Mon, 4 Jun 2018 17:17:51 +0200 Subject: [PATCH] api: change geojson display fields format (#22031) --- tests/test_api.py | 30 ++++++++++++++++++++++++++--- wcs/backoffice/management.py | 14 ++++++++++---- wcs/qommon/static/css/dc2/admin.css | 1 + wcs/qommon/static/js/qommon.map.js | 10 ++++++++-- 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/tests/test_api.py b/tests/test_api.py index 03c04dca..9edc1a94 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1586,8 +1586,9 @@ def test_api_geojson_formdata(pub, local_user): formdef.name = 'test' formdef.workflow_roles = {'_receiver': role.id} formdef.fields = [ - fields.StringField(id='0', label='foobar', varname='foobar'), - ] + fields.StringField(id='0', label='foobar', varname='foobar', type='string'), + fields.FileField(id='1', label='foobar1', varname='file', type='file') + ] formdef.store() data_class = formdef.data_class() @@ -1601,11 +1602,20 @@ def test_api_geojson_formdata(pub, local_user): # even if there's an anonymse parameter resp = get_app(pub).get(sign_uri('/api/forms/test/geojson?anonymise', user=local_user), status=403) + upload = PicklableUpload('test.txt', 'text/plain', 'ascii') + upload.receive(['base64me']) + + foobar = 'FOO BAR' + username = 'Jean Darmette' + + data = {'0': foobar, '1': upload} + local_user.name = username + local_user.store() for i in range(30): formdata = data_class() date = time.strptime('2014-01-20', '%Y-%m-%d') - formdata.data = {'0': 'FOO BAR'} formdata.geolocations = {'base': {'lat': 48, 'lon': 2}} + formdata.data = data formdata.user_id = local_user.id formdata.just_created() if i%3 == 0: @@ -1622,6 +1632,20 @@ def test_api_geojson_formdata(pub, local_user): resp = get_app(pub).get(sign_uri('/api/forms/test/geojson', user=local_user)) assert 'features' in resp.json assert len(resp.json['features']) == 10 + display_fields = resp.json['features'][0]['properties']['display_fields'] + for field in display_fields: + if field['label'] == 'Number': + assert field['html_value'] == '1-28' + assert field['value'] == '1-28' + if field['label'] == 'User Label': + assert field['value'] == username + assert field['html_value'] == "<font color="red">Jean Darmette</font>" + if field['label'] == 'foobar': + assert field['value'] == foobar + assert field['html_value'] == "<font color="red">FOO BAR</font>" + if field['label'] == 'foobar1': + assert field['value'] == "test.txt" + assert field['html_value'] == '
test.txt
' # check with a filter resp = get_app(pub).get(sign_uri('/api/forms/test/geojson?filter=done', user=local_user)) diff --git a/wcs/backoffice/management.py b/wcs/backoffice/management.py index 77755eec..24cb998b 100644 --- a/wcs/backoffice/management.py +++ b/wcs/backoffice/management.py @@ -83,11 +83,17 @@ def geojson_formdatas(formdatas, geoloc_key='base', fields=None): for field in fields: if field.type == 'map': continue - value = formdata.get_field_view_value(field, max_length=60) - value = value.replace('[download]', formdata_backoffice_url) - if not value: + html_value = formdata.get_field_view_value(field, max_length=60) + html_value = html_value.replace('[download]', formdata_backoffice_url) + value = formdata.get_field_view_value(field) + if not html_value and not value: continue - display_fields.append((str(htmlescape(field.label)), str(htmlescape(value)))) + + display_fields.append({ + 'label': field.label, + 'value': str(value), + 'html_value': str(htmlescape(html_value)) + }) feature = { 'type': 'Feature', diff --git a/wcs/qommon/static/css/dc2/admin.css b/wcs/qommon/static/css/dc2/admin.css index 5aefa413..0fc50816 100644 --- a/wcs/qommon/static/css/dc2/admin.css +++ b/wcs/qommon/static/css/dc2/admin.css @@ -1650,6 +1650,7 @@ div.leaflet-popup-content p span { div.leaflet-popup-content .field-value { font-weight: bold; + margin: 0.4em; } div.leaflet-popup-content p.view-link { diff --git a/wcs/qommon/static/js/qommon.map.js b/wcs/qommon/static/js/qommon.map.js index 7d3d4f3e..060de16f 100644 --- a/wcs/qommon/static/js/qommon.map.js +++ b/wcs/qommon/static/js/qommon.map.js @@ -115,8 +115,14 @@ $(window).on('load', function() { if (feature.properties.display_fields.length > 0) { var popup = ''; $.each(feature.properties.display_fields, function(index, field) { - popup += ''; + var $popup_field = $('
'); + $popup_field.find('.field-label').text(field.label); + if (field.html_value) { + $popup_field.find('.field-value').html(field.html_value); + } else { + $popup_field.find('.field-value').text(field.value); + } + popup += $popup_field.html(); }); } else { var popup = ''; -- 2.17.1