Skip to content

Commit

Permalink
Improve and sync number regex in URL/point form.
Browse files Browse the repository at this point in the history
This should prevent errors where a submission such as '53.8487,2.9295.'
was matched and then could not be reversed.
  • Loading branch information
dracos committed Feb 28, 2017
1 parent dcbca66 commit dd72ec8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
3 changes: 1 addition & 2 deletions mapit/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
from django.conf import settings
from django.shortcuts import render

from mapit.utils import re_number as number
from mapit.views import areas, postcodes

handler500 = 'mapit.shortcuts.json_500'

format_end = '(?:\.(?P<format>html|json))?'

number = '-?\d*\.?\d+'

urlpatterns = [
url(r'^$', render, {'template_name': 'mapit/index.html'}, 'mapit_index'),
url(r'^licensing$', render, {'template_name': 'mapit/licensing.html'}),
Expand Down
3 changes: 3 additions & 0 deletions mapit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from mapit import countries


re_number = '[+-]?(?:\d*\.)?\d+'


def is_valid_postcode(pc):
pc = re.sub('\s+', '', pc.upper())

Expand Down
3 changes: 2 additions & 1 deletion mapit/views/areas.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from mapit.shortcuts import output_json, output_html, output_polygon, get_object_or_404, set_timeout
from mapit.middleware import ViewException
from mapit.ratelimitcache import ratelimit
from mapit.utils import re_number
from mapit import countries
from mapit.iterables import iterdict
from mapit.geometryserialiser import GeometrySerialiser, TransformError
Expand Down Expand Up @@ -413,7 +414,7 @@ def point_form_submitted(request):
latlon = request.POST.get('pc', None)
if not request.method == 'POST' or not latlon:
return redirect('mapit_index')
m = re.match('\s*([0-9.-]+)\s*,\s*([0-9.-]+)', latlon)
m = re.match('\s*(%s)\s*,\s*(%s)' % (re_number, re_number), latlon)
if not m:
return redirect('mapit_index')
lat, lon = m.groups()
Expand Down

0 comments on commit dd72ec8

Please sign in to comment.