kpsul/gestioncof/views.py
Guillaume Seguin 8e1bf7b705 Initial import
2012-06-27 23:28:35 +02:00

109 lines
5 KiB
Python

from django.shortcuts import redirect, get_object_or_404
from django.template import RequestContext, loader
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django import forms
from django.forms.widgets import RadioSelect, CheckboxSelectMultiple
from gestioncof.models import Survey, SurveyQuestion, SurveyQuestionAnswer, SurveyAnswer
from gestioncof.models import Event, EventOption, EventOptionChoice, EventRegistration
def render_page (request, data, template):
template = loader.get_template (template)
context = RequestContext (request, data)
return HttpResponse (template.render (context))
@login_required
def home(request):
data = {"surveys": Survey.objects.filter(survey_open = True).all(),
"events": Event.objects.filter(registration_open = True).all()}
return render_page(request, data, "home.html")
def login(request):
if request.user.is_authenticated():
return redirect("gestioncof.views.home")
return render_page(request, {}, "login_switch.html")
@login_required
def logout(request):
if request.user.get_profile().login_clipper:
return redirect("django_cas.views.logout")
else:
return redirect("django.contrib.auth.views.logout")
class SurveyForm(forms.Form):
def __init__(self, *args, **kwargs):
survey = kwargs.pop("survey")
current_answers = kwargs.pop("current_answers", None)
super(SurveyForm, self).__init__(*args, **kwargs)
answers = {}
if current_answers:
for answer in current_answers.all():
if answer.survey_question.id not in answers:
answers[answer.survey_question.id] = [answer.id]
else:
answers[answer.survey_question.id].append(answer.id)
for question in survey.questions.all():
choices = [(answer.id, answer.answer) for answer in question.answers.all()]
if question.multi_answers:
initial = [] if question.id not in answers else answers[question.id]
field = forms.MultipleChoiceField(label = question.question,
choices = choices,
widget = CheckboxSelectMultiple,
required = False,
initial = initial)
else:
initial = None if question.id not in answers else answers[question.id][0]
field = forms.ChoiceField(label = question.question,
choices = choices,
widget = RadioSelect,
required = False,
initial = initial)
field.question_id = question.id
self.fields["question_%d" % question.id] = field
def answers(self):
for name, value in self.cleaned_data.items():
if name.startswith('question_'):
yield (self.fields[name].question_id, value)
@login_required
def survey(request, survey_id):
survey = get_object_or_404(Survey, id = survey_id)
if not survey.survey_open:
raise Http404
success = False
if request.method == "POST":
form = SurveyForm(request.POST, survey = survey)
if form.is_valid():
all_answers = []
for question_id, answers_ids in form.answers():
question = get_object_or_404(SurveyQuestion, id = question_id,
survey = survey)
if type(answers_ids) != list:
answers_ids = [answers_ids]
if not question.multi_answers and len(answers_ids) > 1:
raise Http404
for answer_id in answers_ids:
answer_id = int(answer_id)
answer = SurveyQuestionAnswer.objects.get(
id = answer_id,
survey_question = question)
all_answers.append(answer)
try:
current_answer = SurveyAnswer.objects.get(user = request.user, survey = survey)
except SurveyAnswer.DoesNotExist:
current_answer = SurveyAnswer(user = request.user, survey = survey)
current_answer.save()
current_answer.answers = all_answers
current_answer.save()
success = True
else:
try:
current_answer = SurveyAnswer.objects.get(user = request.user, survey = survey)
form = SurveyForm(survey = survey, current_answers = current_answer.answers)
except SurveyAnswer.DoesNotExist:
form = SurveyForm(survey = survey)
return render_page(request, {"survey": survey, "form": form, "success": success}, "survey.html")