import i eksport serwerów z nowymi danymi

This commit is contained in:
Mateusz Gruszczyński 2025-03-08 15:39:31 +01:00
parent 4ac60ee541
commit 55f9cbfb9a

55
app.py
View File

@ -991,11 +991,32 @@ def export_servers_to_csv():
hosts = Host.query.filter_by(user_id=user_id).all()
si = StringIO()
cw = csv.writer(si)
cw.writerow(['hostname', 'username', 'password', 'port', 'type', 'auth_method', 'private_key', 'key_passphrase'])
# Dodajemy wszystkie istotne pola
cw.writerow([
'id', 'hostname', 'username', 'password', 'port', 'type',
'auth_method', 'private_key', 'key_passphrase', 'auto_deploy_enabled',
'auto_backup_enabled', 'preferred_hostfile_id', 'use_daemon', 'daemon_url', 'daemon_token'
])
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 ''])
cw.writerow([
host.id,
host.hostname,
host.username,
host.password,
host.port,
host.type,
host.auth_method,
host.private_key or '',
host.key_passphrase or '',
host.auto_deploy_enabled,
host.auto_backup_enabled,
host.preferred_hostfile_id if host.preferred_hostfile_id is not None else '',
host.use_daemon,
host.daemon_url or '',
host.daemon_token or ''
])
output = si.getvalue()
return Response(output, mimetype="text/csv", headers={"Content-Disposition": "attachment;filename=servers.csv"})
return Response(output, mimetype="text/csv", headers={"Content-Disposition": "attachment;filename=servers_full.csv"})
@app.route('/import-servers', methods=['GET', 'POST'])
def import_servers():
@ -1008,15 +1029,31 @@ def import_servers():
return redirect(url_for('import_servers'))
stream = StringIO(file.stream.read().decode("UTF8"), newline=None)
csv_input = csv.reader(stream)
header = next(csv_input)
header = next(csv_input) # zakładamy, że pierwszy wiersz zawiera nagłówki
for row in csv_input:
if len(row) < 8:
# Sprawdzamy, czy wiersz zawiera wszystkie wymagane kolumny (w tym przypadku 15)
if len(row) < 15:
continue
hostname, username, password_val, port_str, host_type, auth_method, private_key, key_passphrase = row
# Rozpakowywanie wiersza zgodnie z kolejnością kolumn w eksporcie
(_, hostname, username, password_val, port_str, host_type, auth_method,
private_key, key_passphrase, auto_deploy_enabled, auto_backup_enabled,
preferred_hostfile_id, use_daemon, daemon_url, daemon_token) = row
try:
port = int(port_str)
except ValueError:
port = 22
# Konwersja wartości logicznych
auto_deploy_enabled = auto_deploy_enabled.lower() in ['true', '1', 'yes']
auto_backup_enabled = auto_backup_enabled.lower() in ['true', '1', 'yes']
use_daemon = use_daemon.lower() in ['true', '1', 'yes']
try:
preferred_hostfile_id = int(preferred_hostfile_id) if preferred_hostfile_id else None
except ValueError:
preferred_hostfile_id = None
host = Host(
hostname=hostname,
username=username,
@ -1026,6 +1063,12 @@ def import_servers():
auth_method=auth_method,
private_key=private_key if private_key else None,
key_passphrase=key_passphrase if key_passphrase else None,
auto_deploy_enabled=auto_deploy_enabled,
auto_backup_enabled=auto_backup_enabled,
preferred_hostfile_id=preferred_hostfile_id,
use_daemon=use_daemon,
daemon_url=daemon_url if daemon_url else None,
daemon_token=daemon_token if daemon_token else None,
user_id=session['user_id']
)
db.session.add(host)