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 9bd22c7 commit 36f2f61
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
22 changes: 22 additions & 0 deletions mapit/tests/test_query_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
}
)
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 36f2f61

Please sign in to comment.