From 3e71e57cf847ead39d22003c17eeffd83c785925 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 | 18 +++++++++++++++---
wcs/backoffice/management.py | 17 +++++++++++++----
wcs/qommon/static/js/qommon.map.js | 9 +++++++--
3 files changed, 35 insertions(+), 9 deletions(-)
diff --git a/tests/test_api.py b/tests/test_api.py
index 03c04dca..650ac743 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,10 +1602,13 @@ 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'])
+
for i in range(30):
formdata = data_class()
date = time.strptime('2014-01-20', '%Y-%m-%d')
- formdata.data = {'0': 'FOO BAR'}
+ formdata.data = {'0': 'FOO BAR', '1': upload}
formdata.geolocations = {'base': {'lat': 48, 'lon': 2}}
formdata.user_id = local_user.id
formdata.just_created()
@@ -1622,6 +1626,14 @@ 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'
+ if field['label'] == 'User':
+ assert field['html_value'] == 'Jean Darmette'
+ if field['label'] == 'foobar1':
+ assert field['html_value'] == ''
# 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..2cc7d720 100644
--- a/wcs/backoffice/management.py
+++ b/wcs/backoffice/management.py
@@ -83,11 +83,20 @@ 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))))
+ if field.type == 'file':
+ value = html_value
+
+ data = {'label': field.label}
+ if html_value:
+ data['html_value'] = str(html_value)
+ else:
+ data['value'] = str(value)
+ display_fields.append(data)
feature = {
'type': 'Feature',
diff --git a/wcs/qommon/static/js/qommon.map.js b/wcs/qommon/static/js/qommon.map.js
index 7d3d4f3e..fc2c93b1 100644
--- a/wcs/qommon/static/js/qommon.map.js
+++ b/wcs/qommon/static/js/qommon.map.js
@@ -115,8 +115,13 @@ $(window).on('load', function() {
if (feature.properties.display_fields.length > 0) {
var popup = '';
$.each(feature.properties.display_fields, function(index, field) {
- popup += '';
+ popup += '';
+ }
+ else {
+ popup += '' + encodeURI(field.value) + '
';
+ }
});
} else {
var popup = '';
--
2.17.1