From 325bd8e142578b4a1ce810c07796cdd5ec47ff18 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 | 26 +++++++++++++++++++++++--- wcs/backoffice/management.py | 17 +++++++++++++---- wcs/qommon/static/js/qommon.map.js | 14 ++++++++++++-- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/tests/test_api.py b/tests/test_api.py index 03c04dca..a1c602b3 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,16 @@ 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'] == username + if field['label'] == 'foobar': + assert field['html_value'] == foobar + if field['label'] == 'foobar1': + 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..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..464bb5fb 100644 --- a/wcs/qommon/static/js/qommon.map.js +++ b/wcs/qommon/static/js/qommon.map.js @@ -115,8 +115,18 @@ $(window).on('load', function() { if (feature.properties.display_fields.length > 0) { var popup = ''; $.each(feature.properties.display_fields, function(index, field) { - popup += ''; + $popup_field = $(''; -- 2.17.1