-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
209 lines (185 loc) · 6.56 KB
/
main.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from fasthtml.common import *
import json
from models import QuizQuestion, User
from database import Database
import markdown
import atexit
app, rt = fast_app(hdrs=(Link(rel="stylesheet", href="/static/styles.css"),))
db = Database()
def load_quiz(level):
with open(f'content/quiz-{level}.json', 'r') as f:
return [QuizQuestion(**q) for q in json.load(f)]
def load_tutorial(level):
with open(f'content/tutorial-{level}.md', 'r') as f:
return markdown.markdown(f.read())
def update_tutorial_buttons(username):
user = db.get_user(username)
return Div(
*[Button(f"Level {i}", hx_get=f"/tutorial/{i}?username={username}", hx_target="#content")
for i in range(1, user.current_level + 1)],
id="tutorial-links-content",
hx_swap_oob="true"
)
@rt('/')
def get():
return Titled("Python RE Tutorial",
Div(
Div(
Div(
Form(
Input(name="username", placeholder="Enter your username"),
Button("Start", type="submit"),
hx_post="/start",
hx_target="#content"
),
id="login-form"
),
Div(id="content"),
cls="main-content"
),
Div(id="tutorial-links-content", cls="tutorial-pane"),
cls="container"
)
)
@rt('/start')
def post(username: str):
user = db.get_user(username) or db.create_user(username)
return Div(
quiz_page(user),
update_tutorial_buttons(username),
Script("document.getElementById('login-form').style.display = 'none';")
)
def quiz_page(user):
quiz = load_quiz(user.current_level)
question_index = len(user.quiz_scores) % len(quiz)
return Div(
H2(f"Level {user.current_level} Quiz"),
Form(
P(quiz[question_index].question),
Input(name="answer", placeholder="Your answer", autofocus=True),
Hidden(name="question_index", value=str(question_index)),
Hidden(name="username", value=user.username),
Button("Submit", type="submit"),
hx_post="/check",
hx_target="#content",
_js="this.addEventListener('submit', function(e) { e.preventDefault(); htmx.trigger(this, 'submit'); });"
),
Div(id="result"),
Button("Logout", hx_post="/logout", hx_target="body"),
_replace=True
)
@rt('/check')
def post(username: str, answer: str, question_index: int):
user = db.get_user(username)
quiz = load_quiz(user.current_level)
correct = quiz[question_index].answer == answer
user.quiz_scores.append(1 if correct else 0)
if len(user.quiz_scores) > 4:
user.quiz_scores = user.quiz_scores[-4:]
if correct:
result = P("Correct!")
else:
result = Div(
P("Incorrect. The correct answer is:"),
P(quiz[question_index].answer, cls="correct-answer"),
Form(
Button("Continue", type="submit"),
hx_post=f"/next_question/{username}/{question_index}",
hx_target="#content",
_js="this.addEventListener('submit', function(e) { e.preventDefault(); htmx.trigger(this, 'submit'); });"
)
)
if len(user.quiz_scores) >= 4 and sum(user.quiz_scores) / len(user.quiz_scores) > 0.75:
user.current_level += 1
user.quiz_scores = []
db.update_user(user)
return Div(
H2("Level Complete!"),
P(f"You've advanced to Level {user.current_level}"),
Button("Start Next Level", hx_post=f"/start_level/{username}", hx_target="#content"),
update_tutorial_buttons(username)
)
db.update_user(user)
return Div(
result,
_after=update_tutorial_buttons(username),
_replace=True
) if not correct else next_question(username, question_index)
@rt('/next_question/{username}/{question_index}')
def post(username: str, question_index: int):
return next_question(username, question_index)
def next_question(username: str, question_index: int):
user = db.get_user(username)
quiz = load_quiz(user.current_level)
next_index = (int(question_index) + 1) % len(quiz)
return Div(
H2(f"Level {user.current_level} Quiz"),
Form(
P(quiz[next_index].question),
Input(name="answer", placeholder="Your answer", autofocus=True),
Hidden(name="question_index", value=str(next_index)),
Hidden(name="username", value=username),
Button("Submit", type="submit"),
hx_post="/check",
hx_target="#content", # Changed from htmx_target to hx_target
_js="this.addEventListener('submit', function(e) { e.preventDefault(); htmx.trigger(this, 'submit'); });"
),
Div(id="result"),
Button("Logout", hx_post="/logout", hx_target="body"),
update_tutorial_buttons(username),
_replace=True
)
@rt('/start_level/{username}')
def post(username: str):
user = db.get_user(username)
return Div(
quiz_page(user),
update_tutorial_buttons(username)
)
@rt('/tutorial/{level}')
def get(level: int, request=None, username: str = None):
if not username:
return Div("Please log in first")
content = load_tutorial(level)
return Div(
H2(f"Level {level} Tutorial"),
Div(NotStr(content)),
Button("Back to Quiz", hx_get=f"/quiz?username={username}", hx_target="#content"),
_after=update_tutorial_buttons(username)
)
@rt('/quiz')
def get(username: str = None):
if username:
user = db.get_user(username)
return Div(
quiz_page(user),
update_tutorial_buttons(username)
)
else:
return Titled("Python RE Tutorial",
Form(
Input(name="username", placeholder="Enter your username"),
Button("Start", type="submit"),
hx_post="/start",
hx_target="#content"
),
Div(id="content")
)
@rt('/logout')
def post():
return Div(
Form(
Input(name="username", placeholder="Enter your username"),
Button("Start", type="submit"),
hx_post="/start",
hx_target="#content"
),
id="login-form",
_after=Div(id="content"),
_replace=True
)
def cleanup():
if hasattr(db._local, 'connection'):
db._local.connection.close()
atexit.register(cleanup)
serve()