1 | from django.core import serializers
|
---|
2 | from django.http import HttpResponse
|
---|
3 | from django.template import RequestContext
|
---|
4 |
|
---|
5 | from django.shortcuts import render_to_response, get_object_or_404
|
---|
6 | from foobar.models import Service, Role, Alert
|
---|
7 |
|
---|
8 | from django.forms import ModelForm
|
---|
9 | from django.forms.models import modelformset_factory
|
---|
10 |
|
---|
11 |
|
---|
12 |
|
---|
13 | # Create your views here.
|
---|
14 | def index(request):
|
---|
15 | if request.POST.has_key('roles'):
|
---|
16 | for role in request.POST['roles'].split(','):
|
---|
17 | for service in request.POST['services'].split(','):
|
---|
18 | alert = Alert.objects.get(role__name=role,service__name=service)
|
---|
19 | if request.POST.has_key('%s-%s' % (role, service)):
|
---|
20 | alert.enable = True
|
---|
21 | else:
|
---|
22 | alert.enable = False
|
---|
23 | alert.save()
|
---|
24 |
|
---|
25 | service_list = Service.objects.all()
|
---|
26 | role_list = Role.objects.all()
|
---|
27 | alert_list = Alert.objects.all()
|
---|
28 | AlertFormSet = modelformset_factory(Alert, extra=0)
|
---|
29 | formset = AlertFormSet(queryset=Alert.objects.all())
|
---|
30 | data = { 'data' : {},
|
---|
31 | 'roles' : ','.join(map(str,Role.objects.all())),
|
---|
32 | 'services' : ','.join(map(str,Service.objects.all()))}
|
---|
33 | for role in role_list:
|
---|
34 | data['data'][role] = {}
|
---|
35 | for service in service_list:
|
---|
36 | data['data'][role][service] = Alert.objects.get(role=role,service=service).enable
|
---|
37 |
|
---|
38 | return render_to_response('foobar/index.html', { 'service_list' : service_list, 'role_list' : role_list, 'alert_list' : alert_list, 'formset' : formset, 'data' : data }, context_instance=RequestContext(request))
|
---|