maps: add possibility to filter geojson data (#16977)

This commit is contained in:
Frédéric Péters 2017-06-19 10:37:58 +02:00
parent 584a4473e1
commit e558f02b6f
3 changed files with 46 additions and 1 deletions

View File

@ -96,6 +96,20 @@ class MapLayer(models.Model):
features = data['features']
else:
features = data
if request.GET.get('q'):
query = slugify(request.GET['q'])
def match(feature):
for geo_property in feature['properties'].values():
if not isinstance(geo_property, basestring):
continue
if query in slugify(geo_property):
return True
return False
features = [x for x in features if match(x)]
for feature in features:
feature['properties']['layer'] = {
'colour': self.marker_colour,

View File

@ -22,7 +22,22 @@ SAMPLE_GEOJSON_CONTENT = '''{
"features": [
{
"type": "Feature",
"properties": {},
"properties": {
"name": "Foo"
},
"geometry": {
"type": "Point",
"coordinates": [
2.548828125,
48.83579746243093
]
}
},
{
"type": "Feature",
"properties": {
"name": "Bar"
},
"geometry": {
"type": "Point",
"coordinates": [
@ -147,6 +162,7 @@ def test_get_geojson(layer, user):
json=lambda: json.loads(SAMPLE_GEOJSON_CONTENT),
status_code=200)
resp = client.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id}))
assert len(json.loads(resp.content)['features']) == 2
assert requests_get.call_count == 1
resp = client.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id}))
assert requests_get.call_count == 1 # cache was used
@ -190,3 +206,17 @@ def test_get_geojson(layer, user):
resp = client.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id}))
assert 'orig=combo' in requests_get.call_args[0][1]
assert not 'email=admin%40localhost&' in requests_get.call_args[0][1]
# check query on geojson
layer.geojson_url = 'http://example.org/geojson?t5'
layer.include_user_identifier = False
layer.save()
with mock.patch('combo.utils.RequestsSession.request') as requests_get:
requests_get.return_value = mock.Mock(
content=SAMPLE_GEOJSON_CONTENT,
json=lambda: json.loads(SAMPLE_GEOJSON_CONTENT),
status_code=200)
resp = client.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id}) + '?q=bar')
assert len(json.loads(resp.content)['features']) == 1
assert 'orig=combo' in requests_get.call_args[0][1]
assert not 'email=admin%40localhost&' in requests_get.call_args[0][1]

View File

@ -78,6 +78,7 @@ def test_download_geojson(mock_request, app, admin_user):
test_add_layer(app, admin_user)
layer = MapLayer.objects.get()
mocked_response = mock.Mock()
mock_request.GET = {}
mocked_response.json.return_value = [{'type': 'Feature',
'geometry': {'type': 'Point',
'coordinates': [2.3233688436448574, 48.83369263315934]},