From c62c15ede6e8980ef2eccf067beab88303e028ea Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Mon, 2 Oct 2017 10:18:03 +0100 Subject: [PATCH] Add support for a 'country' filter in query parameters This change means that you can add '?country=E' (say) to queries for multiple areas to restrict the results to those areas that have a Country with code 'E'. This should be particularly helpful for global.mapit.mysociety.org if we set an appropriate Country for each Area, since one is almost always interested in results from a particular country. Fixes #55 --- mapit/templates/mapit/api/areas.html | 1 + mapit/templates/mapit/api/point.html | 5 +++++ mapit/tests/test_query_args.py | 22 ++++++++++++++++++++++ mapit/views/areas.py | 13 +++++++++---- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/mapit/templates/mapit/api/areas.html b/mapit/templates/mapit/api/areas.html index e20ef999..d9978d89 100644 --- a/mapit/templates/mapit/api/areas.html +++ b/mapit/templates/mapit/api/areas.html @@ -23,6 +23,7 @@

{% blocktrans trimmed %}
  • generation, {% trans "to return areas in that generation (type and name lookups only)" %}.
  • min_generation, {% trans "to return areas since that generation (type and name lookups only)" %}.
  • type, {% trans "to restrict results to a type or types (multiple separated by commas; name lookup only)" %}.
  • +
  • country, {% trans "to restrict results to areas with particular country codes (multiple separated by commas; type and name lookups only)" %}.
  • {% trans "Returns" %}:
    diff --git a/mapit/templates/mapit/api/point.html b/mapit/templates/mapit/api/point.html index 072659bd..27155110 100644 --- a/mapit/templates/mapit/api/point.html +++ b/mapit/templates/mapit/api/point.html @@ -58,6 +58,11 @@

    {% blocktrans trimmed %} min_generation, to return results since that generation. {% endblocktrans %} +
  • + {% blocktrans trimmed %} + country, to restrict results to areas with particular country codes (multiple country codes separated by commas). + {% endblocktrans %} +
  • diff --git a/mapit/tests/test_query_args.py b/mapit/tests/test_query_args.py index 92a7fe6e..e2ee19e9 100644 --- a/mapit/tests/test_query_args.py +++ b/mapit/tests/test_query_args.py @@ -108,3 +108,25 @@ def test_min_generation_specified(self): 'generation_low__lte': self.active_generation.id, } ) + + def test_one_country_in_query(self): + args = query_args(FakeRequest({'country': 'DE'}), 'json', None) + self.assertEqual( + args, + { + 'generation_high__gte': self.active_generation.id, + 'generation_low__lte': self.active_generation.id, + 'country__code': 'DE', + } + ) + + def test_two_countries_in_query(self): + args = query_args(FakeRequest({'country': 'DE,FR'}), 'json', None) + self.assertEqual( + args, + { + 'generation_high__gte': self.active_generation.id, + 'generation_low__lte': self.active_generation.id, + 'country__code__in': ['DE', 'FR'], + } + ) diff --git a/mapit/views/areas.py b/mapit/views/areas.py index a70ebf71..6ff9a2f3 100644 --- a/mapit/views/areas.py +++ b/mapit/views/areas.py @@ -64,6 +64,7 @@ def query_args(request, format, type=None): if type is None: type = request.GET.get('type', '') + country_code = request.GET.get('country', '') args = {} if min_generation > -1: @@ -71,10 +72,14 @@ def query_args(request, format, type=None): 'generation_low__lte': generation, 'generation_high__gte': min_generation, } - if ',' in type: - args['type__code__in'] = type.split(',') - elif type: - args['type__code'] = type + for attr, value in [ + ('type', type), + ('country', country_code), + ]: + if ',' in value: + args[attr + '__code__in'] = value.split(',') + elif value: + args[attr + '__code'] = value return args