PageRenderTime 26ms CodeModel.GetById 18ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/gis/sitemaps/views.py

https://code.google.com/p/mango-py/
Python | 111 lines | 83 code | 8 blank | 20 comment | 23 complexity | 27fec0bedcc735dfb506a989e2125d62 MD5 | raw file
  1from django.http import HttpResponse, Http404
  2from django.template import loader
  3from django.contrib.sites.models import get_current_site
  4from django.core import urlresolvers
  5from django.core.paginator import EmptyPage, PageNotAnInteger
  6from django.contrib.gis.db.models.fields import GeometryField
  7from django.db import connections, DEFAULT_DB_ALIAS
  8from django.db.models import get_model
  9from django.utils.encoding import smart_str
 10
 11from django.contrib.gis.shortcuts import render_to_kml, render_to_kmz
 12
 13def index(request, sitemaps):
 14    """
 15    This view generates a sitemap index that uses the proper view
 16    for resolving geographic section sitemap URLs.
 17    """
 18    current_site = get_current_site(request)
 19    sites = []
 20    protocol = request.is_secure() and 'https' or 'http'
 21    for section, site in sitemaps.items():
 22        if callable(site):
 23            pages = site().paginator.num_pages
 24        else:
 25            pages = site.paginator.num_pages
 26        sitemap_url = urlresolvers.reverse('django.contrib.gis.sitemaps.views.sitemap', kwargs={'section': section})
 27        sites.append('%s://%s%s' % (protocol, current_site.domain, sitemap_url))
 28
 29        if pages > 1:
 30            for page in range(2, pages+1):
 31                sites.append('%s://%s%s?p=%s' % (protocol, current_site.domain, sitemap_url, page))
 32    xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites})
 33    return HttpResponse(xml, mimetype='application/xml')
 34
 35def sitemap(request, sitemaps, section=None):
 36    """
 37    This view generates a sitemap with additional geographic
 38    elements defined by Google.
 39    """
 40    maps, urls = [], []
 41    if section is not None:
 42        if section not in sitemaps:
 43            raise Http404("No sitemap available for section: %r" % section)
 44        maps.append(sitemaps[section])
 45    else:
 46        maps = sitemaps.values()
 47
 48    page = request.GET.get("p", 1)
 49    current_site = get_current_site(request)
 50    for site in maps:
 51        try:
 52            if callable(site):
 53                urls.extend(site().get_urls(page=page, site=current_site))
 54            else:
 55                urls.extend(site.get_urls(page=page, site=current_site))
 56        except EmptyPage:
 57            raise Http404("Page %s empty" % page)
 58        except PageNotAnInteger:
 59            raise Http404("No page '%s'" % page)
 60    xml = smart_str(loader.render_to_string('gis/sitemaps/geo_sitemap.xml', {'urlset': urls}))
 61    return HttpResponse(xml, mimetype='application/xml')
 62
 63def kml(request, label, model, field_name=None, compress=False, using=DEFAULT_DB_ALIAS):
 64    """
 65    This view generates KML for the given app label, model, and field name.
 66
 67    The model's default manager must be GeoManager, and the field name
 68    must be that of a geographic field.
 69    """
 70    placemarks = []
 71    klass = get_model(label, model)
 72    if not klass:
 73        raise Http404('You must supply a valid app label and module name.  Got "%s.%s"' % (label, model))
 74
 75    if field_name:
 76        try:
 77            info = klass._meta.get_field_by_name(field_name)
 78            if not isinstance(info[0], GeometryField):
 79                raise Exception
 80        except:
 81            raise Http404('Invalid geometry field.')
 82
 83    connection = connections[using]
 84
 85    if connection.ops.postgis:
 86        # PostGIS will take care of transformation.
 87        placemarks = klass._default_manager.using(using).kml(field_name=field_name)
 88    else:
 89        # There's no KML method on Oracle or MySQL, so we use the `kml`
 90        # attribute of the lazy geometry instead.
 91        placemarks = []
 92        if connection.ops.oracle:
 93            qs = klass._default_manager.using(using).transform(4326, field_name=field_name)
 94        else:
 95            qs = klass._default_manager.using(using).all()
 96        for mod in qs:
 97            mod.kml = getattr(mod, field_name).kml
 98            placemarks.append(mod)
 99
100    # Getting the render function and rendering to the correct.
101    if compress:
102        render = render_to_kmz
103    else:
104        render = render_to_kml
105    return render('gis/kml/placemarks.kml', {'places' : placemarks})
106
107def kmz(request, label, model, field_name=None, using=DEFAULT_DB_ALIAS):
108    """
109    This view returns KMZ for the given app label, model, and field name.
110    """
111    return kml(request, label, model, field_name, compress=True, using=using)