feat(panel): gestión de múltiples administradores en MySQL y simplificación de UI
This commit is contained in:
@@ -117,6 +117,13 @@ def init_db():
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE KEY idx_ui_lang (original_text(255), target_lang)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4''')
|
||||
|
||||
# Tabla para administradores del panel web
|
||||
cursor.execute('''CREATE TABLE IF NOT EXISTS admins
|
||||
(id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
username VARCHAR(50) NOT NULL UNIQUE,
|
||||
password_hash VARCHAR(255) NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4''')
|
||||
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
else:
|
||||
@@ -148,6 +155,22 @@ def init_db():
|
||||
translated_text TEXT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(message_id, target_lang))''')
|
||||
|
||||
# SQLite equivalent for UI translations
|
||||
c.execute('''CREATE TABLE IF NOT EXISTS ui_translations
|
||||
(id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
original_text TEXT NOT NULL,
|
||||
target_lang TEXT NOT NULL,
|
||||
translated_text TEXT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(original_text, target_lang))''')
|
||||
|
||||
# SQLite equivalent for admins
|
||||
c.execute('''CREATE TABLE IF NOT EXISTS admins
|
||||
(id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
username TEXT NOT NULL UNIQUE,
|
||||
password_hash TEXT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)''')
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
@@ -420,3 +443,85 @@ def save_ui_translation(text: str, target_lang: str, translated_text: str):
|
||||
c.execute("INSERT OR REPLACE INTO ui_translations (original_text, target_lang, translated_text) VALUES (?, ?, ?)", (text, target_lang, translated_text))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
# Funciones para administradores
|
||||
def get_admins():
|
||||
db_type = get_db_type()
|
||||
if db_type == "mysql":
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
cursor.execute("SELECT id, username, created_at FROM admins ORDER BY username")
|
||||
rows = cursor.fetchall()
|
||||
cursor.close()
|
||||
return rows
|
||||
else:
|
||||
conn = get_connection()
|
||||
c = conn.cursor()
|
||||
c.execute("SELECT id, username, created_at FROM admins ORDER BY username")
|
||||
rows = [{"id": r[0], "username": r[1], "created_at": r[2]} for r in c.fetchall()]
|
||||
conn.close()
|
||||
return rows
|
||||
|
||||
def get_admin_by_username(username: str):
|
||||
db_type = get_db_type()
|
||||
if db_type == "mysql":
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
cursor.execute("SELECT * FROM admins WHERE username = %s", (username,))
|
||||
row = cursor.fetchone()
|
||||
cursor.close()
|
||||
return row
|
||||
else:
|
||||
conn = get_connection()
|
||||
c = conn.cursor()
|
||||
c.execute("SELECT id, username, password_hash, created_at FROM admins WHERE username = ?", (username,))
|
||||
row = c.fetchone()
|
||||
conn.close()
|
||||
if row:
|
||||
return {"id": row[0], "username": row[1], "password_hash": row[2], "created_at": row[3]}
|
||||
return None
|
||||
|
||||
def add_admin(username: str, password_hash: str):
|
||||
db_type = get_db_type()
|
||||
if db_type == "mysql":
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("INSERT INTO admins (username, password_hash) VALUES (%s, %s)", (username, password_hash))
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
else:
|
||||
conn = get_connection()
|
||||
c = conn.cursor()
|
||||
c.execute("INSERT INTO admins (username, password_hash) VALUES (?, ?)", (username, password_hash))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def delete_admin(admin_id: int):
|
||||
db_type = get_db_type()
|
||||
if db_type == "mysql":
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("DELETE FROM admins WHERE id = %s", (admin_id,))
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
else:
|
||||
conn = get_connection()
|
||||
c = conn.cursor()
|
||||
c.execute("DELETE FROM admins WHERE id = ?", (admin_id,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def update_admin_password(admin_id: int, password_hash: str):
|
||||
db_type = get_db_type()
|
||||
if db_type == "mysql":
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("UPDATE admins SET password_hash = %s WHERE id = %s", (password_hash, admin_id))
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
else:
|
||||
conn = get_connection()
|
||||
c = conn.cursor()
|
||||
c.execute("UPDATE admins SET password_hash = ? WHERE id = ?", (password_hash, admin_id))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
Reference in New Issue
Block a user