import streamlit as st from supabase import create_client, Client import os from datetime import datetime, date import json def init_supabase(): """Initialize Supabase client""" try: url = st.secrets.get("SUPABASE_URL") key = st.secrets.get("SUPABASE_KEY") except: url = os.getenv("SUPABASE_URL") key = os.getenv("SUPABASE_KEY") if not url or not key: st.error("Supabase credentials not found.") return None return create_client(url, key) def test_instructor_notes(): """Test instructor notes functionality""" st.title("๐Ÿงช Instructor Notes Test") supabase = init_supabase() if not supabase: st.error("Failed to initialize Supabase client") return try: # Test 1: Check if instructor_notes table exists st.subheader("๐Ÿ” Testing Database Structure") try: response = supabase.table('instructor_notes').select('*').limit(1).execute() st.success("โœ… instructor_notes table exists and is accessible") except Exception as e: st.error(f"โŒ instructor_notes table error: {str(e)}") return # Test 2: Get all users to find instructor and students st.subheader("๐Ÿ‘ฅ Available Users") try: users_response = supabase.table('users').select('*').execute() if users_response.data: st.success(f"Found {len(users_response.data)} users:") # Create a table to display users user_data = [] instructor_user = None student_users = [] for user in users_response.data: user_data.append({ 'ID': user.get('id'), 'Username': user.get('username'), 'Full Name': user.get('full_name'), 'Role': user.get('role'), 'Email': user.get('email') }) if user.get('role') == 'instructor': instructor_user = user elif user.get('role') == 'student': student_users.append(user) st.dataframe(user_data, use_container_width=True) if not instructor_user: st.error("No instructor user found") return if not student_users: st.error("No student users found") return st.info(f"Using instructor: {instructor_user.get('username')} (ID: {instructor_user.get('id')})") st.info(f"Found {len(student_users)} students") else: st.error("No users found in database") return except Exception as e: st.error(f"โŒ Error fetching users: {str(e)}") return # Test 3: Try to insert a test instructor note st.subheader("๐Ÿ“ค Testing Instructor Notes Insert") # Create sample attendance data attendance_data = { "students": [ { "student_id": student_users[0]['id'], "username": student_users[0]['username'], "full_name": student_users[0]['full_name'], "present": True, "late": False, "notes": "Participated well in today's debate" } ], "total_present": 1, "total_absent": 0 } # Create sample writing skills notes writing_skills_notes = { "students": [ { "student_id": student_users[0]['id'], "username": student_users[0]['username'], "full_name": student_users[0]['full_name'], "notes": "Shows improvement in case construction. Good use of evidence, needs more variety. Strong affirmative case, negative needs work." } ] } # Create sample debate skills notes debate_skills_notes = { "students": [ { "student_id": student_users[0]['id'], "username": student_users[0]['username'], "full_name": student_users[0]['full_name'], "notes": "Clear articulation, good pace. Good eye contact, needs more confidence. Strong cross-examination, needs work on rebuttals." } ] } # Create sample parent communication notes parent_communication_notes = { "students": [ { "student_id": student_users[0]['id'], "username": student_users[0]['username'], "full_name": student_users[0]['full_name'], "notes": "May need extra help with research. Excellent speaking skills. Consider joining debate club." } ] } # Create sample upcoming events notes upcoming_events_notes = { "tournaments": [ { "name": "Regional Debate Championship", "date": "2024-02-15", "location": "City Hall", "notes": "All students encouraged to participate" } ], "important_dates": [ { "event": "Parent-Teacher Conference", "date": "2024-01-30", "notes": "Discuss student progress" } ] } test_data = { 'instructor_id': instructor_user.get('id'), 'class_date': str(date.today()), 'attendance_data': attendance_data, 'writing_skills_notes': writing_skills_notes, 'debate_skills_notes': debate_skills_notes, 'parent_communication_notes': parent_communication_notes, 'upcoming_events_notes': upcoming_events_notes, 'general_notes': 'Test general notes for today\'s class session.' } st.write("**Attempting to insert test instructor note:**") st.json(test_data) try: insert_response = supabase.table('instructor_notes').insert(test_data).execute() st.write(f"**Insert Response:** {insert_response}") st.write(f"**Response Data:** {insert_response.data}") st.write(f"**Response Count:** {insert_response.count}") if insert_response.data: st.success("โœ… Test instructor note inserted successfully!") else: st.error("โŒ No data returned from insert") except Exception as e: st.error(f"โŒ Insert failed: {str(e)}") # Test 4: Try to retrieve the test instructor note st.subheader("๐Ÿ“ฅ Testing Instructor Notes Retrieval") try: retrieve_response = supabase.table('instructor_notes').select('*').eq('instructor_id', instructor_user.get('id')).order('class_date', desc=True).limit(1).execute() st.write(f"**Retrieve Response:** {retrieve_response}") st.write(f"**Retrieve Data:** {retrieve_response.data}") st.write(f"**Retrieve Count:** {retrieve_response.count}") if retrieve_response.data: st.success("โœ… Test instructor note retrieved successfully!") st.json(retrieve_response.data[0]) else: st.warning("โš ๏ธ No instructor note found (insert may have failed)") except Exception as e: st.error(f"โŒ Retrieve failed: {str(e)}") # Test 5: Test getting all notes for instructor st.subheader("๐Ÿ“Š All Instructor Notes") try: all_notes_response = supabase.table('instructor_notes').select('*').eq('instructor_id', instructor_user.get('id')).order('class_date', desc=True).execute() if all_notes_response.data: st.success(f"โœ… Found {len(all_notes_response.data)} instructor notes:") for i, note in enumerate(all_notes_response.data): with st.expander(f"Note {i+1}: {note['class_date']}", expanded=False): st.write(f"**Class Date:** {note['class_date']}") st.write(f"**Created:** {note['created_at']}") st.write(f"**Attendance:** {note['attendance_data']['total_present']} present, {note['attendance_data']['total_absent']} absent") if note['general_notes']: st.write(f"**General Notes:** {note['general_notes']}") else: st.info("โ„น๏ธ No instructor notes found") except Exception as e: st.error(f"โŒ Error fetching all notes: {str(e)}") except Exception as e: st.error(f"โŒ Test failed: {str(e)}") if __name__ == "__main__": test_instructor_notes()