root / edumanage / views.py @ 38f2278d
History | View | Annotate | Download (19.2 kB)
1 |
# -*- coding: utf-8 -*- vim:encoding=utf-8:
|
---|---|
2 |
# vim: tabstop=4:shiftwidth=4:softtabstop=4:expandtab
|
3 |
|
4 |
from django.shortcuts import render_to_response,get_object_or_404 |
5 |
from django.http import HttpResponse,HttpResponseRedirect,Http404 |
6 |
from django.template import RequestContext |
7 |
from django.core.urlresolvers import reverse |
8 |
from django.contrib.auth.decorators import login_required |
9 |
from edumanage.models import * |
10 |
from edumanage.forms import * |
11 |
from django import forms |
12 |
from django.forms.models import modelformset_factory |
13 |
from django.forms.models import inlineformset_factory |
14 |
from django.contrib.contenttypes.generic import generic_inlineformset_factory |
15 |
import json |
16 |
import math |
17 |
from xml.etree import cElementTree as ET |
18 |
|
19 |
from django.conf import settings |
20 |
|
21 |
def index(request): |
22 |
return render_to_response('front/index.html', context_instance=RequestContext(request, base_response(request))) |
23 |
|
24 |
@login_required
|
25 |
def manage(request): |
26 |
services_list = [] |
27 |
servers_list = [] |
28 |
user = request.user |
29 |
try:
|
30 |
profile = user.get_profile() |
31 |
inst = profile.institution |
32 |
except UserProfile.DoesNotExist:
|
33 |
inst = False
|
34 |
services = ServiceLoc.objects.filter(institutionid=inst) |
35 |
services_list.extend([s for s in services]) |
36 |
servers = InstServer.objects.filter(instid=inst) |
37 |
servers_list.extend([s for s in servers]) |
38 |
return render_to_response('edumanage/welcome.html', |
39 |
{ |
40 |
'institution': inst,
|
41 |
'services': services_list,
|
42 |
'servers': servers_list
|
43 |
}, |
44 |
context_instance=RequestContext(request, base_response(request))) |
45 |
|
46 |
@login_required
|
47 |
def institutions(request): |
48 |
user = request.user |
49 |
dict = {} |
50 |
|
51 |
try:
|
52 |
profile = user.get_profile() |
53 |
inst = profile.institution |
54 |
inst.__unicode__ = inst.get_name(request.LANGUAGE_CODE) |
55 |
except UserProfile.DoesNotExist:
|
56 |
inst = False
|
57 |
dict['institution'] = inst.pk |
58 |
form = InstDetailsForm(initial=dict)
|
59 |
form.fields['institution'].widget.attrs['readonly'] = True |
60 |
return render_to_response('edumanage/institution.html', |
61 |
{ |
62 |
'institution': inst,
|
63 |
'form': form,
|
64 |
}, |
65 |
context_instance=RequestContext(request, base_response(request))) |
66 |
|
67 |
|
68 |
|
69 |
@login_required
|
70 |
def add_institution_details(request, institution_pk): |
71 |
user = request.user |
72 |
try:
|
73 |
profile = user.get_profile() |
74 |
inst = profile.institution |
75 |
except UserProfile.DoesNotExist:
|
76 |
inst = False
|
77 |
|
78 |
if (not inst) or (int(inst.pk) != int(institution_pk)): |
79 |
# messages.add_message(request, messages.WARNING,
|
80 |
# _("Insufficient rights on Institution. Contact your administrator"))
|
81 |
return HttpResponseRedirect(reverse("institutions")) |
82 |
if request.method == "GET": |
83 |
|
84 |
# Determine add or edit
|
85 |
request_data = request.POST.copy() |
86 |
try:
|
87 |
inst_details = InstitutionDetails.objects.get(institution=inst) |
88 |
form = InstDetailsForm(instance=inst_details) |
89 |
except InstitutionDetails.DoesNotExist:
|
90 |
form = InstDetailsForm() |
91 |
form.fields['institution'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=institution_pk), empty_label=None) |
92 |
|
93 |
form.fields['contact'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.filter(pk__in=getInstContacts(inst)))
|
94 |
return render_to_response('edumanage/institution_edit.html', { 'institution': inst, 'form': form}, |
95 |
context_instance=RequestContext(request, base_response(request))) |
96 |
elif request.method == 'POST': |
97 |
request_data = request.POST.copy() |
98 |
try:
|
99 |
inst_details = InstitutionDetails.objects.get(institution=inst) |
100 |
form = InstDetailsForm(request_data, instance=inst_details) |
101 |
except InstitutionDetails.DoesNotExist:
|
102 |
form = InstDetailsForm(request_data) |
103 |
if form.is_valid():
|
104 |
instdets = form.save() |
105 |
return HttpResponseRedirect(reverse("institutions")) |
106 |
else:
|
107 |
try:
|
108 |
profile = user.get_profile() |
109 |
inst = profile.institution |
110 |
except UserProfile.DoesNotExist:
|
111 |
inst = False
|
112 |
form.fields['institution'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=institution_pk), empty_label=None) |
113 |
form.fields['contact'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.filter(pk__in=getInstContacts(inst)))
|
114 |
return render_to_response('edumanage/institution_edit.html', { 'institution': inst, 'form': form}, |
115 |
context_instance=RequestContext(request, base_response(request))) |
116 |
|
117 |
|
118 |
@login_required
|
119 |
def services(request): |
120 |
user = request.user |
121 |
dict = {} |
122 |
|
123 |
try:
|
124 |
profile = user.get_profile() |
125 |
inst = profile.institution |
126 |
except UserProfile.DoesNotExist:
|
127 |
inst = False
|
128 |
try:
|
129 |
services = ServiceLoc.objects.filter(institutionid = inst) |
130 |
except ServiceLoc.DoesNotExist:
|
131 |
services = False
|
132 |
|
133 |
|
134 |
return render_to_response('edumanage/services.html', |
135 |
{ |
136 |
'institution': inst,
|
137 |
'services': services,
|
138 |
}, |
139 |
context_instance=RequestContext(request, base_response(request))) |
140 |
|
141 |
|
142 |
|
143 |
@login_required
|
144 |
def add_services(request, service_pk): |
145 |
user = request.user |
146 |
service = False
|
147 |
try:
|
148 |
profile = user.get_profile() |
149 |
inst = profile.institution |
150 |
except UserProfile.DoesNotExist:
|
151 |
inst = False
|
152 |
|
153 |
if request.method == "GET": |
154 |
|
155 |
# Determine add or edit
|
156 |
try:
|
157 |
service = ServiceLoc.objects.get(institutionid=inst, pk=service_pk) |
158 |
form = ServiceLocForm(instance=service) |
159 |
except ServiceLoc.DoesNotExist:
|
160 |
form = ServiceLocForm() |
161 |
form.fields['institutionid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None) |
162 |
UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, can_delete=True) |
163 |
NameFormSet = generic_inlineformset_factory(Name_i18n, extra=2, can_delete=True) |
164 |
urls_form = UrlFormSet(prefix='urlsform')
|
165 |
names_form = NameFormSet(prefix='namesform')
|
166 |
if (service):
|
167 |
NameFormSet = generic_inlineformset_factory(Name_i18n, extra=1, formset=NameFormSetFact, can_delete=True) |
168 |
names_form = NameFormSet(instance=service, prefix='namesform')
|
169 |
UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, formset=UrlFormSetFact, can_delete=True) |
170 |
urls_form = UrlFormSet(instance=service, prefix='urlsform')
|
171 |
form.fields['contact'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.filter(pk__in=getInstContacts(inst)))
|
172 |
return render_to_response('edumanage/services_edit.html', { 'form': form, 'services_form':names_form, 'urls_form': urls_form}, |
173 |
context_instance=RequestContext(request, base_response(request))) |
174 |
elif request.method == 'POST': |
175 |
request_data = request.POST.copy() |
176 |
NameFormSet = generic_inlineformset_factory(Name_i18n, extra=1, formset=NameFormSetFact, can_delete=True) |
177 |
UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, formset=UrlFormSetFact, can_delete=True) |
178 |
try:
|
179 |
service = ServiceLoc.objects.get(institutionid=inst, pk=service_pk) |
180 |
form = ServiceLocForm(request_data, instance=service) |
181 |
names_form = NameFormSet(request_data, instance=service, prefix='namesform')
|
182 |
urls_form = UrlFormSet(request_data, instance=service, prefix='urlsform')
|
183 |
except ServiceLoc.DoesNotExist:
|
184 |
form = ServiceLocForm(request_data) |
185 |
names_form = NameFormSet(request_data, prefix='namesform')
|
186 |
urls_form = UrlFormSet(request_data, prefix='urlsform')
|
187 |
|
188 |
if form.is_valid() and names_form.is_valid() and urls_form.is_valid(): |
189 |
serviceloc = form.save() |
190 |
service = serviceloc |
191 |
names_form.instance = service |
192 |
urls_form.instance = service |
193 |
names_inst = names_form.save() |
194 |
urls_inst = urls_form.save() |
195 |
# for nform in names_inst:
|
196 |
# nform.content_object = serviceloc
|
197 |
# nform.save()
|
198 |
# for uform in urls_inst:
|
199 |
# uform.content_object = serviceloc
|
200 |
# uform.save()
|
201 |
return HttpResponseRedirect(reverse("services")) |
202 |
else:
|
203 |
form.fields['institutionid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None) |
204 |
form.fields['contact'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.filter(pk__in=getInstContacts(inst)))
|
205 |
return render_to_response('edumanage/services_edit.html', { 'institution': inst, 'form': form, 'services_form':names_form, 'urls_form': urls_form}, |
206 |
context_instance=RequestContext(request, base_response(request))) |
207 |
|
208 |
|
209 |
|
210 |
@login_required
|
211 |
def get_service_points(request): |
212 |
if request.method == "GET": |
213 |
user = request.user |
214 |
try:
|
215 |
profile = user.get_profile() |
216 |
inst = profile.institution |
217 |
except UserProfile.DoesNotExist:
|
218 |
inst = False
|
219 |
servicelocs = ServiceLoc.objects.filter(institutionid=inst) |
220 |
|
221 |
locs = [] |
222 |
for sl in servicelocs: |
223 |
response_location = {} |
224 |
response_location['lat'] = u"%s"%sl.latitude |
225 |
response_location['lng'] = u"%s"%sl.longitude |
226 |
response_location['address'] = u"%s<br>%s"%(sl.address_street, sl.address_city) |
227 |
response_location['enc'] = u"%s"%(sl.enc_level) |
228 |
response_location['AP_no'] = u"%s"%(sl.AP_no) |
229 |
response_location['name'] = sl.loc_name.get(lang='en').name |
230 |
response_location['port_restrict'] = u"%s"%(sl.port_restrict) |
231 |
response_location['transp_proxy'] = u"%s"%(sl.transp_proxy) |
232 |
response_location['IPv6'] = u"%s"%(sl.IPv6) |
233 |
response_location['NAT'] = u"%s"%(sl.NAT) |
234 |
response_location['wired'] = u"%s"%(sl.wired) |
235 |
response_location['SSID'] = u"%s"%(sl.SSID) |
236 |
response_location['key'] = u"%s"%sl.pk |
237 |
locs.append(response_location) |
238 |
return HttpResponse(json.dumps(locs), mimetype='application/json') |
239 |
else:
|
240 |
return HttpResponseNotFound('<h1>Something went really wrong</h1>') |
241 |
|
242 |
|
243 |
def get_all_services(request): |
244 |
servicelocs = ServiceLoc.objects.all() |
245 |
locs = [] |
246 |
for sl in servicelocs: |
247 |
response_location = {} |
248 |
response_location['lat'] = u"%s"%sl.latitude |
249 |
response_location['lng'] = u"%s"%sl.longitude |
250 |
response_location['address'] = u"%s<br>%s"%(sl.address_street, sl.address_city) |
251 |
response_location['enc'] = u"%s"%(sl.enc_level) |
252 |
response_location['AP_no'] = u"%s"%(sl.AP_no) |
253 |
response_location['inst'] = sl.institutionid.org_name.get(lang='en').name |
254 |
response_location['name'] = sl.loc_name.get(lang='en').name |
255 |
response_location['port_restrict'] = u"%s"%(sl.port_restrict) |
256 |
response_location['transp_proxy'] = u"%s"%(sl.transp_proxy) |
257 |
response_location['IPv6'] = u"%s"%(sl.IPv6) |
258 |
response_location['NAT'] = u"%s"%(sl.NAT) |
259 |
response_location['wired'] = u"%s"%(sl.wired) |
260 |
response_location['SSID'] = u"%s"%(sl.SSID) |
261 |
response_location['key'] = u"%s"%sl.pk |
262 |
locs.append(response_location) |
263 |
return HttpResponse(json.dumps(locs), mimetype='application/json') |
264 |
|
265 |
|
266 |
|
267 |
def servers(request): |
268 |
user = request.user |
269 |
servers = False
|
270 |
try:
|
271 |
profile = user.get_profile() |
272 |
inst = profile.institution |
273 |
except UserProfile.DoesNotExist:
|
274 |
inst = False
|
275 |
if inst:
|
276 |
servers = InstServer.objects.filter(instid=inst) |
277 |
return render_to_response('edumanage/servers.html', { 'servers': servers}, |
278 |
context_instance=RequestContext(request, base_response(request))) |
279 |
|
280 |
|
281 |
|
282 |
@login_required
|
283 |
def adduser(request): |
284 |
user = request.user |
285 |
try:
|
286 |
profile = user.get_profile() |
287 |
inst = profile.institution |
288 |
except UserProfile.DoesNotExist:
|
289 |
inst = False
|
290 |
if request.method == "GET": |
291 |
form = ContactForm() |
292 |
return render_to_response('edumanage/add_user.html', { 'form' : form }, |
293 |
context_instance=RequestContext(request, base_response(request))) |
294 |
elif request.method == 'POST': |
295 |
request_data = request.POST.copy() |
296 |
form = ContactForm(request_data) |
297 |
if form.is_valid():
|
298 |
contact = form.save() |
299 |
instContPool = InstitutionContactPool(contact=contact, institution=inst) |
300 |
instContPool.save() |
301 |
response_data = {} |
302 |
response_data['value'] = "%s" %contact.pk |
303 |
response_data['text'] = "%s" %contact |
304 |
return HttpResponse(json.dumps(response_data), mimetype='application/json') |
305 |
else:
|
306 |
return render_to_response('edumanage/add_user.html', {'form': form,}, |
307 |
context_instance=RequestContext(request, base_response(request))) |
308 |
|
309 |
@login_required
|
310 |
def add_server(request, server_pk): |
311 |
user = request.user |
312 |
server = False
|
313 |
try:
|
314 |
profile = user.get_profile() |
315 |
inst = profile.institution |
316 |
except UserProfile.DoesNotExist:
|
317 |
inst = False
|
318 |
|
319 |
if request.method == "GET": |
320 |
|
321 |
# Determine add or edit
|
322 |
try:
|
323 |
server = InstServer.objects.get(instid=inst, pk=server_pk) |
324 |
form = InstServerForm(instance=server) |
325 |
except InstServer.DoesNotExist:
|
326 |
form = InstServerForm() |
327 |
form.fields['instid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None) |
328 |
|
329 |
return render_to_response('edumanage/servers_edit.html', { 'form': form}, |
330 |
context_instance=RequestContext(request, base_response(request))) |
331 |
elif request.method == 'POST': |
332 |
request_data = request.POST.copy() |
333 |
try:
|
334 |
server = InstServer.objects.get(instid=inst, pk=server_pk) |
335 |
form = InstServerForm(request_data, instance=server) |
336 |
except InstServer.DoesNotExist:
|
337 |
form = InstServerForm(request_data) |
338 |
|
339 |
if form.is_valid():
|
340 |
instserverf = form.save() |
341 |
return HttpResponseRedirect(reverse("servers")) |
342 |
else:
|
343 |
form.fields['instid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None) |
344 |
print form.errors
|
345 |
return render_to_response('edumanage/servers_edit.html', { 'institution': inst, 'form': form}, |
346 |
context_instance=RequestContext(request, base_response(request))) |
347 |
|
348 |
@login_required
|
349 |
def base_response(request): |
350 |
user = request.user |
351 |
inst = [] |
352 |
server = [] |
353 |
services = [] |
354 |
try:
|
355 |
profile = user.get_profile() |
356 |
institution = profile.institution |
357 |
inst.append(institution) |
358 |
server = InstServer.objects.filter(instid=institution) |
359 |
services = ServiceLoc.objects.filter(institutionid=institution) |
360 |
except UserProfile.DoesNotExist:
|
361 |
pass
|
362 |
|
363 |
return {
|
364 |
'inst_num': len(inst), |
365 |
'servers_num': len(server), |
366 |
'services_num': len(services), |
367 |
|
368 |
} |
369 |
|
370 |
@login_required
|
371 |
def del_server(request): |
372 |
if request.method == 'GET': |
373 |
user = request.user |
374 |
req_data = request.GET.copy() |
375 |
server_pk = req_data['server_pk']
|
376 |
profile = user.get_profile() |
377 |
institution = profile.institution |
378 |
resp = {} |
379 |
try:
|
380 |
server = InstServer.objects.get(instid=institution, pk=server_pk) |
381 |
except InstServer.DoesNotExist:
|
382 |
resp['error'] = "Could not get server or you have no rights to delete" |
383 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
384 |
try:
|
385 |
server.delete() |
386 |
except:
|
387 |
resp['error'] = "Could not delete server" |
388 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
389 |
resp['success'] = "Server successfully deleted" |
390 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
391 |
|
392 |
|
393 |
@login_required
|
394 |
def del_service(request): |
395 |
if request.method == 'GET': |
396 |
user = request.user |
397 |
req_data = request.GET.copy() |
398 |
service_pk = req_data['service_pk']
|
399 |
profile = user.get_profile() |
400 |
institution = profile.institution |
401 |
resp = {} |
402 |
try:
|
403 |
service = ServiceLoc.objects.get(institutionid=institution, pk=service_pk) |
404 |
except ServiceLoc.DoesNotExist:
|
405 |
resp['error'] = "Could not get service or you have no rights to delete" |
406 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
407 |
try:
|
408 |
service.delete() |
409 |
except:
|
410 |
resp['error'] = "Could not delete service" |
411 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
412 |
resp['success'] = "Service successfully deleted" |
413 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
414 |
|
415 |
|
416 |
|
417 |
def geolocate(request): |
418 |
return render_to_response('front/geolocate.html', |
419 |
context_instance=RequestContext(request)) |
420 |
|
421 |
|
422 |
def closest(request): |
423 |
if request.method == 'GET': |
424 |
locs = [] |
425 |
request_data = request.GET.copy() |
426 |
response_location = {} |
427 |
response_location["lat"] = request_data['lat'] |
428 |
response_location["lng"] = request_data['lng'] |
429 |
lat = float(request_data['lat']) |
430 |
lng = float(request_data['lng']) |
431 |
R = 6371
|
432 |
distances = {} |
433 |
closestMarker = {} |
434 |
closest = -1
|
435 |
doc = ET.parse(settings.KML_FILE) |
436 |
root = doc.getroot() |
437 |
r = root.getchildren()[0]
|
438 |
for (counter, i) in enumerate(r.getchildren()): |
439 |
if "id" in i.keys(): |
440 |
j = i.getchildren() |
441 |
pointname = j[0].text
|
442 |
point = j[2].getchildren()[0].text |
443 |
pointlng, pointlat, pointele = point.split(',')
|
444 |
dLat = rad(float(pointlat)-float(lat)) |
445 |
dLong = rad(float(pointlng)-float(lng)) |
446 |
a = math.sin(dLat/2) * math.sin(dLat/2) + math.cos(rad(lat)) * math.cos(rad(float(pointlat))) * math.sin(dLong/2) * math.sin(dLong/2) |
447 |
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) |
448 |
d = R * c |
449 |
distances[counter] = d |
450 |
if (closest == -1 or d < distances[closest]): |
451 |
closest = counter |
452 |
closestMarker = {"name": pointname, "lat": pointlat, "lng": pointlng, "text": j[1].text} |
453 |
return HttpResponse(json.dumps(closestMarker), mimetype='application/json') |
454 |
|
455 |
|
456 |
|
457 |
def getInstContacts(inst): |
458 |
contacts = InstitutionContactPool.objects.filter(institution=inst) |
459 |
contact_pks = [] |
460 |
for contact in contacts: |
461 |
contact_pks.append(contact.contact.pk) |
462 |
return list(set(contact_pks)) |
463 |
|
464 |
def rad(x): |
465 |
return x*math.pi/180 |