Update modules/database/sql_db.py
Browse files- modules/database/sql_db.py +16 -7
modules/database/sql_db.py
CHANGED
@@ -32,26 +32,34 @@ def get_student_user(username):
|
|
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':
|
|
|
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,6 +67,7 @@ def create_user(username, password, role, additional_info=None):
|
|
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 |
|
|
|
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': hashed.decode('utf-8'),
|
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(body=user_data)
|
|
|
|
|
|
|
63 |
logger.info(f"Usuario {role} creado: {username}")
|
64 |
return True
|
65 |
|
|
|
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 |
|