Skip to content

Commit

Permalink
Add max_generation parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed Mar 15, 2024
1 parent 43abf6f commit 4f6f274
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 62 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Unreleased
* New features:
* Add max_generation parameter to all locations min_generation can be used.
* Development improvements:
* Add support for filtering / excluding areas by multiple types or countries in raise generation script.

Expand Down
58 changes: 26 additions & 32 deletions mapit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.contrib.gis.db import models
from django.conf import settings
from django.db import connection
from django.db.models import Q
from django.db.models.query import RawQuerySet
from django.utils.encoding import smart_str
from django.utils.functional import cached_property
Expand Down Expand Up @@ -47,21 +48,26 @@ def new(self):
return latest[0]

def query_args(self, request, format):
try:
generation = int(request.GET.get('generation', 0))
except ValueError:
raise ViewException(format, _('Bad generation specified'), 400)
if not generation:
generation = self.current().id

try:
min_generation = int(request.GET.get('min_generation', 0))
except ValueError:
raise ViewException(format, _('Bad min_generation specified'), 400)
if not min_generation:
min_generation = generation
args = {}
for q in ('generation', 'min_generation', 'max_generation'):
try:
args[q] = int(request.GET.get(q, 0))
except ValueError:
raise ViewException(format, _('Bad value specified for %s parameter') % q, 400)

Check warning on line 56 in mapit/models.py

View check run for this annotation

Codecov / codecov/patch

mapit/models.py#L55-L56

Added lines #L55 - L56 were not covered by tests
if not args['generation']:
args['generation'] = self.current().id

q = {
'generation_low__lte': args['generation']
}
if args['max_generation']:
q['generation_high__lte'] = args['max_generation']
if args['min_generation']:
q['generation_high__gte'] = args['min_generation']
if not args['max_generation'] and not args['min_generation']:
q['generation_high__gte'] = args['generation']

return generation, min_generation
return Q(**q)


class Generation(models.Model):
Expand Down Expand Up @@ -157,28 +163,16 @@ def get_queryset(self):
return super(AreaManager, self).get_queryset().select_related(
'type', 'country', 'parent_area').prefetch_related('countries')

def by_location(self, location, generation=None, min_generation=None):
if generation is None:
generation = Generation.objects.current()
if min_generation is None:
min_generation = Generation.objects.current()
def by_location(self, location, query):
if not location:
return []
return Area.objects.filter(
polygons__subdivided__division__contains=location,
generation_low__lte=generation, generation_high__gte=min_generation
)
query &= Q(polygons__subdivided__division__contains=location)
return Area.objects.filter(query)

def by_postcode(self, postcode, generation=None, min_generation=None):
if generation is None:
generation = Generation.objects.current()
if min_generation is None:
min_generation = Generation.objects.current()
def by_postcode(self, postcode, query):
return list(itertools.chain(
self.by_location(postcode.location, generation, min_generation),
postcode.areas.filter(
generation_low__lte=generation, generation_high__gte=min_generation
)
self.by_location(postcode.location, query),
postcode.areas.filter(query)
))

# In order for this query to be performant, we have to do it ourselves.
Expand Down
3 changes: 3 additions & 0 deletions mapit/templates/mapit/api/area.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ <h3>{% blocktrans trimmed %}<em>lookup</em> related areas {% endblocktrans %}</h
{% blocktrans trimmed %}
<li><i>min_generation</i>, to return areas since that generation (children only).</li>
{% endblocktrans %}
{% blocktrans trimmed %}
<li><i>max_generation</i>, to return results up to that generation, not active after (children only).</li>
{% endblocktrans %}
</ul></dd>
<dt>{% trans "Returns" %}:</dt>
<dd>{% trans "A hash of areas that match the requested lookup." %}
Expand Down
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 %}
<dd><ul>
<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>max_generation</i>, {% trans "to return results up to that generation, not active after (ditto)." %}.</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>
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>max_generation</i>, to return results up to that generation, not active after.
{% endblocktrans %}
</li>
<li>
{% blocktrans trimmed %}
<i>country</i>, to restrict results to areas with particular country codes (multiple country codes separated by commas).
Expand Down
4 changes: 4 additions & 0 deletions mapit/templates/mapit/api/postcode.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ <h3>
{% blocktrans trimmed %}
<i>min_generation</i>, to return results since that generation.
{% endblocktrans %}
</li><li>
{% blocktrans trimmed %}
<i>max_generation</i>, to return results up to that generation, not active after.
{% endblocktrans %}
</li></ul></dd>

{% include "mapit/api/postcode-example.html" %}
Expand Down
72 changes: 54 additions & 18 deletions mapit/tests/test_query_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def __init__(self, params):
class QueryArgsTest(TestCase):

def setUp(self):
self.older_generation = Generation.objects.create(
active=False,
description="Older test generation",
)
self.old_generation = Generation.objects.create(
active=False,
description="Old test generation",
Expand All @@ -32,12 +36,9 @@ def setUp(self):
description="Test generation",
)

def q_equality(self, act, exp):
self.assertEqual(str(act), str(exp))

