Update main.py
Browse files
main.py
CHANGED
|
@@ -766,6 +766,50 @@ def generate_quiz_for_notes(notes_id):
|
|
| 766 |
logging.error(f"Unexpected error generating quiz for user {user.id}, notes {notes_id}: {traceback.format_exc()}")
|
| 767 |
return jsonify({'error': f'An unexpected error occurred: {e}'}), 500
|
| 768 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 769 |
|
| 770 |
@app.route('/api/tutor/quizzes/<uuid:quiz_id>/submit', methods=['POST'])
|
| 771 |
def submit_quiz_attempt(quiz_id):
|
|
|
|
| 766 |
logging.error(f"Unexpected error generating quiz for user {user.id}, notes {notes_id}: {traceback.format_exc()}")
|
| 767 |
return jsonify({'error': f'An unexpected error occurred: {e}'}), 500
|
| 768 |
|
| 769 |
+
@app.route('/api/view/quizzes/<uuid:quiz_id>', methods=['GET'])
|
| 770 |
+
def get_quiz_by_id(quiz_id):
|
| 771 |
+
try:
|
| 772 |
+
# --- Authentication ---
|
| 773 |
+
user, error = verify_token(request.headers.get('Authorization'))
|
| 774 |
+
if error:
|
| 775 |
+
return jsonify({'error': error['error']}), error['status']
|
| 776 |
+
|
| 777 |
+
# --- Query Database ---
|
| 778 |
+
quiz_res = supabase.table('quizzes') \
|
| 779 |
+
.select('''id, difficulty, created_at, questions,
|
| 780 |
+
notes(id, content, study_materials(title, type))''') \
|
| 781 |
+
.eq('id', quiz_id) \
|
| 782 |
+
.eq('user_id', user.id) \
|
| 783 |
+
.maybe_single() \
|
| 784 |
+
.execute()
|
| 785 |
+
|
| 786 |
+
if not quiz_res.data:
|
| 787 |
+
return jsonify({'error': 'Quiz not found or unauthorized'}), 404
|
| 788 |
+
|
| 789 |
+
# --- Format Response ---
|
| 790 |
+
quiz_data = quiz_res.data
|
| 791 |
+
response_data = {
|
| 792 |
+
"quiz": {
|
| 793 |
+
"quiz_id": quiz_data['id'],
|
| 794 |
+
"difficulty": quiz_data['difficulty'],
|
| 795 |
+
"created_at": quiz_data['created_at'],
|
| 796 |
+
"questions": quiz_data['questions'],
|
| 797 |
+
"source_note": {
|
| 798 |
+
"note_id": quiz_data['notes']['id'],
|
| 799 |
+
"content_preview": quiz_data['notes']['content'][:100] + "..." if quiz_data['notes']['content'] else None,
|
| 800 |
+
"material": {
|
| 801 |
+
"title": quiz_data['notes']['study_materials']['title'] if quiz_data['notes']['study_materials'] else None,
|
| 802 |
+
"type": quiz_data['notes']['study_materials']['type'] if quiz_data['notes']['study_materials'] else None
|
| 803 |
+
}
|
| 804 |
+
}
|
| 805 |
+
}
|
| 806 |
+
}
|
| 807 |
+
|
| 808 |
+
return jsonify(response_data)
|
| 809 |
+
|
| 810 |
+
except Exception as e:
|
| 811 |
+
logging.error(f"Error fetching quiz {quiz_id}: {str(e)}")
|
| 812 |
+
return jsonify({'error': 'Internal server error'}), 500
|
| 813 |
|
| 814 |
@app.route('/api/tutor/quizzes/<uuid:quiz_id>/submit', methods=['POST'])
|
| 815 |
def submit_quiz_attempt(quiz_id):
|