Skip to content

Commit

Permalink
Merge branch 'add-django-1.8-testing'
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed Feb 12, 2015
2 parents fd267b3 + 069ed23 commit fc7f2eb
Show file tree
Hide file tree
Showing 24 changed files with 150 additions and 116 deletions.
11 changes: 6 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@ env:
- MODULES="Django>=1.5,<1.6"
- MODULES="Django>=1.6,<1.7"
- MODULES="Django>=1.7,<1.8"
- MODULES="git+https://github.com/django/django.git@stable/1.8.x#egg=django"

# Django 1.4 did not support Python 3.
matrix:
exclude:
- python: "3.2"
env: MODULES="Django>=1.4,<1.5"
allow_failures:
- env: MODULES="git+https://github.com/django/django.git@stable/1.8.x#egg=django"

install:
- sudo rm /etc/apt/sources.list.d/ubuntugis-stable-source.list
- sudo apt-get update -qq
- sudo apt-get install -qq python-gdal gdal-bin binutils
- ln -s /usr/lib/python2.7/dist-packages/osgeo ~/virtualenv/python2.7/lib/python2.7/site-packages/
- ln -s /usr/lib/python2.7/dist-packages/GDAL-1.7.3.egg-info ~/virtualenv/python2.7/lib/python2.7/site-packages/
- pip install $MODULES --use-mirrors
- pip install -r requirements.txt --use-mirrors
- sudo apt-get install -qq gdal-bin binutils
- pip install $MODULES
- pip install -r requirements.txt
# The below is for Django 1.4 compatibility. If you don't need that, you could
# just do the before_script step commented out below.
- createdb -E UTF8 template_postgis &&
Expand Down
16 changes: 14 additions & 2 deletions mapit/djangopatch.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
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.
if django.VERSION < (1, 6):
if django.get_version() < '1.6':
class GetQuerySetMetaclass(type):
def __new__(cls, name, bases, attrs):
new_class = super(GetQuerySetMetaclass, cls).__new__(cls, name, bases, attrs)
Expand All @@ -21,9 +30,12 @@ def __new__(cls, name, bases, attrs):
setattr(base, old_method_name, new_method)

return new_class
else:
elif django.get_version() < '1.8':
# Nothing to do, make an empty metaclass
from django.db.models.manager import RenameManagerMethods

class GetQuerySetMetaclass(RenameManagerMethods):
pass
else:
class GetQuerySetMetaclass(type):
pass
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/templates/mapit/api/area.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load url from future %}
{% load url from compat %}
<section id="api-by_area_id">
<h3><em>lookup</em> by area</h3>
<dl>
Expand Down
2 changes: 1 addition & 1 deletion mapit/templates/mapit/api/areas.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load url from future %}
{% load url from compat %}
<section id="api-multiple_areas">
<h3><em>lookup</em> multiple areas</h3>
<dl>
Expand Down
2 changes: 1 addition & 1 deletion mapit/templates/mapit/api/generations.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% load i18n %}
{% load url from future %}
{% load url from compat %}

<section id="api-generations">
<h3>Generations</h3>
Expand Down
2 changes: 1 addition & 1 deletion mapit/templates/mapit/api/point.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load url from future %}
{% load url from compat %}
<section id="api-by_point">
<h3><em>lookup by</em> point</h3>
<dl>
Expand Down
2 changes: 1 addition & 1 deletion mapit/templates/mapit/api/postcode.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load url from future %}
{% load url from compat %}
<section id="api-by_postcode">
<h3><em>lookup by</em> postcode</h3>
<dl>
Expand Down
2 changes: 1 addition & 1 deletion mapit/templates/mapit/area.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "mapit/base.html" %}
{% load url from future %}
{% load url from compat %}

{% block title %}{{ area.name }}{% endblock title %}

