shortcuts.py 1.1 KB

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