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