AIdeaText commited on
Commit
ad3e638
verified
1 Parent(s): 5a585eb

Update modules/database/sql_db.py

Browse files
Files changed (1) hide show
  1. modules/database/sql_db.py +12 -9
modules/database/sql_db.py CHANGED
@@ -9,12 +9,15 @@ import uuid
9
  logger = logging.getLogger(__name__)
10
 
11
  def get_user(username, role=None):
12
- user_container, _, _ = get_sql_containers()
13
  try:
14
  query = f"SELECT * FROM c WHERE c.id = '{username}'"
15
  if role:
16
  query += f" AND c.role = '{role}'"
17
- items = list(user_container.query_items(query=query, enable_cross_partition_query=True))
 
 
 
18
  return items[0] if items else None
19
  except Exception as e:
20
  logger.error(f"Error al obtener usuario {username}: {str(e)}")
@@ -62,20 +65,20 @@ def create_admin_user(username, password, additional_info=None):
62
  def record_login(username):
63
  """Registra el inicio de sesi贸n de un usuario"""
64
  try:
65
- _, _, session_container = get_sql_containers()
66
-
 
 
67
  session_doc = {
68
  "id": str(uuid.uuid4()),
69
  "type": "session",
70
  "username": username,
71
- "loginTime": datetime.now(timezone.utc).isoformat(),
72
- "partitionKey": username # Asegurarse que coincida con la configuraci贸n
73
  }
74
 
75
- # Crear el documento sin enable_cross_partition_query
76
- result = session_container.create_item(
77
  body=session_doc,
78
- partition_key=username # Usar partition_key en lugar de partitionKey
79
  )
80
  logger.info(f"Sesi贸n registrada para {username}")
81
  return result['id']
 
9
  logger = logging.getLogger(__name__)
10
 
11
  def get_user(username, role=None):
12
+ container = get_container("users")
13
  try:
14
  query = f"SELECT * FROM c WHERE c.id = '{username}'"
15
  if role:
16
  query += f" AND c.role = '{role}'"
17
+ items = list(container.query_items(
18
+ query=query,
19
+ partition_key=username
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)}")
 
65
  def record_login(username):
66
  """Registra el inicio de sesi贸n de un usuario"""
67
  try:
68
+ container = get_container("users_sessions")
69
+ if not container:
70
+ return None
71
+
72
  session_doc = {
73
  "id": str(uuid.uuid4()),
74
  "type": "session",
75
  "username": username,
76
+ "loginTime": datetime.now(timezone.utc).isoformat()
 
77
  }
78
 
79
+ result = container.create_item(
 
80
  body=session_doc,
81
+ partition_key=username
82
  )
83
  logger.info(f"Sesi贸n registrada para {username}")
84
  return result['id']