2008-08-24 03:22:23 +08:00
|
|
|
import cStringIO, zipfile
|
2009-09-13 02:35:08 +08:00
|
|
|
from django.conf import settings
|
2008-08-06 02:13:06 +08:00
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.template import loader
|
|
|
|
|
2008-08-24 03:22:23 +08:00
|
|
|
def compress_kml(kml):
|
|
|
|
"Returns compressed KMZ from the given KML string."
|
|
|
|
kmz = cStringIO.StringIO()
|
2008-12-08 22:50:06 +08:00
|
|
|
zf = zipfile.ZipFile(kmz, 'a', zipfile.ZIP_DEFLATED)
|
2009-09-13 02:35:08 +08:00
|
|
|
zf.writestr('doc.kml', kml.encode(settings.DEFAULT_CHARSET))
|
2008-08-24 03:22:23 +08:00
|
|
|
zf.close()
|
|
|
|
kmz.seek(0)
|
|
|
|
return kmz.read()
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def render_to_kml(*args, **kwargs):
|
2008-08-24 03:22:23 +08:00
|
|
|
"Renders the response as KML (using the correct MIME type)."
|
2008-08-06 02:13:06 +08:00
|
|
|
return HttpResponse(loader.render_to_string(*args, **kwargs),
|
2008-12-06 08:38:48 +08:00
|
|
|
mimetype='application/vnd.google-earth.kml+xml')
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2008-08-24 03:22:23 +08:00
|
|
|
def render_to_kmz(*args, **kwargs):
|
|
|
|
"""
|
|
|
|
Compresses the KML content and returns as KMZ (using the correct
|
|
|
|
MIME type).
|
|
|
|
"""
|
|
|
|
return HttpResponse(compress_kml(loader.render_to_string(*args, **kwargs)),
|
|
|
|
mimetype='application/vnd.google-earth.kmz')
|
|
|
|
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def render_to_text(*args, **kwargs):
|
|
|
|
"Renders the response using the MIME type for plain text."
|
|
|
|
return HttpResponse(loader.render_to_string(*args, **kwargs),
|
|
|
|
mimetype='text/plain')
|