Projet

Général

Profil

0001-maps-require-all-words-to-match-when-querying-layer-.patch

Frédéric Péters, 14 septembre 2018 12:17

Télécharger (3,86 ko)

Voir les différences:

Subject: [PATCH] maps: require all words to match when querying layer features
 (#26268)

 combo/apps/maps/models.py | 21 +++++++++++++--------
 tests/test_maps_cells.py  | 18 ++++++++++++++++++
 2 files changed, 31 insertions(+), 8 deletions(-)
combo/apps/maps/models.py
203 203
            features = [x for x in features if match(x)]
204 204

  
205 205
        if request and request.GET.get('q'):
206
            query = slugify(request.GET['q'])
206
            # all words must match
207
            query_words = [slugify(x) for x in request.GET['q'].split()]
208

  
209
            additional_strings = []
210
            if multiple_layers:  # also match on layer name
211
                additional_strings = [self.label]
207 212

  
208 213
            def match(feature):
209
                for geo_property in feature['properties'].values():
214
                matching_query_words = set()
215
                for geo_property in feature['properties'].values() + additional_strings:
210 216
                    if not isinstance(geo_property, six.string_types):
211 217
                        continue
212
                    if query in slugify(geo_property):
218
                    for word in query_words:
219
                        if word in slugify(geo_property):
220
                            matching_query_words.add(word)
221
                    if len(matching_query_words) == len(query_words):
213 222
                        return True
214 223
                return False
215 224

  
216
            if multiple_layers and query in slugify(self.label):
217
                # also match on layer name, get them all
218
                pass
219
            else:
220
                features = [x for x in features if match(x)]
225
            features = [x for x in features if match(x)]
221 226

  
222 227
        for feature in features:
223 228
            feature['properties']['layer'] = {
tests/test_maps_cells.py
280 280
        resp = app.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id}) + '?lng=2.54&lat=48.84&distance=100')
281 281
        assert len(json.loads(resp.content)['features']) == 0
282 282

  
283
    # check on multiple words
284
    with mock.patch('combo.utils.requests_wrapper.RequestsSession.request') as requests_get:
285
        requests_get.return_value = mock.Mock(
286
                content=SAMPLE_GEOJSON_CONTENT,
287
                json=lambda: json.loads(SAMPLE_GEOJSON_CONTENT),
288
                status_code=200)
289
        resp = app.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id}) + '?q=bar baz')
290
        assert len(json.loads(resp.content)['features']) == 1
291

  
292
        resp = app.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id}) + '?q=quux baz')
293
        assert len(json.loads(resp.content)['features']) == 0
294

  
283 295
    # add a second layer
284 296
    layer2 = MapLayer()
285 297
    layer2.label = 'xxx'
......
305 317
        resp = app.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id}) + '?q=bicycle')
306 318
        assert len(json.loads(resp.content)['features']) == 2
307 319

  
320
        resp = app.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id}) + '?q=bar bicycle')
321
        assert len(json.loads(resp.content)['features']) == 1
322

  
323
        resp = app.get(reverse('mapcell-geojson', kwargs={'cell_id': cell.id}) + '?q=quux bicycle')
324
        assert len(json.loads(resp.content)['features']) == 0
325

  
308 326

  
309 327
def test_get_geojson_properties(app, layer, user):
310 328
    page = Page(title='xxx', slug='new', template_name='standard')
311
-