Projet

Général

Profil

0001-maps-add-possibility-to-filter-geojson-data-16977.patch

Frédéric Péters, 19 juin 2017 10:41

Télécharger (3,4 ko)

Voir les différences:

Subject: [PATCH] maps: add possibility to filter geojson data (#16977)

 combo/apps/maps/models.py | 14 ++++++++++++++
 tests/test_maps_cells.py  | 32 +++++++++++++++++++++++++++++++-
 2 files changed, 45 insertions(+), 1 deletion(-)
combo/apps/maps/models.py
97 97
            features = data['features']
98 98
        else:
99 99
            features = data
100

  
101
        if request.GET.get('q'):
102
            query = slugify(request.GET['q'])
103

  
104
            def match(feature):
105
                for geo_property in feature['properties'].values():
106
                    if not isinstance(geo_property, basestring):
107
                        continue
108
                    if query in slugify(geo_property):
109
                        return True
110
                return False
111

  
112
            features = [x for x in features if match(x)]
113

  
100 114
        for feature in features:
101 115
            feature['properties']['layer'] = {
102 116
                'colour': self.marker_colour,
tests/test_maps_cells.py
22 22
  "features": [
23 23
    {
24 24
      "type": "Feature",
25
      "properties": {},
25
      "properties": {
26
         "name": "Foo"
27
      },
28
      "geometry": {
29
        "type": "Point",
30
        "coordinates": [
31
          2.548828125,
32
          48.83579746243093
33
        ]
34
      }
35
    },
36
    {
37
      "type": "Feature",
38
      "properties": {
39
         "name": "Bar"
40
      },
26 41
      "geometry": {
27 42
        "type": "Point",
28 43
        "coordinates": [
......
147 162
                json=lambda: json.loads(SAMPLE_GEOJSON_CONTENT),
148 163
                status_code=200)
149 164
        resp = client.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id}))
165
        assert len(json.loads(resp.content)['features']) == 2
150 166
        assert requests_get.call_count == 1
151 167
        resp = client.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id}))
152 168
        assert requests_get.call_count == 1 # cache was used
......
190 206
        resp = client.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id}))
191 207
        assert 'orig=combo' in requests_get.call_args[0][1]
192 208
        assert not 'email=admin%40localhost&' in requests_get.call_args[0][1]
209

  
210
    # check query on geojson
211
    layer.geojson_url = 'http://example.org/geojson?t5'
212
    layer.include_user_identifier = False
213
    layer.save()
214
    with mock.patch('combo.utils.RequestsSession.request') as requests_get:
215
        requests_get.return_value = mock.Mock(
216
                content=SAMPLE_GEOJSON_CONTENT,
217
                json=lambda: json.loads(SAMPLE_GEOJSON_CONTENT),
218
                status_code=200)
219
        resp = client.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id}) + '?q=bar')
220
        assert len(json.loads(resp.content)['features']) == 1
221
        assert 'orig=combo' in requests_get.call_args[0][1]
222
        assert not 'email=admin%40localhost&' in requests_get.call_args[0][1]
193
-