Update modules/database.py
Browse files- modules/database.py +22 -19
modules/database.py
CHANGED
|
@@ -71,18 +71,30 @@ def initialize_mongodb_connection():
|
|
| 71 |
return False
|
| 72 |
|
| 73 |
#######################################################################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
|
|
|
| 75 |
def create_admin_user(username, password):
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
'created_at': datetime.utcnow().isoformat()
|
| 83 |
-
}
|
| 84 |
-
user_container.create_item(body=user_data)
|
| 85 |
-
|
| 86 |
#######################################################################################################
|
| 87 |
# Funciones para Cosmos DB SQL API (manejo de usuarios)
|
| 88 |
def get_user(username):
|
|
@@ -99,15 +111,6 @@ def get_user(username):
|
|
| 99 |
print(f"Error al obtener usuario {username}: {str(e)}")
|
| 100 |
return None
|
| 101 |
|
| 102 |
-
#######################################################################################################
|
| 103 |
-
def create_user(user_data):
|
| 104 |
-
try:
|
| 105 |
-
user_container.create_item(body=user_data)
|
| 106 |
-
return True
|
| 107 |
-
except Exception as e:
|
| 108 |
-
logger.error(f"Error al crear usuario: {str(e)}")
|
| 109 |
-
return False
|
| 110 |
-
|
| 111 |
################################################################################
|
| 112 |
# Funciones para Cosmos DB MongoDB API (an谩lisis de texto)
|
| 113 |
|
|
|
|
| 71 |
return False
|
| 72 |
|
| 73 |
#######################################################################################################
|
| 74 |
+
def create_user(username, password, role):
|
| 75 |
+
try:
|
| 76 |
+
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
|
| 77 |
+
user_data = {
|
| 78 |
+
'id': username,
|
| 79 |
+
'password': hashed_password,
|
| 80 |
+
'role': role,
|
| 81 |
+
'created_at': datetime.utcnow().isoformat()
|
| 82 |
+
}
|
| 83 |
+
user_container.create_item(body=user_data)
|
| 84 |
+
print(f"Usuario {role} creado: {username}") # Log para depuraci贸n
|
| 85 |
+
return True
|
| 86 |
+
except Exception as e:
|
| 87 |
+
print(f"Error al crear usuario {role}: {str(e)}") # Log para depuraci贸n
|
| 88 |
+
return False
|
| 89 |
|
| 90 |
+
#######################################################################################################
|
| 91 |
def create_admin_user(username, password):
|
| 92 |
+
return create_user(username, password, 'Administrador')
|
| 93 |
+
|
| 94 |
+
#######################################################################################################
|
| 95 |
+
def create_student_user(username, password):
|
| 96 |
+
return create_user(username, password, 'Estudiante')
|
| 97 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
#######################################################################################################
|
| 99 |
# Funciones para Cosmos DB SQL API (manejo de usuarios)
|
| 100 |
def get_user(username):
|
|
|
|
| 111 |
print(f"Error al obtener usuario {username}: {str(e)}")
|
| 112 |
return None
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
################################################################################
|
| 115 |
# Funciones para Cosmos DB MongoDB API (an谩lisis de texto)
|
| 116 |
|