Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow use of JSONP when redirecting from a code

This fixes issue #49.  There are two changes introduced in
this commit:

 - In the customized area lookup for country 'gb', pass on
   the query string if returning a redirect response.

 - In the JSONPMiddleware, only deal with the callback
   parameter if the response isn't a 302 redirect.
  • Loading branch information...
commit 473387d72813751ff686bab3db71439fe361cb84 1 parent 055f1a7
@mhl mhl authored
View
9 mapit/countries/gb.py
@@ -4,7 +4,7 @@
from mapit.shortcuts import get_object_or_404
-def area_code_lookup(area_id, format):
+def area_code_lookup(request, area_id, format):
from mapit.models import Area, CodeType
area_code = None
if re.match('\d\d([A-Z]{2}|[A-Z]{4}|[A-Z]{2}\d\d\d|[A-Z]|[A-Z]\d\d)$', area_id):
@@ -14,7 +14,12 @@ def area_code_lookup(area_id, format):
if not area_code:
return None
area = get_object_or_404(Area, format=format, codes__type=area_code, codes__code=area_id)
- return HttpResponseRedirect('/area/%d%s' % (area.id, '.%s' % format if format else ''))
+ path = '/area/%d%s' % (area.id, '.%s' % format if format else '')
+ # If there was a query string, make sure it's passed on in the
+ # redirect:
+ if request.META['QUERY_STRING']:
+ path += "?" + request.META['QUERY_STRING']
+ return HttpResponseRedirect(path)
def canonical_postcode(pc):
pc = re.sub('[^A-Z0-9]', '', pc.upper())
View
13 mapit/middleware/__init__.py
@@ -4,8 +4,13 @@
class JSONPMiddleware(object):
def process_response(self, request, response):
- if request.GET.get('callback') and re.match('[a-zA-Z0-9_]+$', request.GET.get('callback')):
- response.content = request.GET.get('callback').encode('utf-8') + '(' + response.content + ')'
- response.status_code = 200 # Must return OK for JSONP to be processed
- return response
+ # If the response is a redirect, the callback will be dealt
+ # on the next request:
+ if response.status_code == 302:
+ return response
+ else:
+ if request.GET.get('callback') and re.match('[a-zA-Z0-9_]+$', request.GET.get('callback')):
+ response.content = request.GET.get('callback').encode('utf-8') + '(' + response.content + ')'
+ response.status_code = 200 # Must return OK for JSONP to be processed
@evdb
evdb added a note

Should probably also have

response.content_type = 'application/javascript; charset=utf-8'

As it is javascript that is being returned now, not JSON. Unfortunately I can't tell from the Django docs if changing the content type of an existing response is possible, might have to create a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ return response
View
6 mapit/views/areas.py
@@ -28,12 +28,14 @@ def generations(request):
@ratelimit(minutes=3, requests=100)
def area(request, area_id, format='json'):
if hasattr(countries, 'area_code_lookup'):
- resp = countries.area_code_lookup(area_id, format)
+ resp = countries.area_code_lookup(request, area_id, format)
if resp: return resp
+
if not re.match('\d+$', area_id):
raise ViewException(format, 'Bad area ID specified', 400)
+
area = get_object_or_404(Area, format=format, id=area_id)
codes = []
@@ -70,7 +72,7 @@ def area(request, area_id, format='json'):
@ratelimit(minutes=3, requests=100)
def area_polygon(request, srid='', area_id='', format='kml'):
if not srid and hasattr(countries, 'area_code_lookup'):
- resp = countries.area_code_lookup(area_id, format)
+ resp = countries.area_code_lookup(request, area_id, format)
if resp: return resp
if not re.match('\d+$', area_id):
Please sign in to comment.
Something went wrong with that request. Please try again.