diff --git a/srv/poe_manager/app.py b/srv/poe_manager/app.py index f9e9ea0..cf76918 100644 --- a/srv/poe_manager/app.py +++ b/srv/poe_manager/app.py @@ -311,103 +311,58 @@ def toggle_device(mac): def switches(): conn = get_db_connection() - # 🔒 Admin-Prüfung - if request.method == 'POST' and not current_user.is_admin: - flash("Zugriff verweigert!") - conn.close() - return redirect(url_for('switches')) - - # ------------------------- - # 🔹 SWITCH HINZUFÜGEN - # ------------------------- + # Inline-Add if request.method == 'POST' and 'add_switch' in request.form: - hostname = request.form['hostname'].strip() - ip = request.form['ip'].strip() - username = request.form['username'].strip() - password = encrypt_password(request.form['password'].strip()) - - # Prüfung auf doppelte Hostnamen - existing_host = conn.execute( - "SELECT hostname FROM switches WHERE hostname = ?", (hostname,) - ).fetchone() - if existing_host: - flash(f"Hostname '{hostname}' existiert bereits!") - conn.close() + if not current_user.is_admin: + flash("Zugriff verweigert!") return redirect(url_for('switches')) + hostname = request.form['hostname'] + ip = request.form['ip'] + username = request.form['username'] + password = encrypt_password(request.form['password']) + try: + conn.execute("INSERT INTO switches (hostname, ip, username, password) VALUES (?, ?, ?, ?)", + (hostname, ip, username, password)) + conn.commit() + flash(f"Switch {hostname} hinzugefügt.") + except sqlite3.IntegrityError: + flash("Hostname existiert bereits oder Eingabefehler!") - # Prüfung auf doppelte IP - existing_ip = conn.execute( - "SELECT hostname FROM switches WHERE ip = ?", (ip,) - ).fetchone() - if existing_ip: - flash(f"IP-Adresse {ip} wird bereits vom Switch '{existing_ip['hostname']}' verwendet!") - conn.close() + # Inline-Edit + if request.method == 'POST' and 'edit_switch' in request.form: + if not current_user.is_admin: + flash("Zugriff verweigert!") return redirect(url_for('switches')) - - # Einfügen - conn.execute( - "INSERT INTO switches (hostname, ip, username, password) VALUES (?, ?, ?, ?)", - (hostname, ip, username, password) - ) - conn.commit() - flash(f"Switch '{hostname}' erfolgreich hinzugefügt.") - - # ------------------------- - # 🔹 SWITCH BEARBEITEN - # ------------------------- - elif request.method == 'POST' and 'edit_switch' in request.form: old_hostname = request.form['old_hostname'] - hostname = request.form['hostname'].strip() - ip = request.form['ip'].strip() - username = request.form['username'].strip() - password = encrypt_password(request.form['password'].strip()) + hostname = request.form['hostname'] + ip = request.form['ip'] + username = request.form['username'] + password = encrypt_password(request.form['password']) + try: + conn.execute(""" + UPDATE switches + SET hostname=?, ip=?, username=?, password=? + WHERE hostname=? + """, (hostname, ip, username, password, old_hostname)) + conn.commit() + flash(f"Switch {hostname} aktualisiert.") + except sqlite3.IntegrityError: + flash("Hostname existiert bereits oder Eingabefehler!") - # Hostname prüfen - existing_host = conn.execute( - "SELECT hostname FROM switches WHERE hostname = ? AND hostname != ?", - (hostname, old_hostname) - ).fetchone() - if existing_host: - flash(f"Hostname '{hostname}' existiert bereits für einen anderen Switch!") - conn.close() + # Inline-Delete + if request.method == 'POST' and 'delete_switch' in request.form: + if not current_user.is_admin: + flash("Zugriff verweigert!") return redirect(url_for('switches')) - - # IP prüfen - existing_ip = conn.execute( - "SELECT hostname FROM switches WHERE ip = ? AND hostname != ?", - (ip, old_hostname) - ).fetchone() - if existing_ip: - flash(f"IP-Adresse {ip} wird bereits vom Switch '{existing_ip['hostname']}' verwendet!") - conn.close() - return redirect(url_for('switches')) - - # Update durchführen - conn.execute(""" - UPDATE switches - SET hostname=?, ip=?, username=?, password=? - WHERE hostname=? - """, (hostname, ip, username, password, old_hostname)) - conn.commit() - flash(f"Switch '{hostname}' erfolgreich aktualisiert.") - - # ------------------------- - # 🔹 SWITCH LÖSCHEN - # ------------------------- - elif request.method == 'POST' and 'delete_switch' in request.form: del_hostname = request.form['delete_switch'] conn.execute("UPDATE devices SET switch_hostname=NULL WHERE switch_hostname=?", (del_hostname,)) conn.execute("DELETE FROM switches WHERE hostname=?", (del_hostname,)) conn.commit() - flash(f"Switch '{del_hostname}' gelöscht.") + flash(f"Switch {del_hostname} gelöscht.") - # ------------------------- - # 🔹 SWITCHES LADEN - # ------------------------- - switches = conn.execute("SELECT hostname, ip, username FROM switches ORDER BY hostname ASC").fetchall() + switches = conn.execute("SELECT hostname, ip, username FROM switches").fetchall() conn.close() - return render_template('switches.html', switches=switches) - + return render_template('switche.html', switches=switches) @app.route("/get_log") @login_required