def submit_form(request):
    if ('going-to-vote' not in request.POST):
        return questionnaire_form(request, "Please say whether you're planning to vote")

    going_to_vote = request.POST['going-to-vote']
    constituency_id = request.POST['constituency']

    if (not constituency_id.isdigit()):
        return questionnaire_form(request, "Please select a constituency")

    constituency = Constituency.objects.get(id=constituency_id)
    parties = Party.objects.filter(abbreviation=request.POST['party'])
    party = None

    if (parties.count() > 0):
        party = parties[0]

    answer_set = AnswerSet(
        constituency=constituency,
        voting_for=party,
        going_to_vote=request.POST['going-to-vote']
    )
    answer_set.save()

    return HttpResponseRedirect(reverse('results', args=(constituency.id,)))
    def test_creation(self):
        """ Create answerset """

        constituency = Constituency(name="Merthyr Tydfil and Rhymney")
        constituency.save()

        party = Party(name="Plaid Cymru", abbreviation="PC")
        party.save()

        answers = AnswerSet(
            constituency=constituency,
            going_to_vote="yes",
            voting_for=party
        )
        answers.save()
 def test_save_override(self):
     '''
         The overriden save method should ensure that the correct question answert is stored in the 
         LatestQuestionAnswer table
         
         If there is no record for this question in this answer_set then create one with this question answer
         If there is a record, but the latest is not this then update it to make this the latest
         If there is a record, and it is already set to this do nothing
     '''
     #intially we need an answerset
     test_answer_set = AnswerSet(user=User.objects.create_user('test', '*****@*****.**', 'test') ,
                                  questionnaire=Questionnaire.objects.get(pk=1), questiongroup=QuestionGroup.objects.get(pk=1))
     
     test_answer_set.save()
     #initialy there shouldnt be any record in the latestquestionanswer so the function should create one
     an_answer = QuestionAnswer(question=Question.objects.get(pk=1),
                                answer='my answer',
                                answer_set = test_answer_set)
     an_answer.save()
     self.assertEqual(len(LatestQuestionAnswer.objects.all()), 1)
     self.assertEqual(LatestQuestionAnswer.objects.latest('id').question_answer, an_answer)
     #now if we save a new question answer then the record should be updated
     
     a_new_answer = QuestionAnswer(question=Question.objects.get(pk=1),
                                answer='my new answer',
                                answer_set = test_answer_set)
     a_new_answer.save()
     self.assertEqual(len(LatestQuestionAnswer.objects.all()), 1)
     self.assertEqual(LatestQuestionAnswer.objects.latest('id').question_answer, a_new_answer)
     a_new_answer_created = LatestQuestionAnswer.objects.latest('id').created
     
     #then if we save this question asnwer again then nothing should happen
     
     a_new_answer.save()
     self.assertEqual(len(LatestQuestionAnswer.objects.all()), 1)
     self.assertEqual(LatestQuestionAnswer.objects.latest('id').question_answer, a_new_answer)
     self.assertEqual(LatestQuestionAnswer.objects.latest('id').created, a_new_answer_created)#the timestamp should'nt have changed