def question_create(request, quiz_id, question_id=None): """ returns a template to create a new quiz. """ quiz = get_object_or_404(Quiz, id=quiz_id, owner=request.user) iscomplete = True if request.method == "POST": question_form = QuestionForm(request.POST) answer_formset = AnswerFormSet(request.POST) if question_form.is_valid() and answer_formset.is_valid(): """ @todo : this must be moved to the quiz-manager """ new_question = question_form.save(commit=False) new_question.number = (quiz.quiz_questions.aggregate(Max('number'))['number__max'] or 0) +1 new_question.owner = request.user new_question.quiz = quiz new_question.save() for answer_form in answer_formset.forms: new_answer = answer_form.save(commit=False) new_answer.owner = request.user new_answer.question = new_question new_answer.save() ##status updation is ugly, must be fixed. iscomplete = iscomplete and not (new_answer.option == '') ##status updation is ugly, must be fixed. iscomplete = iscomplete and not (new_question.text == '') if iscomplete: new_question.status = "complete" new_question.save() if len(quiz.quiz_questions.filter(status="incomplete")) == 0: quiz.status = "complete" else: quiz.status = "incomplete" quiz.save() request.user.message_set.create(message="the question was created successfully. yeppie! add another") return HttpResponseRedirect(reverse("quiz.views.question_create", args=[quiz_id])) #GET Request else: question_form = QuestionForm() answer_formset = AnswerFormSet(queryset=Answer.objects.none()) return render_to_response('quiz/question_form.html', { "question_form" : question_form, "answer_formset" : answer_formset, "questions" : quiz.quiz_questions.all().order_by('number'), "quizobj" : quiz, "selected_button" : "create", "current_question" : (quiz.quiz_questions.aggregate(Max('number'))['number__max'] or 0) +1, "pagetitle" : "Create Question", }, context_instance=RequestContext(request))
def get_context_data(self, **kwargs): context = super(QuestionCreatePageView, self).get_context_data(**kwargs) context['question_form'] = QuestionForm() if self.request.POST: context['answer_formset'] = AnswerFormSet(self.request.POST) else: context['answer_formset'] = AnswerFormSet(initial=[{ 'correct': True }]) context['test'] = 'test' return context
def question_update(request, quiz_id, question_id): """ opens the selected question in the preview mode. """ quiz = get_object_or_404(Quiz, id=quiz_id, owner=request.user) question = get_object_or_404(Question, quiz=quiz, number=question_id) answers = Answer.objects.filter(question=question) iscomplete = True if request.method == "POST": question_form = QuestionForm(request.POST, instance=question) answer_formset = AnswerFormSet(request.POST) if question_form.is_valid() and answer_formset.is_valid(): answers.delete() new_question = question_form.save() for answer_form in answer_formset.forms: answer_form = answer_form.save(commit=False) answer_form.owner = request.user answer_form.question = question answer_form.save() ##status updation is ugly, must be fixed. iscomplete = iscomplete and not (answer_form.option == '') ##status updation is ugly, must be fixed. iscomplete = iscomplete and not (new_question.text == '') if iscomplete: new_question.status = "complete" else: new_question.status = "incomplete" new_question.save() if len(quiz.quiz_questions.filter(status="incomplete")) == 0: quiz.status = "complete" else: quiz.status = "incomplete" quiz.save() request.user.message_set.create(message="the question was updated successfully :D ") return HttpResponseRedirect(reverse("quiz.views.question_view", args=[quiz_id, question_id])) #GET Request else: question_form = QuestionForm(instance=question) answer_formset = AnswerFormSet(queryset=Answer.objects.filter(question=question)) return render_to_response("quiz/question_form.html", { "quizobj" : quiz, "questions" : quiz.quiz_questions.all().order_by('number'), "question_form" : question_form, "answer_formset" : answer_formset, "selected_question": question, "pagetitle" : "Update Question", }, context_instance=RequestContext(request))