Expand Down
2 changes: 1 addition & 1 deletion mapit/templates/mapit/areas_item.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load url from future %}
{% load url from compat %}
<li{% if indent_areas %} class="{{ area.css_indent_class }}"{% endif %}>
<h3><a href="{% url "mapit_index" %}area/{{ area.id }}.html">{{ area.name }}</a></h3>
<p>ID {{ area.id }}, {{ area.type.description }}
Expand Down
2 changes: 1 addition & 1 deletion mapit/templates/mapit/base.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load url from future %}
{% load url from compat %}
{% load static from staticfiles %}
<!DOCTYPE HTML>
<html lang="en-gb">
Expand Down
2 changes: 1 addition & 1 deletion mapit/templates/mapit/example-postcode.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "mapit/base.html" %}
{% load url from future %}
{% load url from compat %}

{% block title %}Example postcode in {{ area.name }}{% endblock title %}

Expand Down
2 changes: 1 addition & 1 deletion mapit/templates/mapit/generations.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "mapit/base.html" %}
{% load i18n %}
{% load url from future %}
{% load url from compat %}

{% block title %}{% trans 'Generations' %}{% endblock title %}

Expand Down
2 changes: 1 addition & 1 deletion mapit/templates/mapit/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "mapit/base.html" %}
{% load url from future %}
{% load url from compat %}
{% load static from staticfiles %}

{% block fulltitle %}
Expand Down
2 changes: 1 addition & 1 deletion mapit/templates/mapit/postcode.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "mapit/base.html" %}
{% load url from future %}
{% load url from compat %}

{% block title %}Results for &ldquo;{{ postcode.postcode }}&rdquo;{% endblock title %}

Expand Down
Empty file added mapit/templatetags/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions mapit/templatetags/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import django
from django.template import Library

if django.get_version() >= '1.5':
from django.template.defaulttags import url as django_url
else:
from django.templatetags.future import url as django_url

register = Library()

@register.tag
def url(parser, token):
return django_url(parser, token)
94 changes: 47 additions & 47 deletions mapit/urls.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
from django.conf.urls import patterns, include
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'

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

