|
0
|
1 |
import cStringIO, zipfile |
|
|
2 |
from django.conf import settings |
|
|
3 |
from django.http import HttpResponse |
|
|
4 |
from django.template import loader |
|
|
5 |
|
|
|
6 |
def 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 |
|
|
|
15 |
def 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 |
|
|
|
20 |
def 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 |
|
|
|
29 |
def 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') |