root / edumanage / views.py @ 62d0c01e
History | View | Annotate | Download (46.9 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 |
from django.db.models import Max |
24 |
|
25 |
from django.views.decorators.cache import never_cache |
26 |
from django.utils.translation import ugettext as _ |
27 |
from django.contrib.auth import authenticate, login |
28 |
|
29 |
|
30 |
def index(request): |
31 |
return render_to_response('front/index.html', context_instance=RequestContext(request)) |
32 |
|
33 |
@login_required
|
34 |
def manage(request): |
35 |
services_list = [] |
36 |
servers_list = [] |
37 |
user = request.user |
38 |
try:
|
39 |
profile = user.get_profile() |
40 |
inst = profile.institution |
41 |
except UserProfile.DoesNotExist:
|
42 |
return render_to_response('edumanage/welcome.html', |
43 |
context_instance=RequestContext(request, base_response(request))) |
44 |
|
45 |
services = ServiceLoc.objects.filter(institutionid=inst) |
46 |
services_list.extend([s for s in services]) |
47 |
servers = InstServer.objects.filter(instid=inst) |
48 |
servers_list.extend([s for s in servers]) |
49 |
return render_to_response('edumanage/welcome.html', |
50 |
{ |
51 |
'institution': inst,
|
52 |
'services': services_list,
|
53 |
'servers': servers_list
|
54 |
}, |
55 |
context_instance=RequestContext(request, base_response(request))) |
56 |
|
57 |
@login_required
|
58 |
def institutions(request): |
59 |
user = request.user |
60 |
dict = {} |
61 |
try:
|
62 |
profile = user.get_profile() |
63 |
inst = profile.institution |
64 |
inst.__unicode__ = inst.get_name(request.LANGUAGE_CODE) |
65 |
except UserProfile.DoesNotExist:
|
66 |
return HttpResponseRedirect(reverse("manage")) |
67 |
dict['institution'] = inst.pk |
68 |
form = InstDetailsForm(initial=dict)
|
69 |
form.fields['institution'].widget.attrs['readonly'] = True |
70 |
return render_to_response('edumanage/institution.html', |
71 |
{ |
72 |
'institution': inst,
|
73 |
'form': form,
|
74 |
}, |
75 |
context_instance=RequestContext(request, base_response(request))) |
76 |
|
77 |
|
78 |
|
79 |
@login_required
|
80 |
def add_institution_details(request, institution_pk): |
81 |
user = request.user |
82 |
try:
|
83 |
profile = user.get_profile() |
84 |
inst = profile.institution |
85 |
except UserProfile.DoesNotExist:
|
86 |
return HttpResponseRedirect(reverse("manage")) |
87 |
|
88 |
if request.method == "GET": |
89 |
# Determine add or edit
|
90 |
request_data = request.POST.copy() |
91 |
try:
|
92 |
inst_details = InstitutionDetails.objects.get(institution=inst) |
93 |
form = InstDetailsForm(instance=inst_details) |
94 |
UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, formset=UrlFormSetFactInst, can_delete=True) |
95 |
urls_form = UrlFormSet(prefix='urlsform', instance = inst_details)
|
96 |
except InstitutionDetails.DoesNotExist:
|
97 |
form = InstDetailsForm() |
98 |
form.fields['institution'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=institution_pk), empty_label=None) |
99 |
UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, can_delete=True) |
100 |
urls_form = UrlFormSet(prefix='urlsform')
|
101 |
|
102 |
form.fields['contact'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.filter(pk__in=getInstContacts(inst)))
|
103 |
return render_to_response('edumanage/institution_edit.html', { 'institution': inst, 'form': form, 'urls_form':urls_form}, |
104 |
context_instance=RequestContext(request, base_response(request))) |
105 |
elif request.method == 'POST': |
106 |
request_data = request.POST.copy() |
107 |
UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, formset=UrlFormSetFactInst, can_delete=True) |
108 |
try:
|
109 |
inst_details = InstitutionDetails.objects.get(institution=inst) |
110 |
form = InstDetailsForm(request_data, instance=inst_details) |
111 |
urls_form = UrlFormSet(request_data, instance=inst_details, prefix='urlsform')
|
112 |
except InstitutionDetails.DoesNotExist:
|
113 |
form = InstDetailsForm(request_data) |
114 |
urls_form = UrlFormSet(request_data, prefix='urlsform')
|
115 |
UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, formset=UrlFormSetFactInst, can_delete=True) |
116 |
if form.is_valid() and urls_form.is_valid(): |
117 |
instdets = form.save() |
118 |
urls_form.instance = instdets |
119 |
urls_inst = urls_form.save() |
120 |
return HttpResponseRedirect(reverse("institutions")) |
121 |
else:
|
122 |
try:
|
123 |
profile = user.get_profile() |
124 |
inst = profile.institution |
125 |
except UserProfile.DoesNotExist:
|
126 |
inst = False
|
127 |
form.fields['institution'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=institution_pk), empty_label=None) |
128 |
form.fields['contact'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.filter(pk__in=getInstContacts(inst)))
|
129 |
return render_to_response('edumanage/institution_edit.html', { 'institution': inst, 'form': form, 'urls_form': urls_form}, |
130 |
context_instance=RequestContext(request, base_response(request))) |
131 |
|
132 |
|
133 |
@login_required
|
134 |
def services(request, service_pk): |
135 |
user = request.user |
136 |
dict = {} |
137 |
try:
|
138 |
profile = user.get_profile() |
139 |
inst = profile.institution |
140 |
except UserProfile.DoesNotExist:
|
141 |
return HttpResponseRedirect(reverse("manage")) |
142 |
if inst.ertype not in [2,3]: |
143 |
messages.add_message(request, messages.ERROR, 'Cannot add/edit Service. Your institution should be either SP or IdP/SP')
|
144 |
return render_to_response('edumanage/services.html', { 'institution': inst }, |
145 |
context_instance=RequestContext(request, base_response(request))) |
146 |
try:
|
147 |
services = ServiceLoc.objects.filter(institutionid = inst) |
148 |
except ServiceLoc.DoesNotExist:
|
149 |
services = False
|
150 |
|
151 |
if service_pk:
|
152 |
services = services.get(pk=service_pk) |
153 |
return render_to_response('edumanage/service_details.html', |
154 |
{ |
155 |
'institution': inst,
|
156 |
'service': services,
|
157 |
}, |
158 |
context_instance=RequestContext(request, base_response(request))) |
159 |
|
160 |
return render_to_response('edumanage/services.html', |
161 |
{ |
162 |
'institution': inst,
|
163 |
'services': services,
|
164 |
}, |
165 |
context_instance=RequestContext(request, base_response(request))) |
166 |
|
167 |
|
168 |
|
169 |
@login_required
|
170 |
def add_services(request, service_pk): |
171 |
user = request.user |
172 |
service = False
|
173 |
edit = False
|
174 |
try:
|
175 |
profile = user.get_profile() |
176 |
inst = profile.institution |
177 |
except UserProfile.DoesNotExist:
|
178 |
return HttpResponseRedirect(reverse("manage")) |
179 |
if inst.ertype not in [2,3]: |
180 |
messages.add_message(request, messages.ERROR, 'Cannot add/edit Service. Your institution should be either SP or IdP/SP')
|
181 |
return render_to_response('edumanage/services_edit.html', { 'edit': edit }, |
182 |
context_instance=RequestContext(request, base_response(request))) |
183 |
if request.method == "GET": |
184 |
|
185 |
# Determine add or edit
|
186 |
try:
|
187 |
service = ServiceLoc.objects.get(institutionid=inst, pk=service_pk) |
188 |
form = ServiceLocForm(instance=service) |
189 |
|
190 |
except ServiceLoc.DoesNotExist:
|
191 |
form = ServiceLocForm() |
192 |
form.fields['institutionid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None) |
193 |
UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, can_delete=True) |
194 |
NameFormSet = generic_inlineformset_factory(Name_i18n, extra=2, can_delete=True) |
195 |
urls_form = UrlFormSet(prefix='urlsform')
|
196 |
names_form = NameFormSet(prefix='namesform')
|
197 |
if (service):
|
198 |
NameFormSet = generic_inlineformset_factory(Name_i18n, extra=1, formset=NameFormSetFact, can_delete=True) |
199 |
names_form = NameFormSet(instance=service, prefix='namesform')
|
200 |
UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, formset=UrlFormSetFact, can_delete=True) |
201 |
urls_form = UrlFormSet(instance=service, prefix='urlsform')
|
202 |
form.fields['contact'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.filter(pk__in=getInstContacts(inst)))
|
203 |
if service:
|
204 |
edit = True
|
205 |
for url_form in urls_form.forms: |
206 |
url_form.fields['urltype'] = forms.ChoiceField(choices=(('', '----------'),('info', 'Info'),)) |
207 |
return render_to_response('edumanage/services_edit.html', { 'form': form, 'services_form':names_form, 'urls_form': urls_form, "edit": edit}, |
208 |
context_instance=RequestContext(request, base_response(request))) |
209 |
elif request.method == 'POST': |
210 |
request_data = request.POST.copy() |
211 |
NameFormSet = generic_inlineformset_factory(Name_i18n, extra=1, formset=NameFormSetFact, can_delete=True) |
212 |
UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, formset=UrlFormSetFact, can_delete=True) |
213 |
try:
|
214 |
service = ServiceLoc.objects.get(institutionid=inst, pk=service_pk) |
215 |
form = ServiceLocForm(request_data, instance=service) |
216 |
names_form = NameFormSet(request_data, instance=service, prefix='namesform')
|
217 |
urls_form = UrlFormSet(request_data, instance=service, prefix='urlsform')
|
218 |
except ServiceLoc.DoesNotExist:
|
219 |
form = ServiceLocForm(request_data) |
220 |
names_form = NameFormSet(request_data, prefix='namesform')
|
221 |
urls_form = UrlFormSet(request_data, prefix='urlsform')
|
222 |
|
223 |
if form.is_valid() and names_form.is_valid() and urls_form.is_valid(): |
224 |
serviceloc = form.save() |
225 |
service = serviceloc |
226 |
names_form.instance = service |
227 |
urls_form.instance = service |
228 |
names_inst = names_form.save() |
229 |
urls_inst = urls_form.save() |
230 |
return HttpResponseRedirect(reverse("services")) |
231 |
else:
|
232 |
form.fields['institutionid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None) |
233 |
form.fields['contact'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.filter(pk__in=getInstContacts(inst)))
|
234 |
if service:
|
235 |
edit = True
|
236 |
for url_form in urls_form.forms: |
237 |
url_form.fields['urltype'] = forms.ChoiceField(choices=(('', '----------'),('info', 'Info'),)) |
238 |
return render_to_response('edumanage/services_edit.html', { 'institution': inst, 'form': form, 'services_form':names_form, 'urls_form': urls_form, "edit": edit}, |
239 |
context_instance=RequestContext(request, base_response(request))) |
240 |
|
241 |
|
242 |
|
243 |
@login_required
|
244 |
def get_service_points(request): |
245 |
if request.method == "GET": |
246 |
user = request.user |
247 |
try:
|
248 |
profile = user.get_profile() |
249 |
inst = profile.institution |
250 |
except UserProfile.DoesNotExist:
|
251 |
inst = False
|
252 |
return HttpResponseNotFound('<h1>Something went really wrong</h1>') |
253 |
servicelocs = ServiceLoc.objects.filter(institutionid=inst) |
254 |
|
255 |
locs = [] |
256 |
for sl in servicelocs: |
257 |
response_location = {} |
258 |
response_location['lat'] = u"%s"%sl.latitude |
259 |
response_location['lng'] = u"%s"%sl.longitude |
260 |
response_location['address'] = u"%s<br>%s"%(sl.address_street, sl.address_city) |
261 |
response_location['enc'] = u"%s"%(sl.enc_level) |
262 |
response_location['AP_no'] = u"%s"%(sl.AP_no) |
263 |
response_location['name'] = sl.loc_name.get(lang='en').name |
264 |
response_location['port_restrict'] = u"%s"%(sl.port_restrict) |
265 |
response_location['transp_proxy'] = u"%s"%(sl.transp_proxy) |
266 |
response_location['IPv6'] = u"%s"%(sl.IPv6) |
267 |
response_location['NAT'] = u"%s"%(sl.NAT) |
268 |
response_location['wired'] = u"%s"%(sl.wired) |
269 |
response_location['SSID'] = u"%s"%(sl.SSID) |
270 |
response_location['key'] = u"%s"%sl.pk |
271 |
locs.append(response_location) |
272 |
return HttpResponse(json.dumps(locs), mimetype='application/json') |
273 |
else:
|
274 |
return HttpResponseNotFound('<h1>Something went really wrong</h1>') |
275 |
|
276 |
|
277 |
def get_all_services(request): |
278 |
servicelocs = ServiceLoc.objects.all() |
279 |
locs = [] |
280 |
for sl in servicelocs: |
281 |
response_location = {} |
282 |
response_location['lat'] = u"%s"%sl.latitude |
283 |
response_location['lng'] = u"%s"%sl.longitude |
284 |
response_location['address'] = u"%s<br>%s"%(sl.address_street, sl.address_city) |
285 |
response_location['enc'] = u"%s"%(sl.enc_level) |
286 |
response_location['AP_no'] = u"%s"%(sl.AP_no) |
287 |
response_location['inst'] = sl.institutionid.org_name.get(lang='en').name |
288 |
response_location['name'] = sl.loc_name.get(lang='en').name |
289 |
response_location['port_restrict'] = u"%s"%(sl.port_restrict) |
290 |
response_location['transp_proxy'] = u"%s"%(sl.transp_proxy) |
291 |
response_location['IPv6'] = u"%s"%(sl.IPv6) |
292 |
response_location['NAT'] = u"%s"%(sl.NAT) |
293 |
response_location['wired'] = u"%s"%(sl.wired) |
294 |
response_location['SSID'] = u"%s"%(sl.SSID) |
295 |
response_location['key'] = u"%s"%sl.pk |
296 |
locs.append(response_location) |
297 |
return HttpResponse(json.dumps(locs), mimetype='application/json') |
298 |
|
299 |
|
300 |
@login_required
|
301 |
def servers(request, server_pk): |
302 |
user = request.user |
303 |
servers = False
|
304 |
try:
|
305 |
profile = user.get_profile() |
306 |
inst = profile.institution |
307 |
except UserProfile.DoesNotExist:
|
308 |
inst = False
|
309 |
return HttpResponseRedirect(reverse("manage")) |
310 |
if inst:
|
311 |
servers = InstServer.objects.filter(instid=inst) |
312 |
if server_pk:
|
313 |
servers = servers.get(pk=server_pk) |
314 |
return render_to_response('edumanage/server_details.html', |
315 |
{ |
316 |
'institution': inst,
|
317 |
'server': servers,
|
318 |
}, |
319 |
context_instance=RequestContext(request, base_response(request))) |
320 |
return render_to_response('edumanage/servers.html', { 'servers': servers}, |
321 |
context_instance=RequestContext(request, base_response(request))) |
322 |
|
323 |
|
324 |
|
325 |
@login_required
|
326 |
def adduser(request): |
327 |
user = request.user |
328 |
try:
|
329 |
profile = user.get_profile() |
330 |
inst = profile.institution |
331 |
except UserProfile.DoesNotExist:
|
332 |
return HttpResponseRedirect(reverse("manage")) |
333 |
if request.method == "GET": |
334 |
form = ContactForm() |
335 |
return render_to_response('edumanage/add_user.html', { 'form' : form }, |
336 |
context_instance=RequestContext(request, base_response(request))) |
337 |
elif request.method == 'POST': |
338 |
request_data = request.POST.copy() |
339 |
form = ContactForm(request_data) |
340 |
if form.is_valid():
|
341 |
contact = form.save() |
342 |
instContPool = InstitutionContactPool(contact=contact, institution=inst) |
343 |
instContPool.save() |
344 |
response_data = {} |
345 |
response_data['value'] = "%s" %contact.pk |
346 |
response_data['text'] = "%s" %contact |
347 |
return HttpResponse(json.dumps(response_data), mimetype='application/json') |
348 |
else:
|
349 |
return render_to_response('edumanage/add_user.html', {'form': form,}, |
350 |
context_instance=RequestContext(request, base_response(request))) |
351 |
|
352 |
@login_required
|
353 |
def add_server(request, server_pk): |
354 |
user = request.user |
355 |
server = False
|
356 |
edit = False
|
357 |
try:
|
358 |
profile = user.get_profile() |
359 |
inst = profile.institution |
360 |
except UserProfile.DoesNotExist:
|
361 |
return HttpResponseRedirect(reverse("manage")) |
362 |
if request.method == "GET": |
363 |
# Determine add or edit
|
364 |
try:
|
365 |
server = InstServer.objects.get(instid=inst, pk=server_pk) |
366 |
form = InstServerForm(instance=server) |
367 |
except InstServer.DoesNotExist:
|
368 |
form = InstServerForm() |
369 |
form.fields['instid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None) |
370 |
if server:
|
371 |
edit = True
|
372 |
return render_to_response('edumanage/servers_edit.html', { 'form': form, 'edit': edit }, |
373 |
context_instance=RequestContext(request, base_response(request))) |
374 |
elif request.method == 'POST': |
375 |
request_data = request.POST.copy() |
376 |
try:
|
377 |
server = InstServer.objects.get(instid=inst, pk=server_pk) |
378 |
form = InstServerForm(request_data, instance=server) |
379 |
except InstServer.DoesNotExist:
|
380 |
form = InstServerForm(request_data) |
381 |
|
382 |
if form.is_valid():
|
383 |
instserverf = form.save() |
384 |
return HttpResponseRedirect(reverse("servers")) |
385 |
else:
|
386 |
form.fields['instid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None) |
387 |
if server:
|
388 |
edit = True
|
389 |
return render_to_response('edumanage/servers_edit.html', { 'institution': inst, 'form': form, 'edit': edit }, |
390 |
context_instance=RequestContext(request, base_response(request))) |
391 |
|
392 |
@login_required
|
393 |
def realms(request): |
394 |
user = request.user |
395 |
servers = False
|
396 |
try:
|
397 |
profile = user.get_profile() |
398 |
inst = profile.institution |
399 |
except UserProfile.DoesNotExist:
|
400 |
return HttpResponseRedirect(reverse("manage")) |
401 |
if inst:
|
402 |
realms = InstRealm.objects.filter(instid=inst) |
403 |
if inst.ertype not in [1,3]: |
404 |
messages.add_message(request, messages.ERROR, 'Cannot add/edit Realms. Your institution should be either IdP or IdP/SP')
|
405 |
return render_to_response('edumanage/realms.html', { 'realms': realms }, |
406 |
context_instance=RequestContext(request, base_response(request))) |
407 |
|
408 |
@login_required
|
409 |
def add_realm(request, realm_pk): |
410 |
user = request.user |
411 |
server = False
|
412 |
realm = False
|
413 |
edit = False
|
414 |
try:
|
415 |
profile = user.get_profile() |
416 |
inst = profile.institution |
417 |
except UserProfile.DoesNotExist:
|
418 |
return HttpResponseRedirect(reverse("manage")) |
419 |
if inst.ertype not in [1,3]: |
420 |
messages.add_message(request, messages.ERROR, 'Cannot add/edit Realm. Your institution should be either IdP or IdP/SP')
|
421 |
return render_to_response('edumanage/realms_edit.html', { 'edit': edit }, |
422 |
context_instance=RequestContext(request, base_response(request))) |
423 |
if request.method == "GET": |
424 |
|
425 |
# Determine add or edit
|
426 |
try:
|
427 |
realm = InstRealm.objects.get(instid=inst, pk=realm_pk) |
428 |
form = InstRealmForm(instance=realm) |
429 |
except InstRealm.DoesNotExist:
|
430 |
form = InstRealmForm() |
431 |
form.fields['instid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None) |
432 |
form.fields['proxyto'] = forms.ModelMultipleChoiceField(queryset=InstServer.objects.filter(pk__in=getInstServers(inst)))
|
433 |
if realm:
|
434 |
edit = True
|
435 |
return render_to_response('edumanage/realms_edit.html', { 'form': form, 'edit': edit }, |
436 |
context_instance=RequestContext(request, base_response(request))) |
437 |
elif request.method == 'POST': |
438 |
request_data = request.POST.copy() |
439 |
try:
|
440 |
realm = InstRealm.objects.get(instid=inst, pk=realm_pk) |
441 |
form = InstRealmForm(request_data, instance=realm) |
442 |
except InstRealm.DoesNotExist:
|
443 |
form = InstRealmForm(request_data) |
444 |
|
445 |
if form.is_valid():
|
446 |
instserverf = form.save() |
447 |
return HttpResponseRedirect(reverse("realms")) |
448 |
else:
|
449 |
form.fields['instid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None) |
450 |
form.fields['proxyto'] = forms.ModelMultipleChoiceField(queryset=InstServer.objects.filter(pk__in=getInstServers(inst)))
|
451 |
if realm:
|
452 |
edit = True
|
453 |
return render_to_response('edumanage/realms_edit.html', { 'institution': inst, 'form': form, 'edit': edit }, |
454 |
context_instance=RequestContext(request, base_response(request))) |
455 |
|
456 |
|
457 |
@login_required
|
458 |
def del_realm(request): |
459 |
if request.method == 'GET': |
460 |
user = request.user |
461 |
req_data = request.GET.copy() |
462 |
realm_pk = req_data['realm_pk']
|
463 |
try:
|
464 |
profile = user.get_profile() |
465 |
institution = profile.institution |
466 |
except UserProfile.DoesNotExist:
|
467 |
resp['error'] = "Not enough rights" |
468 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
469 |
resp = {} |
470 |
try:
|
471 |
realm = InstRealm.objects.get(instid=institution, pk=realm_pk) |
472 |
except InstRealm.DoesNotExist:
|
473 |
resp['error'] = "Could not get realm or you have no rights to delete" |
474 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
475 |
try:
|
476 |
realm.delete() |
477 |
except:
|
478 |
resp['error'] = "Could not delete realm" |
479 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
480 |
resp['success'] = "Realm successfully deleted" |
481 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
482 |
|
483 |
|
484 |
|
485 |
@login_required
|
486 |
def contacts(request): |
487 |
user = request.user |
488 |
servers = False
|
489 |
instcontacts = [] |
490 |
try:
|
491 |
profile = user.get_profile() |
492 |
inst = profile.institution |
493 |
except UserProfile.DoesNotExist:
|
494 |
return HttpResponseRedirect(reverse("manage")) |
495 |
if inst:
|
496 |
instcontacts.extend([x.contact.pk for x in InstitutionContactPool.objects.filter(institution=inst)]) |
497 |
contacts = Contact.objects.filter(pk__in=instcontacts) |
498 |
return render_to_response('edumanage/contacts.html', { 'contacts': contacts}, |
499 |
context_instance=RequestContext(request, base_response(request))) |
500 |
|
501 |
@login_required
|
502 |
def add_contact(request, contact_pk): |
503 |
user = request.user |
504 |
server = False
|
505 |
edit = False
|
506 |
contact = False
|
507 |
try:
|
508 |
profile = user.get_profile() |
509 |
inst = profile.institution |
510 |
except UserProfile.DoesNotExist:
|
511 |
inst = False
|
512 |
return HttpResponseRedirect(reverse("manage")) |
513 |
if request.method == "GET": |
514 |
|
515 |
# Determine add or edit
|
516 |
try:
|
517 |
contactinst = InstitutionContactPool.objects.get(institution=inst, contact__pk=contact_pk) |
518 |
contact = contactinst.contact |
519 |
form = ContactForm(instance=contact) |
520 |
except InstitutionContactPool.DoesNotExist:
|
521 |
form = ContactForm() |
522 |
if contact:
|
523 |
edit = True
|
524 |
return render_to_response('edumanage/contacts_edit.html', { 'form': form, "edit" : edit}, |
525 |
context_instance=RequestContext(request, base_response(request))) |
526 |
elif request.method == 'POST': |
527 |
request_data = request.POST.copy() |
528 |
try:
|
529 |
contactinst = InstitutionContactPool.objects.get(institution=inst, contact__pk=contact_pk) |
530 |
contact = contactinst.contact |
531 |
form = ContactForm(request_data, instance=contact) |
532 |
except InstitutionContactPool.DoesNotExist:
|
533 |
form = ContactForm(request_data) |
534 |
|
535 |
if form.is_valid():
|
536 |
contact = form.save() |
537 |
instContPool, created = InstitutionContactPool.objects.get_or_create(contact=contact, institution=inst) |
538 |
instContPool.save() |
539 |
return HttpResponseRedirect(reverse("contacts")) |
540 |
if contact:
|
541 |
edit = True
|
542 |
return render_to_response('edumanage/contacts_edit.html', { 'form': form, "edit": edit}, |
543 |
context_instance=RequestContext(request, base_response(request))) |
544 |
|
545 |
|
546 |
@login_required
|
547 |
def del_contact(request): |
548 |
if request.method == 'GET': |
549 |
user = request.user |
550 |
req_data = request.GET.copy() |
551 |
contact_pk = req_data['contact_pk']
|
552 |
try:
|
553 |
profile = user.get_profile() |
554 |
institution = profile.institution |
555 |
except UserProfile.DoesNotExist:
|
556 |
resp['error'] = "Could not delete contact. Not enough rights" |
557 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
558 |
resp = {} |
559 |
try:
|
560 |
contactinst = InstitutionContactPool.objects.get(institution=institution, contact__pk=contact_pk) |
561 |
contact = contactinst.contact |
562 |
except InstitutionContactPool.DoesNotExist:
|
563 |
resp['error'] = "Could not get contact or you have no rights to delete" |
564 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
565 |
try:
|
566 |
for service in ServiceLoc.objects.filter(institutionid=institution): |
567 |
if (contact in service.contact.all() and len(service.contact.all()) == 1): |
568 |
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") |
569 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
570 |
if (contact in institution.institutiondetails.contact.all() and len(institution.institutiondetails.contact.all()) == 1): |
571 |
resp['error'] = "Could not delete contact. It is the only contact your institution.<br>Fix it and try again" |
572 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
573 |
contact.delete() |
574 |
except Exception: |
575 |
resp['error'] = "Could not delete contact" |
576 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
577 |
resp['success'] = "Contact successfully deleted" |
578 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
579 |
|
580 |
|
581 |
|
582 |
@login_required
|
583 |
def base_response(request): |
584 |
user = request.user |
585 |
inst = [] |
586 |
server = [] |
587 |
services = [] |
588 |
instrealms = [] |
589 |
instcontacts = [] |
590 |
contacts = [] |
591 |
institution = False
|
592 |
institution_exists = False
|
593 |
try:
|
594 |
profile = user.get_profile() |
595 |
institution = profile.institution |
596 |
institution_exists = True
|
597 |
except UserProfile.DoesNotExist:
|
598 |
institution_exists = False
|
599 |
try:
|
600 |
inst.append(institution) |
601 |
server = InstServer.objects.filter(instid=institution) |
602 |
services = ServiceLoc.objects.filter(institutionid=institution) |
603 |
instrealms = InstRealm.objects.filter(instid=institution) |
604 |
instcontacts.extend([x.contact.pk for x in InstitutionContactPool.objects.filter(institution=institution)]) |
605 |
contacts = Contact.objects.filter(pk__in=instcontacts) |
606 |
except:
|
607 |
pass
|
608 |
|
609 |
return {
|
610 |
'inst_num': len(inst), |
611 |
'servers_num': len(server), |
612 |
'services_num': len(services), |
613 |
'realms_num': len(instrealms), |
614 |
'contacts_num': len(contacts), |
615 |
'instdets': institution,
|
616 |
'institution_exists': institution_exists,
|
617 |
|
618 |
} |
619 |
|
620 |
@login_required
|
621 |
def del_server(request): |
622 |
if request.method == 'GET': |
623 |
user = request.user |
624 |
req_data = request.GET.copy() |
625 |
server_pk = req_data['server_pk']
|
626 |
try:
|
627 |
profile = user.get_profile() |
628 |
institution = profile.institution |
629 |
except UserProfile.DoesNotExist:
|
630 |
resp['error'] = "Could not delete server. Not enough rights" |
631 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
632 |
resp = {} |
633 |
try:
|
634 |
server = InstServer.objects.get(instid=institution, pk=server_pk) |
635 |
except InstServer.DoesNotExist:
|
636 |
resp['error'] = "Could not get server or you have no rights to delete" |
637 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
638 |
try:
|
639 |
server.delete() |
640 |
except:
|
641 |
resp['error'] = "Could not delete server" |
642 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
643 |
resp['success'] = "Server successfully deleted" |
644 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
645 |
|
646 |
|
647 |
@login_required
|
648 |
def del_service(request): |
649 |
if request.method == 'GET': |
650 |
user = request.user |
651 |
req_data = request.GET.copy() |
652 |
service_pk = req_data['service_pk']
|
653 |
try:
|
654 |
profile = user.get_profile() |
655 |
institution = profile.institution |
656 |
except UserProfile.DoesNotExist:
|
657 |
resp['error'] = "Could not delete service. Not enough rights" |
658 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
659 |
resp = {} |
660 |
try:
|
661 |
service = ServiceLoc.objects.get(institutionid=institution, pk=service_pk) |
662 |
except ServiceLoc.DoesNotExist:
|
663 |
resp['error'] = "Could not get service or you have no rights to delete" |
664 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
665 |
try:
|
666 |
service.delete() |
667 |
except:
|
668 |
resp['error'] = "Could not delete service" |
669 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
670 |
resp['success'] = "Service successfully deleted" |
671 |
return HttpResponse(json.dumps(resp), mimetype='application/json') |
672 |
|
673 |
@never_cache
|
674 |
def user_login(request): |
675 |
try:
|
676 |
error_username = False
|
677 |
error_orgname = False
|
678 |
error_entitlement = False
|
679 |
error_mail = False
|
680 |
has_entitlement = False
|
681 |
error = ''
|
682 |
username = request.META['HTTP_EPPN']
|
683 |
if not username: |
684 |
error_username = True
|
685 |
firstname = request.META['HTTP_SHIB_INETORGPERSON_GIVENNAME']
|
686 |
lastname = request.META['HTTP_SHIB_PERSON_SURNAME']
|
687 |
mail = request.META['HTTP_SHIB_INETORGPERSON_MAIL']
|
688 |
#organization = request.META['HTTP_SHIB_HOMEORGANIZATION']
|
689 |
entitlement = request.META['HTTP_SHIB_EP_ENTITLEMENT']
|
690 |
if settings.SHIB_AUTH_ENTITLEMENT in entitlement.split(";"): |
691 |
has_entitlement = True
|
692 |
if not has_entitlement: |
693 |
error_entitlement = True
|
694 |
# if not organization:
|
695 |
# error_orgname = True
|
696 |
if not mail: |
697 |
error_mail = True
|
698 |
if error_username:
|
699 |
error = _("Your idP should release the HTTP_EPPN attribute towards this service<br>")
|
700 |
if error_orgname:
|
701 |
error = error + _("Your idP should release the HTTP_SHIB_HOMEORGANIZATION attribute towards this service<br>")
|
702 |
if error_entitlement:
|
703 |
error = error + _("Your idP should release an appropriate HTTP_SHIB_EP_ENTITLEMENT attribute towards this service<br>")
|
704 |
if error_mail:
|
705 |
error = error + _("Your idP should release the HTTP_SHIB_INETORGPERSON_MAIL attribute towards this service")
|
706 |
if error_username or error_orgname or error_entitlement or error_mail: |
707 |
return render_to_response('error.html', {'error': error, "missing_attributes": True}, |
708 |
context_instance=RequestContext(request)) |
709 |
try:
|
710 |
user = User.objects.get(username__exact=username) |
711 |
user.email = mail |
712 |
user.first_name = firstname |
713 |
user.last_name = lastname |
714 |
user.save() |
715 |
user_exists = True
|
716 |
except User.DoesNotExist:
|
717 |
user_exists = False
|
718 |
user = authenticate(username=username, firstname=firstname, lastname=lastname, mail=mail, authsource='shibboleth')
|
719 |
if user is not None: |
720 |
# try:
|
721 |
# peer = Peer.objects.get(domain_name=organization)
|
722 |
# up = UserProfile.objects.get_or_create(user=user,peer=peer)
|
723 |
# except:
|
724 |
# error = _("Your organization's domain name does not match our peers' domain names<br>Please contact Helpdesk to resolve this issue")
|
725 |
# return render_to_response('error.html', {'error': error}, context_instance=RequestContext(request))
|
726 |
# if not user_exists:
|
727 |
# user_activation_notify(user)
|
728 |
# user does not exist... forward to an institution selection form to create profile
|
729 |
try:
|
730 |
profile = user.get_profile() |
731 |
inst = profile.institution |
732 |
except UserProfile.DoesNotExist:
|
733 |
form = UserProfileForm() |
734 |
form.fields['user'] = forms.ModelChoiceField(queryset=User.objects.filter(pk=user.pk), empty_label=None) |
735 |
form.fields['institution'] = forms.ModelChoiceField(queryset=Institution.objects.all(), empty_label=None) |
736 |
return render_to_response('registration/select_institution.html', {'form': form}, context_instance=RequestContext(request)) |
737 |
if user.is_active:
|
738 |
login(request, user) |
739 |
return HttpResponseRedirect(reverse("manage")) |
740 |
else:
|
741 |
error = _("User account <strong>%s</strong> is pending activation. Administrators have been notified and will activate this account within the next days. <br>If this account has remained inactive for a long time contact your technical coordinator or GRNET Helpdesk") %user.username
|
742 |
return render_to_response('error.html', {'error': error, 'inactive': True}, |
743 |
context_instance=RequestContext(request)) |
744 |
else:
|
745 |
error = _("Something went wrong during user authentication. Contact your administrator %s" %user)
|
746 |
return render_to_response('error.html', {'error': error,}, |
747 |
context_instance=RequestContext(request)) |
748 |
except Exception as e: |
749 |
error = _("Invalid login procedure %s" %e)
|
750 |
return render_to_response('error.html', {'error': error,}, |
751 |
context_instance=RequestContext(request)) |
752 |
# Return an 'invalid login' error message.
|
753 |
# return HttpResponseRedirect(reverse("user-routes"))
|
754 |
|
755 |
def geolocate(request): |
756 |
return render_to_response('front/geolocate.html', |
757 |
context_instance=RequestContext(request)) |
758 |
|
759 |
def selectinst(request): |
760 |
if request.method == 'POST': |
761 |
request_data = request.POST.copy() |
762 |
user = request_data['user']
|
763 |
form = UserProfileForm(request_data) |
764 |
if form.is_valid():
|
765 |
userprofile = form.save() |
766 |
error = _("User account <strong>%s</strong> is pending activation. Administrators have been notified and will activate this account within the next days. <br>If this account has remained inactive for a long time contact your technical coordinator or GRNET Helpdesk") %userprofile.user.username
|
767 |
return render_to_response('error.html', {'error': error, 'inactive': True}, |
768 |
context_instance=RequestContext(request)) |
769 |
else:
|
770 |
form.fields['user'] = forms.ModelChoiceField(queryset=User.objects.filter(pk=user.pk), empty_label=None) |
771 |
form.fields['institution'] = forms.ModelChoiceField(queryset=Institution.objects.all(), empty_label=None) |
772 |
return render_to_response('registration/select_institution.html', {'form': form}, context_instance=RequestContext(request)) |
773 |
|
774 |
|
775 |
def closest(request): |
776 |
if request.method == 'GET': |
777 |
locs = [] |
778 |
request_data = request.GET.copy() |
779 |
response_location = {} |
780 |
response_location["lat"] = request_data['lat'] |
781 |
response_location["lng"] = request_data['lng'] |
782 |
lat = float(request_data['lat']) |
783 |
lng = float(request_data['lng']) |
784 |
R = 6371
|
785 |
distances = {} |
786 |
closestMarker = {} |
787 |
closest = -1
|
788 |
doc = ET.parse(settings.KML_FILE) |
789 |
root = doc.getroot() |
790 |
r = root.getchildren()[0]
|
791 |
for (counter, i) in enumerate(r.getchildren()): |
792 |
if "id" in i.keys(): |
793 |
j = i.getchildren() |
794 |
pointname = j[0].text
|
795 |
point = j[2].getchildren()[0].text |
796 |
pointlng, pointlat, pointele = point.split(',')
|
797 |
dLat = rad(float(pointlat)-float(lat)) |
798 |
dLong = rad(float(pointlng)-float(lng)) |
799 |
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) |
800 |
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) |
801 |
d = R * c |
802 |
distances[counter] = d |
803 |
if (closest == -1 or d < distances[closest]): |
804 |
closest = counter |
805 |
closestMarker = {"name": pointname, "lat": pointlat, "lng": pointlng, "text": j[1].text} |
806 |
return HttpResponse(json.dumps(closestMarker), mimetype='application/json') |
807 |
|
808 |
|
809 |
def instxml(request): |
810 |
ET._namespace_map["http://www.w3.org/2001/XMLSchema-instance"] = 'xsi' |
811 |
root = ET.Element("institutions")
|
812 |
NS_XSI = "{http://www.w3.org/2001/XMLSchema-instance}"
|
813 |
root.set(NS_XSI + "noNamespaceSchemaLocation", "institutions.xsd") |
814 |
#root.attrib["xsi:noNamespaceSchemaLocation"] = "institution.xsd"
|
815 |
institutions = Institution.objects.all() |
816 |
for institution in institutions: |
817 |
try:
|
818 |
inst = institution.institutiondetails |
819 |
if not inst: |
820 |
pass
|
821 |
except InstitutionDetails.DoesNotExist:
|
822 |
pass
|
823 |
|
824 |
instElement = ET.SubElement(root, "institution")
|
825 |
|
826 |
instCountry = ET.SubElement(instElement, "country")
|
827 |
instCountry.text = ("%s" %inst.institution.realmid.country).upper()
|
828 |
|
829 |
instType = ET.SubElement(instElement, "type")
|
830 |
instType.text = "%s" %inst.institution.ertype
|
831 |
|
832 |
for realm in institution.instrealm_set.all(): |
833 |
instRealm = ET.SubElement(instElement, "inst_realm")
|
834 |
instRealm.text = realm.realm |
835 |
|
836 |
for name in inst.institution.org_name.all(): |
837 |
instOrgName = ET.SubElement(instElement, "org_name")
|
838 |
instOrgName.attrib["lang"] = name.lang
|
839 |
instOrgName.text = u"%s" %name.name
|
840 |
|
841 |
instAddress = ET.SubElement(instElement, "address")
|
842 |
|
843 |
instAddrStreet = ET.SubElement(instAddress, "street")
|
844 |
instAddrStreet.text = inst.address_street |
845 |
|
846 |
instAddrCity = ET.SubElement(instAddress, "city")
|
847 |
instAddrCity.text = inst.address_city |
848 |
|
849 |
for contact in inst.contact.all(): |
850 |
instContact = ET.SubElement(instElement, "contact")
|
851 |
|
852 |
instContactName = ET.SubElement(instContact, "name")
|
853 |
instContactName.text = "%s %s" %(contact.firstname, contact.lastname)
|
854 |
|
855 |
instContactEmail = ET.SubElement(instContact, "email")
|
856 |
instContactEmail.text = contact.email |
857 |
|
858 |
instContactPhone = ET.SubElement(instContact, "phone")
|
859 |
instContactPhone.text = contact.phone |
860 |
|
861 |
for url in inst.url.all(): |
862 |
instUrl = ET.SubElement(instElement, "%s_URL"%(url.urltype))
|
863 |
instUrl.attrib["lang"] = url.lang
|
864 |
instUrl.text = url.url |
865 |
|
866 |
#Let's go to Institution Service Locations
|
867 |
|
868 |
for serviceloc in inst.institution.serviceloc_set.all(): |
869 |
instLocation = ET.SubElement(instElement, "location")
|
870 |
|
871 |
instLong = ET.SubElement(instLocation, "longitude")
|
872 |
instLong.text = "%s" %serviceloc.longitude
|
873 |
|
874 |
instLat = ET.SubElement(instLocation, "latitude")
|
875 |
instLat.text = "%s" %serviceloc.latitude
|
876 |
|
877 |
for instlocname in serviceloc.loc_name.all(): |
878 |
instLocName = ET.SubElement(instLocation, "loc_name")
|
879 |
instLocName.attrib["lang"] = instlocname.lang
|
880 |
instLocName.text = instlocname.name |
881 |
|
882 |
instLocAddress = ET.SubElement(instLocation, "address")
|
883 |
|
884 |
instLocAddrStreet = ET.SubElement(instLocAddress, "street")
|
885 |
instLocAddrStreet.text = serviceloc.address_street |
886 |
|
887 |
instLocAddrCity = ET.SubElement(instLocAddress, "city")
|
888 |
instLocAddrCity.text = serviceloc.address_city |
889 |
|
890 |
instLocSSID = ET.SubElement(instLocation, "SSID")
|
891 |
instLocSSID.text = serviceloc.SSID |
892 |
|
893 |
instLocEncLevel = ET.SubElement(instLocation, "enc_level")
|
894 |
instLocEncLevel.text = serviceloc.enc_level |
895 |
|
896 |
instLocPortRestrict = ET.SubElement(instLocation, "port_restrict")
|
897 |
instLocPortRestrict.text = ("%s" %serviceloc.port_restrict).lower()
|
898 |
|
899 |
instLocTransProxy = ET.SubElement(instLocation, "transp_proxy")
|
900 |
instLocTransProxy.text = ("%s" %serviceloc.transp_proxy).lower()
|
901 |
|
902 |
instLocIpv6 = ET.SubElement(instLocation, "IPv6")
|
903 |
instLocIpv6.text = ("%s" %serviceloc.IPv6).lower()
|
904 |
|
905 |
instLocNAT = ET.SubElement(instLocation, "NAT")
|
906 |
instLocNAT.text = ("%s" %serviceloc.NAT).lower()
|
907 |
|
908 |
instLocAP_no = ET.SubElement(instLocation, "AP_no")
|
909 |
instLocAP_no.text = "%s" %int(serviceloc.AP_no) |
910 |
|
911 |
instLocWired = ET.SubElement(instLocation, "wired")
|
912 |
instLocWired.text = ("%s" %serviceloc.wired).lower()
|
913 |
|
914 |
for url in serviceloc.url.all(): |
915 |
instLocUrl = ET.SubElement(instLocation, "%s_URL"%(url.urltype))
|
916 |
instLocUrl.attrib["lang"] = url.lang
|
917 |
instLocUrl.text = url.url |
918 |
|
919 |
instTs = ET.SubElement(instElement, "ts")
|
920 |
instTs.text = "%s" %inst.ts.isoformat()
|
921 |
|
922 |
return render_to_response("general/institution.xml", {"xml":to_xml(root)},context_instance=RequestContext(request,), mimetype="application/xml") |
923 |
|
924 |
|
925 |
def realmxml(request): |
926 |
realm = Realm.objects.all()[0]
|
927 |
ET._namespace_map["http://www.w3.org/2001/XMLSchema-instance"] = 'xsi' |
928 |
root = ET.Element("realms")
|
929 |
NS_XSI = "{http://www.w3.org/2001/XMLSchema-instance}"
|
930 |
root.set(NS_XSI + "noNamespaceSchemaLocation", "realm.xsd") |
931 |
#root.attrib["xsi:noNamespaceSchemaLocation"] = "institution.xsd"
|
932 |
realmElement = ET.SubElement(root, "realm")
|
933 |
|
934 |
realmCountry = ET.SubElement(realmElement, "country")
|
935 |
realmCountry.text = realm.country.upper() |
936 |
|
937 |
realmStype = ET.SubElement(realmElement, "stype")
|
938 |
realmStype.text = "%s" %realm.stype
|
939 |
|
940 |
for name in realm.org_name.all(): |
941 |
realmOrgName = ET.SubElement(realmElement, "org_name")
|
942 |
realmOrgName.attrib["lang"] = name.lang
|
943 |
realmOrgName.text = u"%s" %name.name
|
944 |
|
945 |
realmAddress = ET.SubElement(realmElement, "address")
|
946 |
|
947 |
realmAddrStreet = ET.SubElement(realmAddress, "street")
|
948 |
realmAddrStreet.text = realm.address_street |
949 |
|
950 |
realmAddrCity = ET.SubElement(realmAddress, "city")
|
951 |
realmAddrCity.text = realm.address_city |
952 |
|
953 |
for contact in realm.contact.all(): |
954 |
realmContact = ET.SubElement(realmElement, "contact")
|
955 |
|
956 |
realmContactName = ET.SubElement(realmContact, "name")
|
957 |
realmContactName.text = "%s %s" %(contact.firstname, contact.lastname)
|
958 |
|
959 |
realmContactEmail = ET.SubElement(realmContact, "email")
|
960 |
realmContactEmail.text = contact.email |
961 |
|
962 |
realmContactPhone = ET.SubElement(realmContact, "phone")
|
963 |
realmContactPhone.text = contact.phone |
964 |
|
965 |
for url in realm.url.all(): |
966 |
realmUrl = ET.SubElement(realmElement, "%s_URL"%(url.urltype))
|
967 |
realmUrl.attrib["lang"] = url.lang
|
968 |
realmUrl.text = url.url |
969 |
|
970 |
instTs = ET.SubElement(realmElement, "ts")
|
971 |
instTs.text = "%s" %realm.ts.isoformat()
|
972 |
|
973 |
return render_to_response("general/realm.xml", {"xml":to_xml(root)},context_instance=RequestContext(request,), mimetype="application/xml") |
974 |
|
975 |
def realmdataxml(request): |
976 |
realm = Realm.objects.all()[0]
|
977 |
ET._namespace_map["http://www.w3.org/2001/XMLSchema-instance"] = 'xsi' |
978 |
root = ET.Element("realm_data_root")
|
979 |
NS_XSI = "{http://www.w3.org/2001/XMLSchema-instance}"
|
980 |
root.set(NS_XSI + "noNamespaceSchemaLocation", "realm-data.xsd") |
981 |
|
982 |
realmdataElement = ET.SubElement(root, "realm_data")
|
983 |
|
984 |
realmCountry = ET.SubElement(realmdataElement, "country")
|
985 |
realmCountry.text = realm.country.upper() |
986 |
|
987 |
nIdpCountry = ET.SubElement(realmdataElement, "number_Idp")
|
988 |
nIdpCountry.text = "%s" %len(realm.institution_set.filter(ertype=1)) |
989 |
|
990 |
nSPCountry = ET.SubElement(realmdataElement, "number_SP")
|
991 |
nSPCountry.text = "%s" %len(realm.institution_set.filter(ertype=2)) |
992 |
|
993 |
nSPIdpCountry = ET.SubElement(realmdataElement, "number_SPIdP")
|
994 |
nSPIdpCountry.text = "%s" %len(realm.institution_set.filter(ertype=3)) |
995 |
|
996 |
ninstCountry = ET.SubElement(realmdataElement, "number_inst")
|
997 |
ninstCountry.text = "%s" %len(realm.institution_set.all()) |
998 |
|
999 |
nuserCountry = ET.SubElement(realmdataElement, "number_user")
|
1000 |
insts = realm.institution_set.all() |
1001 |
users = 0
|
1002 |
for inst in insts: |
1003 |
try:
|
1004 |
users = users + inst.institutiondetails.number_user |
1005 |
except InstitutionDetails.DoesNotExist:
|
1006 |
pass
|
1007 |
nuserCountry.text = "%s" %users
|
1008 |
|
1009 |
nIdCountry = ET.SubElement(realmdataElement, "number_id")
|
1010 |
insts = realm.institution_set.all() |
1011 |
ids = 0
|
1012 |
for inst in insts: |
1013 |
try:
|
1014 |
ids = ids + inst.institutiondetails.number_id |
1015 |
except InstitutionDetails.DoesNotExist:
|
1016 |
pass
|
1017 |
nIdCountry.text = "%s" %ids
|
1018 |
|
1019 |
# Get the latest ts from all tables...
|
1020 |
datetimes = [] |
1021 |
datetimes.append(InstitutionDetails.objects.aggregate(Max('ts'))['ts__max']) |
1022 |
datetimes.append(Realm.objects.aggregate(Max('ts'))['ts__max']) |
1023 |
datetimes.append(InstServer.objects.aggregate(Max('ts'))['ts__max']) |
1024 |
datetimes.append(ServiceLoc.objects.aggregate(Max('ts'))['ts__max']) |
1025 |
|
1026 |
instTs = ET.SubElement(realmdataElement, "ts")
|
1027 |
instTs.text = "%s" %max(datetimes).isoformat() |
1028 |
|
1029 |
|
1030 |
return render_to_response("general/realm_data.xml", {"xml":to_xml(root)},context_instance=RequestContext(request,), mimetype="application/xml") |
1031 |
|
1032 |
def to_xml(ele, encoding="UTF-8"): |
1033 |
"Convert and return the XML for an *ele* (:class:`~xml.etree.ElementTree.Element`) with specified *encoding*."
|
1034 |
xml = ET.tostring(ele, encoding) |
1035 |
return xml if xml.startswith('<?xml') else '<?xml version="1.0" encoding="%s"?>%s' % (encoding, xml) |
1036 |
|
1037 |
|
1038 |
def getInstContacts(inst): |
1039 |
contacts = InstitutionContactPool.objects.filter(institution=inst) |
1040 |
contact_pks = [] |
1041 |
for contact in contacts: |
1042 |
contact_pks.append(contact.contact.pk) |
1043 |
return list(set(contact_pks)) |
1044 |
|
1045 |
def getInstServers(inst): |
1046 |
servers = InstServer.objects.filter(instid=inst) |
1047 |
server_pks = [] |
1048 |
for server in servers: |
1049 |
server_pks.append(server.pk) |
1050 |
return list(set(server_pks)) |
1051 |
|
1052 |
|
1053 |
def rad(x): |
1054 |
return x*math.pi/180 |