Statistics
| Branch: | Tag: | Revision:

root / edumanage / views.py @ 27d6f3e2

History | View | Annotate | Download (63.9 kB)

1 a3923fe7 Leonidas Poulopoulos
# -*- coding: utf-8 -*- vim:encoding=utf-8:
2 a3923fe7 Leonidas Poulopoulos
# vim: tabstop=4:shiftwidth=4:softtabstop=4:expandtab
3 a3923fe7 Leonidas Poulopoulos
4 a3923fe7 Leonidas Poulopoulos
from django.shortcuts import render_to_response,get_object_or_404
5 a3923fe7 Leonidas Poulopoulos
from django.http import HttpResponse,HttpResponseRedirect,Http404
6 a3923fe7 Leonidas Poulopoulos
from django.template import RequestContext
7 f60df981 Leonidas Poulopoulos
from django.core.urlresolvers import reverse
8 a3923fe7 Leonidas Poulopoulos
from django.contrib.auth.decorators import login_required
9 a3923fe7 Leonidas Poulopoulos
from edumanage.models import *
10 244d3095 Leonidas Poulopoulos
from accounts.models import *
11 fa8b2f7d Leonidas Poulopoulos
from edumanage.forms import *
12 fa8b2f7d Leonidas Poulopoulos
from django import forms
13 02f2ae43 Leonidas Poulopoulos
from django.forms.models import modelformset_factory
14 02f2ae43 Leonidas Poulopoulos
from django.forms.models import inlineformset_factory
15 02f2ae43 Leonidas Poulopoulos
from django.contrib.contenttypes.generic import generic_inlineformset_factory
16 7973b176 Leonidas Poulopoulos
from django.core.mail.message import EmailMessage
17 7973b176 Leonidas Poulopoulos
from django.contrib.sites.models import Site
18 7973b176 Leonidas Poulopoulos
from django.template.loader import render_to_string
19 d163b72b Leonidas Poulopoulos
import json, bz2
20 20802476 Leonidas Poulopoulos
import math, datetime
21 c2b3014a Leonidas Poulopoulos
from xml.etree import ElementTree as ET
22 af9d484b Leonidas Poulopoulos
23 af9d484b Leonidas Poulopoulos
from django.conf import settings
24 fa2f119d Leonidas Poulopoulos
from django.contrib import messages
25 a3923fe7 Leonidas Poulopoulos
26 0790d933 Leonidas Poulopoulos
from django.db.models import Max
27 0790d933 Leonidas Poulopoulos
28 62d0c01e Leonidas Poulopoulos
from django.views.decorators.cache import never_cache
29 62d0c01e Leonidas Poulopoulos
from django.utils.translation import ugettext as _
30 62d0c01e Leonidas Poulopoulos
from django.contrib.auth import authenticate, login
31 7973b176 Leonidas Poulopoulos
from registration.models import RegistrationProfile
32 d163b72b Leonidas Poulopoulos
from django.core.cache import cache
33 d163b72b Leonidas Poulopoulos
34 bcebee6b Leonidas Poulopoulos
@never_cache
35 a3923fe7 Leonidas Poulopoulos
def index(request):
36 244d3095 Leonidas Poulopoulos
    return render_to_response('front/index.html', context_instance=RequestContext(request))
37 a3923fe7 Leonidas Poulopoulos
38 d163b72b Leonidas Poulopoulos
39 a3923fe7 Leonidas Poulopoulos
@login_required
40 d163b72b Leonidas Poulopoulos
@never_cache
41 a3923fe7 Leonidas Poulopoulos
def manage(request):
42 a3923fe7 Leonidas Poulopoulos
    services_list = []
43 a3923fe7 Leonidas Poulopoulos
    servers_list = []
44 fa8b2f7d Leonidas Poulopoulos
    user = request.user
45 fa8b2f7d Leonidas Poulopoulos
    try:
46 fa8b2f7d Leonidas Poulopoulos
        profile = user.get_profile()
47 fa8b2f7d Leonidas Poulopoulos
        inst = profile.institution
48 fa8b2f7d Leonidas Poulopoulos
    except UserProfile.DoesNotExist:
49 244d3095 Leonidas Poulopoulos
        return render_to_response('edumanage/welcome.html',
50 244d3095 Leonidas Poulopoulos
                              context_instance=RequestContext(request, base_response(request)))
51 244d3095 Leonidas Poulopoulos
        
52 fa8b2f7d Leonidas Poulopoulos
    services = ServiceLoc.objects.filter(institutionid=inst)
53 fa8b2f7d Leonidas Poulopoulos
    services_list.extend([s for s in services])
54 fa8b2f7d Leonidas Poulopoulos
    servers = InstServer.objects.filter(instid=inst)
55 fa8b2f7d Leonidas Poulopoulos
    servers_list.extend([s for s in servers])
56 a3923fe7 Leonidas Poulopoulos
    return render_to_response('edumanage/welcome.html', 
57 a3923fe7 Leonidas Poulopoulos
                              {
58 fa8b2f7d Leonidas Poulopoulos
                               'institution': inst, 
59 a3923fe7 Leonidas Poulopoulos
                               'services': services_list,
60 a3923fe7 Leonidas Poulopoulos
                               'servers': servers_list
61 a3923fe7 Leonidas Poulopoulos
                               },  
62 af9d484b Leonidas Poulopoulos
                              context_instance=RequestContext(request, base_response(request)))
63 a3923fe7 Leonidas Poulopoulos
64 a3923fe7 Leonidas Poulopoulos
@login_required
65 d163b72b Leonidas Poulopoulos
@never_cache
66 a3923fe7 Leonidas Poulopoulos
def institutions(request):
67 a3923fe7 Leonidas Poulopoulos
    user = request.user
68 fa8b2f7d Leonidas Poulopoulos
    dict = {}
69 fa8b2f7d Leonidas Poulopoulos
    try:
70 fa8b2f7d Leonidas Poulopoulos
        profile = user.get_profile()
71 fa8b2f7d Leonidas Poulopoulos
        inst = profile.institution
72 fa8b2f7d Leonidas Poulopoulos
        inst.__unicode__ = inst.get_name(request.LANGUAGE_CODE)
73 fa8b2f7d Leonidas Poulopoulos
    except UserProfile.DoesNotExist:
74 244d3095 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
75 fa8b2f7d Leonidas Poulopoulos
    dict['institution'] = inst.pk
76 fa8b2f7d Leonidas Poulopoulos
    form = InstDetailsForm(initial=dict)
77 fa8b2f7d Leonidas Poulopoulos
    form.fields['institution'].widget.attrs['readonly'] = True
78 fa8b2f7d Leonidas Poulopoulos
    return render_to_response('edumanage/institution.html', 
79 a3923fe7 Leonidas Poulopoulos
                              {
80 fa8b2f7d Leonidas Poulopoulos
                               'institution': inst,
81 fa8b2f7d Leonidas Poulopoulos
                               'form': form, 
82 a3923fe7 Leonidas Poulopoulos
                               },  
83 af9d484b Leonidas Poulopoulos
                              context_instance=RequestContext(request, base_response(request)))
84 fa8b2f7d Leonidas Poulopoulos
85 fa8b2f7d Leonidas Poulopoulos
86 fa8b2f7d Leonidas Poulopoulos
87 fa8b2f7d Leonidas Poulopoulos
@login_required
88 d163b72b Leonidas Poulopoulos
@never_cache
89 fa8b2f7d Leonidas Poulopoulos
def add_institution_details(request, institution_pk):
90 fa8b2f7d Leonidas Poulopoulos
    user = request.user
91 f60df981 Leonidas Poulopoulos
    try:
92 f60df981 Leonidas Poulopoulos
        profile = user.get_profile()
93 f60df981 Leonidas Poulopoulos
        inst = profile.institution
94 7973b176 Leonidas Poulopoulos
        inst.__unicode__ = inst.get_name(request.LANGUAGE_CODE)
95 f60df981 Leonidas Poulopoulos
    except UserProfile.DoesNotExist:
96 244d3095 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
97 244d3095 Leonidas Poulopoulos
    
98 f3bca340 Leonidas Poulopoulos
    if institution_pk and int(inst.pk) != int(institution_pk):
99 f3bca340 Leonidas Poulopoulos
        messages.add_message(request, messages.ERROR, 'You have no rights on this Institution')
100 f3bca340 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("institutions"))
101 f3bca340 Leonidas Poulopoulos
    
102 fa8b2f7d Leonidas Poulopoulos
    if request.method == "GET":
103 f60df981 Leonidas Poulopoulos
        request_data = request.POST.copy()
104 f60df981 Leonidas Poulopoulos
        try:         
105 f60df981 Leonidas Poulopoulos
            inst_details = InstitutionDetails.objects.get(institution=inst)
106 f60df981 Leonidas Poulopoulos
            form = InstDetailsForm(instance=inst_details)
107 b9e65644 Leonidas Poulopoulos
            UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, formset=UrlFormSetFactInst, can_delete=True)
108 0f9fca26 Leonidas Poulopoulos
            urls_form = UrlFormSet(prefix='urlsform', instance = inst_details) 
109 f60df981 Leonidas Poulopoulos
        except InstitutionDetails.DoesNotExist:
110 f60df981 Leonidas Poulopoulos
            form = InstDetailsForm()
111 f60df981 Leonidas Poulopoulos
            form.fields['institution'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=institution_pk), empty_label=None)
112 b9e65644 Leonidas Poulopoulos
            UrlFormSet =  generic_inlineformset_factory(URL_i18n, extra=2, can_delete=True)
113 0f9fca26 Leonidas Poulopoulos
            urls_form = UrlFormSet(prefix='urlsform')
114 f3bca340 Leonidas Poulopoulos
115 0f9fca26 Leonidas Poulopoulos
        
116 38f2278d Leonidas Poulopoulos
        form.fields['contact'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.filter(pk__in=getInstContacts(inst)))
117 b9e65644 Leonidas Poulopoulos
        return render_to_response('edumanage/institution_edit.html', { 'institution': inst, 'form': form, 'urls_form':urls_form},
118 af9d484b Leonidas Poulopoulos
                                  context_instance=RequestContext(request, base_response(request)))
119 fa8b2f7d Leonidas Poulopoulos
    elif request.method == 'POST':
120 fa8b2f7d Leonidas Poulopoulos
        request_data = request.POST.copy()
121 b9e65644 Leonidas Poulopoulos
        UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, formset=UrlFormSetFactInst, can_delete=True)
122 f60df981 Leonidas Poulopoulos
        try:         
123 f60df981 Leonidas Poulopoulos
            inst_details = InstitutionDetails.objects.get(institution=inst)
124 f60df981 Leonidas Poulopoulos
            form = InstDetailsForm(request_data, instance=inst_details)
125 b9e65644 Leonidas Poulopoulos
            urls_form = UrlFormSet(request_data, instance=inst_details, prefix='urlsform')
126 f60df981 Leonidas Poulopoulos
        except InstitutionDetails.DoesNotExist:
127 f60df981 Leonidas Poulopoulos
            form = InstDetailsForm(request_data)
128 b9e65644 Leonidas Poulopoulos
            urls_form = UrlFormSet(request_data, prefix='urlsform')
129 b9e65644 Leonidas Poulopoulos
        UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, formset=UrlFormSetFactInst, can_delete=True)
130 b9e65644 Leonidas Poulopoulos
        if form.is_valid() and urls_form.is_valid():
131 f60df981 Leonidas Poulopoulos
            instdets = form.save()
132 b9e65644 Leonidas Poulopoulos
            urls_form.instance = instdets
133 b9e65644 Leonidas Poulopoulos
            urls_inst = urls_form.save()
134 f60df981 Leonidas Poulopoulos
            return HttpResponseRedirect(reverse("institutions"))
135 fa8b2f7d Leonidas Poulopoulos
        else:
136 b9e65644 Leonidas Poulopoulos
            form.fields['institution'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=institution_pk), empty_label=None)
137 b9e65644 Leonidas Poulopoulos
            form.fields['contact'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.filter(pk__in=getInstContacts(inst)))
138 b9e65644 Leonidas Poulopoulos
            return render_to_response('edumanage/institution_edit.html', { 'institution': inst, 'form': form, 'urls_form': urls_form},
139 af9d484b Leonidas Poulopoulos
                                  context_instance=RequestContext(request, base_response(request)))
140 fa8b2f7d Leonidas Poulopoulos
141 f60df981 Leonidas Poulopoulos
142 f60df981 Leonidas Poulopoulos
@login_required
143 d163b72b Leonidas Poulopoulos
@never_cache
144 c01b785c Leonidas Poulopoulos
def services(request, service_pk):
145 f60df981 Leonidas Poulopoulos
    user = request.user
146 f60df981 Leonidas Poulopoulos
    dict = {}
147 f60df981 Leonidas Poulopoulos
    try:
148 f60df981 Leonidas Poulopoulos
        profile = user.get_profile()
149 f60df981 Leonidas Poulopoulos
        inst = profile.institution
150 7973b176 Leonidas Poulopoulos
        inst.__unicode__ = inst.get_name(request.LANGUAGE_CODE)
151 f60df981 Leonidas Poulopoulos
    except UserProfile.DoesNotExist:
152 244d3095 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
153 7973b176 Leonidas Poulopoulos
    try:
154 7973b176 Leonidas Poulopoulos
        instdetails = inst.institutiondetails
155 7973b176 Leonidas Poulopoulos
    except InstitutionDetails.DoesNotExist:
156 7973b176 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
157 c2cca19e Leonidas Poulopoulos
    if inst.ertype not in [2,3]:
158 f3bca340 Leonidas Poulopoulos
        messages.add_message(request, messages.ERROR, 'Cannot add/edit Location. Your institution should be either SP or IdP/SP')
159 fa2f119d Leonidas Poulopoulos
        return render_to_response('edumanage/services.html', { 'institution': inst },
160 fa2f119d Leonidas Poulopoulos
                              context_instance=RequestContext(request, base_response(request)))
161 f60df981 Leonidas Poulopoulos
    try:
162 f60df981 Leonidas Poulopoulos
        services = ServiceLoc.objects.filter(institutionid = inst)
163 f60df981 Leonidas Poulopoulos
    except ServiceLoc.DoesNotExist:
164 f60df981 Leonidas Poulopoulos
        services = False 
165 c01b785c Leonidas Poulopoulos
    
166 c01b785c Leonidas Poulopoulos
    if service_pk:
167 f3bca340 Leonidas Poulopoulos
        try:
168 f3bca340 Leonidas Poulopoulos
            services = services.get(pk=service_pk)
169 f3bca340 Leonidas Poulopoulos
        except:
170 f3bca340 Leonidas Poulopoulos
            messages.add_message(request, messages.ERROR, 'You have no rights to view this location')
171 f3bca340 Leonidas Poulopoulos
            return HttpResponseRedirect(reverse("services"))
172 c01b785c Leonidas Poulopoulos
        return render_to_response('edumanage/service_details.html', 
173 c01b785c Leonidas Poulopoulos
                              {
174 c01b785c Leonidas Poulopoulos
                               'institution': inst,
175 0f9fca26 Leonidas Poulopoulos
                               'service': services,
176 c01b785c Leonidas Poulopoulos
                               },  
177 c01b785c Leonidas Poulopoulos
                              context_instance=RequestContext(request, base_response(request)))
178 f60df981 Leonidas Poulopoulos
    
179 f60df981 Leonidas Poulopoulos
    return render_to_response('edumanage/services.html', 
180 f60df981 Leonidas Poulopoulos
                              {
181 f60df981 Leonidas Poulopoulos
                               'institution': inst,
182 f60df981 Leonidas Poulopoulos
                               'services': services, 
183 f60df981 Leonidas Poulopoulos
                               },  
184 af9d484b Leonidas Poulopoulos
                              context_instance=RequestContext(request, base_response(request)))
