2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2008-08-24 03:22:23 +08:00
|
|
|
from django.http import HttpResponse, Http404
|
|
|
|
from django.template import loader
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
from django.contrib.sites.models import get_current_site
|
2008-08-24 03:22:23 +08:00
|
|
|
from django.core import urlresolvers
|
|
|
|
from django.core.paginator import EmptyPage, PageNotAnInteger
|
2008-08-26 08:46:30 +08:00
|
|
|
from django.contrib.gis.db.models.fields import GeometryField
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.db import connections, DEFAULT_DB_ALIAS
|
|
|
|
from django.db.models import get_model
|
2012-07-21 03:14:27 +08:00
|
|
|
from django.utils import six
|
2012-02-05 05:01:11 +08:00
|
|
|
from django.utils.translation import ugettext as _
|
2008-08-24 03:22:23 +08:00
|
|
|
|
|
|
|
from django.contrib.gis.shortcuts import render_to_kml, render_to_kmz
|
|
|
|
|
|
|
|
def index(request, sitemaps):
|
|
|
|
"""
|
|
|
|
This view generates a sitemap index that uses the proper view
|
|
|
|
for resolving geographic section sitemap URLs.
|
|
|
|
"""
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
current_site = get_current_site(request)
|
2008-08-24 03:22:23 +08:00
|
|
|
sites = []
|
2013-05-17 22:33:36 +08:00
|
|
|
protocol = 'https' if request.is_secure() else 'http'
|
2008-08-24 03:22:23 +08:00
|
|
|
for section, site in sitemaps.items():
|
|
|
|
if callable(site):
|
|
|
|
pages = site().paginator.num_pages
|
|
|
|
else:
|
|
|
|
pages = site.paginator.num_pages
|
|
|
|
sitemap_url = urlresolvers.reverse('django.contrib.gis.sitemaps.views.sitemap', kwargs={'section': section})
|
|
|
|
sites.append('%s://%s%s' % (protocol, current_site.domain, sitemap_url))
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2008-08-24 03:22:23 +08:00
|
|
|
if pages > 1:
|
|
|
|
for page in range(2, pages+1):
|
|
|
|
sites.append('%s://%s%s?p=%s' % (protocol, current_site.domain, sitemap_url, page))
|
|
|
|
xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites})
|
2011-12-17 11:53:25 +08:00
|
|
|
return HttpResponse(xml, content_type='application/xml')
|
2008-08-24 03:22:23 +08:00
|
|
|
|
|
|
|
def sitemap(request, sitemaps, section=None):
|
|
|
|
"""
|
|
|
|
This view generates a sitemap with additional geographic
|
|
|
|
elements defined by Google.
|
|
|
|
"""
|
|
|
|
maps, urls = [], []
|
|
|
|
if section is not None:
|
|
|
|
if section not in sitemaps:
|
2012-06-08 00:08:47 +08:00
|
|
|
raise Http404(_("No sitemap available for section: %r") % section)
|
2008-08-24 03:22:23 +08:00
|
|
|
maps.append(sitemaps[section])
|
|
|
|
else:
|
2012-07-21 03:14:27 +08:00
|
|
|
maps = list(six.itervalues(sitemaps))
|
2008-08-24 03:22:23 +08:00
|
|
|
|
|
|
|
page = request.GET.get("p", 1)
|
2010-10-11 22:34:42 +08:00
|
|
|
current_site = get_current_site(request)
|
2008-08-24 03:22:23 +08:00
|
|
|
for site in maps:
|
|
|
|
try:
|
|
|
|
if callable(site):
|
2010-10-11 22:34:42 +08:00
|
|
|
urls.extend(site().get_urls(page=page, site=current_site))
|
2008-08-24 03:22:23 +08:00
|
|
|
else:
|
2010-10-11 22:34:42 +08:00
|
|
|
urls.extend(site.get_urls(page=page, site=current_site))
|
2008-08-24 03:22:23 +08:00
|
|
|
except EmptyPage:
|
2012-06-08 00:08:47 +08:00
|
|
|
raise Http404(_("Page %s empty") % page)
|
2008-08-24 03:22:23 +08:00
|
|
|
except PageNotAnInteger:
|
2012-06-08 00:08:47 +08:00
|
|
|
raise Http404(_("No page '%s'") % page)
|
2012-08-29 02:59:56 +08:00
|
|
|
xml = loader.render_to_string('gis/sitemaps/geo_sitemap.xml', {'urlset': urls})
|
2011-12-17 11:53:25 +08:00
|
|
|
return HttpResponse(xml, content_type='application/xml')
|
2008-08-24 03:22:23 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def kml(request, label, model, field_name=None, compress=False, using=DEFAULT_DB_ALIAS):
|
2008-08-24 03:22:23 +08:00
|
|
|
"""
|
|
|
|
This view generates KML for the given app label, model, and field name.
|
|
|
|
|
|
|
|
The model's default manager must be GeoManager, and the field name
|
|
|
|
must be that of a geographic field.
|
|
|
|
"""
|
|
|
|
placemarks = []
|
|
|
|
klass = get_model(label, model)
|
|
|
|
if not klass:
|
2008-08-26 08:46:30 +08:00
|
|
|
raise Http404('You must supply a valid app label and module name. Got "%s.%s"' % (label, model))
|
|
|
|
|
|
|
|
if field_name:
|
|
|
|
try:
|
|
|
|
info = klass._meta.get_field_by_name(field_name)
|
|
|
|
if not isinstance(info[0], GeometryField):
|
|
|
|
raise Exception
|
|
|
|
except:
|
|
|
|
raise Http404('Invalid geometry field.')
|
2008-08-24 03:22:23 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
connection = connections[using]
|
|
|
|
|
|
|
|
if connection.ops.postgis:
|
2008-08-24 03:22:23 +08:00
|
|
|
# PostGIS will take care of transformation.
|
2009-12-22 23:18:51 +08:00
|
|
|
placemarks = klass._default_manager.using(using).kml(field_name=field_name)
|
2008-08-24 03:22:23 +08:00
|
|
|
else:
|
|
|
|
# There's no KML method on Oracle or MySQL, so we use the `kml`
|
|
|
|
# attribute of the lazy geometry instead.
|
|
|
|
placemarks = []
|
2009-12-22 23:18:51 +08:00
|
|
|
if connection.ops.oracle:
|
|
|
|
qs = klass._default_manager.using(using).transform(4326, field_name=field_name)
|
2008-08-24 03:22:23 +08:00
|
|
|
else:
|
2009-12-22 23:18:51 +08:00
|
|
|
qs = klass._default_manager.using(using).all()
|
2008-08-24 03:22:23 +08:00
|
|
|
for mod in qs:
|
2010-09-27 08:20:42 +08:00
|
|
|
mod.kml = getattr(mod, field_name).kml
|
2008-08-24 03:22:23 +08:00
|
|
|
placemarks.append(mod)
|
|
|
|
|
|
|
|
# Getting the render function and rendering to the correct.
|
|
|
|
if compress:
|
|
|
|
render = render_to_kmz
|
|
|
|
else:
|
|
|
|
render = render_to_kml
|
|
|
|
return render('gis/kml/placemarks.kml', {'places' : placemarks})
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def kmz(request, label, model, field_name=None, using=DEFAULT_DB_ALIAS):
|
2008-08-24 03:22:23 +08:00
|
|
|
"""
|
|
|
|
This view returns KMZ for the given app label, model, and field name.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
return kml(request, label, model, field_name, compress=True, using=using)
|