"""Create initial admin user. Run with: docker exec customer_portal python -m customer_portal.scripts.create_admin """ import os from sqlalchemy import create_engine, text from werkzeug.security import generate_password_hash def create_admin(): """Create initial admin user if none exists.""" database_url = os.environ.get( "DATABASE_URL", "postgresql://portal:portal@localhost:5432/customer_portal" ) engine = create_engine(database_url) with engine.connect() as conn: # Check if any admin exists result = conn.execute(text("SELECT COUNT(*) FROM admin_users")) count = result.fetchone()[0] if count > 0: print(f"Es existieren bereits {count} Admin(s).") result = conn.execute(text("SELECT username, name FROM admin_users")) for row in result: print(f" - {row[0]} ({row[1]})") return # Create default admin username = "admin" password = "admin123!" password_hash = generate_password_hash(password) conn.execute( text( """ INSERT INTO admin_users (username, password_hash, name, email, is_active) VALUES (:username, :password_hash, :name, :email, TRUE) """ ), { "username": username, "password_hash": password_hash, "name": "Administrator", "email": "admin@example.com", }, ) conn.commit() print("=" * 50) print("Admin-Benutzer erfolgreich erstellt!") print("=" * 50) print(f" Benutzername: {username}") print(f" Passwort: {password}") print("") print("WICHTIG: Aendern Sie das Passwort nach dem ersten Login!") print("=" * 50) if __name__ == "__main__": create_admin()