root / edumanage / views.py @ b1e7b4ee
History | View | Annotate | Download (10.7 kB)
1 |
# -*- coding: utf-8 -*- vim:encoding=utf-8:
|
---|---|
2 |
# vim: tabstop=4:shiftwidth=4:softtabstop=4:expandtab
|
3 |
|
4 |
from django.shortcuts import render_to_response,get_object_or_404 |
5 |
from django.http import HttpResponse,HttpResponseRedirect,Http404 |
6 |
from django.template import RequestContext |
7 |
from django.core.urlresolvers import reverse |
8 |
from django.contrib.auth.decorators import login_required |
9 |
from edumanage.models import * |
10 |
from edumanage.forms import * |
11 |
from django import forms |
12 |
from django.forms.models import modelformset_factory |
13 |
from django.forms.models import inlineformset_factory |
14 |
from django.contrib.contenttypes.generic import generic_inlineformset_factory |
15 |
import json |
16 |
|
17 |
def index(request): |
18 |
return render_to_response('base.html', context_instance=RequestContext(request)) |
19 |
|
20 |
@login_required
|
21 |
def manage(request): |
22 |
services_list = [] |
23 |
servers_list = [] |
24 |
user = request.user |
25 |
try:
|
26 |
profile = user.get_profile() |
27 |
inst = profile.institution |
28 |
except UserProfile.DoesNotExist:
|
29 |
inst = False
|
30 |
services = ServiceLoc.objects.filter(institutionid=inst) |
31 |
services_list.extend([s for s in services]) |
32 |
servers = InstServer.objects.filter(instid=inst) |
33 |
servers_list.extend([s for s in servers]) |
34 |
return render_to_response('edumanage/welcome.html', |
35 |
{ |
36 |
'institution': inst,
|
37 |
'services': services_list,
|
38 |
'servers': servers_list
|
39 |
}, |
40 |
context_instance=RequestContext(request)) |
41 |
|
42 |
@login_required
|
43 |
def institutions(request): |
44 |
user = request.user |
45 |
dict = {} |
46 |
|
47 |
try:
|
48 |
profile = user.get_profile() |
49 |
inst = profile.institution |
50 |
inst.__unicode__ = inst.get_name(request.LANGUAGE_CODE) |
51 |
except UserProfile.DoesNotExist:
|
52 |
inst = False
|
53 |
dict['institution'] = inst.pk |
54 |
form = InstDetailsForm(initial=dict)
|
55 |
form.fields['institution'].widget.attrs['readonly'] = True |
56 |
return render_to_response('edumanage/institution.html', |
57 |
{ |
58 |
'institution': inst,
|
59 |
'form': form,
|
60 |
}, |
61 |
context_instance=RequestContext(request)) |
62 |
|
63 |
|
64 |
|
65 |
@login_required
|
66 |
def add_institution_details(request, institution_pk): |
67 |
user = request.user |
68 |
try:
|
69 |
profile = user.get_profile() |
70 |
inst = profile.institution |
71 |
except UserProfile.DoesNotExist:
|
72 |
inst = False
|
73 |
|
74 |
if (not inst) or (int(inst.pk) != int(institution_pk)): |
75 |
# messages.add_message(request, messages.WARNING,
|
76 |
# _("Insufficient rights on Institution. Contact your administrator"))
|
77 |
return HttpResponseRedirect(reverse("institutions")) |
78 |
if request.method == "GET": |
79 |
|
80 |
# Determine add or edit
|
81 |
request_data = request.POST.copy() |
82 |
try:
|
83 |
inst_details = InstitutionDetails.objects.get(institution=inst) |
84 |
form = InstDetailsForm(instance=inst_details) |
85 |
except InstitutionDetails.DoesNotExist:
|
86 |
form = InstDetailsForm() |
87 |
form.fields['institution'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=institution_pk), empty_label=None) |
88 |
|
89 |
|
90 |
return render_to_response('edumanage/institution_edit.html', { 'institution': inst, 'form': form}, |
91 |
context_instance=RequestContext(request)) |
92 |
elif request.method == 'POST': |
93 |
request_data = request.POST.copy() |
94 |
try:
|
95 |
inst_details = InstitutionDetails.objects.get(institution=inst) |
96 |
form = InstDetailsForm(request_data, instance=inst_details) |
97 |
except InstitutionDetails.DoesNotExist:
|
98 |
form = InstDetailsForm(request_data) |
99 |
if form.is_valid():
|
100 |
instdets = form.save() |
101 |
print "SAVED M2M" |
102 |
return HttpResponseRedirect(reverse("institutions")) |
103 |
else:
|
104 |
try:
|
105 |
profile = user.get_profile() |
106 |
inst = profile.institution |
107 |
except UserProfile.DoesNotExist:
|
108 |
inst = False
|
109 |
form.fields['institution'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=institution_pk), empty_label=None) |
110 |
return render_to_response('edumanage/institution_edit.html', { 'institution': inst, 'form': form}, |
111 |
context_instance=RequestContext(request)) |
112 |
|
113 |
|
114 |
@login_required
|
115 |
def services(request): |
116 |
user = request.user |
117 |
dict = {} |
118 |
|
119 |
try:
|
120 |
profile = user.get_profile() |
121 |
inst = profile.institution |
122 |
except UserProfile.DoesNotExist:
|
123 |
inst = False
|
124 |
try:
|
125 |
services = ServiceLoc.objects.filter(institutionid = inst) |
126 |
except ServiceLoc.DoesNotExist:
|
127 |
services = False
|
128 |
|
129 |
|
130 |
return render_to_response('edumanage/services.html', |
131 |
{ |
132 |
'institution': inst,
|
133 |
'services': services,
|
134 |
}, |
135 |
context_instance=RequestContext(request)) |
136 |
|
137 |
|
138 |
|
139 |
@login_required
|
140 |
def add_services(request, service_pk): |
141 |
user = request.user |
142 |
service = False
|
143 |
try:
|
144 |
profile = user.get_profile() |
145 |
inst = profile.institution |
146 |
except UserProfile.DoesNotExist:
|
147 |
inst = False
|
148 |
|
149 |
if request.method == "GET": |
150 |
|
151 |
# Determine add or edit
|
152 |
try:
|
153 |
service = ServiceLoc.objects.get(institutionid=inst, pk=service_pk) |
154 |
form = ServiceLocForm(instance=service) |
155 |
except ServiceLoc.DoesNotExist:
|
156 |
form = ServiceLocForm() |
157 |
form.fields['institutionid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None) |
158 |
|
159 |
NameFormSet = generic_inlineformset_factory(Name_i18n, extra=2)
|
160 |
names_form = NameFormSet() |
161 |
if (service):
|
162 |
NameFormSet = generic_inlineformset_factory(Name_i18n, extra=0, formset=NameFormSetFact)
|
163 |
names_form = NameFormSet(instance=service) |
164 |
return render_to_response('edumanage/services_edit.html', { 'form': form, 'services_form':names_form}, |
165 |
context_instance=RequestContext(request)) |
166 |
elif request.method == 'POST': |
167 |
request_data = request.POST.copy() |
168 |
NameFormSet = generic_inlineformset_factory(Name_i18n, extra=0, formset=NameFormSetFact)
|
169 |
try:
|
170 |
service = ServiceLoc.objects.get(institutionid=inst, pk=service_pk) |
171 |
form = ServiceLocForm(request_data, instance=service) |
172 |
print "OKOK" |
173 |
names_form = NameFormSet(request_data, instance=service) |
174 |
except ServiceLoc.DoesNotExist:
|
175 |
form = ServiceLocForm(request_data) |
176 |
names_form = NameFormSet(request_data) |
177 |
|
178 |
if form.is_valid() and names_form.is_valid(): |
179 |
serviceloc = form.save() |
180 |
|
181 |
|
182 |
for nform in names_form.forms: |
183 |
names = nform.save(commit=False)
|
184 |
names.content_object = serviceloc |
185 |
names.save() |
186 |
return HttpResponseRedirect(reverse("services")) |
187 |
else:
|
188 |
form.fields['institutionid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None) |
189 |
|
190 |
return render_to_response('edumanage/services_edit.html', { 'institution': inst, 'form': form, 'services_form':names_form}, |
191 |
context_instance=RequestContext(request)) |
192 |
|
193 |
|
194 |
@login_required
|
195 |
def get_service_points(request): |
196 |
if request.method == "GET": |
197 |
user = request.user |
198 |
try:
|
199 |
profile = user.get_profile() |
200 |
inst = profile.institution |
201 |
except UserProfile.DoesNotExist:
|
202 |
inst = False
|
203 |
servicelocs = ServiceLoc.objects.filter(institutionid=inst) |
204 |
locs = [] |
205 |
for sl in servicelocs: |
206 |
response_location = {} |
207 |
response_location['lat'] = u"%s"%sl.latitude |
208 |
response_location['lng'] = u"%s"%sl.longitude |
209 |
response_location['address'] = u"%s<br>%s"%(sl.address_street, sl.address_city) |
210 |
response_location['enc'] = u"%s"%(sl.enc_level) |
211 |
response_location['AP_no'] = u"%s"%(sl.AP_no) |
212 |
response_location['name'] = sl.loc_name.get(lang='en').name |
213 |
response_location['port_restrict'] = u"%s"%(sl.port_restrict) |
214 |
response_location['transp_proxy'] = u"%s"%(sl.transp_proxy) |
215 |
response_location['IPv6'] = u"%s"%(sl.IPv6) |
216 |
response_location['NAT'] = u"%s"%(sl.NAT) |
217 |
response_location['wired'] = u"%s"%(sl.wired) |
218 |
response_location['SSID'] = u"%s"%(sl.SSID) |
219 |
response_location['key'] = u"%s"%sl.pk |
220 |
locs.append(response_location) |
221 |
return HttpResponse(json.dumps(locs), mimetype='application/json') |
222 |
else:
|
223 |
return HttpResponseNotFound('<h1>Something went really wrong</h1>') |
224 |
|
225 |
|
226 |
def servers(request): |
227 |
servers = InstServer.objects.all() |
228 |
return render_to_response('edumanage/servers.html', { 'servers': servers}, |
229 |
context_instance=RequestContext(request)) |
230 |
|
231 |
|
232 |
@login_required
|
233 |
def add_server(request, server_pk): |
234 |
user = request.user |
235 |
server = False
|
236 |
try:
|
237 |
profile = user.get_profile() |
238 |
inst = profile.institution |
239 |
except UserProfile.DoesNotExist:
|
240 |
inst = False
|
241 |
|
242 |
if request.method == "GET": |
243 |
|
244 |
# Determine add or edit
|
245 |
try:
|
246 |
server = InstServer.objects.get(instid=inst, pk=server_pk) |
247 |
form = InstServerForm(instance=server) |
248 |
except InstServer.DoesNotExist:
|
249 |
form = InstServerForm() |
250 |
form.fields['instid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None) |
251 |
|
252 |
return render_to_response('edumanage/servers_edit.html', { 'form': form}, |
253 |
context_instance=RequestContext(request)) |
254 |
elif request.method == 'POST': |
255 |
request_data = request.POST.copy() |
256 |
try:
|
257 |
server = InstServer.objects.get(instid=inst, pk=server_pk) |
258 |
form = InstServerForm(request_data, instance=server) |
259 |
except InstServer.DoesNotExist:
|
260 |
form = InstServerForm(request_data) |
261 |
|
262 |
if form.is_valid():
|
263 |
instserverf = form.save() |
264 |
return HttpResponseRedirect(reverse("servers")) |
265 |
else:
|
266 |
form.fields['instid'] = forms.ModelChoiceField(queryset=Institution.objects.filter(pk=inst.pk), empty_label=None) |
267 |
print form.errors
|
268 |
return render_to_response('edumanage/servers_edit.html', { 'institution': inst, 'form': form}, |
269 |
context_instance=RequestContext(request)) |