Skip to content

Commit

Permalink
Add min_generation parameter to postcode lookup.
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed Aug 17, 2023
1 parent 2c78c7d commit c6dda90
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 25 deletions.
25 changes: 21 additions & 4 deletions mapit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
from django.db.models.query import RawQuerySet
from django.utils.encoding import smart_str
from django.utils.functional import cached_property
from django.utils.translation import gettext as _

from mapit import countries
from mapit.geometryserialiser import GeometrySerialiser
from mapit.middleware import ViewException


def materialized():
Expand Down Expand Up @@ -44,6 +46,23 @@ def new(self):
return None
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)

Check warning on line 53 in mapit/models.py

View check run for this annotation

Codecov / codecov/patch

mapit/models.py#L52-L53

Added lines #L52 - L53 were not covered by tests
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)

Check warning on line 60 in mapit/models.py

View check run for this annotation

Codecov / codecov/patch

mapit/models.py#L59-L60

Added lines #L59 - L60 were not covered by tests
if not min_generation:
min_generation = generation

return generation, min_generation


class Generation(models.Model):

Expand Down Expand Up @@ -148,13 +167,11 @@ def by_location(self, location, generation=None):
generation_low__lte=generation, generation_high__gte=generation
)

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

Expand Down
10 changes: 7 additions & 3 deletions mapit/templates/mapit/api/postcode.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ <h3>
</dd>

<dt>{% trans "Optional query parameters" %}:</dt>
<dd>
<dd><ul><li>
{% blocktrans trimmed %}
<p><i>generation</i>, to return results from a previous generation.</p>
<i>generation</i>, to return results from a previous generation.
{% endblocktrans %}
</dd>
</li><li>
{% blocktrans trimmed %}
<i>min_generation</i>, to return results since that generation.
{% endblocktrans %}
</li></ul></dd>

{% include "mapit/api/postcode-example.html" %}
</dl>
Expand Down
13 changes: 13 additions & 0 deletions mapit/tests/test_query_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,16 @@ def test_two_countries_in_query(self):
generation_low__lte=self.active_generation.id,
) & (Q() | Q(country__code__in=['DE', 'FR']) | Q(countries__code__in=['DE', 'FR']))
)

def test_generation_manager_query_args(self):
gen, min_gen = 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)

gen, min_gen = 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)
14 changes: 1 addition & 13 deletions mapit/views/areas.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,7 @@ def output_areas(request, title, format, areas, **kwargs):


def query_args(request, format, type=None):
try:
generation = int(request.GET.get('generation', 0))
except ValueError:
raise ViewException(format, _('Bad generation specified'), 400)
if not generation:
generation = Generation.objects.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
generation, min_generation = Generation.objects.query_args(request, format)

if type is None:
type = request.GET.get('type', '')
Expand Down
9 changes: 4 additions & 5 deletions mapit/views/postcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,11 @@ def postcode(request, postcode, format=None):
if not is_valid_postcode(postcode):
raise ViewException(format, "Postcode '%s' is not valid." % postcode, 400)
postcode = get_object_or_404(Postcode, format=format, postcode=postcode)
try:
generation = int(request.GET['generation'])
except:
generation = Generation.objects.current()

generation, min_generation = 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)))
areas = list(add_codes(Area.objects.by_postcode(postcode, generation, min_generation)))
else:
areas = []

Expand Down

0 comments on commit c6dda90

Please sign in to comment.