app/django/contrib/gis/shortcuts.py
changeset 323 ff1a9aa48cfd
equal deleted inserted replaced
322:6641e941ef1e 323:ff1a9aa48cfd
       
     1 import cStringIO, zipfile
       
     2 from django.http import HttpResponse
       
     3 from django.template import loader
       
     4 
       
     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, False)
       
     9     zf.writestr('doc.kml', kml)
       
    10     zf.close()
       
    11     kmz.seek(0)
       
    12     return kmz.read()
       
    13 
       
    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                         mimetype='application/vnd.google-earth.kml+xml kml')
       
    18 
       
    19 def render_to_kmz(*args, **kwargs):
       
    20     """
       
    21     Compresses the KML content and returns as KMZ (using the correct 
       
    22     MIME type).
       
    23     """
       
    24     return HttpResponse(compress_kml(loader.render_to_string(*args, **kwargs)),
       
    25                         mimetype='application/vnd.google-earth.kmz')
       
    26 
       
    27 
       
    28 def render_to_text(*args, **kwargs):
       
    29     "Renders the response using the MIME type for plain text."
       
    30     return HttpResponse(loader.render_to_string(*args, **kwargs),
       
    31                         mimetype='text/plain')