root / edumanage / views.py @ c2b3014a
History | View | Annotate | Download (34.4 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 ElementTree 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 |
return render_to_response('edumanage/servers_edit.html', { 'institution': inst, 'form': form}, |
345 |
context_instance=RequestContext(request, base_response(request))) |
346 |
|
347 |
@login_required
|
348 |
def realms(request): |
349 |
user = request.user |
350 |
servers = False
|
351 |
try:
|
352 |
profile = user.get_profile() |
353 |
inst = profile.institution |
354 |
except UserProfile.DoesNotExist:
|
355 |
inst = False
|
356 |
if inst:
|
357 |
realms = InstRealm.objects.filter(instid=inst) |
358 |
return render_to_response('edumanage/realms.html', { 'realms': realms}, |
359 |
context_instance=RequestContext(request, base_response(request))) |
360 |
|
361 |
@login_required
|
362 |
def add_realm(request, realm_pk): |
363 |
user = request.user |
364 |
server = False
|
365 |
try:
|
366 |
profile = user.get_profile() |
367 |
inst = profile.institution |
368 |
except UserProfile.DoesNotExist:
|
369 |
inst = False
|
370 |
|
371 |
if request.method == "GET": |
372 |
|
373 |
# Determine add or edit
|
374 |
try:
|
375 |
realm = InstRealm.objects.get(instid=inst, pk=realm_pk) |
376 |
form = InstRealmForm(instance=realm) |
377 |
except InstRealm.DoesNotExist:
|
378 |
form = InstRealmForm() |
379 |
form.fields['instid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None) |
380 |
form.fields['proxyto'] = forms.ModelMultipleChoiceField(queryset=InstServer.objects.filter(pk__in=getInstServers(inst)))
|
381 |
return render_to_response('edumanage/realms_edit.html', { 'form': form}, |
382 |
context_instance=RequestContext(request, base_response(request))) |
383 |
elif request.method == 'POST': |
384 |
request_data = request.POST.copy() |
385 |
try:
|
386 |
realm = InstRealm.objects.get(instid=inst, pk=realm_pk) |
387 |
form = InstRealmForm(request_data, instance=realm) |
388 |
except InstRealm.DoesNotExist:
|
389 |
form = InstRealmForm(request_data) |
390 |
|
391 |
if form.is_valid():
|
392 |
instserverf = form.save() |
393 |
return HttpResponseRedirect(reverse("realms")) |
394 |
else:
|
395 |
form.fields['instid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None) |
396 |
form.fields['proxyto'] = forms.ModelMultipleChoiceField(queryset=InstServer.objects.filter(pk__in=getInstServers(inst)))
|
397 |
return render_to_response('edumanage/realms_edit.html', { 'institution': inst, 'form': form}, |
398 |
context_instance=RequestContext(request, base_response(request))) |
399 |
|
400 |
|
401 |
@login_required
|
402 |
def del_realm(request): |
403 |
if request.method == 'GET': |
404 |
user = request.user |
405 |
req_data = request.GET.copy() |
406 |
realm_pk = req_data['realm_pk']
|
407 |
profile = user.get_profile() |
408 |
institution = profile.institution |
409 |
resp = {} |
410 |
try:
|
411 |
realm = InstRealm.objects.get(instid=institution, pk=realm_pk) |
412 |
except InstRealm.DoesNotExist:
|
413 |
resp['error'] = "Could not get realm or you have no rights to delete" |
414 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
415 |
try:
|
416 |
realm.delete() |
417 |
except:
|
418 |
resp['error'] = "Could not delete realm" |
419 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
420 |
resp['success'] = "Realm successfully deleted" |
421 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
422 |
|
423 |
|
424 |
|
425 |
@login_required
|
426 |
def contacts(request): |
427 |
user = request.user |
428 |
servers = False
|
429 |
instcontacts = [] |
430 |
try:
|
431 |
profile = user.get_profile() |
432 |
inst = profile.institution |
433 |
except UserProfile.DoesNotExist:
|
434 |
inst = False
|
435 |
if inst:
|
436 |
instcontacts.extend([x.contact.pk for x in InstitutionContactPool.objects.filter(institution=inst)]) |
437 |
print instcontacts
|
438 |
contacts = Contact.objects.filter(pk__in=instcontacts) |
439 |
print contacts
|
440 |
return render_to_response('edumanage/contacts.html', { 'contacts': contacts}, |
441 |
context_instance=RequestContext(request, base_response(request))) |
442 |
|
443 |
@login_required
|
444 |
def add_contact(request, contact_pk): |
445 |
user = request.user |
446 |
server = False
|
447 |
try:
|
448 |
profile = user.get_profile() |
449 |
inst = profile.institution |
450 |
except UserProfile.DoesNotExist:
|
451 |
inst = False
|
452 |
|
453 |
if request.method == "GET": |
454 |
|
455 |
# Determine add or edit
|
456 |
try:
|
457 |
contactinst = InstitutionContactPool.objects.get(institution=inst, contact__pk=contact_pk) |
458 |
contact = contactinst.contact |
459 |
form = ContactForm(instance=contact) |
460 |
except InstitutionContactPool.DoesNotExist:
|
461 |
form = ContactForm() |
462 |
|
463 |
return render_to_response('edumanage/contacts_edit.html', { 'form': form}, |
464 |
context_instance=RequestContext(request, base_response(request))) |
465 |
elif request.method == 'POST': |
466 |
request_data = request.POST.copy() |
467 |
try:
|
468 |
contactinst = InstitutionContactPool.objects.get(institution=inst, contact__pk=contact_pk) |
469 |
contact = contactinst.contact |
470 |
form = ContactForm(request_data, instance=contact) |
471 |
except InstitutionContactPool.DoesNotExist:
|
472 |
form = ContactForm(request_data) |
473 |
|
474 |
if form.is_valid():
|
475 |
contact = form.save() |
476 |
instContPool, created = InstitutionContactPool.objects.get_or_create(contact=contact, institution=inst) |
477 |
instContPool.save() |
478 |
return HttpResponseRedirect(reverse("contacts")) |
479 |
return render_to_response('edumanage/contacts_edit.html', { 'form': form}, |
480 |
context_instance=RequestContext(request, base_response(request))) |
481 |
|
482 |
|
483 |
@login_required
|
484 |
def del_contact(request): |
485 |
if request.method == 'GET': |
486 |
user = request.user |
487 |
req_data = request.GET.copy() |
488 |
contact_pk = req_data['contact_pk']
|
489 |
profile = user.get_profile() |
490 |
institution = profile.institution |
491 |
resp = {} |
492 |
try:
|
493 |
contactinst = InstitutionContactPool.objects.get(institution=institution, contact__pk=contact_pk) |
494 |
contact = contactinst.contact |
495 |
except InstitutionContactPool.DoesNotExist:
|
496 |
resp['error'] = "Could not get contact or you have no rights to delete" |
497 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
498 |
try:
|
499 |
for service in ServiceLoc.objects.filter(institutionid=institution): |
500 |
if (contact in service.contact.all() and len(service.contact.all()) == 1): |
501 |
resp['error'] = "Could not delete contact. It is the only contact in service <b>%s</b>.<br>Fix it and try again" %service.get_name(lang="en") |
502 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
503 |
if (contact in institution.institutiondetails.contact.all() and len(institution.institutiondetails.contact.all()) == 1): |
504 |
resp['error'] = "Could not delete contact. It is the only contact your institution.<br>Fix it and try again" |
505 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
506 |
contact.delete() |
507 |
except Exception: |
508 |
resp['error'] = "Could not delete contact" |
509 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
510 |
resp['success'] = "Contact successfully deleted" |
511 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
512 |
|
513 |
|
514 |
|
515 |
@login_required
|
516 |
def base_response(request): |
517 |
user = request.user |
518 |
inst = [] |
519 |
server = [] |
520 |
services = [] |
521 |
instrealms = [] |
522 |
instcontacts = [] |
523 |
contacts = [] |
524 |
try:
|
525 |
profile = user.get_profile() |
526 |
institution = profile.institution |
527 |
inst.append(institution) |
528 |
server = InstServer.objects.filter(instid=institution) |
529 |
services = ServiceLoc.objects.filter(institutionid=institution) |
530 |
instrealms = InstRealm.objects.filter(instid=institution) |
531 |
instcontacts.extend([x.contact.pk for x in InstitutionContactPool.objects.filter(institution=institution)]) |
532 |
contacts = Contact.objects.filter(pk__in=instcontacts) |
533 |
except UserProfile.DoesNotExist:
|
534 |
pass
|
535 |
|
536 |
return {
|
537 |
'inst_num': len(inst), |
538 |
'servers_num': len(server), |
539 |
'services_num': len(services), |
540 |
'realms_num': len(instrealms), |
541 |
'contacts_num': len(contacts), |
542 |
|
543 |
} |
544 |
|
545 |
@login_required
|
546 |
def del_server(request): |
547 |
if request.method == 'GET': |
548 |
user = request.user |
549 |
req_data = request.GET.copy() |
550 |
server_pk = req_data['server_pk']
|
551 |
profile = user.get_profile() |
552 |
institution = profile.institution |
553 |
resp = {} |
554 |
try:
|
555 |
server = InstServer.objects.get(instid=institution, pk=server_pk) |
556 |
except InstServer.DoesNotExist:
|
557 |
resp['error'] = "Could not get server or you have no rights to delete" |
558 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
559 |
try:
|
560 |
server.delete() |
561 |
except:
|
562 |
resp['error'] = "Could not delete server" |
563 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
564 |
resp['success'] = "Server successfully deleted" |
565 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
566 |
|
567 |
|
568 |
@login_required
|
569 |
def del_service(request): |
570 |
if request.method == 'GET': |
571 |
user = request.user |
572 |
req_data = request.GET.copy() |
573 |
service_pk = req_data['service_pk']
|
574 |
profile = user.get_profile() |
575 |
institution = profile.institution |
576 |
resp = {} |
577 |
try:
|
578 |
service = ServiceLoc.objects.get(institutionid=institution, pk=service_pk) |
579 |
except ServiceLoc.DoesNotExist:
|
580 |
resp['error'] = "Could not get service or you have no rights to delete" |
581 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
582 |
try:
|
583 |
service.delete() |
584 |
except:
|
585 |
resp['error'] = "Could not delete service" |
586 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
587 |
resp['success'] = "Service successfully deleted" |
588 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
589 |
|
590 |
|
591 |
|
592 |
def geolocate(request): |
593 |
return render_to_response('front/geolocate.html', |
594 |
context_instance=RequestContext(request)) |
595 |
|
596 |
|
597 |
def closest(request): |
598 |
if request.method == 'GET': |
599 |
locs = [] |
600 |
request_data = request.GET.copy() |
601 |
response_location = {} |
602 |
response_location["lat"] = request_data['lat'] |
603 |
response_location["lng"] = request_data['lng'] |
604 |
lat = float(request_data['lat']) |
605 |
lng = float(request_data['lng']) |
606 |
R = 6371
|
607 |
distances = {} |
608 |
closestMarker = {} |
609 |
closest = -1
|
610 |
doc = ET.parse(settings.KML_FILE) |
611 |
root = doc.getroot() |
612 |
r = root.getchildren()[0]
|
613 |
for (counter, i) in enumerate(r.getchildren()): |
614 |
if "id" in i.keys(): |
615 |
j = i.getchildren() |
616 |
pointname = j[0].text
|
617 |
point = j[2].getchildren()[0].text |
618 |
pointlng, pointlat, pointele = point.split(',')
|
619 |
dLat = rad(float(pointlat)-float(lat)) |
620 |
dLong = rad(float(pointlng)-float(lng)) |
621 |
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) |
622 |
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) |
623 |
d = R * c |
624 |
distances[counter] = d |
625 |
if (closest == -1 or d < distances[closest]): |
626 |
closest = counter |
627 |
closestMarker = {"name": pointname, "lat": pointlat, "lng": pointlng, "text": j[1].text} |
628 |
return HttpResponse(json.dumps(closestMarker), mimetype='application/json') |
629 |
|
630 |
|
631 |
def instxml(request): |
632 |
ET._namespace_map["http://www.w3.org/2001/XMLSchema-instance"] = 'xsi' |
633 |
root = ET.Element("institutions")
|
634 |
NS_XSI = "{http://www.w3.org/2001/XMLSchema-instance}"
|
635 |
root.set(NS_XSI + "noNamespaceSchemaLocation", "institutions.xsd") |
636 |
#root.attrib["xsi:noNamespaceSchemaLocation"] = "institution.xsd"
|
637 |
institutions = Institution.objects.all() |
638 |
for institution in institutions: |
639 |
try:
|
640 |
inst = institution.institutiondetails |
641 |
if not inst: |
642 |
pass
|
643 |
except InstitutionDetails.DoesNotExist:
|
644 |
pass
|
645 |
|
646 |
instElement = ET.SubElement(root, "institution")
|
647 |
|
648 |
instCountry = ET.SubElement(instElement, "country")
|
649 |
instCountry.text = ("%s" %inst.institution.realmid.country).upper()
|
650 |
|
651 |
instType = ET.SubElement(instElement, "type")
|
652 |
instType.text = "%s" %inst.ertype
|
653 |
|
654 |
for realm in institution.instrealm_set.all(): |
655 |
instRealm = ET.SubElement(instElement, "inst_realm")
|
656 |
instRealm.text = realm.realm |
657 |
|
658 |
for name in inst.institution.org_name.all(): |
659 |
instOrgName = ET.SubElement(instElement, "org_name")
|
660 |
instOrgName.attrib["lang"] = name.lang
|
661 |
instOrgName.text = u"%s" %name.name
|
662 |
|
663 |
instAddress = ET.SubElement(instElement, "address")
|
664 |
|
665 |
instAddrStreet = ET.SubElement(instAddress, "street")
|
666 |
instAddrStreet.text = inst.address_street |
667 |
|
668 |
instAddrCity = ET.SubElement(instAddress, "city")
|
669 |
instAddrCity.text = inst.address_city |
670 |
|
671 |
for contact in inst.contact.all(): |
672 |
instContact = ET.SubElement(instElement, "contact")
|
673 |
|
674 |
instContactName = ET.SubElement(instContact, "name")
|
675 |
instContactName.text = "%s %s" %(contact.firstname, contact.lastname)
|
676 |
|
677 |
instContactEmail = ET.SubElement(instContact, "email")
|
678 |
instContactEmail.text = contact.email |
679 |
|
680 |
instContactPhone = ET.SubElement(instContact, "phone")
|
681 |
instContactPhone.text = contact.phone |
682 |
|
683 |
for url in inst.url.all(): |
684 |
instUrl = ET.SubElement(instElement, "%s_URL"%(url.urltype))
|
685 |
instUrl.attrib["lang"] = url.lang
|
686 |
instUrl.text = url.url |
687 |
|
688 |
#Let's go to Institution Service Locations
|
689 |
|
690 |
for serviceloc in inst.institution.serviceloc_set.all(): |
691 |
instLocation = ET.SubElement(instElement, "location")
|
692 |
|
693 |
instLong = ET.SubElement(instLocation, "longitude")
|
694 |
instLong.text = "%s" %serviceloc.longitude
|
695 |
|
696 |
instLat = ET.SubElement(instLocation, "latitude")
|
697 |
instLat.text = "%s" %serviceloc.latitude
|
698 |
|
699 |
for instlocname in serviceloc.loc_name.all(): |
700 |
instLocName = ET.SubElement(instLocation, "loc_name")
|
701 |
instLocName.attrib["lang"] = instlocname.lang
|
702 |
instLocName.text = instlocname.name |
703 |
|
704 |
instLocAddress = ET.SubElement(instLocation, "address")
|
705 |
|
706 |
instLocAddrStreet = ET.SubElement(instLocAddress, "street")
|
707 |
instLocAddrStreet.text = serviceloc.address_street |
708 |
|
709 |
instLocAddrCity = ET.SubElement(instLocAddress, "city")
|
710 |
instLocAddrCity.text = serviceloc.address_city |
711 |
|
712 |
instLocSSID = ET.SubElement(instLocation, "SSID")
|
713 |
instLocSSID.text = serviceloc.SSID |
714 |
|
715 |
instLocEncLevel = ET.SubElement(instLocation, "enc_level")
|
716 |
instLocEncLevel.text = serviceloc.enc_level |
717 |
|
718 |
instLocPortRestrict = ET.SubElement(instLocation, "port_restrict")
|
719 |
instLocPortRestrict.text = ("%s" %serviceloc.port_restrict).lower()
|
720 |
|
721 |
instLocTransProxy = ET.SubElement(instLocation, "transp_proxy")
|
722 |
instLocTransProxy.text = ("%s" %serviceloc.transp_proxy).lower()
|
723 |
|
724 |
instLocIpv6 = ET.SubElement(instLocation, "IPv6")
|
725 |
instLocIpv6.text = ("%s" %serviceloc.IPv6).lower()
|
726 |
|
727 |
instLocNAT = ET.SubElement(instLocation, "NAT")
|
728 |
instLocNAT.text = ("%s" %serviceloc.NAT).lower()
|
729 |
|
730 |
instLocAP_no = ET.SubElement(instLocation, "AP_no")
|
731 |
instLocAP_no.text = "%s" %int(serviceloc.AP_no) |
732 |
|
733 |
instLocWired = ET.SubElement(instLocation, "wired")
|
734 |
instLocWired.text = ("%s" %serviceloc.wired).lower()
|
735 |
|
736 |
for url in serviceloc.url.all(): |
737 |
instLocUrl = ET.SubElement(instLocation, "%s_URL"%(url.urltype))
|
738 |
instLocUrl.attrib["lang"] = url.lang
|
739 |
instLocUrl.text = url.url |
740 |
|
741 |
instTs = ET.SubElement(instElement, "ts")
|
742 |
instTs.text = "%s" %inst.ts.isoformat()
|
743 |
|
744 |
return render_to_response("general/institution.xml", {"xml":to_xml(root)},context_instance=RequestContext(request,), mimetype="application/xml") |
745 |
|
746 |
|
747 |
def realmxml(request): |
748 |
realm = Realm.objects.all()[0]
|
749 |
ET._namespace_map["http://www.w3.org/2001/XMLSchema-instance"] = 'xsi' |
750 |
root = ET.Element("realms")
|
751 |
NS_XSI = "{http://www.w3.org/2001/XMLSchema-instance}"
|
752 |
root.set(NS_XSI + "noNamespaceSchemaLocation", "realm.xsd") |
753 |
#root.attrib["xsi:noNamespaceSchemaLocation"] = "institution.xsd"
|
754 |
realmElement = ET.SubElement(root, "realm")
|
755 |
|
756 |
realmCountry = ET.SubElement(realmElement, "country")
|
757 |
realmCountry.text = realm.country |
758 |
|
759 |
realmStype = ET.SubElement(realmElement, "stype")
|
760 |
realmStype.text = "%s" %realm.stype
|
761 |
|
762 |
for name in realm.org_name.all(): |
763 |
realmOrgName = ET.SubElement(realmElement, "org_name")
|
764 |
realmOrgName.attrib["lang"] = name.lang
|
765 |
realmOrgName.text = u"%s" %name.name
|
766 |
|
767 |
realmAddress = ET.SubElement(realmElement, "address")
|
768 |
|
769 |
realmAddrStreet = ET.SubElement(realmAddress, "street")
|
770 |
realmAddrStreet.text = realm.address_street |
771 |
|
772 |
realmAddrCity = ET.SubElement(realmAddress, "city")
|
773 |
realmAddrCity.text = realm.address_city |
774 |
|
775 |
for contact in realm.contact.all(): |
776 |
realmContact = ET.SubElement(realmElement, "contact")
|
777 |
|
778 |
realmContactName = ET.SubElement(realmContact, "name")
|
779 |
realmContactName.text = "%s %s" %(contact.firstname, contact.lastname)
|
780 |
|
781 |
realmContactEmail = ET.SubElement(realmContact, "email")
|
782 |
realmContactEmail.text = contact.email |
783 |
|
784 |
realmContactPhone = ET.SubElement(realmContact, "phone")
|
785 |
realmContactPhone.text = contact.phone |
786 |
|
787 |
for url in realm.url.all(): |
788 |
realmUrl = ET.SubElement(realmElement, "%s_URL"%(url.urltype))
|
789 |
realmUrl.attrib["lang"] = url.lang
|
790 |
realmUrl.text = url.url |
791 |
|
792 |
return render_to_response("general/realm.xml", {"xml":to_xml(root)},context_instance=RequestContext(request,), mimetype="application/xml") |
793 |
|
794 |
def realmdataxml(request): |
795 |
realm = Realm.objects.all()[0]
|
796 |
ET._namespace_map["http://www.w3.org/2001/XMLSchema-instance"] = 'xsi' |
797 |
root = ET.Element("realm-data")
|
798 |
NS_XSI = "{http://www.w3.org/2001/XMLSchema-instance}"
|
799 |
root.set(NS_XSI + "noNamespaceSchemaLocation", "realm-data.xsd") |
800 |
|
801 |
return render_to_response("general/realm_data.xml", {"xml":to_xml(root)},context_instance=RequestContext(request,), mimetype="application/xml") |
802 |
|
803 |
def to_xml(ele, encoding="UTF-8"): |
804 |
"Convert and return the XML for an *ele* (:class:`~xml.etree.ElementTree.Element`) with specified *encoding*."
|
805 |
xml = ET.tostring(ele, encoding) |
806 |
return xml if xml.startswith('<?xml') else '<?xml version="1.0" encoding="%s"?>%s' % (encoding, xml) |
807 |
|
808 |
|
809 |
def getInstContacts(inst): |
810 |
contacts = InstitutionContactPool.objects.filter(institution=inst) |
811 |
contact_pks = [] |
812 |
for contact in contacts: |
813 |
contact_pks.append(contact.contact.pk) |
814 |
return list(set(contact_pks)) |
815 |
|
816 |
def getInstServers(inst): |
817 |
servers = InstServer.objects.filter(instid=inst) |
818 |
server_pks = [] |
819 |
for server in servers: |
820 |
server_pks.append(server.pk) |
821 |
return list(set(server_pks)) |
822 |
|
823 |
|
824 |
def rad(x): |
825 |
return x*math.pi/180 |