File size: 4,025 Bytes
669c0b8
f85cf8e
6768aaa
1dbd98a
669c0b8
013c0dc
f243d4f
6768aaa
 
 
f243d4f
1dbd98a
f85cf8e
1dbd98a
f85cf8e
f243d4f
 
1dbd98a
f85cf8e
1dbd98a
f85cf8e
 
 
 
 
 
 
 
6768aaa
f243d4f
 
 
 
 
 
f85cf8e
 
 
4dec878
 
 
f243d4f
 
 
 
 
 
99cd51c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fbb706
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse
from django.views import generic

from .models import Question, Quiz


class IndexView(generic.ListView):
    model = Quiz
    template_name = "quizzes/index.html"


def display_quiz(request, quiz_id):
    quiz = get_object_or_404(Quiz, pk=quiz_id)
    question = quiz.question_set.first()
    return redirect(reverse("quizzes:display_question", kwargs={"quiz_id": quiz_id, "question_id": question.pk}))


def display_question(request, quiz_id, question_id):
    quiz = get_object_or_404(Quiz, pk=quiz_id)
    # fetch ALL of the questions to find current and next question
    questions = quiz.question_set.all()
    current_question, next_question = None, None
    for ind, question in enumerate(questions):
        if question.pk == question_id:
            current_question = question
            if ind != len(questions) - 1:
                next_question = questions[ind + 1]

    return render(
        request,
        "quizzes/display.html",
        {"quiz": quiz, "question": current_question, "next_question": next_question},
    )


def grade_question(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    answer = question.get_answer()
    if answer is None:
        return render(request, "quizzes/partial.html", {"error": "Question must have an answer"}, status=422)
    is_correct = answer.is_correct(request.POST.get("answer"))
    return render(
        request,
        "quizzes/partial.html",
        {"is_correct": is_correct, "correct_answer": answer.correct_answer},
    )


def grade_question(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    answer = question.get_answer()
    if answer is None:
        return render(request, "quizzes/partial.html", {"error": "Question must have an answer"}, status=422)

    user_answer = request.POST.get("answer")
    if hasattr(answer, "grade"):
        result = answer.grade(user_answer)
        is_correct = result.get("result") == "success"
        correct_answer = result.get("message")
    else:
        is_correct = answer.is_correct(user_answer)
        correct_answer = answer.correct_answer

    return render(
        request,
        "quizzes/partial.html",
        {"is_correct": is_correct, "correct_answer": correct_answer},
    )


def grade_question(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    answer = question.get_answer()
    if answer is None:
        return render(request, "quizzes/partial.html", {"error": "Question must have an answer"}, status=422)

    user_answer = request.POST.get("answer")
    llm_answer = None
    if hasattr(answer, "grade"):
        result = answer.grade(user_answer)
        is_correct = result.get("result") == "success"
        llm_answer = result.get("message")
    else:
        is_correct = answer.is_correct(user_answer)
        llm_answer = None

    return render(
        request,
        "quizzes/partial.html",
        {
            "is_correct": is_correct,
            "correct_answer": answer.correct_answer,
            "llm_answer": llm_answer,
        },
    )


def grade_question(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    answer = question.get_answer()
    if answer is None:
        return render(request, "quizzes/partial.html", {"error": "Question must have an answer"}, status=422)

    user_answer = request.POST.get("answer")
    llm_answer = None
    if hasattr(answer, "grade"):
        result = answer.grade(user_answer)
        is_correct = result.get("result") == "success"
        llm_answer = result.get("message")
    else:
        is_correct = answer.is_correct(user_answer)
        llm_answer = None

    return render(
        request,
        "quizzes/partial.html",
        {
            "is_correct": is_correct,
            "correct_answer": answer.correct_answer,
            "llm_answer": llm_answer,
        },
    )