1 from django.http import HttpResponse, Http404
2
3 from django.template import RequestContext
4 from django.shortcuts import get_object_or_404, render_to_response
5 from models import *
6 import os
7 import itertools
8 import posixpath
9 import urllib
10 import mimetypes as _mimetypes
11 from django.conf import settings
12 from django.template.loader import get_template
13 from madrona.common import default_mimetypes as mimetypes
14 from madrona.common.utils import load_session
15 from django.core.urlresolvers import reverse
16 from madrona.features.views import get_object_for_viewing
17 from django.contrib.auth.models import Group
18 from madrona.common.utils import is_text
19
20 -def get_kml_file(request, uid, session_key='0', input_username=None):
21 load_session(request, session_key)
22 user = request.user
23
24 if input_username and user.username != input_username:
25 return HttpResponse('Access denied', status=401)
26
27 instance = get_object_for_viewing(request, uid)
28 if isinstance(instance, HttpResponse):
29 return instance
30
31 full = instance.kml_full
32 response = HttpResponse(full)
33 if is_text(full):
34 response['Content-Type'] = mimetypes.KML
35 else:
36 response['Content-Type'] = mimetypes.KMZ
37 return response
38
40 """
41 Since privatekmls are not Features, they get their own sharing scheme
42 """
43 if user.is_anonymous() or not user.is_authenticated():
44 return False, HttpResponse('You must be logged in', status=401)
45
46 for user_group in user.groups.all():
47 if user_group in layer.sharing_groups.all():
48 return True, HttpResponse('User %s has permission to view %s' % (user.username, layer.name))
49
50 return False, HttpResponse('Access denied', status=403)
51
53 load_session(request, session_key)
54 user = request.user
55 if user.is_anonymous() or not user.is_authenticated():
56 return HttpResponse('You must be logged in', status=401)
57 all_kmls = PrivateKml.objects.all()
58 accessible_kmls = []
59 for kml in all_kmls:
60 viewable, response = is_privatekml_viewable(kml, user)
61 if viewable:
62 accessible_kmls.append(kml)
63
64 t = get_template('layers/private.kml')
65 kml = t.render(RequestContext(request, {'session_key': session_key,
66 'kmls': accessible_kmls}))
67 response = HttpResponse(kml, mimetype=mimetypes.KML)
68 response['Content-Disposition'] = 'filename=privatekml_%s.kml' % user.username
69 return response
70
72 """
73 Returns a boolean - does user have any private kmls shared with them?
74 """
75 all_kmls = PrivateKml.objects.all()
76 accessible_kmls = []
77 for kml in all_kmls:
78 viewable, response = is_privatekml_viewable(kml, user)
79 if viewable:
80 accessible_kmls.append(kml)
81 if len(accessible_kmls) >= 1:
82 return True
83 else:
84 return False
85
87 load_session(request, session_key)
88 user = request.user
89 if user.is_anonymous() or not user.is_authenticated():
90 return HttpResponse('You must be logged in', status=401)
91 layer = PrivateKml.objects.get(pk=pk)
92 viewable, response = is_privatekml_viewable(layer, user)
93 if not viewable:
94 return response
95 else:
96 mimetype, encoding = _mimetypes.guess_type(layer.base_kml)
97 mimetype = mimetype or 'application/octet-stream'
98 response = HttpResponse(open(layer.base_kml,'rb').read(), status=200, mimetype=mimetype)
99 response['Content-Disposition'] = 'filename=privatekml_%s.kml' % pk
100 return response
101
103 load_session(request, session_key)
104 user = request.user
105 if user.is_anonymous() or not user.is_authenticated():
106 return HttpResponse('You must be logged in', status=401)
107 layer = PrivateKml.objects.get(pk=pk)
108 viewable, response = is_privatekml_viewable(layer, user)
109 if not viewable:
110 return response
111
112
113 path = posixpath.normpath(urllib.unquote(path))
114 path = path.lstrip('/')
115 newpath = ''
116 for part in path.split('/'):
117 if not part:
118
119 continue
120 drive, part = os.path.splitdrive(part)
121 head, part = os.path.split(part)
122 if part in (os.curdir, os.pardir):
123
124 continue
125 newpath = os.path.join(newpath, part).replace('\\', '/')
126
127
128 if newpath and path == newpath:
129 basedir = os.path.dirname(layer.base_kml)
130 requested_file = os.path.join(basedir,newpath)
131 if not os.path.exists(requested_file):
132 raise Http404
133 mimetype, encoding = _mimetypes.guess_type(requested_file)
134 mimetype = mimetype or 'application/octet-stream'
135 return HttpResponse(open(requested_file,'rb').read(), status=200, mimetype=mimetype)
136 else:
137 return HttpResponse("Nice try", status=403)
138
140 """Returns uploaded kml from the :class:`PublicLayerList <madrona.layers.models.PublicLayerList>`
141 object marked ``active``.
142 """
143 try:
144 layer = PublicLayerList.objects.filter(active=True)[0]
145 except:
146 raise Http404
147 response = HttpResponse(layer.kml_file.read(), mimetype=mimetypes.KML)
148 response['Content-Disposition'] = 'attachment; filename=public.kml'
149 return response
150