Spaces:
Sleeping
Sleeping
Update modules/database/sql_db.py
Browse files- modules/database/sql_db.py +68 -0
modules/database/sql_db.py
CHANGED
|
@@ -201,3 +201,71 @@ def get_user_total_time(username):
|
|
| 201 |
except Exception as e:
|
| 202 |
logger.error(f"Error obteniendo tiempo total: {str(e)}")
|
| 203 |
return 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
except Exception as e:
|
| 202 |
logger.error(f"Error obteniendo tiempo total: {str(e)}")
|
| 203 |
return 0
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
# Agregar estas funciones en sql_db.py
|
| 207 |
+
|
| 208 |
+
def update_student_user(username, new_info):
|
| 209 |
+
"""Actualiza la informaci贸n de un estudiante"""
|
| 210 |
+
container = get_container("users")
|
| 211 |
+
try:
|
| 212 |
+
# Primero obtener el usuario existente
|
| 213 |
+
user = get_student_user(username)
|
| 214 |
+
if user:
|
| 215 |
+
# Actualizar la informaci贸n
|
| 216 |
+
if 'additional_info' in user:
|
| 217 |
+
user['additional_info'].update(new_info)
|
| 218 |
+
else:
|
| 219 |
+
user['additional_info'] = new_info
|
| 220 |
+
|
| 221 |
+
# Actualizar el documento usando el partition key correcto
|
| 222 |
+
container.upsert_item(
|
| 223 |
+
body=user,
|
| 224 |
+
partition_key=username
|
| 225 |
+
)
|
| 226 |
+
logger.info(f"Informaci贸n del estudiante actualizada: {username}")
|
| 227 |
+
return True
|
| 228 |
+
else:
|
| 229 |
+
logger.warning(f"Intento de actualizar estudiante no existente: {username}")
|
| 230 |
+
return False
|
| 231 |
+
except Exception as e:
|
| 232 |
+
logger.error(f"Error al actualizar informaci贸n del estudiante {username}: {str(e)}")
|
| 233 |
+
return False
|
| 234 |
+
|
| 235 |
+
def delete_student_user(username):
|
| 236 |
+
"""Elimina un estudiante"""
|
| 237 |
+
container = get_container("users")
|
| 238 |
+
try:
|
| 239 |
+
# Verificar que el usuario existe y es un estudiante
|
| 240 |
+
user = get_student_user(username)
|
| 241 |
+
if user:
|
| 242 |
+
# Eliminar usando el partition key correcto
|
| 243 |
+
container.delete_item(
|
| 244 |
+
item=user['id'],
|
| 245 |
+
partition_key=username
|
| 246 |
+
)
|
| 247 |
+
logger.info(f"Estudiante eliminado: {username}")
|
| 248 |
+
return True
|
| 249 |
+
else:
|
| 250 |
+
logger.warning(f"Intento de eliminar estudiante no existente: {username}")
|
| 251 |
+
return False
|
| 252 |
+
except Exception as e:
|
| 253 |
+
logger.error(f"Error al eliminar estudiante {username}: {str(e)}")
|
| 254 |
+
return False
|
| 255 |
+
|
| 256 |
+
__all__ = [
|
| 257 |
+
'get_user',
|
| 258 |
+
'get_admin_user',
|
| 259 |
+
'get_student_user',
|
| 260 |
+
'get_teacher_user',
|
| 261 |
+
'create_user',
|
| 262 |
+
'create_student_user',
|
| 263 |
+
'create_teacher_user',
|
| 264 |
+
'create_admin_user',
|
| 265 |
+
'update_student_user', # Agregada
|
| 266 |
+
'delete_student_user', # Agregada
|
| 267 |
+
'record_login',
|
| 268 |
+
'record_logout',
|
| 269 |
+
'get_recent_sessions',
|
| 270 |
+
'get_user_total_time'
|
| 271 |
+
]
|