diff --git a/mapit/views/areas.py b/mapit/views/areas.py index 7b385a32..6bc2aa7f 100644 --- a/mapit/views/areas.py +++ b/mapit/views/areas.py @@ -252,12 +252,27 @@ def areas_by_type(request, type, format='json'): @ratelimit(minutes=3, requests=100) def areas_by_name(request, name, format='json'): + fuzzy = request.GET.get('fuzzy', '') + if fuzzy.lower() in ['true', '1']: + return areas_by_name_fuzzy(request, name, format) + args = query_args(request, format) args['name__istartswith'] = name areas = Area.objects.filter(**args) return output_areas(request, _('Areas starting with %s') % name, format, areas) +@ratelimit(minutes=3, requests=100) +def areas_by_name_fuzzy(request, name, format='json'): + args = query_args(request, format) + args['name__similar'] = name + areas = Area.objects.filter(**args)\ + .extra(select={'distance': "similarity(mapit_area.name, %s)"}, + select_params=[name])\ + .order_by('-distance') + return output_areas(request, 'Areas matching %s' % name, format, areas) + + @ratelimit(minutes=3, requests=100) def areas_polygon(request, area_ids, srid='', format='kml'): area_ids = area_ids.split(',') diff --git a/project/settings.py b/project/settings.py index cbd5c685..43d628f0 100644 --- a/project/settings.py +++ b/project/settings.py @@ -230,6 +230,7 @@ 'django.contrib.admin', 'django.contrib.gis', 'django.contrib.staticfiles', + 'djorm_pgtrgm', 'mapit', ]