root / edumanage / views.py @ c01b785c
History | View | Annotate | Download (35.6 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 |
UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, formset=UrlFormSetFactInst, can_delete=True) |
90 |
except InstitutionDetails.DoesNotExist:
|
91 |
form = InstDetailsForm() |
92 |
form.fields['institution'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=institution_pk), empty_label=None) |
93 |
UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, can_delete=True) |
94 |
urls_form = UrlFormSet(prefix='urlsform')
|
95 |
form.fields['contact'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.filter(pk__in=getInstContacts(inst)))
|
96 |
return render_to_response('edumanage/institution_edit.html', { 'institution': inst, 'form': form, 'urls_form':urls_form}, |
97 |
context_instance=RequestContext(request, base_response(request))) |
98 |
elif request.method == 'POST': |
99 |
request_data = request.POST.copy() |
100 |
UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, formset=UrlFormSetFactInst, can_delete=True) |
101 |
try:
|
102 |
inst_details = InstitutionDetails.objects.get(institution=inst) |
103 |
form = InstDetailsForm(request_data, instance=inst_details) |
104 |
urls_form = UrlFormSet(request_data, instance=inst_details, prefix='urlsform')
|
105 |
except InstitutionDetails.DoesNotExist:
|
106 |
form = InstDetailsForm(request_data) |
107 |
urls_form = UrlFormSet(request_data, prefix='urlsform')
|
108 |
UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, formset=UrlFormSetFactInst, can_delete=True) |
109 |
if form.is_valid() and urls_form.is_valid(): |
110 |
instdets = form.save() |
111 |
urls_form.instance = instdets |
112 |
urls_inst = urls_form.save() |
113 |
return HttpResponseRedirect(reverse("institutions")) |
114 |
else:
|
115 |
try:
|
116 |
profile = user.get_profile() |
117 |
inst = profile.institution |
118 |
except UserProfile.DoesNotExist:
|
119 |
inst = False
|
120 |
form.fields['institution'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=institution_pk), empty_label=None) |
121 |
form.fields['contact'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.filter(pk__in=getInstContacts(inst)))
|
122 |
return render_to_response('edumanage/institution_edit.html', { 'institution': inst, 'form': form, 'urls_form': urls_form}, |
123 |
context_instance=RequestContext(request, base_response(request))) |
124 |
|
125 |
|
126 |
@login_required
|
127 |
def services(request, service_pk): |
128 |
user = request.user |
129 |
dict = {} |
130 |
|
131 |
try:
|
132 |
profile = user.get_profile() |
133 |
inst = profile.institution |
134 |
except UserProfile.DoesNotExist:
|
135 |
inst = False
|
136 |
try:
|
137 |
services = ServiceLoc.objects.filter(institutionid = inst) |
138 |
except ServiceLoc.DoesNotExist:
|
139 |
services = False
|
140 |
|
141 |
if service_pk:
|
142 |
services = services.get(pk=service_pk) |
143 |
return render_to_response('edumanage/service_details.html', |
144 |
{ |
145 |
'institution': inst,
|
146 |
'service': services,
|
147 |
}, |
148 |
context_instance=RequestContext(request, base_response(request))) |
149 |
|
150 |
return render_to_response('edumanage/services.html', |
151 |
{ |
152 |
'institution': inst,
|
153 |
'services': services,
|
154 |
}, |
155 |
context_instance=RequestContext(request, base_response(request))) |
156 |
|
157 |
|
158 |
|
159 |
@login_required
|
160 |
def add_services(request, service_pk): |
161 |
user = request.user |
162 |
service = False
|
163 |
try:
|
164 |
profile = user.get_profile() |
165 |
inst = profile.institution |
166 |
except UserProfile.DoesNotExist:
|
167 |
inst = False
|
168 |
|
169 |
if request.method == "GET": |
170 |
|
171 |
# Determine add or edit
|
172 |
try:
|
173 |
service = ServiceLoc.objects.get(institutionid=inst, pk=service_pk) |
174 |
form = ServiceLocForm(instance=service) |
175 |
except ServiceLoc.DoesNotExist:
|
176 |
form = ServiceLocForm() |
177 |
form.fields['institutionid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None) |
178 |
UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, can_delete=True) |
179 |
NameFormSet = generic_inlineformset_factory(Name_i18n, extra=2, can_delete=True) |
180 |
urls_form = UrlFormSet(prefix='urlsform')
|
181 |
names_form = NameFormSet(prefix='namesform')
|
182 |
if (service):
|
183 |
NameFormSet = generic_inlineformset_factory(Name_i18n, extra=1, formset=NameFormSetFact, can_delete=True) |
184 |
names_form = NameFormSet(instance=service, prefix='namesform')
|
185 |
UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, formset=UrlFormSetFact, can_delete=True) |
186 |
urls_form = UrlFormSet(instance=service, prefix='urlsform')
|
187 |
form.fields['contact'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.filter(pk__in=getInstContacts(inst)))
|
188 |
return render_to_response('edumanage/services_edit.html', { 'form': form, 'services_form':names_form, 'urls_form': urls_form}, |
189 |
context_instance=RequestContext(request, base_response(request))) |
190 |
elif request.method == 'POST': |
191 |
request_data = request.POST.copy() |
192 |
NameFormSet = generic_inlineformset_factory(Name_i18n, extra=1, formset=NameFormSetFact, can_delete=True) |
193 |
UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, formset=UrlFormSetFact, can_delete=True) |
194 |
try:
|
195 |
service = ServiceLoc.objects.get(institutionid=inst, pk=service_pk) |
196 |
form = ServiceLocForm(request_data, instance=service) |
197 |
names_form = NameFormSet(request_data, instance=service, prefix='namesform')
|
198 |
urls_form = UrlFormSet(request_data, instance=service, prefix='urlsform')
|
199 |
except ServiceLoc.DoesNotExist:
|
200 |
form = ServiceLocForm(request_data) |
201 |
names_form = NameFormSet(request_data, prefix='namesform')
|
202 |
urls_form = UrlFormSet(request_data, prefix='urlsform')
|
203 |
|
204 |
if form.is_valid() and names_form.is_valid() and urls_form.is_valid(): |
205 |
serviceloc = form.save() |
206 |
service = serviceloc |
207 |
names_form.instance = service |
208 |
urls_form.instance = service |
209 |
names_inst = names_form.save() |
210 |
urls_inst = urls_form.save() |
211 |
# for nform in names_inst:
|
212 |
# nform.content_object = serviceloc
|
213 |
# nform.save()
|
214 |
# for uform in urls_inst:
|
215 |
# uform.content_object = serviceloc
|
216 |
# uform.save()
|
217 |
return HttpResponseRedirect(reverse("services")) |
218 |
else:
|
219 |
form.fields['institutionid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None) |
220 |
form.fields['contact'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.filter(pk__in=getInstContacts(inst)))
|
221 |
return render_to_response('edumanage/services_edit.html', { 'institution': inst, 'form': form, 'services_form':names_form, 'urls_form': urls_form}, |
222 |
context_instance=RequestContext(request, base_response(request))) |
223 |
|
224 |
|
225 |
|
226 |
@login_required
|
227 |
def get_service_points(request): |
228 |
if request.method == "GET": |
229 |
user = request.user |
230 |
try:
|
231 |
profile = user.get_profile() |
232 |
inst = profile.institution |
233 |
except UserProfile.DoesNotExist:
|
234 |
inst = False
|
235 |
servicelocs = ServiceLoc.objects.filter(institutionid=inst) |
236 |
|
237 |
locs = [] |
238 |
for sl in servicelocs: |
239 |
response_location = {} |
240 |
response_location['lat'] = u"%s"%sl.latitude |
241 |
response_location['lng'] = u"%s"%sl.longitude |
242 |
response_location['address'] = u"%s<br>%s"%(sl.address_street, sl.address_city) |
243 |
response_location['enc'] = u"%s"%(sl.enc_level) |
244 |
response_location['AP_no'] = u"%s"%(sl.AP_no) |
245 |
response_location['name'] = sl.loc_name.get(lang='en').name |
246 |
response_location['port_restrict'] = u"%s"%(sl.port_restrict) |
247 |
response_location['transp_proxy'] = u"%s"%(sl.transp_proxy) |
248 |
response_location['IPv6'] = u"%s"%(sl.IPv6) |
249 |
response_location['NAT'] = u"%s"%(sl.NAT) |
250 |
response_location['wired'] = u"%s"%(sl.wired) |
251 |
response_location['SSID'] = u"%s"%(sl.SSID) |
252 |
response_location['key'] = u"%s"%sl.pk |
253 |
locs.append(response_location) |
254 |
return HttpResponse(json.dumps(locs), mimetype='application/json') |
255 |
else:
|
256 |
return HttpResponseNotFound('<h1>Something went really wrong</h1>') |
257 |
|
258 |
|
259 |
def get_all_services(request): |
260 |
servicelocs = ServiceLoc.objects.all() |
261 |
locs = [] |
262 |
for sl in servicelocs: |
263 |
response_location = {} |
264 |
response_location['lat'] = u"%s"%sl.latitude |
265 |
response_location['lng'] = u"%s"%sl.longitude |
266 |
response_location['address'] = u"%s<br>%s"%(sl.address_street, sl.address_city) |
267 |
response_location['enc'] = u"%s"%(sl.enc_level) |
268 |
response_location['AP_no'] = u"%s"%(sl.AP_no) |
269 |
response_location['inst'] = sl.institutionid.org_name.get(lang='en').name |
270 |
response_location['name'] = sl.loc_name.get(lang='en').name |
271 |
response_location['port_restrict'] = u"%s"%(sl.port_restrict) |
272 |
response_location['transp_proxy'] = u"%s"%(sl.transp_proxy) |
273 |
response_location['IPv6'] = u"%s"%(sl.IPv6) |
274 |
response_location['NAT'] = u"%s"%(sl.NAT) |
275 |
response_location['wired'] = u"%s"%(sl.wired) |
276 |
response_location['SSID'] = u"%s"%(sl.SSID) |
277 |
response_location['key'] = u"%s"%sl.pk |
278 |
locs.append(response_location) |
279 |
return HttpResponse(json.dumps(locs), mimetype='application/json') |
280 |
|
281 |
|
282 |
|
283 |
def servers(request): |
284 |
user = request.user |
285 |
servers = False
|
286 |
try:
|
287 |
profile = user.get_profile() |
288 |
inst = profile.institution |
289 |
except UserProfile.DoesNotExist:
|
290 |
inst = False
|
291 |
if inst:
|
292 |
servers = InstServer.objects.filter(instid=inst) |
293 |
return render_to_response('edumanage/servers.html', { 'servers': servers}, |
294 |
context_instance=RequestContext(request, base_response(request))) |
295 |
|
296 |
|
297 |
|
298 |
@login_required
|
299 |
def adduser(request): |
300 |
user = request.user |
301 |
try:
|
302 |
profile = user.get_profile() |
303 |
inst = profile.institution |
304 |
except UserProfile.DoesNotExist:
|
305 |
inst = False
|
306 |
if request.method == "GET": |
307 |
form = ContactForm() |
308 |
return render_to_response('edumanage/add_user.html', { 'form' : form }, |
309 |
context_instance=RequestContext(request, base_response(request))) |
310 |
elif request.method == 'POST': |
311 |
request_data = request.POST.copy() |
312 |
form = ContactForm(request_data) |
313 |
if form.is_valid():
|
314 |
contact = form.save() |
315 |
instContPool = InstitutionContactPool(contact=contact, institution=inst) |
316 |
instContPool.save() |
317 |
response_data = {} |
318 |
response_data['value'] = "%s" %contact.pk |
319 |
response_data['text'] = "%s" %contact |
320 |
return HttpResponse(json.dumps(response_data), mimetype='application/json') |
321 |
else:
|
322 |
return render_to_response('edumanage/add_user.html', {'form': form,}, |
323 |
context_instance=RequestContext(request, base_response(request))) |
324 |
|
325 |
@login_required
|
326 |
def add_server(request, server_pk): |
327 |
user = request.user |
328 |
server = False
|
329 |
try:
|
330 |
profile = user.get_profile() |
331 |
inst = profile.institution |
332 |
except UserProfile.DoesNotExist:
|
333 |
inst = False
|
334 |
|
335 |
if request.method == "GET": |
336 |
|
337 |
# Determine add or edit
|
338 |
try:
|
339 |
server = InstServer.objects.get(instid=inst, pk=server_pk) |
340 |
form = InstServerForm(instance=server) |
341 |
except InstServer.DoesNotExist:
|
342 |
form = InstServerForm() |
343 |
form.fields['instid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None) |
344 |
|
345 |
return render_to_response('edumanage/servers_edit.html', { 'form': form}, |
346 |
context_instance=RequestContext(request, base_response(request))) |
347 |
elif request.method == 'POST': |
348 |
request_data = request.POST.copy() |
349 |
try:
|
350 |
server = InstServer.objects.get(instid=inst, pk=server_pk) |
351 |
form = InstServerForm(request_data, instance=server) |
352 |
except InstServer.DoesNotExist:
|
353 |
form = InstServerForm(request_data) |
354 |
|
355 |
if form.is_valid():
|
356 |
instserverf = form.save() |
357 |
return HttpResponseRedirect(reverse("servers")) |
358 |
else:
|
359 |
form.fields['instid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None) |
360 |
return render_to_response('edumanage/servers_edit.html', { 'institution': inst, 'form': form}, |
361 |
context_instance=RequestContext(request, base_response(request))) |
362 |
|
363 |
@login_required
|
364 |
def realms(request): |
365 |
user = request.user |
366 |
servers = False
|
367 |
try:
|
368 |
profile = user.get_profile() |
369 |
inst = profile.institution |
370 |
except UserProfile.DoesNotExist:
|
371 |
inst = False
|
372 |
if inst:
|
373 |
realms = InstRealm.objects.filter(instid=inst) |
374 |
return render_to_response('edumanage/realms.html', { 'realms': realms}, |
375 |
context_instance=RequestContext(request, base_response(request))) |
376 |
|
377 |
@login_required
|
378 |
def add_realm(request, realm_pk): |
379 |
user = request.user |
380 |
server = False
|
381 |
try:
|
382 |
profile = user.get_profile() |
383 |
inst = profile.institution |
384 |
except UserProfile.DoesNotExist:
|
385 |
inst = False
|
386 |
|
387 |
if request.method == "GET": |
388 |
|
389 |
# Determine add or edit
|
390 |
try:
|
391 |
realm = InstRealm.objects.get(instid=inst, pk=realm_pk) |
392 |
form = InstRealmForm(instance=realm) |
393 |
except InstRealm.DoesNotExist:
|
394 |
form = InstRealmForm() |
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', { 'form': form}, |
398 |
context_instance=RequestContext(request, base_response(request))) |
399 |
elif request.method == 'POST': |
400 |
request_data = request.POST.copy() |
401 |
try:
|
402 |
realm = InstRealm.objects.get(instid=inst, pk=realm_pk) |
403 |
form = InstRealmForm(request_data, instance=realm) |
404 |
except InstRealm.DoesNotExist:
|
405 |
form = InstRealmForm(request_data) |
406 |
|
407 |
if form.is_valid():
|
408 |
instserverf = form.save() |
409 |
return HttpResponseRedirect(reverse("realms")) |
410 |
else:
|
411 |
form.fields['instid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None) |
412 |
form.fields['proxyto'] = forms.ModelMultipleChoiceField(queryset=InstServer.objects.filter(pk__in=getInstServers(inst)))
|
413 |
return render_to_response('edumanage/realms_edit.html', { 'institution': inst, 'form': form}, |
414 |
context_instance=RequestContext(request, base_response(request))) |
415 |
|
416 |
|
417 |
@login_required
|
418 |
def del_realm(request): |
419 |
if request.method == 'GET': |
420 |
user = request.user |
421 |
req_data = request.GET.copy() |
422 |
realm_pk = req_data['realm_pk']
|
423 |
profile = user.get_profile() |
424 |
institution = profile.institution |
425 |
resp = {} |
426 |
try:
|
427 |
realm = InstRealm.objects.get(instid=institution, pk=realm_pk) |
428 |
except InstRealm.DoesNotExist:
|
429 |
resp['error'] = "Could not get realm or you have no rights to delete" |
430 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
431 |
try:
|
432 |
realm.delete() |
433 |
except:
|
434 |
resp['error'] = "Could not delete realm" |
435 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
436 |
resp['success'] = "Realm successfully deleted" |
437 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
438 |
|
439 |
|
440 |
|
441 |
@login_required
|
442 |
def contacts(request): |
443 |
user = request.user |
444 |
servers = False
|
445 |
instcontacts = [] |
446 |
try:
|
447 |
profile = user.get_profile() |
448 |
inst = profile.institution |
449 |
except UserProfile.DoesNotExist:
|
450 |
inst = False
|
451 |
if inst:
|
452 |
instcontacts.extend([x.contact.pk for x in InstitutionContactPool.objects.filter(institution=inst)]) |
453 |
print instcontacts
|
454 |
contacts = Contact.objects.filter(pk__in=instcontacts) |
455 |
print contacts
|
456 |
return render_to_response('edumanage/contacts.html', { 'contacts': contacts}, |
457 |
context_instance=RequestContext(request, base_response(request))) |
458 |
|
459 |
@login_required
|
460 |
def add_contact(request, contact_pk): |
461 |
user = request.user |
462 |
server = False
|
463 |
try:
|
464 |
profile = user.get_profile() |
465 |
inst = profile.institution |
466 |
except UserProfile.DoesNotExist:
|
467 |
inst = False
|
468 |
|
469 |
if request.method == "GET": |
470 |
|
471 |
# Determine add or edit
|
472 |
try:
|
473 |
contactinst = InstitutionContactPool.objects.get(institution=inst, contact__pk=contact_pk) |
474 |
contact = contactinst.contact |
475 |
form = ContactForm(instance=contact) |
476 |
except InstitutionContactPool.DoesNotExist:
|
477 |
form = ContactForm() |
478 |
|
479 |
return render_to_response('edumanage/contacts_edit.html', { 'form': form}, |
480 |
context_instance=RequestContext(request, base_response(request))) |
481 |
elif request.method == 'POST': |
482 |
request_data = request.POST.copy() |
483 |
try:
|
484 |
contactinst = InstitutionContactPool.objects.get(institution=inst, contact__pk=contact_pk) |
485 |
contact = contactinst.contact |
486 |
form = ContactForm(request_data, instance=contact) |
487 |
except InstitutionContactPool.DoesNotExist:
|
488 |
form = ContactForm(request_data) |
489 |
|
490 |
if form.is_valid():
|
491 |
contact = form.save() |
492 |
instContPool, created = InstitutionContactPool.objects.get_or_create(contact=contact, institution=inst) |
493 |
instContPool.save() |
494 |
return HttpResponseRedirect(reverse("contacts")) |
495 |
return render_to_response('edumanage/contacts_edit.html', { 'form': form}, |
496 |
context_instance=RequestContext(request, base_response(request))) |
497 |
|
498 |
|
499 |
@login_required
|
500 |
def del_contact(request): |
501 |
if request.method == 'GET': |
502 |
user = request.user |
503 |
req_data = request.GET.copy() |
504 |
contact_pk = req_data['contact_pk']
|
505 |
profile = user.get_profile() |
506 |
institution = profile.institution |
507 |
resp = {} |
508 |
try:
|
509 |
contactinst = InstitutionContactPool.objects.get(institution=institution, contact__pk=contact_pk) |
510 |
contact = contactinst.contact |
511 |
except InstitutionContactPool.DoesNotExist:
|
512 |
resp['error'] = "Could not get contact or you have no rights to delete" |
513 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
514 |
try:
|
515 |
for service in ServiceLoc.objects.filter(institutionid=institution): |
516 |
if (contact in service.contact.all() and len(service.contact.all()) == 1): |
517 |
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") |
518 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
519 |
if (contact in institution.institutiondetails.contact.all() and len(institution.institutiondetails.contact.all()) == 1): |
520 |
resp['error'] = "Could not delete contact. It is the only contact your institution.<br>Fix it and try again" |
521 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
522 |
contact.delete() |
523 |
except Exception: |
524 |
resp['error'] = "Could not delete contact" |
525 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
526 |
resp['success'] = "Contact successfully deleted" |
527 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
528 |
|
529 |
|
530 |
|
531 |
@login_required
|
532 |
def base_response(request): |
533 |
user = request.user |
534 |
inst = [] |
535 |
server = [] |
536 |
services = [] |
537 |
instrealms = [] |
538 |
instcontacts = [] |
539 |
contacts = [] |
540 |
try:
|
541 |
profile = user.get_profile() |
542 |
institution = profile.institution |
543 |
inst.append(institution) |
544 |
server = InstServer.objects.filter(instid=institution) |
545 |
services = ServiceLoc.objects.filter(institutionid=institution) |
546 |
instrealms = InstRealm.objects.filter(instid=institution) |
547 |
instcontacts.extend([x.contact.pk for x in InstitutionContactPool.objects.filter(institution=institution)]) |
548 |
contacts = Contact.objects.filter(pk__in=instcontacts) |
549 |
except UserProfile.DoesNotExist:
|
550 |
pass
|
551 |
|
552 |
return {
|
553 |
'inst_num': len(inst), |
554 |
'servers_num': len(server), |
555 |
'services_num': len(services), |
556 |
'realms_num': len(instrealms), |
557 |
'contacts_num': len(contacts), |
558 |
|
559 |
} |
560 |
|
561 |
@login_required
|
562 |
def del_server(request): |
563 |
if request.method == 'GET': |
564 |
user = request.user |
565 |
req_data = request.GET.copy() |
566 |
server_pk = req_data['server_pk']
|
567 |
profile = user.get_profile() |
568 |
institution = profile.institution |
569 |
resp = {} |
570 |
try:
|
571 |
server = InstServer.objects.get(instid=institution, pk=server_pk) |
572 |
except InstServer.DoesNotExist:
|
573 |
resp['error'] = "Could not get server or you have no rights to delete" |
574 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
575 |
try:
|
576 |
server.delete() |
577 |
except:
|
578 |
resp['error'] = "Could not delete server" |
579 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
580 |
resp['success'] = "Server successfully deleted" |
581 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
582 |
|
583 |
|
584 |
@login_required
|
585 |
def del_service(request): |
586 |
if request.method == 'GET': |
587 |
user = request.user |
588 |
req_data = request.GET.copy() |
589 |
service_pk = req_data['service_pk']
|
590 |
profile = user.get_profile() |
591 |
institution = profile.institution |
592 |
resp = {} |
593 |
try:
|
594 |
service = ServiceLoc.objects.get(institutionid=institution, pk=service_pk) |
595 |
except ServiceLoc.DoesNotExist:
|
596 |
resp['error'] = "Could not get service or you have no rights to delete" |
597 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
598 |
try:
|
599 |
service.delete() |
600 |
except:
|
601 |
resp['error'] = "Could not delete service" |
602 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
603 |
resp['success'] = "Service successfully deleted" |
604 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
605 |
|
606 |
|
607 |
|
608 |
def geolocate(request): |
609 |
return render_to_response('front/geolocate.html', |
610 |
context_instance=RequestContext(request)) |
611 |
|
612 |
|
613 |
def closest(request): |
614 |
if request.method == 'GET': |
615 |
locs = [] |
616 |
request_data = request.GET.copy() |
617 |
response_location = {} |
618 |
response_location["lat"] = request_data['lat'] |
619 |
response_location["lng"] = request_data['lng'] |
620 |
lat = float(request_data['lat']) |
621 |
lng = float(request_data['lng']) |
622 |
R = 6371
|
623 |
distances = {} |
624 |
closestMarker = {} |
625 |
closest = -1
|
626 |
doc = ET.parse(settings.KML_FILE) |
627 |
root = doc.getroot() |
628 |
r = root.getchildren()[0]
|
629 |
for (counter, i) in enumerate(r.getchildren()): |
630 |
if "id" in i.keys(): |
631 |
j = i.getchildren() |
632 |
pointname = j[0].text
|
633 |
point = j[2].getchildren()[0].text |
634 |
pointlng, pointlat, pointele = point.split(',')
|
635 |
dLat = rad(float(pointlat)-float(lat)) |
636 |
dLong = rad(float(pointlng)-float(lng)) |
637 |
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) |
638 |
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) |
639 |
d = R * c |
640 |
distances[counter] = d |
641 |
if (closest == -1 or d < distances[closest]): |
642 |
closest = counter |
643 |
closestMarker = {"name": pointname, "lat": pointlat, "lng": pointlng, "text": j[1].text} |
644 |
return HttpResponse(json.dumps(closestMarker), mimetype='application/json') |
645 |
|
646 |
|
647 |
def instxml(request): |
648 |
ET._namespace_map["http://www.w3.org/2001/XMLSchema-instance"] = 'xsi' |
649 |
root = ET.Element("institutions")
|
650 |
NS_XSI = "{http://www.w3.org/2001/XMLSchema-instance}"
|
651 |
root.set(NS_XSI + "noNamespaceSchemaLocation", "institutions.xsd") |
652 |
#root.attrib["xsi:noNamespaceSchemaLocation"] = "institution.xsd"
|
653 |
institutions = Institution.objects.all() |
654 |
for institution in institutions: |
655 |
try:
|
656 |
inst = institution.institutiondetails |
657 |
if not inst: |
658 |
pass
|
659 |
except InstitutionDetails.DoesNotExist:
|
660 |
pass
|
661 |
|
662 |
instElement = ET.SubElement(root, "institution")
|
663 |
|
664 |
instCountry = ET.SubElement(instElement, "country")
|
665 |
instCountry.text = ("%s" %inst.institution.realmid.country).upper()
|
666 |
|
667 |
instType = ET.SubElement(instElement, "type")
|
668 |
instType.text = "%s" %inst.ertype
|
669 |
|
670 |
for realm in institution.instrealm_set.all(): |
671 |
instRealm = ET.SubElement(instElement, "inst_realm")
|
672 |
instRealm.text = realm.realm |
673 |
|
674 |
for name in inst.institution.org_name.all(): |
675 |
instOrgName = ET.SubElement(instElement, "org_name")
|
676 |
instOrgName.attrib["lang"] = name.lang
|
677 |
instOrgName.text = u"%s" %name.name
|
678 |
|
679 |
instAddress = ET.SubElement(instElement, "address")
|
680 |
|
681 |
instAddrStreet = ET.SubElement(instAddress, "street")
|
682 |
instAddrStreet.text = inst.address_street |
683 |
|
684 |
instAddrCity = ET.SubElement(instAddress, "city")
|
685 |
instAddrCity.text = inst.address_city |
686 |
|
687 |
for contact in inst.contact.all(): |
688 |
instContact = ET.SubElement(instElement, "contact")
|
689 |
|
690 |
instContactName = ET.SubElement(instContact, "name")
|
691 |
instContactName.text = "%s %s" %(contact.firstname, contact.lastname)
|
692 |
|
693 |
instContactEmail = ET.SubElement(instContact, "email")
|
694 |
instContactEmail.text = contact.email |
695 |
|
696 |
instContactPhone = ET.SubElement(instContact, "phone")
|
697 |
instContactPhone.text = contact.phone |
698 |
|
699 |
for url in inst.url.all(): |
700 |
instUrl = ET.SubElement(instElement, "%s_URL"%(url.urltype))
|
701 |
instUrl.attrib["lang"] = url.lang
|
702 |
instUrl.text = url.url |
703 |
|
704 |
#Let's go to Institution Service Locations
|
705 |
|
706 |
for serviceloc in inst.institution.serviceloc_set.all(): |
707 |
instLocation = ET.SubElement(instElement, "location")
|
708 |
|
709 |
instLong = ET.SubElement(instLocation, "longitude")
|
710 |
instLong.text = "%s" %serviceloc.longitude
|
711 |
|
712 |
instLat = ET.SubElement(instLocation, "latitude")
|
713 |
instLat.text = "%s" %serviceloc.latitude
|
714 |
|
715 |
for instlocname in serviceloc.loc_name.all(): |
716 |
instLocName = ET.SubElement(instLocation, "loc_name")
|
717 |
instLocName.attrib["lang"] = instlocname.lang
|
718 |
instLocName.text = instlocname.name |
719 |
|
720 |
instLocAddress = ET.SubElement(instLocation, "address")
|
721 |
|
722 |
instLocAddrStreet = ET.SubElement(instLocAddress, "street")
|
723 |
instLocAddrStreet.text = serviceloc.address_street |
724 |
|
725 |
instLocAddrCity = ET.SubElement(instLocAddress, "city")
|
726 |
instLocAddrCity.text = serviceloc.address_city |
727 |
|
728 |
instLocSSID = ET.SubElement(instLocation, "SSID")
|
729 |
instLocSSID.text = serviceloc.SSID |
730 |
|
731 |
instLocEncLevel = ET.SubElement(instLocation, "enc_level")
|
732 |
instLocEncLevel.text = serviceloc.enc_level |
733 |
|
734 |
instLocPortRestrict = ET.SubElement(instLocation, "port_restrict")
|
735 |
instLocPortRestrict.text = ("%s" %serviceloc.port_restrict).lower()
|
736 |
|
737 |
instLocTransProxy = ET.SubElement(instLocation, "transp_proxy")
|
738 |
instLocTransProxy.text = ("%s" %serviceloc.transp_proxy).lower()
|
739 |
|
740 |
instLocIpv6 = ET.SubElement(instLocation, "IPv6")
|
741 |
instLocIpv6.text = ("%s" %serviceloc.IPv6).lower()
|
742 |
|
743 |
instLocNAT = ET.SubElement(instLocation, "NAT")
|
744 |
instLocNAT.text = ("%s" %serviceloc.NAT).lower()
|
745 |
|
746 |
instLocAP_no = ET.SubElement(instLocation, "AP_no")
|
747 |
instLocAP_no.text = "%s" %int(serviceloc.AP_no) |
748 |
|
749 |
instLocWired = ET.SubElement(instLocation, "wired")
|
750 |
instLocWired.text = ("%s" %serviceloc.wired).lower()
|
751 |
|
752 |
for url in serviceloc.url.all(): |
753 |
instLocUrl = ET.SubElement(instLocation, "%s_URL"%(url.urltype))
|
754 |
instLocUrl.attrib["lang"] = url.lang
|
755 |
instLocUrl.text = url.url |
756 |
|
757 |
instTs = ET.SubElement(instElement, "ts")
|
758 |
instTs.text = "%s" %inst.ts.isoformat()
|
759 |
|
760 |
return render_to_response("general/institution.xml", {"xml":to_xml(root)},context_instance=RequestContext(request,), mimetype="application/xml") |
761 |
|
762 |
|
763 |
def realmxml(request): |
764 |
realm = Realm.objects.all()[0]
|
765 |
ET._namespace_map["http://www.w3.org/2001/XMLSchema-instance"] = 'xsi' |
766 |
root = ET.Element("realms")
|
767 |
NS_XSI = "{http://www.w3.org/2001/XMLSchema-instance}"
|
768 |
root.set(NS_XSI + "noNamespaceSchemaLocation", "realm.xsd") |
769 |
#root.attrib["xsi:noNamespaceSchemaLocation"] = "institution.xsd"
|
770 |
realmElement = ET.SubElement(root, "realm")
|
771 |
|
772 |
realmCountry = ET.SubElement(realmElement, "country")
|
773 |
realmCountry.text = realm.country |
774 |
|
775 |
realmStype = ET.SubElement(realmElement, "stype")
|
776 |
realmStype.text = "%s" %realm.stype
|
777 |
|
778 |
for name in realm.org_name.all(): |
779 |
realmOrgName = ET.SubElement(realmElement, "org_name")
|
780 |
realmOrgName.attrib["lang"] = name.lang
|
781 |
realmOrgName.text = u"%s" %name.name
|
782 |
|
783 |
realmAddress = ET.SubElement(realmElement, "address")
|
784 |
|
785 |
realmAddrStreet = ET.SubElement(realmAddress, "street")
|
786 |
realmAddrStreet.text = realm.address_street |
787 |
|
788 |
realmAddrCity = ET.SubElement(realmAddress, "city")
|
789 |
realmAddrCity.text = realm.address_city |
790 |
|
791 |
for contact in realm.contact.all(): |
792 |
realmContact = ET.SubElement(realmElement, "contact")
|
793 |
|
794 |
realmContactName = ET.SubElement(realmContact, "name")
|
795 |
realmContactName.text = "%s %s" %(contact.firstname, contact.lastname)
|
796 |
|
797 |
realmContactEmail = ET.SubElement(realmContact, "email")
|
798 |
realmContactEmail.text = contact.email |
799 |
|
800 |
realmContactPhone = ET.SubElement(realmContact, "phone")
|
801 |
realmContactPhone.text = contact.phone |
802 |
|
803 |
for url in realm.url.all(): |
804 |
realmUrl = ET.SubElement(realmElement, "%s_URL"%(url.urltype))
|
805 |
realmUrl.attrib["lang"] = url.lang
|
806 |
realmUrl.text = url.url |
807 |
|
808 |
return render_to_response("general/realm.xml", {"xml":to_xml(root)},context_instance=RequestContext(request,), mimetype="application/xml") |
809 |
|
810 |
def realmdataxml(request): |
811 |
realm = Realm.objects.all()[0]
|
812 |
ET._namespace_map["http://www.w3.org/2001/XMLSchema-instance"] = 'xsi' |
813 |
root = ET.Element("realm-data")
|
814 |
NS_XSI = "{http://www.w3.org/2001/XMLSchema-instance}"
|
815 |
root.set(NS_XSI + "noNamespaceSchemaLocation", "realm-data.xsd") |
816 |
|
817 |
return render_to_response("general/realm_data.xml", {"xml":to_xml(root)},context_instance=RequestContext(request,), mimetype="application/xml") |
818 |
|
819 |
def to_xml(ele, encoding="UTF-8"): |
820 |
"Convert and return the XML for an *ele* (:class:`~xml.etree.ElementTree.Element`) with specified *encoding*."
|
821 |
xml = ET.tostring(ele, encoding) |
822 |
return xml if xml.startswith('<?xml') else '<?xml version="1.0" encoding="%s"?>%s' % (encoding, xml) |
823 |
|
824 |
|
825 |
def getInstContacts(inst): |
826 |
contacts = InstitutionContactPool.objects.filter(institution=inst) |
827 |
contact_pks = [] |
828 |
for contact in contacts: |
829 |
contact_pks.append(contact.contact.pk) |
830 |
return list(set(contact_pks)) |
831 |
|
832 |
def getInstServers(inst): |
833 |
servers = InstServer.objects.filter(instid=inst) |
834 |
server_pks = [] |
835 |
for server in servers: |
836 |
server_pks.append(server.pk) |
837 |
return list(set(server_pks)) |
838 |
|
839 |
|
840 |
def rad(x): |
841 |
return x*math.pi/180 |