Update modules/database/sql_db.py
Browse files- modules/database/sql_db.py +18 -65
modules/database/sql_db.py
CHANGED
@@ -32,34 +32,26 @@ def get_student_user(username):
|
|
32 |
def get_teacher_user(username):
|
33 |
return get_user(username, role='Profesor')
|
34 |
|
35 |
-
#################################################################################
|
36 |
-
#El error ocurre porque el par谩metro `partition_key`
|
37 |
-
#no es soportado en la versi贸n actual del SDK.
|
38 |
-
#Modifiquemos la funci贸n `create_user()` para incluir la partici贸n en el cuerpo del documento:
|
39 |
-
|
40 |
def create_user(username, password, role, additional_info=None):
|
|
|
41 |
container = get_container("users")
|
42 |
if not container:
|
43 |
logger.error("No se pudo obtener el contenedor de usuarios")
|
44 |
return False
|
45 |
|
46 |
try:
|
47 |
-
# Generar salt y hash de la contrase帽a
|
48 |
-
salt = bcrypt.gensalt()
|
49 |
-
hashed = bcrypt.hashpw(password.encode('utf-8'), salt)
|
50 |
-
|
51 |
user_data = {
|
52 |
'id': username,
|
53 |
-
'password':
|
54 |
-
'salt': salt.decode('utf-8'),
|
55 |
'role': role,
|
56 |
'timestamp': datetime.now(timezone.utc).isoformat(),
|
57 |
-
'additional_info': additional_info or {}
|
58 |
-
# La partici贸n va dentro del documento
|
59 |
-
'partitionKey': username
|
60 |
}
|
61 |
|
62 |
-
container.create_item(
|
|
|
|
|
|
|
63 |
logger.info(f"Usuario {role} creado: {username}")
|
64 |
return True
|
65 |
|
@@ -67,7 +59,6 @@ def create_user(username, password, role, additional_info=None):
|
|
67 |
logger.error(f"Error al crear usuario {role}: {str(e)}")
|
68 |
return False
|
69 |
|
70 |
-
#########################################################################
|
71 |
def create_student_user(username, password, additional_info=None):
|
72 |
return create_user(username, password, 'Estudiante', additional_info)
|
73 |
|
@@ -266,20 +257,9 @@ def delete_student_user(username):
|
|
266 |
logger.error(f"Error al eliminar estudiante {username}: {str(e)}")
|
267 |
return False
|
268 |
|
269 |
-
|
270 |
-
############################################
|
271 |
-
|
272 |
def store_application_request(name, lastname, email, institution, current_role, desired_role, reason):
|
273 |
-
|
274 |
try:
|
275 |
-
# Obtener el contenedor usando get_container() que s铆 funciona
|
276 |
-
container = get_container("application_requests")
|
277 |
-
if not container:
|
278 |
-
logger.error("No se pudo obtener el contenedor de solicitudes")
|
279 |
-
return False
|
280 |
-
|
281 |
-
# Crear documento con la solicitud
|
282 |
-
# N贸tese que incluimos email como partition key en el cuerpo del documento
|
283 |
application_request = {
|
284 |
"id": str(uuid.uuid4()),
|
285 |
"name": name,
|
@@ -289,57 +269,30 @@ def store_application_request(name, lastname, email, institution, current_role,
|
|
289 |
"current_role": current_role,
|
290 |
"desired_role": desired_role,
|
291 |
"reason": reason,
|
292 |
-
"requestDate": datetime.utcnow().isoformat()
|
293 |
-
# El campo para partition key debe estar en el documento
|
294 |
-
"partitionKey": email
|
295 |
}
|
296 |
-
|
297 |
-
|
298 |
-
container.create_item(
|
299 |
-
body=application_request # Solo pasamos el body
|
300 |
-
)
|
301 |
-
logger.info(f"Solicitud de aplicaci贸n almacenada para: {email}")
|
302 |
return True
|
303 |
-
|
304 |
except Exception as e:
|
305 |
logger.error(f"Error al almacenar la solicitud de aplicaci贸n: {str(e)}")
|
306 |
-
logger.error(f"Detalles del error: {str(e)}")
|
307 |
return False
|
308 |
|
309 |
-
#########################################################
|
310 |
-
|
311 |
def store_student_feedback(username, name, email, feedback):
|
312 |
-
|
313 |
try:
|
314 |
-
# Obtener el contenedor - verificar disponibilidad
|
315 |
-
logger.info(f"Intentando obtener contenedor user_feedback para usuario: {username}")
|
316 |
-
container = get_container("user_feedback")
|
317 |
-
if not container:
|
318 |
-
logger.error("No se pudo obtener el contenedor user_feedback")
|
319 |
-
return False
|
320 |
-
|
321 |
-
# Crear documento de feedback - asegurar que el username est茅 como partition key
|
322 |
feedback_item = {
|
323 |
"id": str(uuid.uuid4()),
|
324 |
-
"username": username,
|
325 |
"name": name,
|
326 |
"email": email,
|
327 |
"feedback": feedback,
|
328 |
"role": "Estudiante",
|
329 |
-
|
330 |
-
"partitionKey": username # Campo de partici贸n
|
331 |
}
|
332 |
-
|
333 |
-
|
334 |
-
logger.info(f"Intentando almacenar feedback para usuario: {username}")
|
335 |
-
result = container.create_item(
|
336 |
-
body=feedback_item # Solo el body, no par谩metros adicionales
|
337 |
-
)
|
338 |
-
|
339 |
-
logger.info(f"Feedback almacenado exitosamente para el usuario: {username}")
|
340 |
return True
|
341 |
-
|
342 |
except Exception as e:
|
343 |
-
logger.error(f"Error al almacenar el feedback del estudiante {username}")
|
344 |
-
|
345 |
-
return False
|
|
|
32 |
def get_teacher_user(username):
|
33 |
return get_user(username, role='Profesor')
|
34 |
|
|
|
|
|
|
|
|
|
|
|
35 |
def create_user(username, password, role, additional_info=None):
|
36 |
+
"""Crea un nuevo usuario"""
|
37 |
container = get_container("users")
|
38 |
if not container:
|
39 |
logger.error("No se pudo obtener el contenedor de usuarios")
|
40 |
return False
|
41 |
|
42 |
try:
|
|
|
|
|
|
|
|
|
43 |
user_data = {
|
44 |
'id': username,
|
45 |
+
'password': password,
|
|
|
46 |
'role': role,
|
47 |
'timestamp': datetime.now(timezone.utc).isoformat(),
|
48 |
+
'additional_info': additional_info or {}
|
|
|
|
|
49 |
}
|
50 |
|
51 |
+
container.create_item(
|
52 |
+
body=user_data,
|
53 |
+
partition_key=username
|
54 |
+
)
|
55 |
logger.info(f"Usuario {role} creado: {username}")
|
56 |
return True
|
57 |
|
|
|
59 |
logger.error(f"Error al crear usuario {role}: {str(e)}")
|
60 |
return False
|
61 |
|
|
|
62 |
def create_student_user(username, password, additional_info=None):
|
63 |
return create_user(username, password, 'Estudiante', additional_info)
|
64 |
|
|
|
257 |
logger.error(f"Error al eliminar estudiante {username}: {str(e)}")
|
258 |
return False
|
259 |
|
|
|
|
|
|
|
260 |
def store_application_request(name, lastname, email, institution, current_role, desired_role, reason):
|
261 |
+
_, application_requests_container, _ = get_sql_containers()
|
262 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
263 |
application_request = {
|
264 |
"id": str(uuid.uuid4()),
|
265 |
"name": name,
|
|
|
269 |
"current_role": current_role,
|
270 |
"desired_role": desired_role,
|
271 |
"reason": reason,
|
272 |
+
"requestDate": datetime.utcnow().isoformat()
|
|
|
|
|
273 |
}
|
274 |
+
application_requests_container.create_item(body=application_request)
|
275 |
+
logger.info(f"Solicitud de aplicaci贸n almacenada para el email: {email}")
|
|
|
|
|
|
|
|
|
276 |
return True
|
|
|
277 |
except Exception as e:
|
278 |
logger.error(f"Error al almacenar la solicitud de aplicaci贸n: {str(e)}")
|
|
|
279 |
return False
|
280 |
|
|
|
|
|
281 |
def store_student_feedback(username, name, email, feedback):
|
282 |
+
_, _, user_feedback_container = get_sql_containers()
|
283 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
284 |
feedback_item = {
|
285 |
"id": str(uuid.uuid4()),
|
286 |
+
"username": username,
|
287 |
"name": name,
|
288 |
"email": email,
|
289 |
"feedback": feedback,
|
290 |
"role": "Estudiante",
|
291 |
+
'timestamp': datetime.now(timezone.utc).isoformat(),
|
|
|
292 |
}
|
293 |
+
result = user_feedback_container.create_item(body=feedback_item)
|
294 |
+
logger.info(f"Feedback de estudiante almacenado con ID: {result['id']} para el usuario: {username}")
|
|
|
|
|
|
|
|
|
|
|
|
|
295 |
return True
|
|
|
296 |
except Exception as e:
|
297 |
+
logger.error(f"Error al almacenar el feedback del estudiante {username}: {str(e)}")
|
298 |
+
return False
|
|