Update modules/database/sql_db.py
Browse files- modules/database/sql_db.py +30 -5
modules/database/sql_db.py
CHANGED
@@ -9,16 +9,34 @@ import uuid
|
|
9 |
logger = logging.getLogger(__name__)
|
10 |
|
11 |
def get_user(username, role=None):
|
|
|
12 |
container = get_container("users")
|
|
|
|
|
|
|
|
|
13 |
try:
|
14 |
-
|
|
|
|
|
|
|
15 |
if role:
|
16 |
-
query +=
|
|
|
|
|
17 |
items = list(container.query_items(
|
18 |
query=query,
|
19 |
-
|
|
|
20 |
))
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
except Exception as e:
|
23 |
logger.error(f"Error al obtener usuario {username}: {str(e)}")
|
24 |
return None
|
@@ -33,21 +51,28 @@ def get_teacher_user(username):
|
|
33 |
return get_user(username, role='Profesor')
|
34 |
|
35 |
def create_user(username, password, role, additional_info=None):
|
|
|
36 |
container = get_container("users")
|
|
|
|
|
|
|
|
|
37 |
try:
|
38 |
user_data = {
|
39 |
'id': username,
|
40 |
-
'password': password,
|
41 |
'role': role,
|
42 |
'timestamp': datetime.now(timezone.utc).isoformat(),
|
43 |
'additional_info': additional_info or {}
|
44 |
}
|
|
|
45 |
container.create_item(
|
46 |
body=user_data,
|
47 |
partition_key=username
|
48 |
)
|
49 |
logger.info(f"Usuario {role} creado: {username}")
|
50 |
return True
|
|
|
51 |
except Exception as e:
|
52 |
logger.error(f"Error al crear usuario {role}: {str(e)}")
|
53 |
return False
|
|
|
9 |
logger = logging.getLogger(__name__)
|
10 |
|
11 |
def get_user(username, role=None):
|
12 |
+
"""Obtiene un usuario por su username y opcionalmente por rol"""
|
13 |
container = get_container("users")
|
14 |
+
if not container:
|
15 |
+
logger.error("No se pudo obtener el contenedor de usuarios")
|
16 |
+
return None
|
17 |
+
|
18 |
try:
|
19 |
+
# Construir la consulta
|
20 |
+
query = "SELECT * FROM c WHERE c.id = @username"
|
21 |
+
parameters = [{"name": "@username", "value": username}]
|
22 |
+
|
23 |
if role:
|
24 |
+
query += " AND c.role = @role"
|
25 |
+
parameters.append({"name": "@role", "value": role})
|
26 |
+
|
27 |
items = list(container.query_items(
|
28 |
query=query,
|
29 |
+
parameters=parameters,
|
30 |
+
enable_cross_partition_query=True # Necesario para búsquedas por rol
|
31 |
))
|
32 |
+
|
33 |
+
if items:
|
34 |
+
logger.info(f"Usuario encontrado: {username}")
|
35 |
+
return items[0]
|
36 |
+
else:
|
37 |
+
logger.warning(f"Usuario no encontrado: {username}")
|
38 |
+
return None
|
39 |
+
|
40 |
except Exception as e:
|
41 |
logger.error(f"Error al obtener usuario {username}: {str(e)}")
|
42 |
return None
|
|
|
51 |
return get_user(username, role='Profesor')
|
52 |
|
53 |
def create_user(username, password, role, additional_info=None):
|
54 |
+
"""Crea un nuevo usuario"""
|
55 |
container = get_container("users")
|
56 |
+
if not container:
|
57 |
+
logger.error("No se pudo obtener el contenedor de usuarios")
|
58 |
+
return False
|
59 |
+
|
60 |
try:
|
61 |
user_data = {
|
62 |
'id': username,
|
63 |
+
'password': password,
|
64 |
'role': role,
|
65 |
'timestamp': datetime.now(timezone.utc).isoformat(),
|
66 |
'additional_info': additional_info or {}
|
67 |
}
|
68 |
+
|
69 |
container.create_item(
|
70 |
body=user_data,
|
71 |
partition_key=username
|
72 |
)
|
73 |
logger.info(f"Usuario {role} creado: {username}")
|
74 |
return True
|
75 |
+
|
76 |
except Exception as e:
|
77 |
logger.error(f"Error al crear usuario {role}: {str(e)}")
|
78 |
return False
|