PageRenderTime 323ms CodeModel.GetById 282ms app.highlight 3ms RepoModel.GetById 37ms app.codeStats 0ms

/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
 1import cStringIO, zipfile
 2from django.conf import settings
 3from django.http import HttpResponse
 4from django.template import loader
 5
 6def compress_kml(kml):
 7    "Returns compressed KMZ from the given KML string."
 8    kmz = cStringIO.StringIO()
 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
15def render_to_kml(*args, **kwargs):
16    "Renders the response as KML (using the correct MIME type)."
17    return HttpResponse(loader.render_to_string(*args, **kwargs),
18                        mimetype='application/vnd.google-earth.kml+xml')
19
20def render_to_kmz(*args, **kwargs):
21    """
22    Compresses the KML content and returns as KMZ (using the correct 
23    MIME type).
24    """
25    return HttpResponse(compress_kml(loader.render_to_string(*args, **kwargs)),
26                        mimetype='application/vnd.google-earth.kmz')
27
28
29def render_to_text(*args, **kwargs):
30    "Renders the response using the MIME type for plain text."
31    return HttpResponse(loader.render_to_string(*args, **kwargs),
32                        mimetype='text/plain')