Update modules/database/sql_db.py
Browse files- modules/database/sql_db.py +13 -18
modules/database/sql_db.py
CHANGED
@@ -8,6 +8,8 @@ import uuid
|
|
8 |
|
9 |
logger = logging.getLogger(__name__)
|
10 |
|
|
|
|
|
11 |
def get_user(username, role=None):
|
12 |
container = get_container("users")
|
13 |
try:
|
@@ -15,14 +17,16 @@ def get_user(username, role=None):
|
|
15 |
if role:
|
16 |
query += f" AND c.role = '{role}'"
|
17 |
items = list(container.query_items(
|
18 |
-
query=query
|
19 |
-
enable_cross_partition_query=True
|
20 |
))
|
21 |
return items[0] if items else None
|
22 |
except Exception as e:
|
23 |
logger.error(f"Error al obtener usuario {username}: {str(e)}")
|
24 |
return None
|
25 |
|
|
|
|
|
|
|
26 |
def get_admin_user(username):
|
27 |
return get_user(username, role='Administrador')
|
28 |
|
@@ -87,14 +91,11 @@ def record_login(username):
|
|
87 |
"username": username,
|
88 |
"loginTime": datetime.now(timezone.utc).isoformat(),
|
89 |
"additional_info": {},
|
90 |
-
# El campo para partition key debe estar en el documento
|
91 |
"partitionKey": username
|
92 |
}
|
93 |
|
94 |
-
# Crear el documento usando options
|
95 |
result = container.create_item(
|
96 |
-
body=session_doc
|
97 |
-
enable_cross_partition_query=True
|
98 |
)
|
99 |
logger.info(f"Sesi贸n {session_id} registrada para {username}")
|
100 |
return session_id
|
@@ -102,7 +103,6 @@ def record_login(username):
|
|
102 |
logger.error(f"Error registrando login: {str(e)}")
|
103 |
return None
|
104 |
|
105 |
-
|
106 |
###########################################################
|
107 |
|
108 |
def record_logout(username, session_id):
|
@@ -113,7 +113,6 @@ def record_logout(username, session_id):
|
|
113 |
logger.error("No se pudo obtener el contenedor users_sessions")
|
114 |
return False
|
115 |
|
116 |
-
# Obtener la sesi贸n actual
|
117 |
query = "SELECT * FROM c WHERE c.id = @id AND c.username = @username"
|
118 |
params = [
|
119 |
{"name": "@id", "value": session_id},
|
@@ -122,8 +121,7 @@ def record_logout(username, session_id):
|
|
122 |
|
123 |
items = list(container.query_items(
|
124 |
query=query,
|
125 |
-
parameters=params
|
126 |
-
enable_cross_partition_query=True
|
127 |
))
|
128 |
|
129 |
if not items:
|
@@ -137,12 +135,12 @@ def record_logout(username, session_id):
|
|
137 |
|
138 |
session.update({
|
139 |
"logoutTime": logout_time.isoformat(),
|
140 |
-
"sessionDuration": duration
|
|
|
141 |
})
|
142 |
|
143 |
-
# Actualizar el documento
|
144 |
container.upsert_item(
|
145 |
-
body=session
|
146 |
)
|
147 |
logger.info(f"Sesi贸n {session_id} cerrada para {username}, duraci贸n: {duration}s")
|
148 |
return True
|
@@ -171,11 +169,9 @@ def get_recent_sessions(limit=10):
|
|
171 |
|
172 |
sessions = list(container.query_items(
|
173 |
query=query,
|
174 |
-
parameters=[{"name": "@limit", "value": limit}]
|
175 |
-
enable_cross_partition_query=True
|
176 |
))
|
177 |
|
178 |
-
# Validar y limpiar los datos
|
179 |
clean_sessions = []
|
180 |
for session in sessions:
|
181 |
try:
|
@@ -215,8 +211,7 @@ def get_user_total_time(username):
|
|
215 |
|
216 |
result = list(container.query_items(
|
217 |
query=query,
|
218 |
-
parameters=[{"name": "@username", "value": username}]
|
219 |
-
enable_cross_partition_query=True
|
220 |
))
|
221 |
|
222 |
return result[0] if result and result[0] is not None else 0
|
|
|
8 |
|
9 |
logger = logging.getLogger(__name__)
|
10 |
|
11 |
+
###########################################
|
12 |
+
|
13 |
def get_user(username, role=None):
|
14 |
container = get_container("users")
|
15 |
try:
|
|
|
17 |
if role:
|
18 |
query += f" AND c.role = '{role}'"
|
19 |
items = list(container.query_items(
|
20 |
+
query=query # Removido enable_cross_partition_query
|
|
|
21 |
))
|
22 |
return items[0] if items else None
|
23 |
except Exception as e:
|
24 |
logger.error(f"Error al obtener usuario {username}: {str(e)}")
|
25 |
return None
|
26 |
|
27 |
+
###########################################
|
28 |
+
|
29 |
+
|
30 |
def get_admin_user(username):
|
31 |
return get_user(username, role='Administrador')
|
32 |
|
|
|
91 |
"username": username,
|
92 |
"loginTime": datetime.now(timezone.utc).isoformat(),
|
93 |
"additional_info": {},
|
|
|
94 |
"partitionKey": username
|
95 |
}
|
96 |
|
|
|
97 |
result = container.create_item(
|
98 |
+
body=session_doc # Solo el body
|
|
|
99 |
)
|
100 |
logger.info(f"Sesi贸n {session_id} registrada para {username}")
|
101 |
return session_id
|
|
|
103 |
logger.error(f"Error registrando login: {str(e)}")
|
104 |
return None
|
105 |
|
|
|
106 |
###########################################################
|
107 |
|
108 |
def record_logout(username, session_id):
|
|
|
113 |
logger.error("No se pudo obtener el contenedor users_sessions")
|
114 |
return False
|
115 |
|
|
|
116 |
query = "SELECT * FROM c WHERE c.id = @id AND c.username = @username"
|
117 |
params = [
|
118 |
{"name": "@id", "value": session_id},
|
|
|
121 |
|
122 |
items = list(container.query_items(
|
123 |
query=query,
|
124 |
+
parameters=params # Removido enable_cross_partition_query
|
|
|
125 |
))
|
126 |
|
127 |
if not items:
|
|
|
135 |
|
136 |
session.update({
|
137 |
"logoutTime": logout_time.isoformat(),
|
138 |
+
"sessionDuration": duration,
|
139 |
+
"partitionKey": username # Asegurar que partitionKey est茅 presente
|
140 |
})
|
141 |
|
|
|
142 |
container.upsert_item(
|
143 |
+
body=session # Solo el body
|
144 |
)
|
145 |
logger.info(f"Sesi贸n {session_id} cerrada para {username}, duraci贸n: {duration}s")
|
146 |
return True
|
|
|
169 |
|
170 |
sessions = list(container.query_items(
|
171 |
query=query,
|
172 |
+
parameters=[{"name": "@limit", "value": limit}] # Removido enable_cross_partition_query
|
|
|
173 |
))
|
174 |
|
|
|
175 |
clean_sessions = []
|
176 |
for session in sessions:
|
177 |
try:
|
|
|
211 |
|
212 |
result = list(container.query_items(
|
213 |
query=query,
|
214 |
+
parameters=[{"name": "@username", "value": username}] # Removido enable_cross_partition_query
|
|
|
215 |
))
|
216 |
|
217 |
return result[0] if result and result[0] is not None else 0
|