/django/contrib/gis/shortcuts.py

https://code.google.com/p/mango-py/ · Python · 32 lines · 23 code · 5 blank · 4 comment · 0 complexity · fa89ec50980ba11ad993d9dab65a9a49 MD5 · raw file

  1. import cStringIO, zipfile
  2. from django.conf import settings
  3. from django.http import HttpResponse
  4. from django.template import loader
  5. def compress_kml(kml):
  6. "Returns compressed KMZ from the given KML string."
  7. kmz = cStringIO.StringIO()
  8. zf = zipfile.ZipFile(kmz, 'a', zipfile.ZIP_DEFLATED)
  9. zf.writestr('doc.kml', kml.encode(settings.DEFAULT_CHARSET))
  10. zf.close()
  11. kmz.seek(0)
  12. return kmz.read()
  13. def render_to_kml(*args, **kwargs):
  14. "Renders the response as KML (using the correct MIME type)."
  15. return HttpResponse(loader.render_to_string(*args, **kwargs),
  16. mimetype='application/vnd.google-earth.kml+xml')
  17. def render_to_kmz(*args, **kwargs):
  18. """
  19. Compresses the KML content and returns as KMZ (using the correct
  20. MIME type).
  21. """
  22. return HttpResponse(compress_kml(loader.render_to_string(*args, **kwargs)),
  23. mimetype='application/vnd.google-earth.kmz')
  24. def render_to_text(*args, **kwargs):
  25. "Renders the response using the MIME type for plain text."
  26. return HttpResponse(loader.render_to_string(*args, **kwargs),
  27. mimetype='text/plain')