From ce9563794a76876cf3c7d7d6b92343a9fd34e367 Mon Sep 17 00:00:00 2001 From: Quality App System Date: Thu, 15 Jan 2026 19:43:46 +0200 Subject: [PATCH] Fix: Add truncate-table endpoint and improve dark mode visibility for usernames - Added missing /api/maintenance/truncate-table endpoint to fix 404 error - Fixed dark mode visibility issue for usernames in Current Users table - Added explicit color styling for username strong tags in both light and dark modes --- py_app/app/routes.py | 80 +++++++++++++++++++ .../app/templates/user_management_simple.html | 9 +++ 2 files changed, 89 insertions(+) diff --git a/py_app/app/routes.py b/py_app/app/routes.py index 93985de..93e7c37 100644 --- a/py_app/app/routes.py +++ b/py_app/app/routes.py @@ -5216,6 +5216,86 @@ def drop_table(): }), 500 +@bp.route('/api/maintenance/truncate-table', methods=['POST']) +@admin_plus +def truncate_table(): + """Truncate a database table - removes all rows but keeps structure""" + try: + data = request.json + table_name = data.get('table_name', '').strip() + + if not table_name: + return jsonify({ + 'success': False, + 'message': 'Table name is required' + }), 400 + + # Validate table name to prevent SQL injection + if not table_name.replace('_', '').isalnum(): + return jsonify({ + 'success': False, + 'message': 'Invalid table name format' + }), 400 + + # Load database config directly + settings_file = os.path.join(current_app.instance_path, 'external_server.conf') + config = {} + with open(settings_file, 'r') as f: + for line in f: + if '=' in line: + key, value = line.strip().split('=', 1) + config[key] = value + + conn = mariadb.connect( + host=config.get('server_domain', 'localhost'), + port=int(config.get('port', '3306')), + user=config.get('username', 'root'), + password=config.get('password', ''), + database=config.get('database_name', 'trasabilitate') + ) + cursor = conn.cursor() + + # Verify table exists and get row count + cursor.execute(""" + SELECT COUNT(*) as count + FROM information_schema.TABLES + WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s + """, (config.get('database_name', 'trasabilitate'), table_name)) + + result = cursor.fetchone() + if not result or result[0] == 0: + cursor.close() + conn.close() + return jsonify({ + 'success': False, + 'message': f'Table "{table_name}" does not exist' + }), 404 + + # Get current row count before truncating + cursor.execute(f"SELECT COUNT(*) FROM `{table_name}`") + row_count = cursor.fetchone()[0] + + # Truncate the table + cursor.execute(f"TRUNCATE TABLE `{table_name}`") + conn.commit() + + cursor.close() + conn.close() + + return jsonify({ + 'success': True, + 'message': f'Table "{table_name}" has been cleared successfully', + 'rows_cleared': row_count, + 'structure_preserved': True + }) + + except Exception as e: + return jsonify({ + 'success': False, + 'message': f'Failed to truncate table: {str(e)}' + }), 500 + + @bp.route('/api/backup/table', methods=['POST']) @admin_plus def backup_single_table(): diff --git a/py_app/app/templates/user_management_simple.html b/py_app/app/templates/user_management_simple.html index c177e73..d2d1434 100644 --- a/py_app/app/templates/user_management_simple.html +++ b/py_app/app/templates/user_management_simple.html @@ -245,6 +245,15 @@ background-color: rgba(255, 255, 255, 0.075) !important; } + /* Theme-aware username styling */ + body.light-mode .user-management-page .table td strong { + color: #000 !important; + } + + body.dark-mode .user-management-page .table td strong { + color: #fff !important; + } + /* Theme-aware form elements */ .user-management-page .form-control { border: 1px solid #ced4da !important;