Пример #1
0
def edit_quiz(request,quiz_id):
    try:
        # check only the owner can edit
        quiz = Quiz.objects.get(pk=quiz_id,deleted=0,owner=request.user)
    except Quiz.DoesNotExist:
        raise Http404
    # TODO check a quiz can't be edited if already has attempts not by the owner
    # TODO don't allow editing of quizzes with digest props?
    QuestionFormSet = formset_factory(QuestionForm, extra=0, formset=BaseQuestionFormSet)
    
    if request.method == 'POST':
        quiz_form = QuizForm(request.POST)
        question_formset = QuestionFormSet(request.POST)
        if quiz_form.is_valid() and question_formset.is_valid():
            quiz.title = quiz_form.cleaned_data.get("title").strip()
            quiz.description = quiz_form.cleaned_data.get("description").strip()
            quiz.lastupdated_date = datetime.datetime.now()
            quiz.save()
            
            # delete all quiz questions and props
            Question.objects.filter(quiz=quiz).delete()
            QuizProps.objects.filter(quiz=quiz).delete()
            
            process_quiz(request,quiz,question_formset)
            return HttpResponseRedirect('../saved/')
    else:
        quiz_form = QuizForm(initial={'title':quiz.title,
                                    'description':quiz.description})
        initial = []
        questions = Question.objects.filter(quiz=quiz)
        for q in questions:
            data = {}
            data['title'] = q.title
            data['type'] = q.type
            responses = Response.objects.filter(question=q)
            for idx, r in enumerate(responses):
                data['response'+str(idx+1)] = r.title
                data['score'+str(idx+1)] = r.score
                try:
                    r_feedback = ResponseProps.objects.get(response=r,name='feedback')
                    data['feedback'+str(idx+1)] = r_feedback.value
                except:
                    data['feedback'+str(idx+1)] = ""
            initial.append(data)
        question_formset = QuestionFormSet(initial=initial)

    return render(request, 'mquiz/quiz/quiz.html', {'title': _(u"Edit Quiz"), 'quiz_form': quiz_form,'question_formset':question_formset})
Пример #2
0
def create_quiz(request):
    QuestionFormSet = formset_factory(QuestionForm, extra=3, formset=BaseQuestionFormSet)
    
    if request.method == 'POST':
        quiz_form = QuizForm(request.POST)
        question_formset = QuestionFormSet(request.POST)
        
        if quiz_form.is_valid() and question_formset.is_valid():
            quiz = quiz_form.save(commit=False)
            quiz.lastupdated_date = datetime.datetime.now()
            quiz.owner = request.user
            quiz.save()
            process_quiz(request, quiz, question_formset)
            return HttpResponseRedirect('../%d/saved/' % quiz.id)
    else:
        quiz_form = QuizForm() # An unbound form
        question_formset = QuestionFormSet()

    return render(request, 'mquiz/quiz/quiz.html', {'title': _(u"Create Quiz"), 'quiz_form': quiz_form,'question_formset':question_formset})