Skip to content

Commit

Permalink
Updates for template changes in Django 1.8.
Browse files Browse the repository at this point in the history
Use Django's render() shortcut rather than our own; create a
render_to_string patch that uses request/context appropriately; stub out
Context() when not needed; add TEMPLATES setting.
  • Loading branch information
dracos committed Feb 11, 2015
1 parent 83c2b82 commit 4f83039
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 21 deletions.
9 changes: 9 additions & 0 deletions mapit/djangopatch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import django
import inspect

# Django 1.8 changed how templates operate.
if django.get_version() >= '1.8':
from django.template.loader import render_to_string
else:
from django.template import loader, RequestContext
def render_to_string(template_name, context=None, request=None):
context_instance = RequestContext(request) if request else None
return loader.render_to_string(template_name, context, context_instance)

# Django 1.6 renamed Manager's get_query_set to get_queryset, and the old
# function will be removed entirely in 1.8. We work back to 1.4, so use a
# metaclass to not worry about it.
Expand Down
5 changes: 2 additions & 3 deletions mapit/middleware/view_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
# and output it as either HTML or JSON appropriately

from django import http
from django.template import RequestContext
from django.template.loader import render_to_string
from mapit.shortcuts import output_json
from mapit.djangopatch import render_to_string


class ViewException(Exception):
Expand All @@ -27,6 +26,6 @@ def process_exception(self, request, exception):
return response_type(render_to_string(
'mapit/%s.html' % code,
{'error': message},
context_instance=RequestContext(request)
request=request
))
return output_json({'error': message}, code=code)
20 changes: 9 additions & 11 deletions mapit/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@
from django.db import connection
from django.conf import settings
from django.shortcuts import get_object_or_404 as orig_get_object_or_404
from django.shortcuts import render_to_response
from django.template import loader, RequestContext, Context
from django.template import loader
from django.utils.six.moves import map
from django.utils.encoding import smart_str
from django.utils.translation import ugettext as _

from mapit.iterables import defaultiter
from mapit.djangopatch import render_to_string

# In 1.8, we no longer need to pass a Context() so stub it out
if django.get_version() < '1.8':
from django.template import Context
else:
Context = lambda x: x

from django.core.serializers.json import DjangoJSONEncoder
# Assuming at least python 2.6, in Django < 1.6, the above class is either a
Expand All @@ -37,18 +43,10 @@ def default(self, o):
return super(GEOS_JSONEncoder, self).default(o)


def render(request, template_name, context=None):
if context is None:
context = {}
return render_to_response(
template_name, context, context_instance=RequestContext(request)
)


def output_html(request, title, areas, **kwargs):
kwargs['json_url'] = request.get_full_path().replace('.html', '')
kwargs['title'] = title
tpl = loader.render_to_string('mapit/data.html', kwargs, context_instance=RequestContext(request))
tpl = render_to_string('mapit/data.html', kwargs, request=request)
wraps = tpl.split('!!!DATA!!!')

indent_areas = kwargs.get('indent_areas', False)
Expand Down
2 changes: 1 addition & 1 deletion mapit/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.conf.urls import include, url
from django.conf import settings
from django.shortcuts import render

from mapit.shortcuts import render
from mapit.views import areas, postcodes

handler500 = 'mapit.shortcuts.json_500'
Expand Down
4 changes: 2 additions & 2 deletions mapit/views/areas.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import resolve, reverse
from django.conf import settings
from django.shortcuts import redirect
from django.shortcuts import redirect, render

from mapit.models import Area, Generation, Geometry, Code, Name, TransformError
from mapit.shortcuts import output_json, output_html, render, get_object_or_404, set_timeout
from mapit.shortcuts import output_json, output_html, get_object_or_404, set_timeout
from mapit.middleware import ViewException
from mapit.ratelimitcache import ratelimit
from mapit import countries
Expand Down
4 changes: 2 additions & 2 deletions mapit/views/postcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import itertools
from django.db.utils import DatabaseError

from django.shortcuts import redirect
from django.shortcuts import redirect, render
from django.contrib.gis.geos import Point
from django.contrib.gis.measure import D

from mapit.models import Postcode, Area, Generation
from mapit.utils import is_valid_postcode, is_valid_partial_postcode
from mapit.shortcuts import output_json, get_object_or_404, set_timeout, render
from mapit.shortcuts import output_json, get_object_or_404, set_timeout
from mapit.middleware import ViewException
from mapit.ratelimitcache import ratelimit
from mapit.views.areas import add_codes
Expand Down
2 changes: 1 addition & 1 deletion mapit_gb/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.conf.urls import url

from mapit.shortcuts import render
from django.shortcuts import render

urlpatterns = [
url(r'^changelog$', render, {'template_name': 'mapit/changelog.html'}, 'mapit_changelog'),
Expand Down
12 changes: 11 additions & 1 deletion project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@
'mapit.context_processors.analytics',
)

if django.get_version() >= '1.8':
TEMPLATES = [ {
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': TEMPLATE_DIRS,
'OPTIONS': {
'context_processors': map(lambda x: x.replace('django.core', 'django.template'), TEMPLATE_CONTEXT_PROCESSORS),
'loaders': TEMPLATE_LOADERS,
},
} ]

INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
Expand All @@ -208,7 +218,7 @@
'django.contrib.staticfiles',
'mapit',
]
if django.VERSION < (1, 7):
if django.get_version() < '1.7':
INSTALLED_APPS.append('south')

if MAPIT_COUNTRY:
Expand Down

0 comments on commit 4f83039

Please sign in to comment.