Source

teapoll / survey / views.py

Full commit
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext

from . import models as survey_models

@login_required
def list_surveys(request):
  return render_to_response('survey/list_surveys.html', dict(
    surveys = survey_models.Survey.objects.all()),
    context_instance = RequestContext(request))

@login_required
def enter_survey_instance(request, survey_id):
  survey = get_object_or_404(survey_models.Survey, pk = survey_id)
  if request.method == 'POST':
    try:
      subject = survey.submit(request.POST, user = request.user)
      return render_to_response('survey/submit_done.html', dict(
        survey = survey, subject = subject), context_instance = RequestContext(request))
    except survey_models.ValidationError:
      pass

  return render_to_response('survey/enter.html', dict(
    survey = survey), context_instance = RequestContext(request))

@login_required
def list_subjects(request, survey_id):
  survey = get_object_or_404(survey_models.Survey, pk = survey_id)
  return render_to_response('survey/list_subjects.html', dict(
    survey = survey), context_instance = RequestContext(request))

@login_required
def edit_survey_instance(request, survey_id, subject_id):
  survey = get_object_or_404(survey_models.Survey, pk = survey_id)
  subject = get_object_or_404(survey_models.Subject, pk = subject_id)
  if request.method == 'POST':
    try:
      survey.submit(request.POST, subject)
      return render_to_response('survey/submit_done.html', dict(
        survey = survey, subject = subject), context_instance = RequestContext(request))
    except survey_models.ValidationError:
      pass

  answers = []
  for question in survey.questions.all():
    if question.get_form() is None:
      continue
    
    answer = survey.subject_answers.get(question = question, subject = subject)
    answers.append(answer)

  return render_to_response('survey/edit.html', dict(
    survey = survey,
    subject = subject,
    answers = answers), context_instance = RequestContext(request))