wzender commited on
Commit
c5754b1
ยท
1 Parent(s): 9e3b5e5

Enhance answer saving and submission handling in quizzes and attempts; add error handling for subjects tab loading

Browse files
mathly2/attempts.py CHANGED
@@ -68,7 +68,7 @@ def settle(store, bank, row: dict, now: datetime) -> dict:
68
  if not is_overdue(row, now):
69
  return row
70
  value, _ = grade(bank, row["questions"], row["answers"])
71
- saved = store.save_attempt(row["id"], submitted_at=row["due_at"], timed_out=True, grade=value)
72
  add_wrong_to_review(store, bank, saved, row["due_at"])
73
  return saved
74
 
@@ -81,7 +81,7 @@ def submit(store, bank, row: dict, now: datetime) -> dict:
81
  if is_overdue(row, now):
82
  return settle(store, bank, row, now)
83
  value, _ = grade(bank, row["questions"], row["answers"])
84
- saved = store.save_attempt(row["id"], submitted_at=now, timed_out=False, grade=value)
85
  add_wrong_to_review(store, bank, saved, now)
86
  return saved
87
 
 
68
  if not is_overdue(row, now):
69
  return row
70
  value, _ = grade(bank, row["questions"], row["answers"])
71
+ saved = store.save_attempt(row["id"], submitted_at=row["due_at"], timed_out=True, grade=value, answers=row["answers"])
72
  add_wrong_to_review(store, bank, saved, row["due_at"])
73
  return saved
74
 
 
81
  if is_overdue(row, now):
82
  return settle(store, bank, row, now)
83
  value, _ = grade(bank, row["questions"], row["answers"])
84
+ saved = store.save_attempt(row["id"], submitted_at=now, timed_out=False, grade=value, answers=row["answers"]) # the answers it was graded on
85
  add_wrong_to_review(store, bank, saved, now)
86
  return saved
87
 
