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