Update main.py
Browse files
main.py
CHANGED
|
@@ -866,7 +866,75 @@ def speak_notes(notes_id):
|
|
| 866 |
except Exception as e:
|
| 867 |
logging.error(f"Unexpected error generating TTS for user {user.id}, notes {notes_id}: {traceback.format_exc()}")
|
| 868 |
return jsonify({'error': f'An unexpected error occurred: {e}'}), 500
|
|
|
|
|
|
|
| 869 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 870 |
|
| 871 |
@app.route('/api/user/performance', methods=['GET'])
|
| 872 |
def get_user_performance():
|
|
|
|
| 866 |
except Exception as e:
|
| 867 |
logging.error(f"Unexpected error generating TTS for user {user.id}, notes {notes_id}: {traceback.format_exc()}")
|
| 868 |
return jsonify({'error': f'An unexpected error occurred: {e}'}), 500
|
| 869 |
+
|
| 870 |
+
# ---------- View Notes and Quizzes Endpoints ----------
|
| 871 |
|
| 872 |
+
@app.route('/api/view/notes', methods=['GET'])
|
| 873 |
+
def view_notes():
|
| 874 |
+
try:
|
| 875 |
+
# --- Authentication ---
|
| 876 |
+
user, error = verify_token(request.headers.get('Authorization'))
|
| 877 |
+
if error: return jsonify({'error': error['error']}), error['status']
|
| 878 |
+
|
| 879 |
+
# --- Query Notes for the Authenticated User ---
|
| 880 |
+
notes_res = supabase.table('notes') \
|
| 881 |
+
.select('id, content, created_at, tts_audio_url, study_materials(title, type)') \
|
| 882 |
+
.eq('user_id', user.id) \
|
| 883 |
+
.order('created_at', desc=True) \
|
| 884 |
+
.execute()
|
| 885 |
+
|
| 886 |
+
if notes_res.error:
|
| 887 |
+
raise Exception(notes_res.error.message)
|
| 888 |
+
|
| 889 |
+
# Format response
|
| 890 |
+
formatted_notes = []
|
| 891 |
+
for note in notes_res.data:
|
| 892 |
+
formatted_notes.append({
|
| 893 |
+
"note_id": note['id'],
|
| 894 |
+
"content": note['content'],
|
| 895 |
+
"audio_url": note['tts_audio_url'],
|
| 896 |
+
"created_at": note['created_at'],
|
| 897 |
+
"material_title": note['study_materials']['title'] if note['study_materials'] else None,
|
| 898 |
+
"material_type": note['study_materials']['type'] if note['study_materials'] else None
|
| 899 |
+
})
|
| 900 |
+
|
| 901 |
+
return jsonify({"notes": formatted_notes})
|
| 902 |
+
|
| 903 |
+
except Exception as e:
|
| 904 |
+
return jsonify({'error': str(e)}), 500
|
| 905 |
+
|
| 906 |
+
@app.route('/api/view/quizzes', methods=['GET'])
|
| 907 |
+
def view_quizzes():
|
| 908 |
+
try:
|
| 909 |
+
# --- Authentication ---
|
| 910 |
+
user, error = verify_token(request.headers.get('Authorization'))
|
| 911 |
+
if error: return jsonify({'error': error['error']}), error['status']
|
| 912 |
+
|
| 913 |
+
# --- Query Quizzes for the Authenticated User ---
|
| 914 |
+
quizzes_res = supabase.table('quizzes') \
|
| 915 |
+
.select('id, difficulty, created_at, notes(content, study_materials(title))') \
|
| 916 |
+
.eq('user_id', user.id) \
|
| 917 |
+
.order('created_at', desc=True) \
|
| 918 |
+
.execute()
|
| 919 |
+
|
| 920 |
+
if quizzes_res.error:
|
| 921 |
+
raise Exception(quizzes_res.error.message)
|
| 922 |
+
|
| 923 |
+
# Format response
|
| 924 |
+
formatted_quizzes = []
|
| 925 |
+
for quiz in quizzes_res.data:
|
| 926 |
+
formatted_quizzes.append({
|
| 927 |
+
"quiz_id": quiz['id'],
|
| 928 |
+
"difficulty": quiz['difficulty'],
|
| 929 |
+
"created_at": quiz['created_at'],
|
| 930 |
+
"notes_preview": quiz['notes']['content'][:100] + "..." if quiz['notes'] else None,
|
| 931 |
+
"material_title": quiz['notes']['study_materials']['title'] if quiz['notes'] and quiz['notes']['study_materials'] else None
|
| 932 |
+
})
|
| 933 |
+
|
| 934 |
+
return jsonify({"quizzes": formatted_quizzes})
|
| 935 |
+
|
| 936 |
+
except Exception as e:
|
| 937 |
+
return jsonify({'error': str(e)}), 500
|
| 938 |
|
| 939 |
@app.route('/api/user/performance', methods=['GET'])
|
| 940 |
def get_user_performance():
|