from django.core import serializers from django.http import HttpResponse from django.template import RequestContext from django.shortcuts import render_to_response, get_object_or_404 from foobar.models import Service, Role, Alert from django.forms import ModelForm from django.forms.models import modelformset_factory # Create your views here. def index(request): if request.POST.has_key('roles'): for role in request.POST['roles'].split(','): for service in request.POST['services'].split(','): alert = Alert.objects.get(role__name=role,service__name=service) if request.POST.has_key('%s-%s' % (role, service)): alert.enable = True else: alert.enable = False alert.save() service_list = Service.objects.all() role_list = Role.objects.all() alert_list = Alert.objects.all() AlertFormSet = modelformset_factory(Alert, extra=0) formset = AlertFormSet(queryset=Alert.objects.all()) data = { 'data' : {}, 'roles' : ','.join(map(str,Role.objects.all())), 'services' : ','.join(map(str,Service.objects.all()))} for role in role_list: data['data'][role] = {} for service in service_list: data['data'][role][service] = Alert.objects.get(role=role,service=service).enable 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))