185 f60df981 Leonidas Poulopoulos
186 f60df981 Leonidas Poulopoulos
187 f60df981 Leonidas Poulopoulos
188 f60df981 Leonidas Poulopoulos
@login_required
189 d163b72b Leonidas Poulopoulos
@never_cache
190 f60df981 Leonidas Poulopoulos
def add_services(request, service_pk):
191 f60df981 Leonidas Poulopoulos
    user = request.user
192 02f2ae43 Leonidas Poulopoulos
    service = False
193 0f9fca26 Leonidas Poulopoulos
    edit = False
194 f60df981 Leonidas Poulopoulos
    try:
195 f60df981 Leonidas Poulopoulos
        profile = user.get_profile()
196 f60df981 Leonidas Poulopoulos
        inst = profile.institution
197 7973b176 Leonidas Poulopoulos
        inst.__unicode__ = inst.get_name(request.LANGUAGE_CODE)
198 f60df981 Leonidas Poulopoulos
    except UserProfile.DoesNotExist:
199 244d3095 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
200 7973b176 Leonidas Poulopoulos
    try:
201 7973b176 Leonidas Poulopoulos
        instdetails = inst.institutiondetails
202 7973b176 Leonidas Poulopoulos
    except InstitutionDetails.DoesNotExist:
203 7973b176 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
204 c2cca19e Leonidas Poulopoulos
    if inst.ertype not in [2,3]:
205 fa2f119d Leonidas Poulopoulos
        messages.add_message(request, messages.ERROR, 'Cannot add/edit Service. Your institution should be either SP or IdP/SP')
206 fa2f119d Leonidas Poulopoulos
        return render_to_response('edumanage/services_edit.html', { 'edit': edit },
207 fa2f119d Leonidas Poulopoulos
                                  context_instance=RequestContext(request, base_response(request)))
208 f60df981 Leonidas Poulopoulos
    if request.method == "GET":
209 f60df981 Leonidas Poulopoulos
210 f60df981 Leonidas Poulopoulos
        # Determine add or edit
211 f60df981 Leonidas Poulopoulos
        try:         
212 f60df981 Leonidas Poulopoulos
            service = ServiceLoc.objects.get(institutionid=inst, pk=service_pk)
213 f60df981 Leonidas Poulopoulos
            form = ServiceLocForm(instance=service)
214 f60df981 Leonidas Poulopoulos
        except ServiceLoc.DoesNotExist:
215 f60df981 Leonidas Poulopoulos
            form = ServiceLocForm()
216 f3bca340 Leonidas Poulopoulos
            if service_pk:
217 f3bca340 Leonidas Poulopoulos
                messages.add_message(request, messages.ERROR, 'You have no rights to edit this location')
218 f3bca340 Leonidas Poulopoulos
                return HttpResponseRedirect(reverse("services"))
219 02f2ae43 Leonidas Poulopoulos
        form.fields['institutionid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None)
220 af9d484b Leonidas Poulopoulos
        UrlFormSet =  generic_inlineformset_factory(URL_i18n, extra=2, can_delete=True)
221 af9d484b Leonidas Poulopoulos
        NameFormSet = generic_inlineformset_factory(Name_i18n, extra=2, can_delete=True)
222 c2cca19e Leonidas Poulopoulos
        urls_form = UrlFormSet(prefix='urlsform')
223 af9d484b Leonidas Poulopoulos
        names_form = NameFormSet(prefix='namesform')
224 02f2ae43 Leonidas Poulopoulos
        if (service):
225 af9d484b Leonidas Poulopoulos
            NameFormSet = generic_inlineformset_factory(Name_i18n, extra=1, formset=NameFormSetFact, can_delete=True)
226 af9d484b Leonidas Poulopoulos
            names_form = NameFormSet(instance=service, prefix='namesform')
227 af9d484b Leonidas Poulopoulos
            UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, formset=UrlFormSetFact, can_delete=True)
228 af9d484b Leonidas Poulopoulos
            urls_form = UrlFormSet(instance=service, prefix='urlsform')
229 38f2278d Leonidas Poulopoulos
        form.fields['contact'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.filter(pk__in=getInstContacts(inst)))
230 0f9fca26 Leonidas Poulopoulos
        if service:
231 0f9fca26 Leonidas Poulopoulos
            edit = True
232 c2cca19e Leonidas Poulopoulos
        for url_form in urls_form.forms:
233 c2cca19e Leonidas Poulopoulos
            url_form.fields['urltype'] = forms.ChoiceField(choices=(('', '----------'),('info', 'Info'),))
234 0f9fca26 Leonidas Poulopoulos
        return render_to_response('edumanage/services_edit.html', { 'form': form, 'services_form':names_form, 'urls_form': urls_form, "edit": edit},
235 af9d484b Leonidas Poulopoulos
                                  context_instance=RequestContext(request, base_response(request)))
236 f60df981 Leonidas Poulopoulos
    elif request.method == 'POST':
237 f60df981 Leonidas Poulopoulos
        request_data = request.POST.copy()
238 af9d484b Leonidas Poulopoulos
        NameFormSet = generic_inlineformset_factory(Name_i18n, extra=1, formset=NameFormSetFact, can_delete=True)
239 af9d484b Leonidas Poulopoulos
        UrlFormSet = generic_inlineformset_factory(URL_i18n, extra=2, formset=UrlFormSetFact, can_delete=True)
240 f60df981 Leonidas Poulopoulos
        try:         
241 f60df981 Leonidas Poulopoulos
            service = ServiceLoc.objects.get(institutionid=inst, pk=service_pk)
242 f60df981 Leonidas Poulopoulos
            form = ServiceLocForm(request_data, instance=service)
243 af9d484b Leonidas Poulopoulos
            names_form = NameFormSet(request_data, instance=service, prefix='namesform')
244 af9d484b Leonidas Poulopoulos
            urls_form = UrlFormSet(request_data, instance=service, prefix='urlsform')
245 f60df981 Leonidas Poulopoulos
        except ServiceLoc.DoesNotExist:
246 f60df981 Leonidas Poulopoulos
            form = ServiceLocForm(request_data)
247 af9d484b Leonidas Poulopoulos
            names_form = NameFormSet(request_data, prefix='namesform')
248 af9d484b Leonidas Poulopoulos
            urls_form = UrlFormSet(request_data, prefix='urlsform')
249 f3bca340 Leonidas Poulopoulos
            if service_pk:
250 f3bca340 Leonidas Poulopoulos
                messages.add_message(request, messages.ERROR, 'You have no rights to edit this location')
251 f3bca340 Leonidas Poulopoulos
                return HttpResponseRedirect(reverse("services"))
252 02f2ae43 Leonidas Poulopoulos
        
253 af9d484b Leonidas Poulopoulos
        if form.is_valid() and names_form.is_valid() and urls_form.is_valid():
254 02f2ae43 Leonidas Poulopoulos
            serviceloc = form.save()
255 af9d484b Leonidas Poulopoulos
            service = serviceloc
256 af9d484b Leonidas Poulopoulos
            names_form.instance = service
257 af9d484b Leonidas Poulopoulos
            urls_form.instance = service
258 af9d484b Leonidas Poulopoulos
            names_inst = names_form.save()
259 af9d484b Leonidas Poulopoulos
            urls_inst = urls_form.save()
260 f60df981 Leonidas Poulopoulos
            return HttpResponseRedirect(reverse("services"))
261 f60df981 Leonidas Poulopoulos
        else:
