Spaces:
Sleeping
Sleeping
File size: 6,999 Bytes
840101a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
import streamlit as st
from supabase import create_client, Client
import os
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 get_user_id(username):
"""Get user ID from database by username"""
supabase = init_supabase()
if not supabase:
return None
try:
response = supabase.rpc('get_user_by_username', {'username_param': username}).execute()
if response.data:
return response.data[0]['id']
# Fallback: query the table directly
response = supabase.table('users').select('id').eq('username', username).execute()
if response.data:
return response.data[0]['id']
return None
except Exception as e:
st.error(f"Database error: {str(e)}")
return None
def debug_user_id():
"""Debug user ID issues"""
st.title("π User ID Debug")
supabase = init_supabase()
if not supabase:
st.error("Failed to initialize Supabase client")
return
try:
# Test 1: Show all users in database
st.subheader("π All Users in Database")
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 = []
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')
})
st.dataframe(user_data, use_container_width=True)
else:
st.error("No users found in database")
return
except Exception as e:
st.error(f"β Error fetching users: {str(e)}")
return
# Test 2: Test get_user_id function for each user
st.subheader("π§ Testing get_user_id Function")
for user in users_response.data:
username = user.get('username')
actual_id = user.get('id')
st.write(f"**Testing username:** {username}")
st.write(f"**Actual ID in database:** {actual_id}")
# Test the function
function_id = get_user_id(username)
st.write(f"**Function returned ID:** {function_id}")
if function_id == actual_id:
st.success("β
Function returns correct ID")
else:
st.error("β Function returns wrong ID")
st.write("---")
# Test 3: Test with the problematic ID
st.subheader("π¨ Testing Problematic ID")
problematic_id = "6ab26f7f-8c00-40bf-b925-a2f59a0c09ef"
st.write(f"**Problematic ID:** {problematic_id}")
# Check if this ID exists in users table
try:
check_response = supabase.table('users').select('*').eq('id', problematic_id).execute()
if check_response.data:
st.success("β
This ID exists in users table")
st.write(f"**User:** {check_response.data[0]}")
else:
st.error("β This ID does NOT exist in users table")
except Exception as e:
st.error(f"β Error checking ID: {str(e)}")
# Test 4: Try to find which user this ID belongs to (if any)
st.subheader("π Finding User for Problematic ID")
# Check if this ID exists in any table
try:
# Check student_submissions table
submissions_response = supabase.table('student_submissions').select('*').eq('student_id', problematic_id).execute()
if submissions_response.data:
st.warning(f"β οΈ Found {len(submissions_response.data)} submissions with this student_id")
for submission in submissions_response.data:
st.write(f" - Week {submission.get('week_number')}, Type: {submission.get('submission_type')}")
else:
st.info("βΉοΈ No submissions found with this student_id")
except Exception as e:
st.error(f"β Error checking submissions: {str(e)}")
# Test 5: Test submission with valid user
st.subheader("π€ Test Submission with Valid User")
if users_response.data:
# Use the first user for testing
test_user = users_response.data[0]
st.write(f"**Using user:** {test_user.get('username')} (ID: {test_user.get('id')})")
test_data = {
'student_id': test_user.get('id'),
'week_number': 1,
'submission_type': 'debug_test',
'submission_data': {
"questions": [{
"id": "debug_q1",
"type": "textarea",
"question": "Debug test question",
"student_answer": "Debug test answer",
"max_length": 500
}]
},
'score': 5,
'max_score': 5
}
st.write("**Test submission data:**")
st.json(test_data)
try:
insert_response = supabase.table('student_submissions').insert(test_data).execute()
if insert_response.data:
st.success("β
Test submission successful!")
else:
st.error("β Test submission failed - no data returned")
except Exception as e:
st.error(f"β Test submission failed: {str(e)}")
# Test 6: Clean up
st.subheader("π§Ή Cleanup")
if st.button("Clean up debug test submissions"):
try:
delete_response = supabase.table('student_submissions').delete().eq('submission_type', 'debug_test').execute()
st.success(f"β
Cleaned up {len(delete_response.data) if delete_response.data else 0} debug test submissions")
except Exception as e:
st.error(f"β Cleanup failed: {str(e)}")
except Exception as e:
st.error(f"β Debug failed: {str(e)}")
if __name__ == "__main__":
debug_user_id() |