quiz fix sherika
Browse files- utils/api.py +27 -5
utils/api.py
CHANGED
|
@@ -644,13 +644,35 @@ def record_profit_puzzler_play(user_id: int, puzzles_solved: int, mistakes: int,
|
|
| 644 |
|
| 645 |
|
| 646 |
def generate_quiz(lesson_id: int, level_slug: str, lesson_title: str):
|
| 647 |
-
|
| 648 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 649 |
"level_slug": level_slug,
|
| 650 |
"lesson_title": lesson_title
|
| 651 |
-
}
|
| 652 |
-
|
| 653 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 654 |
|
| 655 |
def submit_quiz(lesson_id: int, level_slug: str, user_answers: list[dict], original_quiz: list[dict]):
|
| 656 |
r = requests.post(f"{BACKEND}/submit_quiz", json={
|
|
|
|
| 644 |
|
| 645 |
|
| 646 |
def generate_quiz(lesson_id: int, level_slug: str, lesson_title: str):
|
| 647 |
+
"""
|
| 648 |
+
Robustly call the backend to generate a quiz.
|
| 649 |
+
|
| 650 |
+
This uses _try_candidates so the code will automatically try likely prefixes
|
| 651 |
+
(e.g. "", "/api", "/v1", "/api/v1") and also fallback to the compatible
|
| 652 |
+
/quiz/generate endpoint (which returns {"items": [...]})
|
| 653 |
+
"""
|
| 654 |
+
payload = {
|
| 655 |
+
"lesson_id": int(lesson_id),
|
| 656 |
"level_slug": level_slug,
|
| 657 |
"lesson_title": lesson_title
|
| 658 |
+
}
|
| 659 |
+
|
| 660 |
+
# Try the preferred route first; _try_candidates will also try prefix variants
|
| 661 |
+
try:
|
| 662 |
+
resp = _try_candidates("POST", [
|
| 663 |
+
("/generate_quiz", {"json": payload}),
|
| 664 |
+
# compatibility fallback that the backend exposes (/quiz/generate)
|
| 665 |
+
("/quiz/generate", {"json": {"content": lesson_title, "n_questions": 5, "subject": "finance", "level": level_slug}})
|
| 666 |
+
])
|
| 667 |
+
except Exception as e:
|
| 668 |
+
# Re-raise with helpful context (Streamlit UI will show this)
|
| 669 |
+
raise RuntimeError(f"generate_quiz failed when contacting backend {BACKEND}: {e}") from e
|
| 670 |
+
|
| 671 |
+
# backend may return {"quiz": [...]} or {"items": [...]} or directly the list
|
| 672 |
+
if isinstance(resp, dict):
|
| 673 |
+
return resp.get("quiz") or resp.get("items") or resp
|
| 674 |
+
return resp
|
| 675 |
+
|
| 676 |
|
| 677 |
def submit_quiz(lesson_id: int, level_slug: str, user_answers: list[dict], original_quiz: list[dict]):
|
| 678 |
r = requests.post(f"{BACKEND}/submit_quiz", json={
|