urlpatterns = patterns(
'',
(r'^$', render, {'template_name': 'mapit/index.html'}, 'mapit_index'),
(r'^licensing$', render, {'template_name': 'mapit/licensing.html'}),
(r'^overview$', render, {'template_name': 'mapit/overview.html'}),

(r'^generations%s$' % format_end, 'mapit.views.areas.generations', {}, 'mapit_generations'),

(r'^postcode/$', 'mapit.views.postcodes.form_submitted'),
(r'^postcode/(?P<postcode>[A-Za-z0-9 +]+)%s$' % format_end, 'mapit.views.postcodes.postcode'),
(r'^postcode/partial/(?P<postcode>[A-Za-z0-9 ]+)%s$' % format_end, 'mapit.views.postcodes.partial_postcode'),

(r'^area/(?P<area_id>[0-9A-Z]+)%s$' % format_end, 'mapit.views.areas.area'),
(r'^area/(?P<area_id>[0-9]+)/example_postcode%s$' % format_end, 'mapit.views.postcodes.example_postcode_for_area'),
(r'^area/(?P<area_id>[0-9]+)/children%s$' % format_end, 'mapit.views.areas.area_children'),
(r'^area/(?P<area_id>[0-9]+)/geometry$', 'mapit.views.areas.area_geometry'),
(r'^area/(?P<area_id>[0-9]+)/touches%s$' % format_end, 'mapit.views.areas.area_touches'),
(r'^area/(?P<area_id>[0-9]+)/overlaps%s$' % format_end, 'mapit.views.areas.area_overlaps'),
(r'^area/(?P<area_id>[0-9]+)/covers%s$' % format_end, 'mapit.views.areas.area_covers'),
(r'^area/(?P<area_id>[0-9]+)/covered%s$' % format_end, 'mapit.views.areas.area_covered'),
(r'^area/(?P<area_id>[0-9]+)/coverlaps%s$' % format_end, 'mapit.views.areas.area_coverlaps'),
(r'^area/(?P<area_id>[0-9]+)/intersects%s$' % format_end, 'mapit.views.areas.area_intersects'),
(r'^area/(?P<area_id>[0-9A-Z]+)\.(?P<format>kml|geojson|wkt)$', 'mapit.views.areas.area_polygon'),
(r'^area/(?P<srid>[0-9]+)/(?P<area_id>[0-9]+)\.(?P<format>kml|json|geojson|wkt)$',
'mapit.views.areas.area_polygon'),

(r'^point/$', 'mapit.views.areas.point_form_submitted'),
(r'^point/(?P<srid>[0-9]+)/(?P<x>[0-9.-]+),(?P<y>[0-9.-]+)(?:/(?P<bb>box))?%s$' % format_end,
'mapit.views.areas.areas_by_point'),
(r'^point/latlon/(?P<lat>[0-9.-]+),(?P<lon>[0-9.-]+)(?:/(?P<bb>box))?%s$' % format_end,
'mapit.views.areas.areas_by_point_latlon'),
(r'^point/osgb/(?P<e>[0-9.-]+),(?P<n>[0-9.-]+)(?:/(?P<bb>box))?%s$' % format_end,
'mapit.views.areas.areas_by_point_osgb'),

(r'^nearest/(?P<srid>[0-9]+)/(?P<x>[0-9.-]+),(?P<y>[0-9.-]+)%s$' % format_end, 'mapit.views.postcodes.nearest'),

(r'^areas/(?P<area_ids>[0-9]+(?:,[0-9]+)*)%s$' % format_end, 'mapit.views.areas.areas'),
(r'^areas/(?P<area_ids>[0-9]+(?:,[0-9]+)*)/geometry$', 'mapit.views.areas.areas_geometry'),
(r'^areas/(?P<type>[A-Z0-9,]*[A-Z0-9]+)%s$' % format_end, 'mapit.views.areas.areas_by_type'),
(r'^areas/(?P<name>.+?)%s$' % format_end, 'mapit.views.areas.areas_by_name'),
(r'^areas$', 'mapit.views.areas.deal_with_POST', {'call': 'areas'}),
(r'^code/(?P<code_type>[^/]+)/(?P<code_value>[^/]+?)%s$' % format_end, 'mapit.views.areas.area_from_code'),
)
urlpatterns = [
url(r'^$', render, {'template_name': 'mapit/index.html'}, 'mapit_index'),
url(r'^licensing$', render, {'template_name': 'mapit/licensing.html'}),
url(r'^overview$', render, {'template_name': 'mapit/overview.html'}),

url(r'^generations%s$' % format_end, areas.generations, {}, 'mapit_generations'),

url(r'^postcode/$', postcodes.form_submitted),
url(r'^postcode/(?P<postcode>[A-Za-z0-9 +]+)%s$' % format_end, postcodes.postcode, name="mapit-postcode"),
url(r'^postcode/partial/(?P<postcode>[A-Za-z0-9 ]+)%s$' % format_end, postcodes.partial_postcode, name="mapit-postcode-partial"),

url(r'^area/(?P<area_id>[0-9A-Z]+)%s$' % format_end, areas.area),
url(r'^area/(?P<area_id>[0-9]+)/example_postcode%s$' % format_end, postcodes.example_postcode_for_area),
url(r'^area/(?P<area_id>[0-9]+)/children%s$' % format_end, areas.area_children),
url(r'^area/(?P<area_id>[0-9]+)/geometry$', areas.area_geometry),
url(r'^area/(?P<area_id>[0-9]+)/touches%s$' % format_end, areas.area_touches),
url(r'^area/(?P<area_id>[0-9]+)/overlaps%s$' % format_end, areas.area_overlaps),
url(r'^area/(?P<area_id>[0-9]+)/covers%s$' % format_end, areas.area_covers),
url(r'^area/(?P<area_id>[0-9]+)/covered%s$' % format_end, areas.area_covered),
url(r'^area/(?P<area_id>[0-9]+)/coverlaps%s$' % format_end, areas.area_coverlaps),
url(r'^area/(?P<area_id>[0-9]+)/intersects%s$' % format_end, areas.area_intersects),
url(r'^area/(?P<area_id>[0-9A-Z]+)\.(?P<format>kml|geojson|wkt)$', areas.area_polygon),
url(r'^area/(?P<srid>[0-9]+)/(?P<area_id>[0-9]+)\.(?P<format>kml|json|geojson|wkt)$',
areas.area_polygon),

url(r'^point/$', areas.point_form_submitted),
url(r'^point/(?P<srid>[0-9]+)/(?P<x>[0-9.-]+),(?P<y>[0-9.-]+)(?:/(?P<bb>box))?%s$' % format_end,
areas.areas_by_point),
url(r'^point/latlon/(?P<lat>[0-9.-]+),(?P<lon>[0-9.-]+)(?:/(?P<bb>box))?%s$' % format_end,
areas.areas_by_point_latlon),
url(r'^point/osgb/(?P<e>[0-9.-]+),(?P<n>[0-9.-]+)(?:/(?P<bb>box))?%s$' % format_end,
areas.areas_by_point_osgb),

url(r'^nearest/(?P<srid>[0-9]+)/(?P<x>[0-9.-]+),(?P<y>[0-9.-]+)%s$' % format_end, postcodes.nearest),

url(r'^areas/(?P<area_ids>[0-9]+(?:,[0-9]+)*)%s$' % format_end, areas.areas),
url(r'^areas/(?P<area_ids>[0-9]+(?:,[0-9]+)*)/geometry$', areas.areas_geometry),
url(r'^areas/(?P<type>[A-Z0-9,]*[A-Z0-9]+)%s$' % format_end, areas.areas_by_type),
url(r'^areas/(?P<name>.+?)%s$' % format_end, areas.areas_by_name),
url(r'^areas$', areas.deal_with_POST, {'call': 'areas'}),
url(r'^code/(?P<code_type>[^/]+)/(?P<code_value>[^/]+?)%s$' % format_end, areas.area_from_code),
]