def test_no_type(self):
q = query_args(FakeRequest({}), 'json', None)
self.q_equality(
self.assertEqual(
q,
Q(
generation_high__gte=self.active_generation.id,
Expand All @@ -47,7 +48,7 @@ def test_no_type(self):

def test_one_type_in_query(self):
q = query_args(FakeRequest({'type': 'WMC'}), 'json', None)
self.q_equality(
self.assertEqual(
q,
Q(
generation_high__gte=self.active_generation.id,
Expand All @@ -57,7 +58,7 @@ def test_one_type_in_query(self):

def test_two_types_in_query(self):
q = query_args(FakeRequest({'type': 'WMC,EUR'}), 'json', None)
self.q_equality(
self.assertEqual(
q,
Q(
generation_high__gte=self.active_generation.id,
Expand All @@ -67,7 +68,7 @@ def test_two_types_in_query(self):

def test_one_type_in_params(self):
q = query_args(FakeRequest({}), 'json', 'WMC')
self.q_equality(
self.assertEqual(
q,
Q(
generation_high__gte=self.active_generation.id,
Expand All @@ -77,7 +78,7 @@ def test_one_type_in_params(self):

def test_two_types_in_params(self):
q = query_args(FakeRequest({}), 'json', 'WMC,EUR')
self.q_equality(
self.assertEqual(
q,
Q(
generation_high__gte=self.active_generation.id,
Expand All @@ -90,7 +91,7 @@ def test_generation_specified(self):
FakeRequest({'generation': self.old_generation.id}),
'json',
None)
self.q_equality(
self.assertEqual(
q,
Q(
generation_high__gte=self.old_generation.id,
Expand All @@ -103,7 +104,7 @@ def test_min_generation_specified(self):
FakeRequest({'min_generation': self.old_generation.id}),
'json',
None)
self.q_equality(
self.assertEqual(
q,
Q(
generation_high__gte=self.old_generation.id,
Expand All @@ -113,7 +114,7 @@ def test_min_generation_specified(self):

def test_one_country_in_query(self):
q = query_args(FakeRequest({'country': 'DE'}), 'json', None)
self.q_equality(
self.assertEqual(
q,
Q(
generation_high__gte=self.active_generation.id,
Expand All @@ -123,7 +124,7 @@ def test_one_country_in_query(self):

def test_two_countries_in_query(self):
q = query_args(FakeRequest({'country': 'DE,FR'}), 'json', None)
self.q_equality(
self.assertEqual(
q,
Q(
generation_high__gte=self.active_generation.id,
Expand All @@ -132,14 +133,49 @@ def test_two_countries_in_query(self):
)

def test_generation_manager_query_args(self):
gen, min_gen = Generation.objects.query_args(
q = Generation.objects.query_args(
FakeRequest({'generation': self.old_generation.id}),
'json')
self.assertEqual(gen, self.old_generation.id)
self.assertEqual(min_gen, self.old_generation.id)
self.assertEqual(
q,
Q(
generation_high__gte=self.old_generation.id,
generation_low__lte=self.old_generation.id,
)
)

gen, min_gen = Generation.objects.query_args(
q = Generation.objects.query_args(
FakeRequest({'min_generation': self.old_generation.id}),
'json')
self.assertEqual(min_gen, self.old_generation.id)
self.assertEqual(gen, self.active_generation.id)
self.assertEqual(
q,
Q(
generation_high__gte=self.old_generation.id,
generation_low__lte=self.active_generation.id,
)
)

q = Generation.objects.query_args(
FakeRequest({'max_generation': self.old_generation.id}),
'json')
self.assertEqual(
q,
Q(
generation_high__lte=self.old_generation.id,
generation_low__lte=self.active_generation.id,
)
)

q = Generation.objects.query_args(
FakeRequest({
'max_generation': self.old_generation.id, 'min_generation': self.older_generation.id
}),
'json')
self.assertEqual(
q,
Q(
generation_high__gte=self.older_generation.id,
generation_high__lte=self.old_generation.id,
generation_low__lte=self.active_generation.id,
)
)
11 changes: 1 addition & 10 deletions mapit/views/areas.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,12 @@ def output_areas(request, title, format, areas, **kwargs):


def query_args(request, format, type=None):
generation, min_generation = Generation.objects.query_args(request, format)
query = Generation.objects.query_args(request, format)

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,
}

query = Q(**args)

for attr, value in [
(['type'], type),
(['country', 'countries'], country_code),
Expand Down
4 changes: 2 additions & 2 deletions mapit/views/postcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ def postcode(request, postcode, format=None):
raise ViewException(format, "Postcode '%s' is not valid." % postcode, 400)
postcode = get_object_or_404(Postcode, format=format, postcode=postcode)

generation, min_generation = Generation.objects.query_args(request, format)
query = Generation.objects.query_args(request, format)

if not hasattr(countries, 'is_special_postcode') or not countries.is_special_postcode(postcode.postcode):
areas = list(add_codes(Area.objects.by_postcode(postcode, generation, min_generation)))
areas = list(add_codes(Area.objects.by_postcode(postcode, query)))
else:
areas = []

Expand Down

0 comments on commit 4f6f274

Please sign in to comment.