Skip to content

Commit

Permalink
Add support for a 'country' filter in query parameters
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mhl committed Oct 3, 2017
1 parent 7c88b24 commit 61739b5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions mapit/templates/mapit/api/areas.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ <h3>{% blocktrans trimmed %}
<li><i>generation</i>, {% trans "to return areas in that generation (type and name lookups only)" %}.</li>
<li><i>min_generation</i>, {% trans "to return areas since that generation (type and name lookups only)" %}.</li>
<li><i>type</i>, {% trans "to restrict results to a type or types (multiple separated by commas; name lookup only)" %}.</li>
<li><i>country</i>, {% trans "to restrict results to areas with particular country codes (multiple separated by commas; type and name lookups only)" %}.</li>
</ul></dd>

<dt>{% trans "Returns" %}:</dt>
Expand Down
5 changes: 5 additions & 0 deletions mapit/templates/mapit/api/point.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ <h3>{% blocktrans trimmed %}
<i>min_generation</i>, to return results since that generation.
{% endblocktrans %}
</li>
<li>
{% blocktrans trimmed %}
<i>country</i>, to restrict results to areas with particular country codes (multiple country codes separated by commas).
{% endblocktrans %}
</li>
</ul>
</dd>

Expand Down
22 changes: 22 additions & 0 deletions mapit/tests/test_query_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,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'],
}
)
13 changes: 9 additions & 4 deletions mapit/views/areas.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,22 @@ 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:
args = {
'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

Expand Down

0 comments on commit 61739b5

Please sign in to comment.