From 4b5d7526ffc7360fcdd3c21891685a57153432cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Gruszczy=C5=84ski?= Date: Tue, 25 Feb 2025 09:44:24 +0100 Subject: [PATCH] refactor web interface --- app.py | 63 ++++++++++++++++++++++++++--------- templates/backups.html | 1 + templates/clear_servers.html | 64 +++++++++++++++++++++++++++++++----- 3 files changed, 103 insertions(+), 25 deletions(-) diff --git a/app.py b/app.py index afc5b92..30eefd3 100644 --- a/app.py +++ b/app.py @@ -176,8 +176,22 @@ def automated_backup_for_host(host): def automated_backups(): with app.app_context(): hosts = Host.query.all() + now = datetime.now(timezone.utc) for host in hosts: - automated_backup_for_host(host) + # Pobieramy ustawienia użytkownika + settings = UserSettings.query.filter_by(user_id=host.user_id).first() + backup_interval = settings.backup_interval if settings and settings.backup_interval else 60 + + # Pobieramy ostatni backup dla danego hosta + last_backup = Backup.query.filter_by(user_id=host.user_id, host_id=host.id).order_by(Backup.created_at.desc()).first() + if last_backup: + last_backup_time = last_backup.created_at + else: + last_backup_time = None + + # Jeśli brak backupu lub minęło wystarczająco czasu, wykonujemy backup + if (last_backup_time is None) or ((now - last_backup_time).total_seconds() >= backup_interval * 60): + automated_backup_for_host(host) def wrap_content_with_comments(content): now_str = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC") @@ -421,27 +435,44 @@ def test_server_connection(id): def clear_server(): if 'user_id' not in session: return redirect(url_for('login')) + # Pobierz listę hostów dla bieżącego użytkownika + hosts = Host.query.filter_by(user_id=session['user_id']).all() if request.method == 'POST': - user_id = session['user_id'] + clear_option = request.form.get('clear_option', 'all') linux_clear = request.form.get('linux') mikrotik_clear = request.form.get('mikrotik') - hosts = Host.query.filter_by(user_id=user_id).all() default_content = ensure_local_defaults("") - for h in hosts: - if h.type == 'linux' and linux_clear: + if clear_option == 'one': + host_id = request.form.get('host_id') + if host_id: + host = db.session.get(Host, int(host_id)) + if host and host.user_id == session['user_id']: + try: + if host.type == 'linux' and linux_clear: + clear_linux(host, default_content) + elif host.type == 'mikrotik' and mikrotik_clear: + clear_mikrotik(host) + flash(f'Cleared host: {host.hostname}', 'success') + except Exception as e: + flash(f'Error clearing host {host.hostname}: {str(e)}', 'danger') + else: + flash("Wybrany host nie został znaleziony lub nie masz do niego dostępu", "danger") + else: + flash("Nie wybrano hosta", "danger") + else: + # Opcja "all": przetwórz wszystkie hosty + for h in hosts: try: - clear_linux(h, default_content) - flash(f'Cleared Linux host: {h.hostname}', 'success') + if h.type == 'linux' and linux_clear: + clear_linux(h, default_content) + flash(f'Cleared Linux host: {h.hostname}', 'success') + elif h.type == 'mikrotik' and mikrotik_clear: + clear_mikrotik(h) + flash(f'Cleared Mikrotik host: {h.hostname}', 'success') except Exception as e: - flash(f'Error clearing Linux host {h.hostname}: {str(e)}', 'danger') - elif h.type == 'mikrotik' and mikrotik_clear: - try: - clear_mikrotik(h) - flash(f'Cleared Mikrotik host: {h.hostname}', 'success') - except Exception as e: - flash(f'Error clearing Mikrotik host {h.hostname}: {str(e)}', 'danger') + flash(f'Error clearing host {h.hostname}: {str(e)}', 'danger') return redirect(url_for('clear_server')) - return render_template('clear_servers.html') + return render_template('clear_servers.html', hosts=hosts) # ------------------- # ZARZĄDZANIE PLIKAMI HOSTS (WIELOKROTNE PLIKI) @@ -1075,7 +1106,7 @@ def scheduled_deployments(): scheduler = BackgroundScheduler() scheduler.add_job(func=scheduled_deployments, trigger="interval", minutes=5) -scheduler.add_job(func=automated_backups, trigger="interval", minutes=60) +scheduler.add_job(func=automated_backups, trigger="interval", minutes=5) scheduler.add_job(func=cleanup_old_backups, trigger="interval", hours=24) scheduler.start() diff --git a/templates/backups.html b/templates/backups.html index 52ab2f5..73337a7 100644 --- a/templates/backups.html +++ b/templates/backups.html @@ -31,6 +31,7 @@ {{ backup.description }} Podgląd + Przywróć
diff --git a/templates/clear_servers.html b/templates/clear_servers.html index 7ddb6be..07ba0b8 100644 --- a/templates/clear_servers.html +++ b/templates/clear_servers.html @@ -12,21 +12,67 @@ {% block content %}
-

Wyczyść serwery

+

Wyczyść /etc/hosts

-
- - +
+ +
+ + +
+
+ + +
-
- - + - +
+
+ + +
+
+ + +
+
+
- + +{% endblock %} +{% block extra_js %} + {{ super() }} + {% endblock %}