From f58e2c5da01067358e6045cd7401d90fbc0fa741 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Gruszczy=C5=84ski?= Date: Sun, 23 Feb 2025 22:44:24 +0100 Subject: [PATCH] poprawki i funkcje --- app.py | 124 +++++++++++++++++++++++++++++++--- templates/base.html | 34 ++++++---- templates/dashboard.html | 42 ++++++++++++ templates/index.html | 21 +++--- templates/reset_password.html | 24 +++++++ templates/settings.html | 17 ++++- 6 files changed, 226 insertions(+), 36 deletions(-) create mode 100644 templates/dashboard.html create mode 100644 templates/reset_password.html diff --git a/app.py b/app.py index 6a6b160..46f6810 100644 --- a/app.py +++ b/app.py @@ -12,8 +12,10 @@ import threading import time import requests import smtplib -import atexit from email.mime.text import MIMEText +from flask import current_app as app +from flask import render_template +import atexit from datetime import timedelta # Konfiguracja aplikacji @@ -108,10 +110,13 @@ def send_pushover_notification(user, message): print("Błąd przy wysyłaniu powiadomienia Pushover:", e) def send_email_notification(user, subject, message): + # Sprawdzamy, czy ustawienia e-mail są poprawnie skonfigurowane if not user.settings or not user.settings.email_notifications_enabled or not user.settings.smtp_server: return try: - msg = MIMEText(message) + # Uzyskujemy sformatowaną treść e-maila + html_body = get_email_template(subject, message) + msg = MIMEText(html_body, 'html') msg["Subject"] = subject msg["From"] = user.settings.smtp_username msg["To"] = user.email @@ -121,8 +126,9 @@ def send_email_notification(user, subject, message): s.login(user.settings.smtp_username, user.settings.smtp_password) s.sendmail(user.settings.smtp_username, [user.email], msg.as_string()) s.quit() + app.logger.debug("E-mail wysłany pomyślnie") except Exception as e: - print("Błąd przy wysyłaniu powiadomienia e-mail:", e) + app.logger.error(f"Błąd przy wysyłaniu powiadomienia e-mail: {e}", exc_info=True) # FUNKCJA SPRAWDZAJĄCA AKTUALIZACJE URZĄDZENIA def check_device_update(device): @@ -197,7 +203,61 @@ def check_device_update(device): app.logger.error(f"Error connecting to device {device.ip}: {e}", exc_info=True) return f"Error: {str(e)}", False, None, None - +def get_email_template(subject, message): + return f""" + + + + + +
+
+

{subject}

+
+
+

{message}

+
+ +
+ + + """ def check_all_devices(): with app.app_context(): @@ -272,8 +332,23 @@ scheduler.start() @app.route('/') def index(): + if current_user.is_authenticated: + return redirect(url_for('dashboard')) return render_template('index.html') +@app.route('/dashboard') +@login_required +def dashboard(): + devices_count = Device.query.count() + pending_updates_count = Device.query.filter_by(update_required=True).count() + logs_count = Log.query.count() + users_count = User.query.count() + return render_template('dashboard.html', + devices_count=devices_count, + pending_updates_count=pending_updates_count, + logs_count=logs_count, + users_count=users_count) + # Rejestracja @app.route('/register', methods=['GET', 'POST']) def register(): @@ -307,7 +382,7 @@ def login(): if user and user.check_password(password): login_user(user) flash("Zalogowano pomyślnie.") - return redirect(url_for('devices')) + return redirect(url_for('dashboard')) else: flash("Nieprawidłowa nazwa użytkownika lub hasło.") return render_template('login.html') @@ -421,7 +496,7 @@ def settings(): db.session.commit() - # Aktualizacja interwału zadania scheduler'a + # Aktualizacja interwału zadania scheduler'a, jeśli masz takie zadanie try: scheduler.reschedule_job("check_all_devices", trigger="interval", seconds=user_settings.check_interval) app.logger.debug(f"Scheduler rescheduled with new interval: {user_settings.check_interval} seconds") @@ -433,7 +508,6 @@ def settings(): return render_template('settings.html', settings=user_settings) - @app.route('/device//edit', methods=['GET', 'POST']) @login_required def edit_device(device_id): @@ -531,7 +605,41 @@ def update_firmware(device_id): flash(f"Błąd podczas aktualizacji firmware: {e}") return redirect(url_for('device_detail', device_id=device.id)) +@app.route('/test_pushover', methods=['POST']) +@login_required +def test_pushover(): + message = "To jest testowe powiadomienie Pushover z RouterOS Update." + send_pushover_notification(current_user, message) + flash("Test powiadomienia Pushover wysłany.") + return redirect(url_for('settings')) +@app.route('/test_email', methods=['POST']) +@login_required +def test_email(): + subject = "Testowy E-mail z RouterOS Update" + message = "To jest testowa wiadomość e-mail wysłana z RouterOS Update." + send_email_notification(current_user, subject, message) + flash("Testowy e-mail wysłany.") + return redirect(url_for('settings')) + +@app.route('/reset_password', methods=['GET', 'POST']) +@login_required +def reset_password(): + if request.method == 'POST': + old_password = request.form.get('old_password') + new_password = request.form.get('new_password') + confirm_password = request.form.get('confirm_password') + if not current_user.check_password(old_password): + flash("Stare hasło jest nieprawidłowe.") + return redirect(url_for('reset_password')) + if new_password != confirm_password: + flash("Nowe hasło i potwierdzenie nie są zgodne.") + return redirect(url_for('reset_password')) + current_user.set_password(new_password) + db.session.commit() + flash("Hasło zostało zresetowane.") + return redirect(url_for('reset_password')) + return render_template('reset_password.html') # Zamknięcie harmonogramu przy zatrzymaniu aplikacji @@ -539,4 +647,4 @@ atexit.register(lambda: scheduler.shutdown()) if __name__ == '__main__': scheduler.add_job(func=clean_old_logs, trigger="interval", days=1) - app.run(host='0.0.0.0', port=5581, use_reloader=False, debug=True) \ No newline at end of file + app.run(host='0.0.0.0', port=5582, use_reloader=False, debug=True) \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index 79bb233..c4d4a1e 100644 --- a/templates/base.html +++ b/templates/base.html @@ -10,27 +10,31 @@