mathly2/quizzes.py CHANGED
@@ -114,7 +114,9 @@ def attempt_answer(attempt_id: str, body: AnswerBody, request: Request, user: di
114
  ref = next((q for q in row["questions"] if q["key"] == body.key), None)
115
  if ref is None or bank.grade(ref["skill_id"], body.key, body.choice) is None:
116
  raise HTTPException(422, "ืชืฉื•ื‘ื” ืœื ื—ื•ืงื™ืช")
117
- saved = store.save_attempt(row["id"], answers={**row["answers"], body.key: body.choice})
 
 
118
  return {"saved": len(saved["answers"]), "of": len(saved["questions"])}
119
 
120
 
 
114
  ref = next((q for q in row["questions"] if q["key"] == body.key), None)
115
  if ref is None or bank.grade(ref["skill_id"], body.key, body.choice) is None:
116
  raise HTTPException(422, "ืชืฉื•ื‘ื” ืœื ื—ื•ืงื™ืช")
117
+ saved = store.save_open_answers(row["id"], {**row["answers"], body.key: body.choice})
118
+ if saved is None: # submitted between the read and the save: the answer came too late
119
+ raise HTTPException(409, "ื”ื–ืžืŸ ื ื’ืžืจ ื•ื”ื‘ื•ื—ืŸ ื”ื•ื’ืฉ")
120
  return {"saved": len(saved["answers"]), "of": len(saved["questions"])}
121
 
122
 
mathly2/static/manager.js CHANGED
@@ -12,9 +12,15 @@ const tabs = () => `<div class="subtabs" id="mgr-tabs" role="group" aria-label="
12
  `<button type="button" data-tab="${id}" aria-pressed="${tab === id || (tab === "lesson" && id === "subjects")}">${label}</button>`).join("")}</div>`;
13
 
14
  async function render() {
15
- if (tab === "edit") subjects = (await api("/api/subjects")).subjects;
16
- const body = tab === "classes" ? await classesView() : tab === "students" ? await studentsView() : tab === "curriculum" ? await curriculumView()
17
- : tab === "subjects" ? await subjectsTab.view() : tab === "lesson" ? `<div id="lesson-slot"></div>` : editorView();
 
 
 
 
 
 
18
  draw(`<section id="mgr"><h2>ืžื ื”ืœ ื›ื™ืชื•ืช</h2>${tabs()}${note()}${body}</section>`);
19
  document.getElementById("mgr-tabs").addEventListener("click", event => {
20
  const next = event.target.closest("[data-tab]")?.dataset.tab;
@@ -23,6 +29,7 @@ async function render() {
23
  draft = null;
24
  go(next);
25
  });
 
26
  if (tab === "classes") wireClasses();
27
  if (tab === "curriculum") wireCurriculum();
28
  const openLesson = id => { lessonId = id; go("lesson"); };
 
12
  `<button type="button" data-tab="${id}" aria-pressed="${tab === id || (tab === "lesson" && id === "subjects")}">${label}</button>`).join("")}</div>`;
13
 
14
  async function render() {
15
+ let body, failed = false;
16
+ try { // a tab whose data cannot be read says so, instead of the click doing nothing
17
+ if (tab === "edit") subjects = (await api("/api/subjects")).subjects;
18
+ body = tab === "classes" ? await classesView() : tab === "students" ? await studentsView() : tab === "curriculum" ? await curriculumView()
19
+ : tab === "subjects" ? await subjectsTab.view() : tab === "lesson" ? `<div id="lesson-slot"></div>` : editorView();
20
+ } catch (e) {
21
+ failed = true;
22
+ body = `<p class="error" role="alert" id="tab-error">ืœื ื”ืฆืœื—ื ื• ืœืคืชื•ื— ืืช ื”ืœืฉื•ื ื™ืช: ${esc(e.message)}</p>`;
23
+ }
24
  draw(`<section id="mgr"><h2>ืžื ื”ืœ ื›ื™ืชื•ืช</h2>${tabs()}${note()}${body}</section>`);
25
  document.getElementById("mgr-tabs").addEventListener("click", event => {
26
  const next = event.target.closest("[data-tab]")?.dataset.tab;
 
29
  draft = null;
30
  go(next);
31
  });
32
+ if (failed) { flash = { message: "", error: "" }; return; }
33
  if (tab === "classes") wireClasses();
34
  if (tab === "curriculum") wireCurriculum();
35
  const openLesson = id => { lessonId = id; go("lesson"); };
mathly2/static/quiz.js CHANGED
@@ -103,6 +103,7 @@ async function tick() {
103
  async function finish(submit) {
104
  const id = attempt.id;
105
  try {
 
106
  const data = submit ? await api(`/api/attempts/${id}/submit`, { method: "POST" }) : await api(`/api/attempts/${id}`);
107
  if (data.state === "open") { setTimeout(() => layer && finish(false), 1000); return; } // a hair early on the device: ask again
108
  show(data);
@@ -141,13 +142,17 @@ function renderQuestion() {
141
  window.MathlyBoard?.mount(layer, `quiz:${attempt.id}:${question.key}`, { question: () => q("#qz-q") });
142
  }
143
 
 
 
144
  async function choose(button) {
145
  const question = attempt.questions[at], index = Number(button.dataset.i), before = attempt.answers[question.key];
146
  attempt.answers[question.key] = index;
147
  layer.querySelectorAll(".qz-choice").forEach(b => { const on = Number(b.dataset.i) === index; b.classList.toggle("picked", on); b.setAttribute("aria-pressed", String(on)); });
148
  q(`.qz-dot[data-go="${at}"]`)?.classList.add("done");
 
 
149
  try {
150
- await api(`/api/attempts/${attempt.id}/answers`, { method: "PUT", body: { key: question.key, choice: index } });
151
  } catch (error) {
152
  if (error instanceof ApiError && error.status === 409) return finish(false); // time ran out: the server already submitted
153
  if (before === undefined) delete attempt.answers[question.key]; else attempt.answers[question.key] = before;
 
103
  async function finish(submit) {
104
  const id = attempt.id;
105
  try {
106
+ if (submit) await saving;
107
  const data = submit ? await api(`/api/attempts/${id}/submit`, { method: "POST" }) : await api(`/api/attempts/${id}`);
108
  if (data.state === "open") { setTimeout(() => layer && finish(false), 1000); return; } // a hair early on the device: ask again
109
  show(data);
 
142
  window.MathlyBoard?.mount(layer, `quiz:${attempt.id}:${question.key}`, { question: () => q("#qz-q") });
143
  }
144
 
145
+ let saving = Promise.resolve(); // the answer saves still on their way to the server
146
+
147
  async function choose(button) {
148
  const question = attempt.questions[at], index = Number(button.dataset.i), before = attempt.answers[question.key];
149
  attempt.answers[question.key] = index;
150
  layer.querySelectorAll(".qz-choice").forEach(b => { const on = Number(b.dataset.i) === index; b.classList.toggle("picked", on); b.setAttribute("aria-pressed", String(on)); });
151
  q(`.qz-dot[data-go="${at}"]`)?.classList.add("done");
152
+ const id = attempt.id, save = saving.then(() => api(`/api/attempts/${id}/answers`, { method: "PUT", body: { key: question.key, choice: index } }));
153
+ saving = save.catch(() => {}); // one save at a time, and a submit waits for the last one (else it is graded without it)
154
  try {
155
+ await save;
156
  } catch (error) {
157
  if (error instanceof ApiError && error.status === 409) return finish(false); // time ran out: the server already submitted
158
  if (before === undefined) delete attempt.answers[question.key]; else attempt.answers[question.key] = before;
mathly2/store.py CHANGED
@@ -729,6 +729,17 @@ class InMemoryStore:
729
  row.update({k: (dict(v) if k == "answers" else v) for k, v in fields.items()})
730
  return _copy_attempt(row)
731
 
 
 
 
 
 
 
 
 
 
 
 
732
  def attempts_of(self, user_id: str, kind: str, scope: str | None = None) -> list[dict]:
733
  """A student's attempts of one kind (of one scope, when given), oldest first."""
734
  with self._lock:
@@ -1437,6 +1448,14 @@ class SupabaseStore:
1437
  raise KeyError(attempt_id)
1438
  return _attempt_row(rows[0])
1439
 
 
 
 
 
 
 
 
 
1440
  def attempts_of(self, user_id: str, kind: str, scope: str | None = None) -> list[dict]:
1441
  params = {"user_id": self._eq(user_id), "kind": self._eq(kind), "select": "*", "order": "started_at"}
1442
  if scope is not None:
 
729
  row.update({k: (dict(v) if k == "answers" else v) for k, v in fields.items()})
730
  return _copy_attempt(row)
731
 
732
+ def save_open_answers(self, attempt_id: str, answers: dict) -> dict | None:
733
+ """Save the answers only while the attempt is still open, in one step; None once it was submitted (the grade stands)."""
734
+ with self._lock:
735
+ row = self._attempts.get(str(attempt_id))
736
+ if row is None:
737
+ raise KeyError(attempt_id)
738
+ if row["submitted_at"] is not None:
739
+ return None
740
+ row["answers"] = dict(answers)
741
+ return _copy_attempt(row)
742
+
743
  def attempts_of(self, user_id: str, kind: str, scope: str | None = None) -> list[dict]:
744
  """A student's attempts of one kind (of one scope, when given), oldest first."""
745
  with self._lock:
 
1448
  raise KeyError(attempt_id)
1449
  return _attempt_row(rows[0])
1450
 
1451
+ def save_open_answers(self, attempt_id: str, answers: dict) -> dict | None:
1452
+ rows = self._request("PATCH", "m2_attempts", {"id": self._eq(attempt_id), "submitted_at": "is.null"}, {"answers": answers}, returning=True)
1453
+ if rows:
1454
+ return _attempt_row(rows[0])
1455
+ if self.get_attempt(attempt_id) is None:
1456
+ raise KeyError(attempt_id)
1457
+ return None
1458
+
1459
  def attempts_of(self, user_id: str, kind: str, scope: str | None = None) -> list[dict]:
1460
  params = {"user_id": self._eq(user_id), "kind": self._eq(kind), "select": "*", "order": "started_at"}
1461
  if scope is not None:
mathly2/subjects.py CHANGED
@@ -68,12 +68,18 @@ def list_subjects(request: Request, user: dict = Depends(need("content.manage"))
68
  """Every subject in order with its lessons: math's file lessons (fixed) and the lessons kept in the database."""
69
  store, bank = _store(request), request.app.state.bank
70
  by_subject: dict[str, list[dict]] = {}
71
- for row in store.lessons_of():
72
- by_subject.setdefault(row["subject_id"], []).append(lesson_summary(row))
73
- return {"icons": list(ICONS), "content_error": getattr(request.app.state, "content_error", None),
 
 
 
 
 
 
74
  "subjects": [{**{k: s[k] for k in ("id", "name", "icon")}, "fixed": s["id"] == "math",
75
  "file_lessons": [{"id": u["id"], "name": u["name"]} for u in request.app.state.base_bank.lessons] if s["id"] == "math" else [],
76
- "lessons": by_subject.get(s["id"], [])} for s in store.subjects()],
77
  "served_lessons": len(bank.lessons)}
78
 
79
 
 
68
  """Every subject in order with its lessons: math's file lessons (fixed) and the lessons kept in the database."""
69
  store, bank = _store(request), request.app.state.bank
70
  by_subject: dict[str, list[dict]] = {}
71
+ error = getattr(request.app.state, "content_error", None)
72
+ try:
73
+ subjects = store.subjects()
74
+ for row in store.lessons_of():
75
+ by_subject.setdefault(row["subject_id"], []).append(lesson_summary(row))
76
+ except Exception as exc: # the subjects tables cannot be read: show math from the file and say why, rather than fail the tab
77
+ subjects, by_subject = bank.subjects, {}
78
+ error = error or str(exc) or type(exc).__name__
79
+ return {"icons": list(ICONS), "content_error": error,
80
  "subjects": [{**{k: s[k] for k in ("id", "name", "icon")}, "fixed": s["id"] == "math",
81
  "file_lessons": [{"id": u["id"], "name": u["name"]} for u in request.app.state.base_bank.lessons] if s["id"] == "math" else [],
82
+ "lessons": by_subject.get(s["id"], [])} for s in subjects],
83
  "served_lessons": len(bank.lessons)}
84
 
85
 
mathly2/tests/test_quizzes_api.py CHANGED
@@ -238,3 +238,35 @@ def test_thirteen_of_fifteen_passes_twelve_does_not_and_a_pass_stays(world):
238
  take(client, bank, lesson, [True] * 6 + [False] * 9) # 40: worse
239
  assert lesson_row()["passed"] is True and lesson_row()["completed"] is True
240
  assert all(s["state"] == "not_started" for s in lesson_row()["skills"]) # the skills themselves are untouched
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
  take(client, bank, lesson, [True] * 6 + [False] * 9) # 40: worse
239
  assert lesson_row()["passed"] is True and lesson_row()["completed"] is True
240
  assert all(s["state"] == "not_started" for s in lesson_row()["skills"]) # the skills themselves are untouched
241
+
242
+
243
+ def test_an_answer_that_lands_while_the_quiz_is_submitted_is_refused_and_the_grade_matches_the_answers(world):
244
+ """The last answer's save still on its way when the student hands in: it must not slip in after the grading, or the result says
245
+ "all right" beside a grade that counted it wrong."""
246
+ from mathly2 import attempts, clock
247
+ client, store, bank = world
248
+ attempt = start(client, lesson_id(bank)).json()
249
+ *first, last = attempt["questions"]
250
+ for question in first:
251
+ assert answer(client, attempt, bank, question["key"]).status_code == 200
252
+ save = store.save_open_answers
253
+
254
+ def submitted_meanwhile(attempt_id, answers): # the submit request wins the race, between the answer's read and its save
255
+ attempts.submit(store, bank, store.get_attempt(attempt_id), clock.now())
256
+ return save(attempt_id, answers)
257
+ store.save_open_answers = submitted_meanwhile
258
+ assert answer(client, attempt, bank, last["key"]).status_code == 409
259
+ got = client.get(f"/api/attempts/{attempt['id']}", headers=auth(KID)).json()
260
+ assert got["right"] == len(first) and got["grade"] == attempts.percent(len(first), len(attempt["questions"]))
261
+ assert [s["missed"] for s in got["lost_skills"]] == [1]
262
+
263
+
264
+ def test_a_submit_keeps_the_answers_it_graded(world):
265
+ from mathly2 import attempts, clock
266
+ client, store, bank = world
267
+ attempt = start(client, lesson_id(bank)).json()
268
+ assert answer(client, attempt, bank, attempt["questions"][0]["key"]).status_code == 200
269
+ row = store.get_attempt(attempt["id"])
270
+ store.save_attempt(attempt["id"], answers={**row["answers"], attempt["questions"][1]["key"]: 0}) # saved after the submit read the row
271
+ saved = attempts.submit(store, bank, row, clock.now())
272
+ assert saved["answers"] == row["answers"] and saved["grade"] == attempts.percent(1, len(attempt["questions"]))
mathly2/tests/test_subjects_api.py CHANGED
@@ -36,6 +36,19 @@ def test_a_manager_creates_renames_and_lists_subjects_with_math_fixed_first():
36
  assert [s["name"] for s in c.app.state.bank.subjects] == ["ืžืชืžื˜ื™ืงื”", "ื‘ื™ื•ืœื•ื’ื™ื”"] # the served bank knows it at once
37
 
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  def test_subject_names_are_unique_ignoring_case_and_need_a_listed_icon():
40
  c, _ = world()
41
  assert c.post("/api/subjects", json={"name": "Science", "icon": "โš—"}, headers=auth(MGR)).status_code == 201
 
36
  assert [s["name"] for s in c.app.state.bank.subjects] == ["ืžืชืžื˜ื™ืงื”", "ื‘ื™ื•ืœื•ื’ื™ื”"] # the served bank knows it at once
37
 
38
 
39
+ def test_the_subjects_tab_still_opens_when_the_subjects_tables_cannot_be_read():
40
+ c, store = world()
41
+
42
+ def missing(*_a, **_k):
43
+ raise RuntimeError("relation m2_subjects does not exist")
44
+ store.subjects = missing
45
+ answer = c.get("/api/subjects", headers=auth(MGR))
46
+ assert answer.status_code == 200
47
+ body = answer.json()
48
+ assert [s["id"] for s in body["subjects"]] == ["math"] and body["subjects"][0]["file_lessons"]
49
+ assert "m2_subjects" in body["content_error"] # the tab shows why
50
+
51
+
52
  def test_subject_names_are_unique_ignoring_case_and_need_a_listed_icon():
53
  c, _ = world()
54
  assert c.post("/api/subjects", json={"name": "Science", "icon": "โš—"}, headers=auth(MGR)).status_code == 201