Exemplo n.º 1
0
def taken(request, pk):
    quiz = get_object_or_404(Quiz, pk=pk)
    user = get_current_user(request)
    answers = None

    try:
        answers = Answers.objects.get(answerer__pk=user.pk, quiz__pk=quiz.pk)
        answers.choices = []
    except (Answers.DoesNotExist):
        answers = Answers(quiz=quiz, answerer=user)

    try:
        for question in quiz.question_set.all():
            choice_pk = request.POST[f'question_{question.pk}_choice']
            answers.choices.append(choice_pk)
    except (KeyError, Choice.DoesNotExist):
        return render(
            request, 'quizzes/take.html', {
                'current_user': get_current_user(request),
                'quiz': quiz,
                'error_messages': ['All questions must be answered.']
            })
    answers.save()

    quiz.taken_by.add(user)
    quiz.save()

    return HttpResponseRedirect(
        reverse('users:taken_quizzes', args=(user.username, )))
Exemplo n.º 2
0
def details(request, pk):
    quiz = get_object_or_404(Quiz, pk=pk)

    return render(request, 'quizzes/details.html', {
        'current_user': get_current_user(request),
        'quiz': quiz
    })
Exemplo n.º 3
0
def take(request, pk):
    quiz = get_object_or_404(Quiz, pk=pk)

    return render(
        request, 'quizzes/take.html', {
            'current_user': get_current_user(request),
            'quiz': quiz,
            'error_messages': []
        })
Exemplo n.º 4
0
def home(request):
    return render(request, 'QuizSite/home.html', {
        'current_user': get_current_user(request)
    })
Exemplo n.º 5
0
def creating(request):
    # get questions
    post_copy = copy.deepcopy(request.POST)
    question_list = []
    choice_dict = {}
    quiz_name = ''
    curr_question_index = 0
    do_loop = True
    while do_loop:
        do_loop = False
        for key in post_copy:
            val = post_copy[key]
            if val is None:
                continue

            contains_question = key.count('question') == 1
            contains_choice = key.count('choice') == 1

            # question_1_choice_1
            if contains_question and contains_choice:
                split = key.split('_')
                question_index = int(split[1])
                choice_index = int(split[-1])
                dict_val = choice_dict.get(question_index)
                if dict_val is None:
                    choice_dict[question_index] = [val]
                    post_copy[key] = None
                elif len(dict_val) is choice_index:
                    dict_val.append(val)
                    post_copy[key] = None
                do_loop = True

            # question_1
            elif contains_question:
                if key.endswith(str(curr_question_index)):
                    question_list.append(val)
                    curr_question_index += 1
                    post_copy[key] = None
                    do_loop = True
            elif key is not 'csrfmiddlewaretoken':
                quiz_name = val
                post_copy[key] = None

    error_messages = validate_nonempty_quiz_elements(quiz_name, question_list,
                                                     choice_dict)
    if error_messages != []:
        return render(
            request, 'quizzes/create.html', {
                'current_user': get_current_user(request),
                'error_messages': error_messages
            })

    user = get_current_user(request)
    new_quiz = Quiz(author=user, name=quiz_name)
    new_quiz.save()

    index = 0
    for question in question_list:
        new_question = Question(quiz=new_quiz, text=question)
        new_question.save()
        for choice in choice_dict[index]:
            new_choice = Choice(question=new_question, text=choice)
            new_choice.save()

    return HttpResponseRedirect(
        reverse('users:own_quizzes', args=(user.username, )))
Exemplo n.º 6
0
def create(request):
    return render(request, 'quizzes/create.html',
                  {'current_user': get_current_user(request)})