Skip to content

Commit

Permalink
Fix workaround for Django ticket 24240 in Python 3
Browse files Browse the repository at this point in the history
c3ff8fb used the wrong utility function in its workaround for
https://code.djangoproject.com/ticket/24240, providing byte strings only
in Python 2. This commit switches to the correct function, and changes a
test to use the gzip middleware to show this is really fixed now.
  • Loading branch information
dracos committed May 31, 2015
1 parent c49c70d commit 72716db
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 3 additions & 3 deletions mapit/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.shortcuts import get_object_or_404 as orig_get_object_or_404
from django.template import loader
from django.utils.six.moves import map
from django.utils.encoding import smart_str
from django.utils.encoding import smart_bytes
from django.utils.translation import ugettext as _

from mapit.iterables import defaultiter
Expand Down Expand Up @@ -54,7 +54,7 @@ def output_html(request, title, areas, **kwargs):
areas = map(lambda area: item_tpl.render(Context({'area': area, 'indent_areas': indent_areas})), areas)
areas = defaultiter(areas, '<li>' + _('No matching areas found.') + '</li>')
content = itertools.chain(wraps[0:1], areas, wraps[1:])
content = map(smart_str, content) # Workaround Django bug #24240
content = map(smart_bytes, content) # Workaround Django bug #24240

if django.get_version() >= '1.5':
response_type = http.StreamingHttpResponse
Expand All @@ -76,7 +76,7 @@ def output_json(out, code=200):
indent = 4
encoder = GEOS_JSONEncoder(ensure_ascii=False, indent=indent)
content = encoder.iterencode(out)
content = map(smart_str, content) # Workaround Django bug #24240
content = map(smart_bytes, content) # Workaround Django bug #24240

types = {
400: http.HttpResponseBadRequest,
Expand Down
2 changes: 1 addition & 1 deletion mapit/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_areas_by_latlon(self):

def test_areas_by_point(self):
url = '/point/4326/-3.4,51.5.json'
response = self.client.get(url)
response = self.client.get(url, HTTP_ACCEPT_ENCODING='gzip')

content = get_content(response)

Expand Down
9 changes: 9 additions & 0 deletions mapit/tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import json
import zlib


def get_content(response):
if getattr(response, 'streaming', None):
content = b''.join(response.streaming_content)
else:
content = response.content

# Just in case the response is gzipped
try:
# http://www.zlib.net/manual.html - add 16 to decode only the gzip format
content = zlib.decompress(content, 16+zlib.MAX_WBITS)
except zlib.error:
pass

content = json.loads(content.decode('utf-8'))
return content

0 comments on commit 72716db

Please sign in to comment.