262 02f2ae43 Leonidas Poulopoulos
            form.fields['institutionid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None)
263 38f2278d Leonidas Poulopoulos
            form.fields['contact'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.filter(pk__in=getInstContacts(inst)))
264 0f9fca26 Leonidas Poulopoulos
        if service:
265 0f9fca26 Leonidas Poulopoulos
            edit = True
266 c2cca19e Leonidas Poulopoulos
        for url_form in urls_form.forms:
267 c2cca19e Leonidas Poulopoulos
            url_form.fields['urltype'] = forms.ChoiceField(choices=(('', '----------'),('info', 'Info'),))
268 0f9fca26 Leonidas Poulopoulos
        return render_to_response('edumanage/services_edit.html', { 'institution': inst, 'form': form, 'services_form':names_form, 'urls_form': urls_form, "edit": edit},
269 af9d484b Leonidas Poulopoulos
                                  context_instance=RequestContext(request, base_response(request)))
270 af9d484b Leonidas Poulopoulos
271 d163b72b Leonidas Poulopoulos
272 02f2ae43 Leonidas Poulopoulos
@login_required
273 d163b72b Leonidas Poulopoulos
@never_cache
274 7973b176 Leonidas Poulopoulos
def del_service(request):
275 7973b176 Leonidas Poulopoulos
    if request.method == 'GET':
276 02f2ae43 Leonidas Poulopoulos
        user = request.user
277 7973b176 Leonidas Poulopoulos
        req_data = request.GET.copy()
278 7973b176 Leonidas Poulopoulos
        service_pk = req_data['service_pk']
279 02f2ae43 Leonidas Poulopoulos
        try:
280 02f2ae43 Leonidas Poulopoulos
            profile = user.get_profile()
281 7973b176 Leonidas Poulopoulos
            institution = profile.institution
282 02f2ae43 Leonidas Poulopoulos
        except UserProfile.DoesNotExist:
283 7973b176 Leonidas Poulopoulos
            resp['error'] = "Could not delete service. Not enough rights"
284 7973b176 Leonidas Poulopoulos
            return HttpResponse(json.dumps(resp), mimetype='application/json')
285 7973b176 Leonidas Poulopoulos
        resp = {}
286 7973b176 Leonidas Poulopoulos
        try:
287 7973b176 Leonidas Poulopoulos
            service = ServiceLoc.objects.get(institutionid=institution, pk=service_pk)
288 7973b176 Leonidas Poulopoulos
        except ServiceLoc.DoesNotExist:
289 7973b176 Leonidas Poulopoulos
            resp['error'] = "Could not get service or you have no rights to delete"
290 7973b176 Leonidas Poulopoulos
            return HttpResponse(json.dumps(resp), mimetype='application/json')
291 7973b176 Leonidas Poulopoulos
        try:
292 7973b176 Leonidas Poulopoulos
            service.delete()
293 7973b176 Leonidas Poulopoulos
        except:
294 7973b176 Leonidas Poulopoulos
            resp['error'] = "Could not delete service"
295 7973b176 Leonidas Poulopoulos
            return HttpResponse(json.dumps(resp), mimetype='application/json')
296 7973b176 Leonidas Poulopoulos
        resp['success'] = "Service successfully deleted"
297 7973b176 Leonidas Poulopoulos
        return HttpResponse(json.dumps(resp), mimetype='application/json')
298 af9d484b Leonidas Poulopoulos
299 d163b72b Leonidas Poulopoulos
300 0f9fca26 Leonidas Poulopoulos
@login_required
301 d163b72b Leonidas Poulopoulos
@never_cache
302 0f9fca26 Leonidas Poulopoulos
def servers(request, server_pk):
303 af9d484b Leonidas Poulopoulos
    user = request.user
304 af9d484b Leonidas Poulopoulos
    servers = False
305 af9d484b Leonidas Poulopoulos
    try:
306 af9d484b Leonidas Poulopoulos
        profile = user.get_profile()
307 af9d484b Leonidas Poulopoulos
        inst = profile.institution
308 af9d484b Leonidas Poulopoulos
    except UserProfile.DoesNotExist:
309 af9d484b Leonidas Poulopoulos
        inst = False
310 244d3095 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
311 af9d484b Leonidas Poulopoulos
    if inst:
312 af9d484b Leonidas Poulopoulos
        servers = InstServer.objects.filter(instid=inst)
313 0f9fca26 Leonidas Poulopoulos
    if server_pk:
314 0f9fca26 Leonidas Poulopoulos
        servers = servers.get(pk=server_pk)
315 0f9fca26 Leonidas Poulopoulos
        return render_to_response('edumanage/server_details.html', 
316 0f9fca26 Leonidas Poulopoulos
                              {
317 0f9fca26 Leonidas Poulopoulos
                               'institution': inst,
318 0f9fca26 Leonidas Poulopoulos
                               'server': servers,
319 0f9fca26 Leonidas Poulopoulos
                               },  
320 0f9fca26 Leonidas Poulopoulos
                              context_instance=RequestContext(request, base_response(request)))
321 edac002f Leonidas Poulopoulos
    return render_to_response('edumanage/servers.html', { 'servers': servers},
322 af9d484b Leonidas Poulopoulos
                                  context_instance=RequestContext(request, base_response(request)))
323 af9d484b Leonidas Poulopoulos
324 d163b72b Leonidas Poulopoulos
325 af9d484b Leonidas Poulopoulos
@login_required
326 d163b72b Leonidas Poulopoulos
@never_cache
327 edac002f Leonidas Poulopoulos
def add_server(request, server_pk):
328 edac002f Leonidas Poulopoulos
    user = request.user
329 edac002f Leonidas Poulopoulos
    server = False
330 0f9fca26 Leonidas Poulopoulos
    edit = False
331 edac002f Leonidas Poulopoulos
    try:
332 edac002f Leonidas Poulopoulos
        profile = user.get_profile()
333 edac002f Leonidas Poulopoulos
        inst = profile.institution
334 7973b176 Leonidas Poulopoulos
        inst.__unicode__ = inst.get_name(request.LANGUAGE_CODE)
335 edac002f Leonidas Poulopoulos
    except UserProfile.DoesNotExist:
336 244d3095 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
337 7973b176 Leonidas Poulopoulos
    try:
338 7973b176 Leonidas Poulopoulos
        instdetails = inst.institutiondetails
339 7973b176 Leonidas Poulopoulos
    except InstitutionDetails.DoesNotExist:
340 7973b176 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
341 edac002f Leonidas Poulopoulos
    if request.method == "GET":
342 edac002f Leonidas Poulopoulos
        # Determine add or edit
343 edac002f Leonidas Poulopoulos
        try:         
344 edac002f Leonidas Poulopoulos
            server = InstServer.objects.get(instid=inst, pk=server_pk)
345 edac002f Leonidas Poulopoulos
            form = InstServerForm(instance=server)
346 edac002f Leonidas Poulopoulos
        except InstServer.DoesNotExist:
347 edac002f Leonidas Poulopoulos
            form = InstServerForm()
348 f3bca340 Leonidas Poulopoulos
            if server_pk:
349 f3bca340 Leonidas Poulopoulos
                messages.add_message(request, messages.ERROR, 'You have no rights to edit this server')
350 f3bca340 Leonidas Poulopoulos
                return HttpResponseRedirect(reverse("servers"))  
351 edac002f Leonidas Poulopoulos
        form.fields['instid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None)
352 0f9fca26 Leonidas Poulopoulos
        if server:
353 0f9fca26 Leonidas Poulopoulos
            edit = True
354 f3bca340 Leonidas Poulopoulos
                      
355 0f9fca26 Leonidas Poulopoulos
        return render_to_response('edumanage/servers_edit.html', { 'form': form, 'edit': edit },
356 af9d484b Leonidas Poulopoulos
                                  context_instance=RequestContext(request, base_response(request)))
357 edac002f Leonidas Poulopoulos
    elif request.method == 'POST':
358 edac002f Leonidas Poulopoulos
        request_data = request.POST.copy()
359 edac002f Leonidas Poulopoulos
        try:         
360 edac002f Leonidas Poulopoulos
            server = InstServer.objects.get(instid=inst, pk=server_pk)
361 edac002f Leonidas Poulopoulos
            form = InstServerForm(request_data, instance=server)
362 edac002f Leonidas Poulopoulos
        except InstServer.DoesNotExist:
363 edac002f Leonidas Poulopoulos
            form = InstServerForm(request_data)
364 f3bca340 Leonidas Poulopoulos
            if server_pk:
365 f3bca340 Leonidas Poulopoulos
                messages.add_message(request, messages.ERROR, 'You have no rights to edit this server')
366 f3bca340 Leonidas Poulopoulos
                return HttpResponseRedirect(reverse("servers")) 
367 edac002f Leonidas Poulopoulos
        
368 edac002f Leonidas Poulopoulos
        if form.is_valid():
369 edac002f Leonidas Poulopoulos
            instserverf = form.save()
370 edac002f Leonidas Poulopoulos
            return HttpResponseRedirect(reverse("servers"))
371 edac002f Leonidas Poulopoulos
        else:
372 edac002f Leonidas Poulopoulos
            form.fields['instid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None)
373 0f9fca26 Leonidas Poulopoulos
        if server:
374 0f9fca26 Leonidas Poulopoulos
            edit = True
375 0f9fca26 Leonidas Poulopoulos
        return render_to_response('edumanage/servers_edit.html', { 'institution': inst, 'form': form, 'edit': edit },
376 af9d484b Leonidas Poulopoulos
                                  context_instance=RequestContext(request, base_response(request)))
377 af9d484b Leonidas Poulopoulos
378 d163b72b Leonidas Poulopoulos
379 af9d484b Leonidas Poulopoulos
@login_required
380 d163b72b Leonidas Poulopoulos
@never_cache
381 7973b176 Leonidas Poulopoulos
def del_server(request):
382 7973b176 Leonidas Poulopoulos
    if request.method == 'GET':
383 7973b176 Leonidas Poulopoulos
        user = request.user
384 7973b176 Leonidas Poulopoulos
        req_data = request.GET.copy()
385 7973b176 Leonidas Poulopoulos
        server_pk = req_data['server_pk']
386 7973b176 Leonidas Poulopoulos
        try:
387 7973b176 Leonidas Poulopoulos
            profile = user.get_profile()
388 7973b176 Leonidas Poulopoulos
            institution = profile.institution
389 7973b176 Leonidas Poulopoulos
        except UserProfile.DoesNotExist:
390 7973b176 Leonidas Poulopoulos
            resp['error'] = "Could not delete server. Not enough rights"
391 7973b176 Leonidas Poulopoulos
            return HttpResponse(json.dumps(resp), mimetype='application/json')
392 7973b176 Leonidas Poulopoulos
        resp = {}
393 7973b176 Leonidas Poulopoulos
        try:
394 7973b176 Leonidas Poulopoulos
            server = InstServer.objects.get(instid=institution, pk=server_pk)
395 7973b176 Leonidas Poulopoulos
        except InstServer.DoesNotExist:
396 7973b176 Leonidas Poulopoulos
            resp['error'] = "Could not get server or you have no rights to delete"
397 7973b176 Leonidas Poulopoulos
            return HttpResponse(json.dumps(resp), mimetype='application/json')
398 7973b176 Leonidas Poulopoulos
        try:
399 7973b176 Leonidas Poulopoulos
            server.delete()
400 7973b176 Leonidas Poulopoulos
        except:
401 7973b176 Leonidas Poulopoulos
            resp['error'] = "Could not delete server"
402 7973b176 Leonidas Poulopoulos
            return HttpResponse(json.dumps(resp), mimetype='application/json')
403 7973b176 Leonidas Poulopoulos
        resp['success'] = "Server successfully deleted"
404 7973b176 Leonidas Poulopoulos
        return HttpResponse(json.dumps(resp), mimetype='application/json')
405 7973b176 Leonidas Poulopoulos
406 7973b176 Leonidas Poulopoulos
407 7973b176 Leonidas Poulopoulos
@login_required
408 d163b72b Leonidas Poulopoulos
@never_cache
409 04df82f7 Leonidas Poulopoulos
def realms(request):
410 04df82f7 Leonidas Poulopoulos
    user = request.user
411 04df82f7 Leonidas Poulopoulos
    servers = False
412 04df82f7 Leonidas Poulopoulos
    try:
413 04df82f7 Leonidas Poulopoulos
        profile = user.get_profile()
414 04df82f7 Leonidas Poulopoulos
        inst = profile.institution
415 04df82f7 Leonidas Poulopoulos
    except UserProfile.DoesNotExist:
416 244d3095 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
417 04df82f7 Leonidas Poulopoulos
    if inst:
418 04df82f7 Leonidas Poulopoulos
        realms = InstRealm.objects.filter(instid=inst)
419 c2cca19e Leonidas Poulopoulos
    if inst.ertype not in [1,3]:
420 fa2f119d Leonidas Poulopoulos
        messages.add_message(request, messages.ERROR, 'Cannot add/edit Realms. Your institution should be either IdP or IdP/SP')
421 fa2f119d Leonidas Poulopoulos
    return render_to_response('edumanage/realms.html', { 'realms': realms },
422 04df82f7 Leonidas Poulopoulos
                                  context_instance=RequestContext(request, base_response(request)))
423 04df82f7 Leonidas Poulopoulos
424 d163b72b Leonidas Poulopoulos
425 04df82f7 Leonidas Poulopoulos
@login_required
426 d163b72b Leonidas Poulopoulos
@never_cache
427 04df82f7 Leonidas Poulopoulos
def add_realm(request, realm_pk):
428 04df82f7 Leonidas Poulopoulos
    user = request.user
429 04df82f7 Leonidas Poulopoulos
    server = False
430 0f9fca26 Leonidas Poulopoulos
    realm = False
431 0f9fca26 Leonidas Poulopoulos
    edit = False
432 04df82f7 Leonidas Poulopoulos
    try:
433 04df82f7 Leonidas Poulopoulos
        profile = user.get_profile()
434 04df82f7 Leonidas Poulopoulos
        inst = profile.institution
435 7973b176 Leonidas Poulopoulos
        inst.__unicode__ = inst.get_name(request.LANGUAGE_CODE)
436 04df82f7 Leonidas Poulopoulos
    except UserProfile.DoesNotExist:
437 244d3095 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
438 7973b176 Leonidas Poulopoulos
    try:
439 7973b176 Leonidas Poulopoulos
        instdetails = inst.institutiondetails
440 7973b176 Leonidas Poulopoulos
    except InstitutionDetails.DoesNotExist:
441 7973b176 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
442 c2cca19e Leonidas Poulopoulos
    if inst.ertype not in [1,3]:
443 fa2f119d Leonidas Poulopoulos
        messages.add_message(request, messages.ERROR, 'Cannot add/edit Realm. Your institution should be either IdP or IdP/SP')
444 fa2f119d Leonidas Poulopoulos
        return render_to_response('edumanage/realms_edit.html', { 'edit': edit },
445 fa2f119d Leonidas Poulopoulos
                                  context_instance=RequestContext(request, base_response(request)))
446 04df82f7 Leonidas Poulopoulos
    if request.method == "GET":
447 04df82f7 Leonidas Poulopoulos
448 04df82f7 Leonidas Poulopoulos
        # Determine add or edit
449 04df82f7 Leonidas Poulopoulos
        try:         
450 04df82f7 Leonidas Poulopoulos
            realm = InstRealm.objects.get(instid=inst, pk=realm_pk)
451 04df82f7 Leonidas Poulopoulos
            form = InstRealmForm(instance=realm)
452 04df82f7 Leonidas Poulopoulos
        except InstRealm.DoesNotExist:
453 04df82f7 Leonidas Poulopoulos
            form = InstRealmForm()
454 f3bca340 Leonidas Poulopoulos
            if realm_pk:
455 f3bca340 Leonidas Poulopoulos
                messages.add_message(request, messages.ERROR, 'You have no rights to edit this realm')
456 f3bca340 Leonidas Poulopoulos
                return HttpResponseRedirect(reverse("realms"))
457 04df82f7 Leonidas Poulopoulos
        form.fields['instid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None)
458 04df82f7 Leonidas Poulopoulos
        form.fields['proxyto'] = forms.ModelMultipleChoiceField(queryset=InstServer.objects.filter(pk__in=getInstServers(inst)))
459 0f9fca26 Leonidas Poulopoulos
        if realm:
460 0f9fca26 Leonidas Poulopoulos
            edit = True
461 0f9fca26 Leonidas Poulopoulos
        return render_to_response('edumanage/realms_edit.html', { 'form': form, 'edit': edit },
462 04df82f7 Leonidas Poulopoulos
                                  context_instance=RequestContext(request, base_response(request)))
463 04df82f7 Leonidas Poulopoulos
    elif request.method == 'POST':
464 04df82f7 Leonidas Poulopoulos
        request_data = request.POST.copy()
465 04df82f7 Leonidas Poulopoulos
        try:         
466 04df82f7 Leonidas Poulopoulos
            realm = InstRealm.objects.get(instid=inst, pk=realm_pk)
467 04df82f7 Leonidas Poulopoulos
            form = InstRealmForm(request_data, instance=realm)
468 04df82f7 Leonidas Poulopoulos
        except InstRealm.DoesNotExist:
469 04df82f7 Leonidas Poulopoulos
            form = InstRealmForm(request_data)
470 f3bca340 Leonidas Poulopoulos
            if realm_pk:
471 f3bca340 Leonidas Poulopoulos
                messages.add_message(request, messages.ERROR, 'You have no rights to edit this realm')
472 f3bca340 Leonidas Poulopoulos
                return HttpResponseRedirect(reverse("realms")) 
473 04df82f7 Leonidas Poulopoulos
        if form.is_valid():
474 04df82f7 Leonidas Poulopoulos
            instserverf = form.save()
475 04df82f7 Leonidas Poulopoulos
            return HttpResponseRedirect(reverse("realms"))
476 04df82f7 Leonidas Poulopoulos
        else:
477 04df82f7 Leonidas Poulopoulos
            form.fields['instid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None)
478 04df82f7 Leonidas Poulopoulos
            form.fields['proxyto'] = forms.ModelMultipleChoiceField(queryset=InstServer.objects.filter(pk__in=getInstServers(inst)))
479 0f9fca26 Leonidas Poulopoulos
        if realm:
480 0f9fca26 Leonidas Poulopoulos
            edit = True
481 0f9fca26 Leonidas Poulopoulos
        return render_to_response('edumanage/realms_edit.html', { 'institution': inst, 'form': form, 'edit': edit },
482 04df82f7 Leonidas Poulopoulos
                                  context_instance=RequestContext(request, base_response(request)))
483 04df82f7 Leonidas Poulopoulos
484 04df82f7 Leonidas Poulopoulos
485 04df82f7 Leonidas Poulopoulos
@login_required
486 d163b72b Leonidas Poulopoulos
@never_cache
487 04df82f7 Leonidas Poulopoulos
def del_realm(request):
488 04df82f7 Leonidas Poulopoulos
    if request.method == 'GET':
489 04df82f7 Leonidas Poulopoulos
        user = request.user
490 04df82f7 Leonidas Poulopoulos
        req_data = request.GET.copy()
491 04df82f7 Leonidas Poulopoulos
        realm_pk = req_data['realm_pk']
492 244d3095 Leonidas Poulopoulos
        try:
493 244d3095 Leonidas Poulopoulos
            profile = user.get_profile()
494 244d3095 Leonidas Poulopoulos
            institution = profile.institution
495 244d3095 Leonidas Poulopoulos
        except UserProfile.DoesNotExist:
496 244d3095 Leonidas Poulopoulos
            resp['error'] = "Not enough rights"
497 244d3095 Leonidas Poulopoulos
            return HttpResponse(json.dumps(resp), mimetype='application/json')
498 04df82f7 Leonidas Poulopoulos
        resp = {}
499 04df82f7 Leonidas Poulopoulos
        try:
500 04df82f7 Leonidas Poulopoulos
            realm = InstRealm.objects.get(instid=institution, pk=realm_pk)
501 04df82f7 Leonidas Poulopoulos
        except InstRealm.DoesNotExist:
502 04df82f7 Leonidas Poulopoulos
            resp['error'] = "Could not get realm or you have no rights to delete"
503 04df82f7 Leonidas Poulopoulos
            return HttpResponse(json.dumps(resp), mimetype='application/json')
504 04df82f7 Leonidas Poulopoulos
        try:
505 04df82f7 Leonidas Poulopoulos
            realm.delete()
506 04df82f7 Leonidas Poulopoulos
        except:
507 04df82f7 Leonidas Poulopoulos
            resp['error'] = "Could not delete realm"
508 04df82f7 Leonidas Poulopoulos
            return HttpResponse(json.dumps(resp), mimetype='application/json')
509 04df82f7 Leonidas Poulopoulos
        resp['success'] = "Realm successfully deleted"
510 04df82f7 Leonidas Poulopoulos
        return HttpResponse(json.dumps(resp), mimetype='application/json')
511 04df82f7 Leonidas Poulopoulos
512 04df82f7 Leonidas Poulopoulos
513 c2b3014a Leonidas Poulopoulos
@login_required
514 d163b72b Leonidas Poulopoulos
@never_cache
515 c2b3014a Leonidas Poulopoulos
def contacts(request):
516 c2b3014a Leonidas Poulopoulos
    user = request.user
517 c2b3014a Leonidas Poulopoulos
    servers = False
518 c2b3014a Leonidas Poulopoulos
    instcontacts = []
519 c2b3014a Leonidas Poulopoulos
    try:
520 c2b3014a Leonidas Poulopoulos
        profile = user.get_profile()
521 c2b3014a Leonidas Poulopoulos
        inst = profile.institution
522 7973b176 Leonidas Poulopoulos
        inst.__unicode__ = inst.get_name(request.LANGUAGE_CODE)
523 c2b3014a Leonidas Poulopoulos
    except UserProfile.DoesNotExist:
524 244d3095 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
525 7973b176 Leonidas Poulopoulos
    try:
526 7973b176 Leonidas Poulopoulos
        instdetails = inst.institutiondetails
527 7973b176 Leonidas Poulopoulos
    except InstitutionDetails.DoesNotExist:
528 7973b176 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
529 c2b3014a Leonidas Poulopoulos
    if inst:
530 c2b3014a Leonidas Poulopoulos
        instcontacts.extend([x.contact.pk for x in InstitutionContactPool.objects.filter(institution=inst)])
531 c2b3014a Leonidas Poulopoulos
        contacts = Contact.objects.filter(pk__in=instcontacts)
532 c2b3014a Leonidas Poulopoulos
    return render_to_response('edumanage/contacts.html', { 'contacts': contacts},
533 c2b3014a Leonidas Poulopoulos
                                  context_instance=RequestContext(request, base_response(request)))
534 c2b3014a Leonidas Poulopoulos
535 d163b72b Leonidas Poulopoulos
536 c2b3014a Leonidas Poulopoulos
@login_required
537 d163b72b Leonidas Poulopoulos
@never_cache
538 c2b3014a Leonidas Poulopoulos
def add_contact(request, contact_pk):
539 c2b3014a Leonidas Poulopoulos
    user = request.user
540 c2b3014a Leonidas Poulopoulos
    server = False
541 0f9fca26 Leonidas Poulopoulos
    edit = False
542 0f9fca26 Leonidas Poulopoulos
    contact = False
543 c2b3014a Leonidas Poulopoulos
    try:
544 c2b3014a Leonidas Poulopoulos
        profile = user.get_profile()
545 c2b3014a Leonidas Poulopoulos
        inst = profile.institution
546 7973b176 Leonidas Poulopoulos
        inst.__unicode__ = inst.get_name(request.LANGUAGE_CODE)
547 c2b3014a Leonidas Poulopoulos
    except UserProfile.DoesNotExist:
548 7973b176 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
549 7973b176 Leonidas Poulopoulos
    try:
550 7973b176 Leonidas Poulopoulos
        instdetails = inst.institutiondetails
551 7973b176 Leonidas Poulopoulos
    except InstitutionDetails.DoesNotExist:
552 244d3095 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
553 c2b3014a Leonidas Poulopoulos
    if request.method == "GET":
554 c2b3014a Leonidas Poulopoulos
555 c2b3014a Leonidas Poulopoulos
        # Determine add or edit
556 c2b3014a Leonidas Poulopoulos
        try:         
557 c2b3014a Leonidas Poulopoulos
            contactinst = InstitutionContactPool.objects.get(institution=inst, contact__pk=contact_pk)
558 c2b3014a Leonidas Poulopoulos
            contact = contactinst.contact
559 c2b3014a Leonidas Poulopoulos
            form = ContactForm(instance=contact)
560 c2b3014a Leonidas Poulopoulos
        except InstitutionContactPool.DoesNotExist:
561 c2b3014a Leonidas Poulopoulos
            form = ContactForm()
562 f3bca340 Leonidas Poulopoulos
            if contact_pk:
563 f3bca340 Leonidas Poulopoulos
                messages.add_message(request, messages.ERROR, 'You have no rights to edit this contact')
564 f3bca340 Leonidas Poulopoulos
                return HttpResponseRedirect(reverse("contacts"))
565 0f9fca26 Leonidas Poulopoulos
        if contact:
566 0f9fca26 Leonidas Poulopoulos
            edit = True
567 0f9fca26 Leonidas Poulopoulos
        return render_to_response('edumanage/contacts_edit.html', { 'form': form, "edit" : edit},
568 c2b3014a Leonidas Poulopoulos
                                  context_instance=RequestContext(request, base_response(request)))
569 c2b3014a Leonidas Poulopoulos
    elif request.method == 'POST':
570 c2b3014a Leonidas Poulopoulos
        request_data = request.POST.copy()
571 c2b3014a Leonidas Poulopoulos
        try:         
572 c2b3014a Leonidas Poulopoulos
            contactinst = InstitutionContactPool.objects.get(institution=inst, contact__pk=contact_pk)
573 c2b3014a Leonidas Poulopoulos
            contact = contactinst.contact
574 c2b3014a Leonidas Poulopoulos
            form = ContactForm(request_data, instance=contact)
575 c2b3014a Leonidas Poulopoulos
        except InstitutionContactPool.DoesNotExist:
576 c2b3014a Leonidas Poulopoulos
            form = ContactForm(request_data)
577 f3bca340 Leonidas Poulopoulos
            if contact_pk:
578 f3bca340 Leonidas Poulopoulos
                messages.add_message(request, messages.ERROR, 'You have no rights to edit this contact')
579 f3bca340 Leonidas Poulopoulos
                return HttpResponseRedirect(reverse("contacts"))
580 c2b3014a Leonidas Poulopoulos
        
581 c2b3014a Leonidas Poulopoulos
        if form.is_valid():
582 c2b3014a Leonidas Poulopoulos
            contact = form.save()
583 c2b3014a Leonidas Poulopoulos
            instContPool, created = InstitutionContactPool.objects.get_or_create(contact=contact, institution=inst)
584 c2b3014a Leonidas Poulopoulos
            instContPool.save()
585 c2b3014a Leonidas Poulopoulos
            return HttpResponseRedirect(reverse("contacts"))
586 0f9fca26 Leonidas Poulopoulos
        if contact:
587 0f9fca26 Leonidas Poulopoulos
            edit = True
588 0f9fca26 Leonidas Poulopoulos
        return render_to_response('edumanage/contacts_edit.html', { 'form': form, "edit": edit},
589 c2b3014a Leonidas Poulopoulos
                                  context_instance=RequestContext(request, base_response(request)))
590 c2b3014a Leonidas Poulopoulos
591 c2b3014a Leonidas Poulopoulos
592 c2b3014a Leonidas Poulopoulos
@login_required
593 d163b72b Leonidas Poulopoulos
@never_cache
594 c2b3014a Leonidas Poulopoulos
def del_contact(request):
595 c2b3014a Leonidas Poulopoulos
    if request.method == 'GET':
596 c2b3014a Leonidas Poulopoulos
        user = request.user
597 c2b3014a Leonidas Poulopoulos
        req_data = request.GET.copy()
598 c2b3014a Leonidas Poulopoulos
        contact_pk = req_data['contact_pk']
599 244d3095 Leonidas Poulopoulos
        try:
600 244d3095 Leonidas Poulopoulos
            profile = user.get_profile()
601 244d3095 Leonidas Poulopoulos
            institution = profile.institution
602 244d3095 Leonidas Poulopoulos
        except UserProfile.DoesNotExist:
603 244d3095 Leonidas Poulopoulos
            resp['error'] = "Could not delete contact. Not enough rights"
604 244d3095 Leonidas Poulopoulos
            return HttpResponse(json.dumps(resp), mimetype='application/json')
605 c2b3014a Leonidas Poulopoulos
        resp = {}
606 c2b3014a Leonidas Poulopoulos
        try:
607 c2b3014a Leonidas Poulopoulos
            contactinst = InstitutionContactPool.objects.get(institution=institution, contact__pk=contact_pk)
608 c2b3014a Leonidas Poulopoulos
            contact = contactinst.contact
609 c2b3014a Leonidas Poulopoulos
        except InstitutionContactPool.DoesNotExist:
610 c2b3014a Leonidas Poulopoulos
            resp['error'] = "Could not get contact or you have no rights to delete"
611 c2b3014a Leonidas Poulopoulos
            return HttpResponse(json.dumps(resp), mimetype='application/json')
612 c2b3014a Leonidas Poulopoulos
        try:
613 c2b3014a Leonidas Poulopoulos
            for service in ServiceLoc.objects.filter(institutionid=institution):
614 c2b3014a Leonidas Poulopoulos
                if (contact in service.contact.all() and len(service.contact.all()) == 1):
615 c2b3014a Leonidas Poulopoulos
                    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")
616 c2b3014a Leonidas Poulopoulos
                    return HttpResponse(json.dumps(resp), mimetype='application/json')
617 c2b3014a Leonidas Poulopoulos
            if (contact in institution.institutiondetails.contact.all() and len(institution.institutiondetails.contact.all()) == 1):
618 c2b3014a Leonidas Poulopoulos
                    resp['error'] = "Could not delete contact. It is the only contact your institution.<br>Fix it and try again"
619 c2b3014a Leonidas Poulopoulos
                    return HttpResponse(json.dumps(resp), mimetype='application/json')
620 c2b3014a Leonidas Poulopoulos
            contact.delete()
621 c2b3014a Leonidas Poulopoulos
        except Exception:
622 c2b3014a Leonidas Poulopoulos
            resp['error'] = "Could not delete contact"
623 c2b3014a Leonidas Poulopoulos
            return HttpResponse(json.dumps(resp), mimetype='application/json')
624 c2b3014a Leonidas Poulopoulos
        resp['success'] = "Contact successfully deleted"
625 c2b3014a Leonidas Poulopoulos
        return HttpResponse(json.dumps(resp), mimetype='application/json')
626 d163b72b Leonidas Poulopoulos
627 d163b72b Leonidas Poulopoulos
628 7973b176 Leonidas Poulopoulos
@login_required
629 d163b72b Leonidas Poulopoulos
@never_cache
630 9db05e62 Leonidas Poulopoulos
def instrealmmon(request):
631 9db05e62 Leonidas Poulopoulos
    user = request.user
632 9db05e62 Leonidas Poulopoulos
    servers = False
633 9db05e62 Leonidas Poulopoulos
    instcontacts = []
634 9db05e62 Leonidas Poulopoulos
    try:
635 9db05e62 Leonidas Poulopoulos
        profile = user.get_profile()
636 9db05e62 Leonidas Poulopoulos
        inst = profile.institution
637 9db05e62 Leonidas Poulopoulos
        inst.__unicode__ = inst.get_name(request.LANGUAGE_CODE)
638 9db05e62 Leonidas Poulopoulos
    except UserProfile.DoesNotExist:
639 9db05e62 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
640 9db05e62 Leonidas Poulopoulos
    try:
641 9db05e62 Leonidas Poulopoulos
        instdetails = inst.institutiondetails
642 9db05e62 Leonidas Poulopoulos
    except InstitutionDetails.DoesNotExist:
643 9db05e62 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
644 9db05e62 Leonidas Poulopoulos
    if inst:
645 9db05e62 Leonidas Poulopoulos
        instrealmmons = InstRealmMon.objects.filter(realm__instid=inst)
646 9db05e62 Leonidas Poulopoulos
    return render_to_response('edumanage/instrealmmons.html', { 'realms': instrealmmons},
647 9db05e62 Leonidas Poulopoulos
                                  context_instance=RequestContext(request, base_response(request)))
648 9db05e62 Leonidas Poulopoulos
649 9db05e62 Leonidas Poulopoulos
@login_required
650 9db05e62 Leonidas Poulopoulos
@never_cache
651 9db05e62 Leonidas Poulopoulos
def add_instrealmmon(request, instrealmmon_pk):
652 9db05e62 Leonidas Poulopoulos
    user = request.user
653 9db05e62 Leonidas Poulopoulos
    instrealmmon = False
654 9db05e62 Leonidas Poulopoulos
    edit = False
655 9db05e62 Leonidas Poulopoulos
    try:
656 9db05e62 Leonidas Poulopoulos
        profile = user.get_profile()
657 9db05e62 Leonidas Poulopoulos
        inst = profile.institution
658 9db05e62 Leonidas Poulopoulos
        inst.__unicode__ = inst.get_name(request.LANGUAGE_CODE)
659 9db05e62 Leonidas Poulopoulos
    except UserProfile.DoesNotExist:
660 9db05e62 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
661 9db05e62 Leonidas Poulopoulos
    try:
662 9db05e62 Leonidas Poulopoulos
        instdetails = inst.institutiondetails
663 9db05e62 Leonidas Poulopoulos
    except InstitutionDetails.DoesNotExist:
664 9db05e62 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
665 9db05e62 Leonidas Poulopoulos
    if request.method == "GET":
666 9db05e62 Leonidas Poulopoulos
        # Determine add or edit
667 9db05e62 Leonidas Poulopoulos
        try:         
668 9db05e62 Leonidas Poulopoulos
            instrealmmon = InstRealmMon.objects.get(pk=instrealmmon_pk, realm__instid=inst)
669 9db05e62 Leonidas Poulopoulos
            form = InstRealmMonForm(instance=instrealmmon)
670 9db05e62 Leonidas Poulopoulos
        except InstRealmMon.DoesNotExist:
671 9db05e62 Leonidas Poulopoulos
            form = InstRealmMonForm()
672 f3bca340 Leonidas Poulopoulos
            if instrealmmon_pk:
673 f3bca340 Leonidas Poulopoulos
                messages.add_message(request, messages.ERROR, 'You have no rights to edit this Monitoring Realm')
674 f3bca340 Leonidas Poulopoulos
                return HttpResponseRedirect(reverse("instrealmmon"))
675 9db05e62 Leonidas Poulopoulos
        if instrealmmon:
676 9db05e62 Leonidas Poulopoulos
            edit = True
677 9db05e62 Leonidas Poulopoulos
        form.fields['realm'] = forms.ModelChoiceField(queryset=InstRealm.objects.filter(instid=inst.pk).exclude(realm__startswith="*"), empty_label=None)
678 9db05e62 Leonidas Poulopoulos
        return render_to_response('edumanage/instrealmmon_edit.html', { 'form': form, "edit" : edit},
679 9db05e62 Leonidas Poulopoulos
                                  context_instance=RequestContext(request, base_response(request)))
680 9db05e62 Leonidas Poulopoulos
    elif request.method == 'POST':
681 9db05e62 Leonidas Poulopoulos
        request_data = request.POST.copy()
682 9db05e62 Leonidas Poulopoulos
        try:         
683 9db05e62 Leonidas Poulopoulos
            instrealmmon = InstRealmMon.objects.get(pk=instrealmmon_pk, realm__instid=inst)
684 9db05e62 Leonidas Poulopoulos
            form = InstRealmMonForm(request_data, instance=instrealmmon)
685 9db05e62 Leonidas Poulopoulos
        except InstRealmMon.DoesNotExist:
686 9db05e62 Leonidas Poulopoulos
            form = InstRealmMonForm(request_data)
687 f3bca340 Leonidas Poulopoulos
            if instrealmmon_pk:
688 f3bca340 Leonidas Poulopoulos
                messages.add_message(request, messages.ERROR, 'You have no rights to edit this Monitoring Realm')
689 f3bca340 Leonidas Poulopoulos
                return HttpResponseRedirect(reverse("instrealmmon"))
690 9db05e62 Leonidas Poulopoulos
        if form.is_valid():
691 9db05e62 Leonidas Poulopoulos
            instrealmmonobj = form.save()
692 9db05e62 Leonidas Poulopoulos
            return HttpResponseRedirect(reverse("instrealmmon"))
693 9db05e62 Leonidas Poulopoulos
        if instrealmmon:
694 9db05e62 Leonidas Poulopoulos
            edit = True
695 450099e1 Leonidas Poulopoulos
        form.fields['realm'] = forms.ModelChoiceField(queryset=InstRealm.objects.filter(instid=inst.pk).exclude(realm__startswith="*"), empty_label=None)
696 9db05e62 Leonidas Poulopoulos
        return render_to_response('edumanage/instrealmmon_edit.html', { 'form': form, "edit": edit},
697 9db05e62 Leonidas Poulopoulos
                                  context_instance=RequestContext(request, base_response(request)))
698 9db05e62 Leonidas Poulopoulos
699 450099e1 Leonidas Poulopoulos
@login_required
700 450099e1 Leonidas Poulopoulos
@never_cache
701 450099e1 Leonidas Poulopoulos
def del_instrealmmon(request):
702 450099e1 Leonidas Poulopoulos
    if request.method == 'GET':
703 450099e1 Leonidas Poulopoulos
        user = request.user
704 450099e1 Leonidas Poulopoulos
        req_data = request.GET.copy()
705 450099e1 Leonidas Poulopoulos
        instrealmmon_pk = req_data['instrealmmon_pk']
706 450099e1 Leonidas Poulopoulos
        try:
707 450099e1 Leonidas Poulopoulos
            profile = user.get_profile()
708 450099e1 Leonidas Poulopoulos
            institution = profile.institution
709 450099e1 Leonidas Poulopoulos
        except UserProfile.DoesNotExist:
710 450099e1 Leonidas Poulopoulos
            resp['error'] = "Could not delete monitored realm. Not enough rights"
711 450099e1 Leonidas Poulopoulos
            return HttpResponse(json.dumps(resp), mimetype='application/json')
712 450099e1 Leonidas Poulopoulos
        resp = {}
713 450099e1 Leonidas Poulopoulos
        try:
714 450099e1 Leonidas Poulopoulos
            instrealmmon = InstRealmMon.objects.get(pk=instrealmmon_pk, realm__instid=institution)
715 450099e1 Leonidas Poulopoulos
            instrealmmon.delete()
716 450099e1 Leonidas Poulopoulos
        except InstRealmMon.DoesNotExist:
717 450099e1 Leonidas Poulopoulos
            resp['error'] = "Could not get monitored realm or you have no rights to delete"
718 450099e1 Leonidas Poulopoulos
            return HttpResponse(json.dumps(resp), mimetype='application/json')
719 450099e1 Leonidas Poulopoulos
        resp['success'] = "Contact successfully deleted"
720 450099e1 Leonidas Poulopoulos
        return HttpResponse(json.dumps(resp), mimetype='application/json')
721 9db05e62 Leonidas Poulopoulos
722 9db05e62 Leonidas Poulopoulos
@login_required
723 9db05e62 Leonidas Poulopoulos
@never_cache
724 9db05e62 Leonidas Poulopoulos
def add_monlocauthpar(request, instrealmmon_pk, monlocauthpar_pk):
725 9db05e62 Leonidas Poulopoulos
    user = request.user
726 9db05e62 Leonidas Poulopoulos
    monlocauthpar = False
727 9db05e62 Leonidas Poulopoulos
    edit = False
728 9db05e62 Leonidas Poulopoulos
    try:
729 9db05e62 Leonidas Poulopoulos
        profile = user.get_profile()
730 9db05e62 Leonidas Poulopoulos
        inst = profile.institution
731 9db05e62 Leonidas Poulopoulos
        inst.__unicode__ = inst.get_name(request.LANGUAGE_CODE)
732 9db05e62 Leonidas Poulopoulos
    except UserProfile.DoesNotExist:
733 9db05e62 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
734 9db05e62 Leonidas Poulopoulos
    try:
735 9db05e62 Leonidas Poulopoulos
        instdetails = inst.institutiondetails
736 9db05e62 Leonidas Poulopoulos
    except InstitutionDetails.DoesNotExist:
737 9db05e62 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
738 9db05e62 Leonidas Poulopoulos
    if request.method == "GET":
739 9db05e62 Leonidas Poulopoulos
        # Determine add or edit
740 9db05e62 Leonidas Poulopoulos
        try:
741 9db05e62 Leonidas Poulopoulos
            instrealmmon = InstRealmMon.objects.get(pk=instrealmmon_pk, realm__instid=inst)
742 9db05e62 Leonidas Poulopoulos
            monlocauthpar = MonLocalAuthnParam.objects.get(pk=monlocauthpar_pk, instrealmmonid__realm__instid=inst)
743 9db05e62 Leonidas Poulopoulos
            form = MonLocalAuthnParamForm(instance=monlocauthpar)
744 9db05e62 Leonidas Poulopoulos
        except MonLocalAuthnParam.DoesNotExist:
745 9db05e62 Leonidas Poulopoulos
            form = MonLocalAuthnParamForm()
746 f3bca340 Leonidas Poulopoulos
            if monlocauthpar_pk:
747 f3bca340 Leonidas Poulopoulos
                messages.add_message(request, messages.ERROR, 'You have no rights to edit this Monitoring Realm Parameters')
748 f3bca340 Leonidas Poulopoulos
                return HttpResponseRedirect(reverse("instrealmmon"))
749 9db05e62 Leonidas Poulopoulos
        except InstRealmMon.DoesNotExist:
750 f3bca340 Leonidas Poulopoulos
            if instrealmmon_pk:
751 f3bca340 Leonidas Poulopoulos
                messages.add_message(request, messages.ERROR, 'You have no rights to edit this Monitoring Realm Parameters')
752 f3bca340 Leonidas Poulopoulos
            return HttpResponseRedirect(reverse("instrealmmon"))
753 9db05e62 Leonidas Poulopoulos
        if monlocauthpar:
754 9db05e62 Leonidas Poulopoulos
            edit = True
755 9db05e62 Leonidas Poulopoulos
        form.fields['instrealmmonid'] = forms.ModelChoiceField(queryset=InstRealmMon.objects.filter(pk=instrealmmon.pk), empty_label=None)
756 9db05e62 Leonidas Poulopoulos
        return render_to_response('edumanage/monlocauthpar_edit.html', { 'form': form, "edit" : edit, "realm":instrealmmon },
757 9db05e62 Leonidas Poulopoulos
                                  context_instance=RequestContext(request, base_response(request)))
758 9db05e62 Leonidas Poulopoulos
    elif request.method == 'POST':
759 9db05e62 Leonidas Poulopoulos
        request_data = request.POST.copy()
760 9db05e62 Leonidas Poulopoulos
        try:         
761 9db05e62 Leonidas Poulopoulos
            instrealmmon = InstRealmMon.objects.get(pk=instrealmmon_pk, realm__instid=inst)
762 9db05e62 Leonidas Poulopoulos
            monlocauthpar = MonLocalAuthnParam.objects.get(pk=monlocauthpar_pk, instrealmmonid__realm__instid=inst)
763 9db05e62 Leonidas Poulopoulos
            form = MonLocalAuthnParamForm(request_data, instance=monlocauthpar)
764 9db05e62 Leonidas Poulopoulos
        except MonLocalAuthnParam.DoesNotExist:
765 9db05e62 Leonidas Poulopoulos
            form = MonLocalAuthnParamForm(request_data)
766 f3bca340 Leonidas Poulopoulos
            if monlocauthpar_pk:
767 f3bca340 Leonidas Poulopoulos
                messages.add_message(request, messages.ERROR, 'You have no rights to edit this Monitoring Realm Parameters')
768 f3bca340 Leonidas Poulopoulos
                return HttpResponseRedirect(reverse("instrealmmon"))
769 9db05e62 Leonidas Poulopoulos
        except InstRealmMon.DoesNotExist:
770 f3bca340 Leonidas Poulopoulos
            if instrealmmon_pk:
771 f3bca340 Leonidas Poulopoulos
                messages.add_message(request, messages.ERROR, 'You have no rights to edit this Monitoring Realm Parameters')
772 f3bca340 Leonidas Poulopoulos
            return HttpResponseRedirect(reverse("instrealmmon"))
773 9db05e62 Leonidas Poulopoulos
        if form.is_valid():
774 9db05e62 Leonidas Poulopoulos
            monlocauthparobj = form.save()
775 9db05e62 Leonidas Poulopoulos
            return HttpResponseRedirect(reverse("instrealmmon"))
776 9db05e62 Leonidas Poulopoulos
        if monlocauthpar:
777 9db05e62 Leonidas Poulopoulos
            edit = True
778 9db05e62 Leonidas Poulopoulos
        form.fields['instrealmmonid'] = forms.ModelChoiceField(queryset=InstRealmMon.objects.filter(pk=instrealmmon.pk), empty_label=None)
779 9db05e62 Leonidas Poulopoulos
        return render_to_response('edumanage/monlocauthpar_edit.html', { 'form': form, "edit": edit, "realm":instrealmmon},
780 9db05e62 Leonidas Poulopoulos
                                  context_instance=RequestContext(request, base_response(request)))
781 9db05e62 Leonidas Poulopoulos
782 9db05e62 Leonidas Poulopoulos
@login_required
783 9db05e62 Leonidas Poulopoulos
@never_cache
784 450099e1 Leonidas Poulopoulos
def del_monlocauthpar(request):
785 450099e1 Leonidas Poulopoulos
    if request.method == 'GET':
786 450099e1 Leonidas Poulopoulos
        user = request.user
787 450099e1 Leonidas Poulopoulos
        req_data = request.GET.copy()
788 450099e1 Leonidas Poulopoulos
        monlocauthpar_pk = req_data['monlocauthpar_pk']
789 450099e1 Leonidas Poulopoulos
        try:
790 450099e1 Leonidas Poulopoulos
            profile = user.get_profile()
791 450099e1 Leonidas Poulopoulos
            institution = profile.institution
792 450099e1 Leonidas Poulopoulos
        except UserProfile.DoesNotExist:
793 450099e1 Leonidas Poulopoulos
            resp['error'] = "Could not delete realm monitoring parameters. Not enough rights"
794 450099e1 Leonidas Poulopoulos
            return HttpResponse(json.dumps(resp), mimetype='application/json')
795 450099e1 Leonidas Poulopoulos
        resp = {}
796 450099e1 Leonidas Poulopoulos
        try:
797 450099e1 Leonidas Poulopoulos
            monlocauthpar = MonLocalAuthnParam.objects.get(pk=monlocauthpar_pk, instrealmmonid__realm__instid=institution)
798 450099e1 Leonidas Poulopoulos
            monlocauthpar.delete()
799 450099e1 Leonidas Poulopoulos
        except MonLocalAuthnParam.DoesNotExist:
800 450099e1 Leonidas Poulopoulos
            resp['error'] = "Could not get realm monitoring parameters or you have no rights to delete"
801 450099e1 Leonidas Poulopoulos
            return HttpResponse(json.dumps(resp), mimetype='application/json')
802 450099e1 Leonidas Poulopoulos
        resp['success'] = "Contact successfully deleted"
803 450099e1 Leonidas Poulopoulos
        return HttpResponse(json.dumps(resp), mimetype='application/json')
804 450099e1 Leonidas Poulopoulos
805 450099e1 Leonidas Poulopoulos
@login_required
806 450099e1 Leonidas Poulopoulos
@never_cache
807 7973b176 Leonidas Poulopoulos
def adduser(request):
808 7973b176 Leonidas Poulopoulos
    user = request.user
809 7973b176 Leonidas Poulopoulos
    try:
810 7973b176 Leonidas Poulopoulos
        profile = user.get_profile()
811 7973b176 Leonidas Poulopoulos
        inst = profile.institution
812 7973b176 Leonidas Poulopoulos
        inst.__unicode__ = inst.get_name(request.LANGUAGE_CODE)
813 7973b176 Leonidas Poulopoulos
    except UserProfile.DoesNotExist:
814 7973b176 Leonidas Poulopoulos
        return HttpResponseRedirect(reverse("manage"))
815 c2b3014a Leonidas Poulopoulos
816 7973b176 Leonidas Poulopoulos
    if request.method == "GET":
817 7973b176 Leonidas Poulopoulos
        form = ContactForm()
818 7973b176 Leonidas Poulopoulos
        return render_to_response('edumanage/add_user.html', { 'form' : form },
819 7973b176 Leonidas Poulopoulos
                                  context_instance=RequestContext(request, base_response(request)))
820 7973b176 Leonidas Poulopoulos
    elif request.method == 'POST':
821 7973b176 Leonidas Poulopoulos
        request_data = request.POST.copy()
822 7973b176 Leonidas Poulopoulos
        form = ContactForm(request_data)
823 7973b176 Leonidas Poulopoulos
        if form.is_valid():
824 7973b176 Leonidas Poulopoulos
            contact = form.save()
825 7973b176 Leonidas Poulopoulos
            instContPool = InstitutionContactPool(contact=contact, institution=inst)
826 7973b176 Leonidas Poulopoulos
            instContPool.save()
827 7973b176 Leonidas Poulopoulos
            response_data = {}
828 7973b176 Leonidas Poulopoulos
            response_data['value'] = "%s" %contact.pk
829 7973b176 Leonidas Poulopoulos
            response_data['text'] = "%s" %contact
830 7973b176 Leonidas Poulopoulos
            return HttpResponse(json.dumps(response_data), mimetype='application/json')
831 7973b176 Leonidas Poulopoulos
        else:
832 7973b176 Leonidas Poulopoulos
            return render_to_response('edumanage/add_user.html', {'form': form,},
833 7973b176 Leonidas Poulopoulos
                                      context_instance=RequestContext(request, base_response(request)))
834 c2b3014a Leonidas Poulopoulos
835 d163b72b Leonidas Poulopoulos
836 04df82f7 Leonidas Poulopoulos
@login_required
837 af9d484b Leonidas Poulopoulos
def base_response(request):
838 af9d484b Leonidas Poulopoulos
    user = request.user
839 af9d484b Leonidas Poulopoulos
    inst = []
840 af9d484b Leonidas Poulopoulos
    server = []
841 af9d484b Leonidas Poulopoulos
    services = []
842 04df82f7 Leonidas Poulopoulos
    instrealms = []
843 c2b3014a Leonidas Poulopoulos
    instcontacts = []
844 c2b3014a Leonidas Poulopoulos
    contacts = []
845 244d3095 Leonidas Poulopoulos
    institution = False
846 244d3095 Leonidas Poulopoulos
    institution_exists = False
847 af9d484b Leonidas Poulopoulos
    try:
848 af9d484b Leonidas Poulopoulos
        profile = user.get_profile()
849 af9d484b Leonidas Poulopoulos
        institution = profile.institution
850 244d3095 Leonidas Poulopoulos
        institution_exists = True
851 244d3095 Leonidas Poulopoulos
    except UserProfile.DoesNotExist:
852 244d3095 Leonidas Poulopoulos
        institution_exists = False
853 244d3095 Leonidas Poulopoulos
    try:
854 af9d484b Leonidas Poulopoulos
        inst.append(institution)
855 af9d484b Leonidas Poulopoulos
        server = InstServer.objects.filter(instid=institution)
856 af9d484b Leonidas Poulopoulos
        services = ServiceLoc.objects.filter(institutionid=institution)
857 04df82f7 Leonidas Poulopoulos
        instrealms = InstRealm.objects.filter(instid=institution)
858 c2b3014a Leonidas Poulopoulos
        instcontacts.extend([x.contact.pk for x in InstitutionContactPool.objects.filter(institution=institution)])
859 c2b3014a Leonidas Poulopoulos
        contacts = Contact.objects.filter(pk__in=instcontacts)
860 9db05e62 Leonidas Poulopoulos
        instrealmmons = InstRealmMon.objects.filter(realm__instid=institution)
861 244d3095 Leonidas Poulopoulos
    except:
862 af9d484b Leonidas Poulopoulos
        pass
863 7973b176 Leonidas Poulopoulos
    try:
864 7973b176 Leonidas Poulopoulos
        instututiondetails = institution.institutiondetails
865 7973b176 Leonidas Poulopoulos
    except:
866 7973b176 Leonidas Poulopoulos
        instututiondetails = False
867 af9d484b Leonidas Poulopoulos
    return { 
868 af9d484b Leonidas Poulopoulos
            'inst_num': len(inst),
869 af9d484b Leonidas Poulopoulos
            'servers_num': len(server),
870 af9d484b Leonidas Poulopoulos
            'services_num': len(services),
871 04df82f7 Leonidas Poulopoulos
            'realms_num': len(instrealms),
872 c2b3014a Leonidas Poulopoulos
            'contacts_num': len(contacts),
873 9db05e62 Leonidas Poulopoulos
            'monrealms_num': len(instrealmmons),
874 7973b176 Leonidas Poulopoulos
            'institution': institution,
875 7973b176 Leonidas Poulopoulos
            'institutiondetails': instututiondetails,
876 244d3095 Leonidas Poulopoulos
            'institution_exists': institution_exists,
877 af9d484b Leonidas Poulopoulos
            
878 af9d484b Leonidas Poulopoulos
        }
879 af9d484b Leonidas Poulopoulos
880 7973b176 Leonidas Poulopoulos
881 2398cbad Leonidas Poulopoulos
@login_required
882 d163b72b Leonidas Poulopoulos
@never_cache
883 7973b176 Leonidas Poulopoulos
def get_service_points(request):
884 7973b176 Leonidas Poulopoulos
    if request.method == "GET":
885 2398cbad Leonidas Poulopoulos
        user = request.user
886 244d3095 Leonidas Poulopoulos
        try:
887 244d3095 Leonidas Poulopoulos
            profile = user.get_profile()
888 7973b176 Leonidas Poulopoulos
            inst = profile.institution
889 244d3095 Leonidas Poulopoulos
        except UserProfile.DoesNotExist:
890 7973b176 Leonidas Poulopoulos
            inst = False
891 7973b176 Leonidas Poulopoulos
            return HttpResponseNotFound('<h1>Something went really wrong</h1>')
892 7973b176 Leonidas Poulopoulos
        servicelocs = ServiceLoc.objects.filter(institutionid=inst)
893 7973b176 Leonidas Poulopoulos
        
894 7973b176 Leonidas Poulopoulos
        locs = []
895 7973b176 Leonidas Poulopoulos
        for sl in servicelocs:
896 7973b176 Leonidas Poulopoulos
            response_location = {}
897 7973b176 Leonidas Poulopoulos
            response_location['lat'] = u"%s"%sl.latitude
898 7973b176 Leonidas Poulopoulos
            response_location['lng'] = u"%s"%sl.longitude
899 7973b176 Leonidas Poulopoulos
            response_location['address'] = u"%s<br>%s"%(sl.address_street, sl.address_city)
900 27d6f3e2 Leonidas Poulopoulos
            if len(sl.enc_level[0]) != 0:
901 27d6f3e2 Leonidas Poulopoulos
                response_location['enc'] = u"%s"%(','.join(sl.enc_level))
902 27d6f3e2 Leonidas Poulopoulos
            else:
903 27d6f3e2 Leonidas Poulopoulos
                response_location['enc'] = u"-"
904 7973b176 Leonidas Poulopoulos
            response_location['AP_no'] = u"%s"%(sl.AP_no)
905 7973b176 Leonidas Poulopoulos
            response_location['name'] = sl.loc_name.get(lang='en').name
906 7973b176 Leonidas Poulopoulos
            response_location['port_restrict'] = u"%s"%(sl.port_restrict)
907 7973b176 Leonidas Poulopoulos
            response_location['transp_proxy'] = u"%s"%(sl.transp_proxy)
908 7973b176 Leonidas Poulopoulos
            response_location['IPv6'] = u"%s"%(sl.IPv6)
909 7973b176 Leonidas Poulopoulos
            response_location['NAT'] = u"%s"%(sl.NAT)
910 7973b176 Leonidas Poulopoulos
            response_location['wired'] = u"%s"%(sl.wired)
911 7973b176 Leonidas Poulopoulos
            response_location['SSID'] = u"%s"%(sl.SSID)
912 7973b176 Leonidas Poulopoulos
            response_location['key'] = u"%s"%sl.pk
913 7973b176 Leonidas Poulopoulos
            locs.append(response_location)
914 7973b176 Leonidas Poulopoulos
        return HttpResponse(json.dumps(locs), mimetype='application/json')
915 7973b176 Leonidas Poulopoulos
    else:
916 7973b176 Leonidas Poulopoulos
       return HttpResponseNotFound('<h1>Something went really wrong</h1>')
917 7973b176 Leonidas Poulopoulos
918 d163b72b Leonidas Poulopoulos
@never_cache
919 7973b176 Leonidas Poulopoulos
def get_all_services(request):
920 d163b72b Leonidas Poulopoulos
    lang = request.LANGUAGE_CODE
921 7973b176 Leonidas Poulopoulos
    servicelocs = ServiceLoc.objects.all()
922 7973b176 Leonidas Poulopoulos
    locs = []
923 7973b176 Leonidas Poulopoulos
    for sl in servicelocs:
924 7973b176 Leonidas Poulopoulos
        response_location = {}
925 7973b176 Leonidas Poulopoulos
        response_location['lat'] = u"%s"%sl.latitude
926 7973b176 Leonidas Poulopoulos
        response_location['lng'] = u"%s"%sl.longitude
927 7973b176 Leonidas Poulopoulos
        response_location['address'] = u"%s<br>%s"%(sl.address_street, sl.address_city)
928 27d6f3e2 Leonidas Poulopoulos
        if len(sl.enc_level[0]) != 0:
929 27d6f3e2 Leonidas Poulopoulos
            response_location['enc'] = u"%s"%(','.join(sl.enc_level))
930 27d6f3e2 Leonidas Poulopoulos
        else:
931 27d6f3e2 Leonidas Poulopoulos
            response_location['enc'] = u"-"
932 7973b176 Leonidas Poulopoulos
        response_location['AP_no'] = u"%s"%(sl.AP_no)
933 d163b72b Leonidas Poulopoulos
        try:
934 d163b72b Leonidas Poulopoulos
            response_location['inst'] = sl.institutionid.org_name.get(lang=lang).name
935 d163b72b Leonidas Poulopoulos
        except Name_i18n.DoesNotExist:
936 d163b72b Leonidas Poulopoulos
            response_location['inst'] = sl.institutionid.org_name.get(lang='en').name
937 d163b72b Leonidas Poulopoulos
        try:
938 d163b72b Leonidas Poulopoulos
            response_location['name'] = sl.loc_name.get(lang=lang).name
939 d163b72b Leonidas Poulopoulos
        except Name_i18n.DoesNotExist:
940 d163b72b Leonidas Poulopoulos
            response_location['name'] = sl.loc_name.get(lang='en').name
941 7973b176 Leonidas Poulopoulos
        response_location['port_restrict'] = u"%s"%(sl.port_restrict)
942 7973b176 Leonidas Poulopoulos
        response_location['transp_proxy'] = u"%s"%(sl.transp_proxy)
943 7973b176 Leonidas Poulopoulos
        response_location['IPv6'] = u"%s"%(sl.IPv6)
944 7973b176 Leonidas Poulopoulos
        response_location['NAT'] = u"%s"%(sl.NAT)
945 7973b176 Leonidas Poulopoulos
        response_location['wired'] = u"%s"%(sl.wired)
946 7973b176 Leonidas Poulopoulos
        response_location['SSID'] = u"%s"%(sl.SSID)
947 7973b176 Leonidas Poulopoulos
        response_location['key'] = u"%s"%sl.pk
948 7973b176 Leonidas Poulopoulos
        locs.append(response_location)
949 7973b176 Leonidas Poulopoulos
    return HttpResponse(json.dumps(locs), mimetype='application/json')
950 7973b176 Leonidas Poulopoulos
951 62d0c01e Leonidas Poulopoulos
@never_cache
952 62d0c01e Leonidas Poulopoulos
def user_login(request):
953 62d0c01e Leonidas Poulopoulos
    try:
954 62d0c01e Leonidas Poulopoulos
        error_username = False
955 62d0c01e Leonidas Poulopoulos
        error_orgname = False
956 62d0c01e Leonidas Poulopoulos
        error_entitlement = False
957 62d0c01e Leonidas Poulopoulos
        error_mail = False
958 62d0c01e Leonidas Poulopoulos
        has_entitlement = False
959 62d0c01e Leonidas Poulopoulos
        error = ''
960 62d0c01e Leonidas Poulopoulos
        username = request.META['HTTP_EPPN']
961 62d0c01e Leonidas Poulopoulos
        if not username:
962 62d0c01e Leonidas Poulopoulos
            error_username = True
963 62d0c01e Leonidas Poulopoulos
        firstname = request.META['HTTP_SHIB_INETORGPERSON_GIVENNAME']
964 62d0c01e Leonidas Poulopoulos
        lastname = request.META['HTTP_SHIB_PERSON_SURNAME']
965 62d0c01e Leonidas Poulopoulos
        mail = request.META['HTTP_SHIB_INETORGPERSON_MAIL']
966 62d0c01e Leonidas Poulopoulos
        #organization = request.META['HTTP_SHIB_HOMEORGANIZATION']
967 62d0c01e Leonidas Poulopoulos
        entitlement = request.META['HTTP_SHIB_EP_ENTITLEMENT']
968 62d0c01e Leonidas Poulopoulos
        if settings.SHIB_AUTH_ENTITLEMENT in entitlement.split(";"):
969 62d0c01e Leonidas Poulopoulos
            has_entitlement = True
970 62d0c01e Leonidas Poulopoulos
        if not has_entitlement:
971 62d0c01e Leonidas Poulopoulos
            error_entitlement = True
972 62d0c01e Leonidas Poulopoulos
#        if not organization:
973 62d0c01e Leonidas Poulopoulos
#            error_orgname = True
974 62d0c01e Leonidas Poulopoulos
        if not mail:
975 62d0c01e Leonidas Poulopoulos
            error_mail = True
976 62d0c01e Leonidas Poulopoulos
        if error_username:
977 1c417c61 Leonidas Poulopoulos
            error = _("Your idP should release the eduPersonPrincipalName attribute towards this service<br>")
978 62d0c01e Leonidas Poulopoulos
        if error_entitlement:
979 1c417c61 Leonidas Poulopoulos
            error = error + _("Your idP should release an appropriate eduPersonEntitlement attribute towards this service<br>")
980 62d0c01e Leonidas Poulopoulos
        if error_mail:
981 1c417c61 Leonidas Poulopoulos
            error = error + _("Your idP should release the mail attribute towards this service")
982 62d0c01e Leonidas Poulopoulos
        if error_username or error_orgname or error_entitlement or error_mail:
983 7973b176 Leonidas Poulopoulos
            return render_to_response('status.html', {'error': error, "missing_attributes": True},
984 62d0c01e Leonidas Poulopoulos
                                  context_instance=RequestContext(request))
985 62d0c01e Leonidas Poulopoulos
        try:
986 62d0c01e Leonidas Poulopoulos
            user = User.objects.get(username__exact=username)
987 62d0c01e Leonidas Poulopoulos
            user.email = mail
988 62d0c01e Leonidas Poulopoulos
            user.first_name = firstname
989 62d0c01e Leonidas Poulopoulos
            user.last_name = lastname
990 62d0c01e Leonidas Poulopoulos
            user.save()
991 62d0c01e Leonidas Poulopoulos
            user_exists = True
992 62d0c01e Leonidas Poulopoulos
        except User.DoesNotExist:
993 62d0c01e Leonidas Poulopoulos
            user_exists = False
994 62d0c01e Leonidas Poulopoulos
        user = authenticate(username=username, firstname=firstname, lastname=lastname, mail=mail, authsource='shibboleth')
995 62d0c01e Leonidas Poulopoulos
        if user is not None:
996 62d0c01e Leonidas Poulopoulos
            try:
997 62d0c01e Leonidas Poulopoulos
                profile = user.get_profile()
998 62d0c01e Leonidas Poulopoulos
                inst = profile.institution
999 62d0c01e Leonidas Poulopoulos
            except UserProfile.DoesNotExist:
1000 62d0c01e Leonidas Poulopoulos
                form = UserProfileForm()
1001 62d0c01e Leonidas Poulopoulos
                form.fields['user'] = forms.ModelChoiceField(queryset=User.objects.filter(pk=user.pk), empty_label=None)
1002 62d0c01e Leonidas Poulopoulos
                form.fields['institution'] = forms.ModelChoiceField(queryset=Institution.objects.all(), empty_label=None)
1003 62d0c01e Leonidas Poulopoulos
                return render_to_response('registration/select_institution.html', {'form': form}, context_instance=RequestContext(request))
1004 62d0c01e Leonidas Poulopoulos
            if user.is_active:
1005 62d0c01e Leonidas Poulopoulos
               login(request, user)
1006 62d0c01e Leonidas Poulopoulos
               return HttpResponseRedirect(reverse("manage"))
1007 62d0c01e Leonidas Poulopoulos
            else:
1008 7973b176 Leonidas Poulopoulos
                status = _("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
1009 7973b176 Leonidas Poulopoulos
                return render_to_response('status.html', {'status': status, 'inactive': True},
1010 62d0c01e Leonidas Poulopoulos
                                  context_instance=RequestContext(request))
1011 62d0c01e Leonidas Poulopoulos
        else:
1012 62d0c01e Leonidas Poulopoulos
            error = _("Something went wrong during user authentication. Contact your administrator %s" %user)
1013 7973b176 Leonidas Poulopoulos
            return render_to_response('status.html', {'error': error,},
1014 62d0c01e Leonidas Poulopoulos
                                  context_instance=RequestContext(request))
1015 7973b176 Leonidas Poulopoulos
    except Exception:
1016 7973b176 Leonidas Poulopoulos
        error = _("Invalid login procedure")
1017 7973b176 Leonidas Poulopoulos
        return render_to_response('status.html', {'error': error,},
1018 62d0c01e Leonidas Poulopoulos
                                  context_instance=RequestContext(request))
1019 7973b176 Leonidas Poulopoulos
1020 d163b72b Leonidas Poulopoulos
@never_cache
1021 af9d484b Leonidas Poulopoulos
def geolocate(request):
1022 af9d484b Leonidas Poulopoulos
    return render_to_response('front/geolocate.html',
1023 af9d484b Leonidas Poulopoulos
                                  context_instance=RequestContext(request))
1024 d163b72b Leonidas Poulopoulos
@never_cache
1025 7b9cc79e Leonidas Poulopoulos
def participants(request):
1026 7b9cc79e Leonidas Poulopoulos
    institutions = Institution.objects.all()
1027 7b9cc79e Leonidas Poulopoulos
    dets = []
1028 7b9cc79e Leonidas Poulopoulos
    for i in institutions:
1029 7b9cc79e Leonidas Poulopoulos
        try:
1030 7b9cc79e Leonidas Poulopoulos
            dets.append(i.institutiondetails)
1031 7b9cc79e Leonidas Poulopoulos
        except InstitutionDetails.DoesNotExist:
1032 7b9cc79e Leonidas Poulopoulos
            pass
1033 7b9cc79e Leonidas Poulopoulos
    return render_to_response('front/participants.html', {'institutions': dets},
1034 7b9cc79e Leonidas Poulopoulos
                                  context_instance=RequestContext(request))
1035 d163b72b Leonidas Poulopoulos
@never_cache
1036 62d0c01e Leonidas Poulopoulos
def selectinst(request):
1037 62d0c01e Leonidas Poulopoulos
    if request.method == 'POST':
1038 62d0c01e Leonidas Poulopoulos
        request_data = request.POST.copy()
1039 62d0c01e Leonidas Poulopoulos
        user = request_data['user']
1040 7973b176 Leonidas Poulopoulos
        try:
1041 7973b176 Leonidas Poulopoulos
            existingProfile = UserProfile.objects.get(user=user)
1042 7973b176 Leonidas Poulopoulos
            error = _("Violation warning: User account is already associated with an institution.The event has been logged and our administrators will be notified about it")
1043 7973b176 Leonidas Poulopoulos
            return render_to_response('status.html', {'error': error, 'inactive': True},
1044 7973b176 Leonidas Poulopoulos
                                  context_instance=RequestContext(request))
1045 7973b176 Leonidas Poulopoulos
        except UserProfile.DoesNotExist:
1046 7973b176 Leonidas Poulopoulos
            pass
1047 7973b176 Leonidas Poulopoulos
            
1048 62d0c01e Leonidas Poulopoulos
        form = UserProfileForm(request_data)
1049 62d0c01e Leonidas Poulopoulos
        if form.is_valid():
1050 62d0c01e Leonidas Poulopoulos
            userprofile = form.save()
1051 7973b176 Leonidas Poulopoulos
            user_activation_notify(userprofile)
1052 62d0c01e Leonidas Poulopoulos
            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
1053 7973b176 Leonidas Poulopoulos
            return render_to_response('status.html', {'status': error, 'inactive': True},
1054 62d0c01e Leonidas Poulopoulos
                                  context_instance=RequestContext(request))
1055 62d0c01e Leonidas Poulopoulos
        else:
1056 62d0c01e Leonidas Poulopoulos
            form.fields['user'] = forms.ModelChoiceField(queryset=User.objects.filter(pk=user.pk), empty_label=None)
1057 62d0c01e Leonidas Poulopoulos
            form.fields['institution'] = forms.ModelChoiceField(queryset=Institution.objects.all(), empty_label=None)
1058 62d0c01e Leonidas Poulopoulos
            return render_to_response('registration/select_institution.html', {'form': form}, context_instance=RequestContext(request))
1059 62d0c01e Leonidas Poulopoulos
1060 af9d484b Leonidas Poulopoulos
1061 7973b176 Leonidas Poulopoulos
def user_activation_notify(userprofile):
1062 7973b176 Leonidas Poulopoulos
    current_site = Site.objects.get_current()
1063 7973b176 Leonidas Poulopoulos
    subject = render_to_string('registration/activation_email_subject.txt',
1064 7973b176 Leonidas Poulopoulos
                                   { 'site': current_site })
1065 7973b176 Leonidas Poulopoulos
    # Email subject *must not* contain newlines
1066 7973b176 Leonidas Poulopoulos
    subject = ''.join(subject.splitlines())
1067 7973b176 Leonidas Poulopoulos
    registration_profile = RegistrationProfile.objects.create_profile(userprofile.user)
1068 7973b176 Leonidas Poulopoulos
    message = render_to_string('registration/activation_email.txt',
1069 7973b176 Leonidas Poulopoulos
                                   { 'activation_key': registration_profile.activation_key,
1070 7973b176 Leonidas Poulopoulos
                                     'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
1071 7973b176 Leonidas Poulopoulos
                                     'site': current_site,
1072 7973b176 Leonidas Poulopoulos
                                     'user': userprofile.user,
1073 7973b176 Leonidas Poulopoulos
                                     'institution': userprofile.institution })
1074 7973b176 Leonidas Poulopoulos
    send_new_mail(settings.EMAIL_SUBJECT_PREFIX + subject, 
1075 7973b176 Leonidas Poulopoulos
                              message, settings.SERVER_EMAIL,
1076 7973b176 Leonidas Poulopoulos
                             settings.NOTIFY_ADMIN_MAILS, [])
1077 d163b72b Leonidas Poulopoulos
@never_cache
1078 af9d484b Leonidas Poulopoulos
def closest(request):
1079 af9d484b Leonidas Poulopoulos
    if request.method == 'GET':
1080 af9d484b Leonidas Poulopoulos
        locs = []
1081 af9d484b Leonidas Poulopoulos
        request_data = request.GET.copy()
1082 af9d484b Leonidas Poulopoulos
        response_location = {}
1083 433489b5 Leonidas Poulopoulos
        if 'lat' in request.GET and 'lng' in request.GET:
1084 433489b5 Leonidas Poulopoulos
            response_location["lat"] = request_data['lat']
1085 433489b5 Leonidas Poulopoulos
            response_location["lng"] = request_data['lng']
1086 433489b5 Leonidas Poulopoulos
        else:
1087 433489b5 Leonidas Poulopoulos
            response = {"status":"Cannot parse a request without longitude or latitude. Use ?lng=<langitude>&lat=<latitude>&_=<random_num> in your query"}
1088 433489b5 Leonidas Poulopoulos
            return HttpResponse(json.dumps(response), mimetype='application/json')
1089 af9d484b Leonidas Poulopoulos
        lat = float(request_data['lat'])
1090 af9d484b Leonidas Poulopoulos
        lng = float(request_data['lng'])
1091 af9d484b Leonidas Poulopoulos
        R = 6371
1092 af9d484b Leonidas Poulopoulos
        distances = {}
1093 af9d484b Leonidas Poulopoulos
        closestMarker = {}
1094 af9d484b Leonidas Poulopoulos
        closest = -1
1095 d163b72b Leonidas Poulopoulos
        points = getPoints()
1096 d163b72b Leonidas Poulopoulos
        for (counter, i) in enumerate(points):
1097 d163b72b Leonidas Poulopoulos
            pointname = i['text']
1098 d163b72b Leonidas Poulopoulos
            pointlng = i['lng'] 
1099 d163b72b Leonidas Poulopoulos
            pointlat = i['lat']
1100 d163b72b Leonidas Poulopoulos
            pointtext = i['text']
1101 d163b72b Leonidas Poulopoulos
            dLat = rad(float(pointlat)-float(lat))
1102 d163b72b Leonidas Poulopoulos
            dLong = rad(float(pointlng)-float(lng))
1103 d163b72b Leonidas Poulopoulos
            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) 
1104 d163b72b Leonidas Poulopoulos
            c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
1105 d163b72b Leonidas Poulopoulos
            d = R * c
1106 d163b72b Leonidas Poulopoulos
            distances[counter] = d
1107 d163b72b Leonidas Poulopoulos
            if (closest == -1 or d < distances[closest]):
1108 d163b72b Leonidas Poulopoulos
                closest = counter
1109 d163b72b Leonidas Poulopoulos
                closestMarker = {"name": pointname, "lat": pointlat, "lng": pointlng, "text": pointtext}
1110 d163b72b Leonidas Poulopoulos
        return HttpResponse(json.dumps(closestMarker), mimetype='application/json')
1111 433489b5 Leonidas Poulopoulos
    else:
1112 433489b5 Leonidas Poulopoulos
        response = {"status":"Use a GET method for your request"}
1113 433489b5 Leonidas Poulopoulos
        return HttpResponse(json.dumps(response), mimetype='application/json')
1114 d163b72b Leonidas Poulopoulos
1115 d163b72b Leonidas Poulopoulos
@never_cache
1116 d163b72b Leonidas Poulopoulos
def worldPoints(request):
1117 d163b72b Leonidas Poulopoulos
    if request.method == 'GET':
1118 d163b72b Leonidas Poulopoulos
        points = getPoints()
1119 d163b72b Leonidas Poulopoulos
        return HttpResponse(json.dumps(points), mimetype='application/json')
1120 d163b72b Leonidas Poulopoulos
1121 d163b72b Leonidas Poulopoulos
@never_cache
1122 d163b72b Leonidas Poulopoulos
def world(request):
1123 d163b72b Leonidas Poulopoulos
        return render_to_response('front/world.html',
1124 d163b72b Leonidas Poulopoulos
                                  context_instance=RequestContext(request))
1125 d163b72b Leonidas Poulopoulos
1126 1c417c61 Leonidas Poulopoulos
1127 1c417c61 Leonidas Poulopoulos
@never_cache
1128 1c417c61 Leonidas Poulopoulos
def managementPage(request):
1129 1c417c61 Leonidas Poulopoulos
    return render_to_response('front/management.html',
1130 1c417c61 Leonidas Poulopoulos
                                  context_instance=RequestContext(request))
1131 1c417c61 Leonidas Poulopoulos
1132 d163b72b Leonidas Poulopoulos
def getPoints():
1133 d163b72b Leonidas Poulopoulos
    points = cache.get('points')
1134 d163b72b Leonidas Poulopoulos
    if points:
1135 d163b72b Leonidas Poulopoulos
        points = bz2.decompress(points)
1136 d163b72b Leonidas Poulopoulos
        return json.loads(points)
1137 d163b72b Leonidas Poulopoulos
    else:
1138 d163b72b Leonidas Poulopoulos
        point_list = []
1139 af9d484b Leonidas Poulopoulos
        doc = ET.parse(settings.KML_FILE)
1140 af9d484b Leonidas Poulopoulos
        root = doc.getroot()
1141 af9d484b Leonidas Poulopoulos
        r = root.getchildren()[0]
1142 af9d484b Leonidas Poulopoulos
        for (counter, i) in enumerate(r.getchildren()):
1143 af9d484b Leonidas Poulopoulos
            if "id" in i.keys():
1144 af9d484b Leonidas Poulopoulos
                j = i.getchildren()
1145 af9d484b Leonidas Poulopoulos
                pointname = j[0].text
1146 af9d484b Leonidas Poulopoulos
                point = j[2].getchildren()[0].text
1147 af9d484b Leonidas Poulopoulos
                pointlng, pointlat, pointele = point.split(',')
1148 d163b72b Leonidas Poulopoulos
                Marker = {"name": pointname, "lat": pointlat, "lng": pointlng, "text": j[1].text}
1149 d163b72b Leonidas Poulopoulos
                point_list.append(Marker);
1150 d163b72b Leonidas Poulopoulos
        points = json.dumps(point_list)
1151 d163b72b Leonidas Poulopoulos
        cache.set('points', bz2.compress(points), 60 * 3600 * 24)
1152 d163b72b Leonidas Poulopoulos
        return json.loads(points)
1153 c2b3014a Leonidas Poulopoulos
1154 d163b72b Leonidas Poulopoulos
1155 d163b72b Leonidas Poulopoulos
@never_cache
1156 c2b3014a Leonidas Poulopoulos
def instxml(request):
1157 c2b3014a Leonidas Poulopoulos
    ET._namespace_map["http://www.w3.org/2001/XMLSchema-instance"] = 'xsi'
1158 c2b3014a Leonidas Poulopoulos
    root = ET.Element("institutions")
1159 c2b3014a Leonidas Poulopoulos
    NS_XSI = "{http://www.w3.org/2001/XMLSchema-instance}"
1160 10e9ee83 Leonidas Poulopoulos
    root.set(NS_XSI + "noNamespaceSchemaLocation", "institution.xsd")
1161 c2b3014a Leonidas Poulopoulos
    #root.attrib["xsi:noNamespaceSchemaLocation"] = "institution.xsd"
1162 c2b3014a Leonidas Poulopoulos
    institutions = Institution.objects.all()
1163 c2b3014a Leonidas Poulopoulos
    for institution in institutions:
1164 c2b3014a Leonidas Poulopoulos
        try:
1165 c2b3014a Leonidas Poulopoulos
            inst = institution.institutiondetails
1166 c2b3014a Leonidas Poulopoulos
            if not inst:
1167 c2b3014a Leonidas Poulopoulos
                pass
1168 c2b3014a Leonidas Poulopoulos
        except InstitutionDetails.DoesNotExist:
1169 c2b3014a Leonidas Poulopoulos
            pass
1170 c2b3014a Leonidas Poulopoulos
        
1171 c2b3014a Leonidas Poulopoulos
        instElement = ET.SubElement(root, "institution")
1172 c2b3014a Leonidas Poulopoulos
        
1173 c2b3014a Leonidas Poulopoulos
        instCountry = ET.SubElement(instElement, "country")
1174 c2b3014a Leonidas Poulopoulos
        instCountry.text = ("%s" %inst.institution.realmid.country).upper()
1175 c2b3014a Leonidas Poulopoulos
        
1176 c2b3014a Leonidas Poulopoulos
        instType = ET.SubElement(instElement, "type")
1177 0790d933 Leonidas Poulopoulos
        instType.text = "%s" %inst.institution.ertype
1178 c2b3014a Leonidas Poulopoulos
        
1179 c2b3014a Leonidas Poulopoulos
        for realm in institution.instrealm_set.all():
1180 c2b3014a Leonidas Poulopoulos
            instRealm = ET.SubElement(instElement, "inst_realm")
1181 c2b3014a Leonidas Poulopoulos
            instRealm.text = realm.realm
1182 c2b3014a Leonidas Poulopoulos
        
1183 c2b3014a Leonidas Poulopoulos
        for name in inst.institution.org_name.all():
1184 c2b3014a Leonidas Poulopoulos
            instOrgName = ET.SubElement(instElement, "org_name")
1185 c2b3014a Leonidas Poulopoulos
            instOrgName.attrib["lang"] = name.lang
1186 c2b3014a Leonidas Poulopoulos
            instOrgName.text = u"%s" %name.name
1187 c2b3014a Leonidas Poulopoulos
        
1188 c2b3014a Leonidas Poulopoulos
        instAddress = ET.SubElement(instElement, "address")
1189 c2b3014a Leonidas Poulopoulos
        
1190 c2b3014a Leonidas Poulopoulos
        instAddrStreet = ET.SubElement(instAddress, "street")
1191 c2b3014a Leonidas Poulopoulos
        instAddrStreet.text = inst.address_street
1192 c2b3014a Leonidas Poulopoulos
        
1193 c2b3014a Leonidas Poulopoulos
        instAddrCity = ET.SubElement(instAddress, "city")
1194 c2b3014a Leonidas Poulopoulos
        instAddrCity.text = inst.address_city
1195 c2b3014a Leonidas Poulopoulos
        
1196 c2b3014a Leonidas Poulopoulos
        for contact in inst.contact.all():
1197 c2b3014a Leonidas Poulopoulos
            instContact = ET.SubElement(instElement, "contact")
1198 c2b3014a Leonidas Poulopoulos
            
1199 c2b3014a Leonidas Poulopoulos
            instContactName = ET.SubElement(instContact, "name")
1200 9741b379 Leonidas Poulopoulos
            instContactName.text = "%s" %(contact.name)
1201 c2b3014a Leonidas Poulopoulos
            
1202 c2b3014a Leonidas Poulopoulos
            instContactEmail = ET.SubElement(instContact, "email")
1203 c2b3014a Leonidas Poulopoulos
            instContactEmail.text = contact.email
1204 c2b3014a Leonidas Poulopoulos
            
1205 c2b3014a Leonidas Poulopoulos
            instContactPhone = ET.SubElement(instContact, "phone")
1206 c2b3014a Leonidas Poulopoulos
            instContactPhone.text = contact.phone
1207 af9d484b Leonidas Poulopoulos
        
1208 c2b3014a Leonidas Poulopoulos
        for url in inst.url.all():
1209 c2b3014a Leonidas Poulopoulos
            instUrl = ET.SubElement(instElement, "%s_URL"%(url.urltype))
1210 c2b3014a Leonidas Poulopoulos
            instUrl.attrib["lang"] = url.lang
1211 c2b3014a Leonidas Poulopoulos
            instUrl.text = url.url
1212 c2b3014a Leonidas Poulopoulos
        
1213 10e9ee83 Leonidas Poulopoulos
        instTs = ET.SubElement(instElement, "ts")
1214 10e9ee83 Leonidas Poulopoulos
        instTs.text = "%s" %inst.ts.isoformat()
1215 c2b3014a Leonidas Poulopoulos
        #Let's go to Institution Service Locations
1216 af9d484b Leonidas Poulopoulos
1217 c2b3014a Leonidas Poulopoulos
        for serviceloc in inst.institution.serviceloc_set.all():
1218 c2b3014a Leonidas Poulopoulos
            instLocation = ET.SubElement(instElement, "location")
1219 c2b3014a Leonidas Poulopoulos
            
1220 c2b3014a Leonidas Poulopoulos
            instLong = ET.SubElement(instLocation, "longitude")
1221 c2b3014a Leonidas Poulopoulos
            instLong.text = "%s" %serviceloc.longitude
1222 c2b3014a Leonidas Poulopoulos
            
1223 c2b3014a Leonidas Poulopoulos
            instLat = ET.SubElement(instLocation, "latitude")
1224 c2b3014a Leonidas Poulopoulos
            instLat.text = "%s" %serviceloc.latitude
1225 c2b3014a Leonidas Poulopoulos
            
1226 c2b3014a Leonidas Poulopoulos
            for instlocname in serviceloc.loc_name.all():
1227 c2b3014a Leonidas Poulopoulos
                instLocName = ET.SubElement(instLocation, "loc_name")
1228 c2b3014a Leonidas Poulopoulos
                instLocName.attrib["lang"] = instlocname.lang
1229 c2b3014a Leonidas Poulopoulos
                instLocName.text = instlocname.name
1230 c2b3014a Leonidas Poulopoulos
            
1231 c2b3014a Leonidas Poulopoulos
            instLocAddress = ET.SubElement(instLocation, "address")
1232 c2b3014a Leonidas Poulopoulos
        
1233 c2b3014a Leonidas Poulopoulos
            instLocAddrStreet = ET.SubElement(instLocAddress, "street")
1234 c2b3014a Leonidas Poulopoulos
            instLocAddrStreet.text = serviceloc.address_street
1235 c2b3014a Leonidas Poulopoulos
        
1236 c2b3014a Leonidas Poulopoulos
            instLocAddrCity = ET.SubElement(instLocAddress, "city")
1237 c2b3014a Leonidas Poulopoulos
            instLocAddrCity.text = serviceloc.address_city
1238 c2b3014a Leonidas Poulopoulos
            
1239 138dad8b Leonidas Poulopoulos
            for contact in serviceloc.contact.all():
1240 138dad8b Leonidas Poulopoulos
                instLocContact = ET.SubElement(instLocation, "contact")
1241 138dad8b Leonidas Poulopoulos
                
1242 138dad8b Leonidas Poulopoulos
                instLocContactName = ET.SubElement(instLocContact, "name")
1243 9741b379 Leonidas Poulopoulos
                instLocContactName.text = "%s" %(contact.name)
1244 138dad8b Leonidas Poulopoulos
                
1245 138dad8b Leonidas Poulopoulos
                instLocContactEmail = ET.SubElement(instLocContact, "email")
1246 138dad8b Leonidas Poulopoulos
                instLocContactEmail.text = contact.email
1247 138dad8b Leonidas Poulopoulos
                
1248 138dad8b Leonidas Poulopoulos
                instLocContactPhone = ET.SubElement(instLocContact, "phone")
1249 138dad8b Leonidas Poulopoulos
                instLocContactPhone.text = contact.phone
1250 138dad8b Leonidas Poulopoulos
            
1251 c2b3014a Leonidas Poulopoulos
            instLocSSID = ET.SubElement(instLocation, "SSID")
1252 c2b3014a Leonidas Poulopoulos
            instLocSSID.text = serviceloc.SSID
1253 c2b3014a Leonidas Poulopoulos
            
1254 c2b3014a Leonidas Poulopoulos
            instLocEncLevel = ET.SubElement(instLocation, "enc_level")
1255 138dad8b Leonidas Poulopoulos
            instLocEncLevel.text = ', '.join(serviceloc.enc_level)
1256 c2b3014a Leonidas Poulopoulos
            
1257 c2b3014a Leonidas Poulopoulos
            instLocPortRestrict = ET.SubElement(instLocation, "port_restrict")
1258 c2b3014a Leonidas Poulopoulos
            instLocPortRestrict.text = ("%s" %serviceloc.port_restrict).lower()
1259 c2b3014a Leonidas Poulopoulos
            
1260 c2b3014a Leonidas Poulopoulos
            instLocTransProxy = ET.SubElement(instLocation, "transp_proxy")
1261 c2b3014a Leonidas Poulopoulos
            instLocTransProxy.text = ("%s" %serviceloc.transp_proxy).lower()
1262 c2b3014a Leonidas Poulopoulos
            
1263 c2b3014a Leonidas Poulopoulos
            instLocIpv6 = ET.SubElement(instLocation, "IPv6")
1264 c2b3014a Leonidas Poulopoulos
            instLocIpv6.text = ("%s" %serviceloc.IPv6).lower()
1265 c2b3014a Leonidas Poulopoulos
            
1266 c2b3014a Leonidas Poulopoulos
            instLocNAT = ET.SubElement(instLocation, "NAT")
1267 c2b3014a Leonidas Poulopoulos
            instLocNAT.text = ("%s" %serviceloc.NAT).lower()
1268 c2b3014a Leonidas Poulopoulos
            
1269 c2b3014a Leonidas Poulopoulos
            instLocAP_no = ET.SubElement(instLocation, "AP_no")
1270 c2b3014a Leonidas Poulopoulos
            instLocAP_no.text = "%s" %int(serviceloc.AP_no)
1271 c2b3014a Leonidas Poulopoulos
            
1272 c2b3014a Leonidas Poulopoulos
            instLocWired = ET.SubElement(instLocation, "wired")
1273 c2b3014a Leonidas Poulopoulos
            instLocWired.text = ("%s" %serviceloc.wired).lower()
1274 c2b3014a Leonidas Poulopoulos
            
1275 c2b3014a Leonidas Poulopoulos
            for url in serviceloc.url.all():
1276 c2b3014a Leonidas Poulopoulos
                instLocUrl = ET.SubElement(instLocation, "%s_URL"%(url.urltype))
1277 c2b3014a Leonidas Poulopoulos
                instLocUrl.attrib["lang"] = url.lang
1278 c2b3014a Leonidas Poulopoulos
                instLocUrl.text = url.url
1279 c2b3014a Leonidas Poulopoulos
            
1280 c2b3014a Leonidas Poulopoulos
    return render_to_response("general/institution.xml", {"xml":to_xml(root)},context_instance=RequestContext(request,), mimetype="application/xml")
1281 c2b3014a Leonidas Poulopoulos
        
1282 d163b72b Leonidas Poulopoulos
@never_cache
1283 c2b3014a Leonidas Poulopoulos
def realmxml(request):
1284 c2b3014a Leonidas Poulopoulos
    realm = Realm.objects.all()[0]
1285 c2b3014a Leonidas Poulopoulos
    ET._namespace_map["http://www.w3.org/2001/XMLSchema-instance"] = 'xsi'
1286 c2b3014a Leonidas Poulopoulos
    root = ET.Element("realms")
1287 c2b3014a Leonidas Poulopoulos
    NS_XSI = "{http://www.w3.org/2001/XMLSchema-instance}"
1288 c2b3014a Leonidas Poulopoulos
    root.set(NS_XSI + "noNamespaceSchemaLocation", "realm.xsd")
1289 c2b3014a Leonidas Poulopoulos
    #root.attrib["xsi:noNamespaceSchemaLocation"] = "institution.xsd"
1290 c2b3014a Leonidas Poulopoulos
    realmElement = ET.SubElement(root, "realm")
1291 c2b3014a Leonidas Poulopoulos
    
1292 c2b3014a Leonidas Poulopoulos
    realmCountry = ET.SubElement(realmElement, "country")
1293 0790d933 Leonidas Poulopoulos
    realmCountry.text = realm.country.upper()
1294 c2b3014a Leonidas Poulopoulos
        
1295 c2b3014a Leonidas Poulopoulos
    realmStype = ET.SubElement(realmElement, "stype")
1296 c2b3014a Leonidas Poulopoulos
    realmStype.text = "%s" %realm.stype
1297 c2b3014a Leonidas Poulopoulos
    
1298 c2b3014a Leonidas Poulopoulos
    for name in realm.org_name.all():
1299 c2b3014a Leonidas Poulopoulos
        realmOrgName = ET.SubElement(realmElement, "org_name")
1300 c2b3014a Leonidas Poulopoulos
        realmOrgName.attrib["lang"] = name.lang
1301 c2b3014a Leonidas Poulopoulos
        realmOrgName.text = u"%s" %name.name
1302 c2b3014a Leonidas Poulopoulos
    
1303 c2b3014a Leonidas Poulopoulos
    realmAddress = ET.SubElement(realmElement, "address")
1304 c2b3014a Leonidas Poulopoulos
        
1305 c2b3014a Leonidas Poulopoulos
    realmAddrStreet = ET.SubElement(realmAddress, "street")
1306 c2b3014a Leonidas Poulopoulos
    realmAddrStreet.text = realm.address_street
1307 c2b3014a Leonidas Poulopoulos
    
1308 c2b3014a Leonidas Poulopoulos
    realmAddrCity = ET.SubElement(realmAddress, "city")
1309 c2b3014a Leonidas Poulopoulos
    realmAddrCity.text = realm.address_city
1310 c2b3014a Leonidas Poulopoulos
    
1311 c2b3014a Leonidas Poulopoulos
    for contact in realm.contact.all():
1312 c2b3014a Leonidas Poulopoulos
        realmContact = ET.SubElement(realmElement, "contact")
1313 c2b3014a Leonidas Poulopoulos
        
1314 c2b3014a Leonidas Poulopoulos
        realmContactName = ET.SubElement(realmContact, "name")
1315 9741b379 Leonidas Poulopoulos
        realmContactName.text = "%s" %(contact.name)
1316 c2b3014a Leonidas Poulopoulos
        
1317 c2b3014a Leonidas Poulopoulos
        realmContactEmail = ET.SubElement(realmContact, "email")
1318 c2b3014a Leonidas Poulopoulos
        realmContactEmail.text = contact.email
1319 c2b3014a Leonidas Poulopoulos
        
1320 c2b3014a Leonidas Poulopoulos
        realmContactPhone = ET.SubElement(realmContact, "phone")
1321 c2b3014a Leonidas Poulopoulos
        realmContactPhone.text = contact.phone
1322 c2b3014a Leonidas Poulopoulos
    
1323 c2b3014a Leonidas Poulopoulos
    for url in realm.url.all():
1324 c2b3014a Leonidas Poulopoulos
        realmUrl = ET.SubElement(realmElement, "%s_URL"%(url.urltype))
1325 c2b3014a Leonidas Poulopoulos
        realmUrl.attrib["lang"] = url.lang
1326 c2b3014a Leonidas Poulopoulos
        realmUrl.text = url.url
1327 c2b3014a Leonidas Poulopoulos
    
1328 0790d933 Leonidas Poulopoulos
    instTs = ET.SubElement(realmElement, "ts")
1329 0790d933 Leonidas Poulopoulos
    instTs.text = "%s" %realm.ts.isoformat()
1330 0790d933 Leonidas Poulopoulos
    
1331 c2b3014a Leonidas Poulopoulos
    return render_to_response("general/realm.xml", {"xml":to_xml(root)},context_instance=RequestContext(request,), mimetype="application/xml")
1332 c2b3014a Leonidas Poulopoulos
1333 d163b72b Leonidas Poulopoulos
@never_cache
1334 c2b3014a Leonidas Poulopoulos
def realmdataxml(request):
1335 c2b3014a Leonidas Poulopoulos
    realm = Realm.objects.all()[0]
1336 c2b3014a Leonidas Poulopoulos
    ET._namespace_map["http://www.w3.org/2001/XMLSchema-instance"] = 'xsi'
1337 0790d933 Leonidas Poulopoulos
    root = ET.Element("realm_data_root")
1338 c2b3014a Leonidas Poulopoulos
    NS_XSI = "{http://www.w3.org/2001/XMLSchema-instance}"
1339 d9a68b19 Leonidas Poulopoulos
    root.set(NS_XSI + "noNamespaceSchemaLocation", "realm_data.xsd")
1340 c2b3014a Leonidas Poulopoulos
    
1341 0790d933 Leonidas Poulopoulos
    realmdataElement = ET.SubElement(root, "realm_data")
1342 0790d933 Leonidas Poulopoulos
    
1343 0790d933 Leonidas Poulopoulos
    realmCountry = ET.SubElement(realmdataElement, "country")
1344 0790d933 Leonidas Poulopoulos
    realmCountry.text = realm.country.upper()
1345 0790d933 Leonidas Poulopoulos
    
1346 d9a68b19 Leonidas Poulopoulos
    nIdpCountry = ET.SubElement(realmdataElement, "number_IdP")
1347 0790d933 Leonidas Poulopoulos
    nIdpCountry.text = "%s" %len(realm.institution_set.filter(ertype=1))
1348 0790d933 Leonidas Poulopoulos
    
1349 0790d933 Leonidas Poulopoulos
    nSPCountry = ET.SubElement(realmdataElement, "number_SP")
1350 0790d933 Leonidas Poulopoulos
    nSPCountry.text = "%s" %len(realm.institution_set.filter(ertype=2))
1351 0790d933 Leonidas Poulopoulos
    
1352 0790d933 Leonidas Poulopoulos
    nSPIdpCountry = ET.SubElement(realmdataElement, "number_SPIdP")
1353 0790d933 Leonidas Poulopoulos
    nSPIdpCountry.text = "%s" %len(realm.institution_set.filter(ertype=3))
1354 0790d933 Leonidas Poulopoulos
    
1355 0790d933 Leonidas Poulopoulos
    ninstCountry = ET.SubElement(realmdataElement, "number_inst")
1356 0790d933 Leonidas Poulopoulos
    ninstCountry.text = "%s" %len(realm.institution_set.all())
1357 0790d933 Leonidas Poulopoulos
    
1358 0790d933 Leonidas Poulopoulos
    nuserCountry = ET.SubElement(realmdataElement, "number_user")
1359 0790d933 Leonidas Poulopoulos
    insts = realm.institution_set.all()
1360 0790d933 Leonidas Poulopoulos
    users = 0
1361 0790d933 Leonidas Poulopoulos
    for inst in insts:
1362 0790d933 Leonidas Poulopoulos
        try:
1363 f545b59c Leonidas Poulopoulos
            if inst.institutiondetails.number_user:
1364 f545b59c Leonidas Poulopoulos
                users = users + inst.institutiondetails.number_user
1365 0790d933 Leonidas Poulopoulos
        except InstitutionDetails.DoesNotExist:
1366 0790d933 Leonidas Poulopoulos
            pass
1367 0790d933 Leonidas Poulopoulos
    nuserCountry.text = "%s" %users
1368 0790d933 Leonidas Poulopoulos
    
1369 0790d933 Leonidas Poulopoulos
    nIdCountry = ET.SubElement(realmdataElement, "number_id")
1370 0790d933 Leonidas Poulopoulos
    insts = realm.institution_set.all()
1371 0790d933 Leonidas Poulopoulos
    ids = 0
1372 0790d933 Leonidas Poulopoulos
    for inst in insts:
1373 0790d933 Leonidas Poulopoulos
        try:
1374 6f08eac3 Leonidas Poulopoulos
            if inst.institutiondetails.number_id:
1375 6f08eac3 Leonidas Poulopoulos
                ids = ids + inst.institutiondetails.number_id
1376 0790d933 Leonidas Poulopoulos
        except InstitutionDetails.DoesNotExist:
1377 0790d933 Leonidas Poulopoulos
            pass
1378 0790d933 Leonidas Poulopoulos
    nIdCountry.text = "%s" %ids
1379 0790d933 Leonidas Poulopoulos
    
1380 0790d933 Leonidas Poulopoulos
    # Get the latest ts from all tables...
1381 0790d933 Leonidas Poulopoulos
    datetimes = []
1382 20802476 Leonidas Poulopoulos
    if InstitutionDetails.objects.aggregate(Max('ts'))['ts__max']:
1383 20802476 Leonidas Poulopoulos
        datetimes.append(InstitutionDetails.objects.aggregate(Max('ts'))['ts__max'])
1384 20802476 Leonidas Poulopoulos
    if Realm.objects.aggregate(Max('ts'))['ts__max']:
1385 20802476 Leonidas Poulopoulos
        datetimes.append(Realm.objects.aggregate(Max('ts'))['ts__max'])
1386 20802476 Leonidas Poulopoulos
    if InstServer.objects.aggregate(Max('ts'))['ts__max']:
1387 20802476 Leonidas Poulopoulos
        datetimes.append(InstServer.objects.aggregate(Max('ts'))['ts__max'])
1388 20802476 Leonidas Poulopoulos
    if ServiceLoc.objects.aggregate(Max('ts'))['ts__max']:
1389 20802476 Leonidas Poulopoulos
        datetimes.append(ServiceLoc.objects.aggregate(Max('ts'))['ts__max'])
1390 20802476 Leonidas Poulopoulos
    if len(datetimes) == 0:
1391 20802476 Leonidas Poulopoulos
        datetimes.append(datetime.datetime.now())
1392 0790d933 Leonidas Poulopoulos
    instTs = ET.SubElement(realmdataElement, "ts")
1393 0790d933 Leonidas Poulopoulos
    instTs.text = "%s" %max(datetimes).isoformat()
1394 0790d933 Leonidas Poulopoulos
    
1395 0790d933 Leonidas Poulopoulos
    
1396 c2b3014a Leonidas Poulopoulos
    return render_to_response("general/realm_data.xml", {"xml":to_xml(root)},context_instance=RequestContext(request,), mimetype="application/xml")
1397 c2b3014a Leonidas Poulopoulos
1398 c2b3014a Leonidas Poulopoulos
def to_xml(ele, encoding="UTF-8"):
1399 c2b3014a Leonidas Poulopoulos
    "Convert and return the XML for an *ele* (:class:`~xml.etree.ElementTree.Element`) with specified *encoding*."
1400 c2b3014a Leonidas Poulopoulos
    xml = ET.tostring(ele, encoding)
1401 c2b3014a Leonidas Poulopoulos
    return xml if xml.startswith('<?xml') else '<?xml version="1.0" encoding="%s"?>%s' % (encoding, xml)
1402 c2b3014a Leonidas Poulopoulos
    
1403 38f2278d Leonidas Poulopoulos
def getInstContacts(inst):
1404 38f2278d Leonidas Poulopoulos
    contacts = InstitutionContactPool.objects.filter(institution=inst)
1405 38f2278d Leonidas Poulopoulos
    contact_pks = []
1406 38f2278d Leonidas Poulopoulos
    for contact in contacts:
1407 38f2278d Leonidas Poulopoulos
        contact_pks.append(contact.contact.pk)
1408 38f2278d Leonidas Poulopoulos
    return list(set(contact_pks))
1409 38f2278d Leonidas Poulopoulos
1410 04df82f7 Leonidas Poulopoulos
def getInstServers(inst):
1411 04df82f7 Leonidas Poulopoulos
    servers = InstServer.objects.filter(instid=inst)
1412 04df82f7 Leonidas Poulopoulos
    server_pks = []
1413 04df82f7 Leonidas Poulopoulos
    for server in servers:
1414 04df82f7 Leonidas Poulopoulos
        server_pks.append(server.pk)
1415 04df82f7 Leonidas Poulopoulos
    return list(set(server_pks))
1416 04df82f7 Leonidas Poulopoulos
1417 04df82f7 Leonidas Poulopoulos
1418 af9d484b Leonidas Poulopoulos
def rad(x):
1419 c2b3014a Leonidas Poulopoulos
    return x*math.pi/180
1420 7973b176 Leonidas Poulopoulos
1421 7973b176 Leonidas Poulopoulos
def send_new_mail(subject, message, from_email, recipient_list, bcc_list):
1422 7973b176 Leonidas Poulopoulos
    return EmailMessage(subject, message, from_email, recipient_list, bcc_list).send()