# Include app-specific urls
if (settings.MAPIT_COUNTRY == 'GB'):
urlpatterns += patterns(
'', (r'^', include('mapit_gb.urls')),
urlpatterns.append(
url(r'^', include('mapit_gb.urls')),
)
15 changes: 8 additions & 7 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 Expand Up @@ -50,21 +50,21 @@ def output_areas(request, title, format, areas, **kwargs):

def query_args(request, format, type=None):
try:
generation = int(request.REQUEST.get('generation', 0))
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.REQUEST.get('min_generation', 0))
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

if type is None:
type = request.REQUEST.get('type', '')
type = request.GET.get('type', '')

args = {}
if min_generation > -1:
Expand Down Expand Up @@ -180,7 +180,7 @@ def area_intersect(query_type, title, request, area_id, format):
raise ViewException(format, 'No polygons found', 404)

generation = Generation.objects.current()
types = [_f for _f in request.REQUEST.get('type', '').split(',') if _f]
types = [_f for _f in request.GET.get('type', '').split(',') if _f]

set_timeout(format)
try:
Expand Down Expand Up @@ -330,7 +330,7 @@ def areas_by_point(request, srid, x, y, bb=False, format='json'):
method = 'box' if bb and bb != 'polygon' else 'polygon'

args = query_args(request, format)
type = request.REQUEST.get('type', '')
type = request.GET.get('type', '')

if type and method == 'polygon':
args = dict(("area__%s" % k, v) for k, v in args.items())
Expand Down Expand Up @@ -392,5 +392,6 @@ def deal_with_POST(request, call='areas'):
if not url:
return output_json({'error': 'No content specified'}, code=400)
view, args, kwargs = resolve('/%s/%s' % (call, url))
request.GET = request.POST
kwargs['request'] = request
return view(*args, **kwargs)
Loading

0 comments on commit fc7f2eb

Please sign in to comment.