refactor web interface
This commit is contained in:
84
app.py
84
app.py
@@ -302,14 +302,14 @@ def change_password():
|
||||
user.password = new_password
|
||||
db.session.commit()
|
||||
flash('Password changed successfully', 'success')
|
||||
return redirect(url_for('dashboard'))
|
||||
return redirect(url_for('change_password'))
|
||||
return render_template('change_password.html')
|
||||
|
||||
# -------------------
|
||||
# ZARZĄDZANIE HOSTAMI
|
||||
# -------------------
|
||||
@app.route('/hosts', methods=['GET', 'POST'])
|
||||
def manage_hosts():
|
||||
@app.route('/add_server', methods=['GET', 'POST'])
|
||||
def add_server():
|
||||
if 'user_id' not in session:
|
||||
return redirect(url_for('login'))
|
||||
if request.method == 'POST':
|
||||
@@ -341,11 +341,12 @@ def manage_hosts():
|
||||
db.session.add(host)
|
||||
db.session.commit()
|
||||
flash('Host added successfully', 'success')
|
||||
hosts = Host.query.filter_by(user_id=session['user_id']).all()
|
||||
return render_template('hosts.html', hosts=hosts)
|
||||
# Po dodaniu możesz przekierować do listy serwerów lub pozostawić na formularzu
|
||||
return redirect(url_for('server_list'))
|
||||
return render_template('add_server.html')
|
||||
|
||||
@app.route('/delete-host/<int:id>')
|
||||
def delete_host(id):
|
||||
@app.route('/delete-server/<int:id>')
|
||||
def delete_server(id):
|
||||
if 'user_id' not in session:
|
||||
return redirect(url_for('login'))
|
||||
host = db.session.get(Host, id)
|
||||
@@ -355,16 +356,23 @@ def delete_host(id):
|
||||
flash('Host deleted', 'info')
|
||||
else:
|
||||
flash('Host not found or unauthorized', 'danger')
|
||||
return redirect(url_for('manage_hosts'))
|
||||
return redirect(url_for('server_list'))
|
||||
|
||||
@app.route('/edit-host/<int:id>', methods=['GET', 'POST'])
|
||||
def edit_host(id):
|
||||
@app.route('/server-list', methods=['GET'])
|
||||
def server_list():
|
||||
if 'user_id' not in session:
|
||||
return redirect(url_for('login'))
|
||||
hosts = Host.query.filter_by(user_id=session['user_id']).all()
|
||||
return render_template('server_list.html', hosts=hosts)
|
||||
|
||||
@app.route('/edit-server/<int:id>', methods=['GET', 'POST'])
|
||||
def edit_server(id):
|
||||
if 'user_id' not in session:
|
||||
return redirect(url_for('login'))
|
||||
host = db.session.get(Host, id)
|
||||
if not host or host.user_id != session['user_id']:
|
||||
flash('Host not found or unauthorized', 'danger')
|
||||
return redirect(url_for('manage_hosts'))
|
||||
flash('Server not found or unauthorized', 'danger')
|
||||
return redirect(url_for('server_list'))
|
||||
if request.method == 'POST':
|
||||
host.hostname = request.form['hostname']
|
||||
host.username = request.form['username']
|
||||
@@ -383,34 +391,34 @@ def edit_host(id):
|
||||
if host.auth_method == 'ssh_key' and new_passphrase:
|
||||
host.key_passphrase = new_passphrase
|
||||
db.session.commit()
|
||||
flash('Host updated successfully', 'success')
|
||||
return redirect(url_for('manage_hosts'))
|
||||
return render_template('edit_host.html', host=host)
|
||||
flash('Server updated successfully', 'success')
|
||||
return redirect(url_for('server_list'))
|
||||
return render_template('edit_server.html', host=host)
|
||||
|
||||
# -------------------
|
||||
# TESTOWANIE POŁĄCZENIA SSH DLA HOSTA
|
||||
# -------------------
|
||||
@app.route('/test-host/<int:id>', methods=['GET'])
|
||||
def test_host(id):
|
||||
@app.route('/test-server-connection/<int:id>', methods=['GET'])
|
||||
def test_server_connection(id):
|
||||
if 'user_id' not in session:
|
||||
return redirect(url_for('login'))
|
||||
host = db.session.get(Host, id)
|
||||
if not host or host.user_id != session['user_id']:
|
||||
flash('Host not found or unauthorized', 'danger')
|
||||
return redirect(url_for('manage_hosts'))
|
||||
return redirect(url_for('server_list'))
|
||||
try:
|
||||
ssh = open_ssh_connection(host)
|
||||
ssh.close()
|
||||
flash(f'SSH connection to {host.hostname} successful.', 'success')
|
||||
except Exception as e:
|
||||
flash(f'SSH connection to {host.hostname} failed: {str(e)}', 'danger')
|
||||
return redirect(url_for('manage_hosts'))
|
||||
return redirect(url_for('server_list'))
|
||||
|
||||
# -------------------
|
||||
# ROUTE: CZYSZCZENIE HOSTS - CAŁA GRUPA
|
||||
# -------------------
|
||||
@app.route('/clear-hosts', methods=['GET', 'POST'])
|
||||
def clear_all_hosts():
|
||||
@app.route('/clear-server', methods=['GET', 'POST'])
|
||||
def clear_server():
|
||||
if 'user_id' not in session:
|
||||
return redirect(url_for('login'))
|
||||
if request.method == 'POST':
|
||||
@@ -432,8 +440,8 @@ def clear_all_hosts():
|
||||
flash(f'Cleared Mikrotik host: {h.hostname}', 'success')
|
||||
except Exception as e:
|
||||
flash(f'Error clearing Mikrotik host {h.hostname}: {str(e)}', 'danger')
|
||||
return redirect(url_for('dashboard'))
|
||||
return render_template('clear_hosts.html')
|
||||
return redirect(url_for('clear_server'))
|
||||
return render_template('clear_servers.html')
|
||||
|
||||
# -------------------
|
||||
# ZARZĄDZANIE PLIKAMI HOSTS (WIELOKROTNE PLIKI)
|
||||
@@ -532,14 +540,14 @@ def deploy_hosts_file(file_id):
|
||||
# -------------------
|
||||
# BACKUP
|
||||
# -------------------
|
||||
@app.route('/backup-host/<int:host_id>', methods=['GET'])
|
||||
def backup_host(host_id):
|
||||
@app.route('/server-backup/<int:host_id>', methods=['GET'])
|
||||
def server_backup(host_id):
|
||||
if 'user_id' not in session:
|
||||
return redirect(url_for('login'))
|
||||
host = db.session.get(Host, host_id)
|
||||
if not host or host.user_id != session['user_id']:
|
||||
flash('Host not found or unauthorized', 'danger')
|
||||
return redirect(url_for('manage_hosts'))
|
||||
return redirect(url_for('server_list'))
|
||||
try:
|
||||
if host.type == 'mikrotik':
|
||||
ssh = open_ssh_connection(host)
|
||||
@@ -566,7 +574,7 @@ def backup_host(host_id):
|
||||
flash(f'Backup for host {host.hostname} created successfully.', 'success')
|
||||
except Exception as e:
|
||||
flash(f'Error creating backup for host {host.hostname}: {str(e)}', 'danger')
|
||||
return redirect(url_for('manage_hosts'))
|
||||
return redirect(url_for('server_list'))
|
||||
|
||||
@app.route('/backups')
|
||||
def backups():
|
||||
@@ -674,8 +682,8 @@ def backup_all():
|
||||
# -------------------
|
||||
# IMPORT/EXPORT HOSTÓW
|
||||
# -------------------
|
||||
@app.route('/export-hosts', methods=['GET'])
|
||||
def export_hosts():
|
||||
@app.route('/export-servers-to-csv', methods=['GET'])
|
||||
def export_servers_to_csv():
|
||||
if 'user_id' not in session:
|
||||
return redirect(url_for('login'))
|
||||
user_id = session['user_id']
|
||||
@@ -686,17 +694,17 @@ def export_hosts():
|
||||
for host in hosts:
|
||||
cw.writerow([host.hostname, host.username, host.password, host.port, host.type, host.auth_method, host.private_key or '', host.key_passphrase or ''])
|
||||
output = si.getvalue()
|
||||
return Response(output, mimetype="text/csv", headers={"Content-Disposition": "attachment;filename=hosts.csv"})
|
||||
return Response(output, mimetype="text/csv", headers={"Content-Disposition": "attachment;filename=servers.csv"})
|
||||
|
||||
@app.route('/import-hosts', methods=['GET', 'POST'])
|
||||
def import_hosts():
|
||||
@app.route('/import-servers', methods=['GET', 'POST'])
|
||||
def import_servers():
|
||||
if 'user_id' not in session:
|
||||
return redirect(url_for('login'))
|
||||
if request.method == 'POST':
|
||||
file = request.files.get('file')
|
||||
if not file:
|
||||
flash('No file uploaded', 'danger')
|
||||
return redirect(url_for('import_hosts'))
|
||||
return redirect(url_for('import_servers'))
|
||||
stream = StringIO(file.stream.read().decode("UTF8"), newline=None)
|
||||
csv_input = csv.reader(stream)
|
||||
header = next(csv_input)
|
||||
@@ -722,8 +730,8 @@ def import_hosts():
|
||||
db.session.add(host)
|
||||
db.session.commit()
|
||||
flash('Hosts imported successfully', 'success')
|
||||
return redirect(url_for('manage_hosts'))
|
||||
return render_template('import_hosts.html')
|
||||
return redirect(url_for('server_list'))
|
||||
return render_template('import_servers.html')
|
||||
|
||||
@app.route('/clear-host/<int:id>', methods=['GET'])
|
||||
def clear_host(id):
|
||||
@@ -732,7 +740,7 @@ def clear_host(id):
|
||||
host = db.session.get(Host, id)
|
||||
if not host or host.user_id != session['user_id']:
|
||||
flash('Host not found or unauthorized', 'danger')
|
||||
return redirect(url_for('manage_hosts'))
|
||||
return redirect(url_for('server_list'))
|
||||
try:
|
||||
if host.type == 'linux':
|
||||
default_content = ensure_local_defaults("")
|
||||
@@ -742,7 +750,7 @@ def clear_host(id):
|
||||
flash(f'Cleared host: {host.hostname}', 'success')
|
||||
except Exception as e:
|
||||
flash(f'Error clearing host {host.hostname}: {str(e)}', 'danger')
|
||||
return redirect(url_for('manage_hosts'))
|
||||
return redirect(url_for('server_list'))
|
||||
|
||||
# -------------------
|
||||
# STRONA USTAWIEŃ (SETTINGS)
|
||||
@@ -784,7 +792,7 @@ def settings():
|
||||
|
||||
db.session.commit()
|
||||
flash('Settings updated', 'success')
|
||||
return redirect(url_for('dashboard'))
|
||||
return redirect(url_for('settings'))
|
||||
|
||||
return render_template('settings.html', settings=user_settings)
|
||||
|
||||
|
Reference in New Issue
Block a user