new options
This commit is contained in:
parent
f58e2c5da0
commit
04c1e6d49a
BIN
__pycache__/app.cpython-313.pyc
Normal file
BIN
__pycache__/app.cpython-313.pyc
Normal file
Binary file not shown.
53
app.py
53
app.py
@ -31,7 +31,6 @@ login_manager = LoginManager(app)
|
|||||||
login_manager.login_view = 'login'
|
login_manager.login_view = 'login'
|
||||||
|
|
||||||
# MODELE BAZY DANYCH
|
# MODELE BAZY DANYCH
|
||||||
|
|
||||||
class User(UserMixin, db.Model):
|
class User(UserMixin, db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
username = db.Column(db.String(80), unique=True, nullable=False)
|
username = db.Column(db.String(80), unique=True, nullable=False)
|
||||||
@ -45,7 +44,6 @@ class User(UserMixin, db.Model):
|
|||||||
|
|
||||||
def check_password(self, password):
|
def check_password(self, password):
|
||||||
return check_password_hash(self.password_hash, password)
|
return check_password_hash(self.password_hash, password)
|
||||||
|
|
||||||
class Device(db.Model):
|
class Device(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
name = db.Column(db.String(120))
|
name = db.Column(db.String(120))
|
||||||
@ -62,7 +60,6 @@ class Device(db.Model):
|
|||||||
use_ssl = db.Column(db.Boolean, default=False) # Czy używać SSL?
|
use_ssl = db.Column(db.Boolean, default=False) # Czy używać SSL?
|
||||||
ssl_insecure = db.Column(db.Boolean, default=False) # Jeśli True – nie weryfikować certyfikatu SSL
|
ssl_insecure = db.Column(db.Boolean, default=False) # Jeśli True – nie weryfikować certyfikatu SSL
|
||||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
||||||
|
|
||||||
class Settings(db.Model):
|
class Settings(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
pushover_user_key = db.Column(db.String(255))
|
pushover_user_key = db.Column(db.String(255))
|
||||||
@ -73,16 +70,17 @@ class Settings(db.Model):
|
|||||||
smtp_username = db.Column(db.String(255))
|
smtp_username = db.Column(db.String(255))
|
||||||
smtp_password = db.Column(db.String(255))
|
smtp_password = db.Column(db.String(255))
|
||||||
email_notifications_enabled = db.Column(db.Boolean, default=False)
|
email_notifications_enabled = db.Column(db.Boolean, default=False)
|
||||||
check_interval = db.Column(db.Integer, default=60) # interwał sprawdzania w sekundach
|
check_interval = db.Column(db.Integer, default=60)
|
||||||
log_retention_days = db.Column(db.Integer, default=30) # nowe pole – retencja logów w dniach
|
log_retention_days = db.Column(db.Integer, default=30)
|
||||||
|
recipient_email = db.Column(db.String(120))
|
||||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False, unique=True)
|
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False, unique=True)
|
||||||
|
|
||||||
class Log(db.Model):
|
class Log(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
|
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
|
||||||
message = db.Column(db.Text)
|
message = db.Column(db.Text)
|
||||||
device_id = db.Column(db.Integer, db.ForeignKey('device.id'))
|
device_id = db.Column(db.Integer, db.ForeignKey('device.id'))
|
||||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
|
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
|
||||||
|
device = db.relationship('Device', backref='logs')
|
||||||
|
|
||||||
# Inicjalizacja bazy (utworzyć bazę przy pierwszym uruchomieniu)
|
# Inicjalizacja bazy (utworzyć bazę przy pierwszym uruchomieniu)
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
@ -110,21 +108,21 @@ def send_pushover_notification(user, message):
|
|||||||
print("Błąd przy wysyłaniu powiadomienia Pushover:", e)
|
print("Błąd przy wysyłaniu powiadomienia Pushover:", e)
|
||||||
|
|
||||||
def send_email_notification(user, subject, message):
|
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:
|
if not user.settings or not user.settings.email_notifications_enabled or not user.settings.smtp_server:
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
# Uzyskujemy sformatowaną treść e-maila
|
|
||||||
html_body = get_email_template(subject, message)
|
html_body = get_email_template(subject, message)
|
||||||
msg = MIMEText(html_body, 'html')
|
msg = MIMEText(html_body, 'html')
|
||||||
msg["Subject"] = subject
|
msg["Subject"] = subject
|
||||||
msg["From"] = user.settings.smtp_username
|
msg["From"] = user.settings.smtp_username
|
||||||
msg["To"] = user.email
|
# Używamy adresu z ustawień, jeśli został podany, lub domyślnie adresu z profilu użytkownika
|
||||||
|
to_email = user.settings.recipient_email if user.settings.recipient_email else user.email
|
||||||
|
msg["To"] = to_email
|
||||||
|
|
||||||
s = smtplib.SMTP(user.settings.smtp_server, user.settings.smtp_port)
|
s = smtplib.SMTP(user.settings.smtp_server, user.settings.smtp_port)
|
||||||
s.starttls()
|
s.starttls()
|
||||||
s.login(user.settings.smtp_username, user.settings.smtp_password)
|
s.login(user.settings.smtp_username, user.settings.smtp_password)
|
||||||
s.sendmail(user.settings.smtp_username, [user.email], msg.as_string())
|
s.sendmail(user.settings.smtp_username, [to_email], msg.as_string())
|
||||||
s.quit()
|
s.quit()
|
||||||
app.logger.debug("E-mail wysłany pomyślnie")
|
app.logger.debug("E-mail wysłany pomyślnie")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -271,7 +269,9 @@ def check_all_devices():
|
|||||||
device.current_firmware = current_firmware
|
device.current_firmware = current_firmware
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
# Zapis do tabeli logów
|
# Zapis do tabeli logów
|
||||||
log_entry = Log(message=result, device_id=device.id, user_id=device.user_id)
|
#log_entry = Log(message=result, device_id=device.id, user_id=device.user_id)
|
||||||
|
log_message = f"Urządzenie {device.name or device.ip} - {result}"
|
||||||
|
log_entry = Log(message=log_message, device_id=device.id, user_id=device.user_id)
|
||||||
db.session.add(log_entry)
|
db.session.add(log_entry)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
# Powiadomienia, jeśli dostępna aktualizacja
|
# Powiadomienia, jeśli dostępna aktualizacja
|
||||||
@ -487,27 +487,29 @@ def settings():
|
|||||||
user_settings.smtp_username = request.form.get('smtp_username')
|
user_settings.smtp_username = request.form.get('smtp_username')
|
||||||
user_settings.smtp_password = request.form.get('smtp_password')
|
user_settings.smtp_password = request.form.get('smtp_password')
|
||||||
user_settings.email_notifications_enabled = bool(request.form.get('email_notifications_enabled'))
|
user_settings.email_notifications_enabled = bool(request.form.get('email_notifications_enabled'))
|
||||||
|
# Aktualizacja adresu e-mail odbiorcy (może być inny niż email z profilu)
|
||||||
|
user_settings.recipient_email = request.form.get('recipient_email')
|
||||||
# Aktualizacja interwału sprawdzania
|
# Aktualizacja interwału sprawdzania
|
||||||
interval = request.form.get('check_interval')
|
interval = request.form.get('check_interval')
|
||||||
user_settings.check_interval = int(interval) if interval else 60
|
user_settings.check_interval = int(interval) if interval else 60
|
||||||
# Aktualizacja retencji logów
|
# Aktualizacja retencji logów
|
||||||
retention = request.form.get('log_retention_days')
|
retention = request.form.get('log_retention_days')
|
||||||
user_settings.log_retention_days = int(retention) if retention else 30
|
user_settings.log_retention_days = int(retention) if retention else 30
|
||||||
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
# Aktualizacja interwału zadania scheduler'a, jeśli masz takie zadanie
|
|
||||||
try:
|
try:
|
||||||
scheduler.reschedule_job("check_all_devices", trigger="interval", seconds=user_settings.check_interval)
|
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")
|
app.logger.debug(f"Scheduler rescheduled with new interval: {user_settings.check_interval} seconds")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
app.logger.error(f"Error rescheduling job: {e}")
|
app.logger.error(f"Error rescheduling job: {e}")
|
||||||
|
|
||||||
flash("Ustawienia zapisane.")
|
flash("Ustawienia zapisane.")
|
||||||
return redirect(url_for('settings'))
|
return redirect(url_for('settings'))
|
||||||
return render_template('settings.html', settings=user_settings)
|
return render_template('settings.html', settings=user_settings)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/device/<int:device_id>/edit', methods=['GET', 'POST'])
|
@app.route('/device/<int:device_id>/edit', methods=['GET', 'POST'])
|
||||||
@login_required
|
@login_required
|
||||||
def edit_device(device_id):
|
def edit_device(device_id):
|
||||||
@ -529,7 +531,6 @@ def edit_device(device_id):
|
|||||||
return redirect(url_for('devices'))
|
return redirect(url_for('devices'))
|
||||||
return render_template('edit_device.html', device=device)
|
return render_template('edit_device.html', device=device)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/device/<int:device_id>/force_check')
|
@app.route('/device/<int:device_id>/force_check')
|
||||||
@login_required
|
@login_required
|
||||||
def force_check(device_id):
|
def force_check(device_id):
|
||||||
@ -581,8 +582,6 @@ def update_device(device_id):
|
|||||||
flash(f"Błąd podczas aktualizacji: {e}")
|
flash(f"Błąd podczas aktualizacji: {e}")
|
||||||
return redirect(url_for('device_detail', device_id=device.id))
|
return redirect(url_for('device_detail', device_id=device.id))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/device/<int:device_id>/update_firmware', methods=['POST'])
|
@app.route('/device/<int:device_id>/update_firmware', methods=['POST'])
|
||||||
@login_required
|
@login_required
|
||||||
def update_firmware(device_id):
|
def update_firmware(device_id):
|
||||||
@ -641,6 +640,24 @@ def reset_password():
|
|||||||
return redirect(url_for('reset_password'))
|
return redirect(url_for('reset_password'))
|
||||||
return render_template('reset_password.html')
|
return render_template('reset_password.html')
|
||||||
|
|
||||||
|
@app.route('/logs/clean', methods=['POST'])
|
||||||
|
@login_required
|
||||||
|
def clean_logs():
|
||||||
|
days = request.form.get('days')
|
||||||
|
if not days:
|
||||||
|
flash("Podaj liczbę dni.")
|
||||||
|
return redirect(url_for('logs'))
|
||||||
|
try:
|
||||||
|
days = int(days)
|
||||||
|
except ValueError:
|
||||||
|
flash("Niepoprawna wartość dni.")
|
||||||
|
return redirect(url_for('logs'))
|
||||||
|
cutoff = datetime.utcnow() - timedelta(days=days)
|
||||||
|
num_deleted = Log.query.filter(Log.user_id == current_user.id, Log.timestamp < cutoff).delete()
|
||||||
|
db.session.commit()
|
||||||
|
flash(f"Usunięto {num_deleted} logów starszych niż {days} dni.")
|
||||||
|
return redirect(url_for('logs'))
|
||||||
|
|
||||||
# Zamknięcie harmonogramu przy zatrzymaniu aplikacji
|
# Zamknięcie harmonogramu przy zatrzymaniu aplikacji
|
||||||
|
|
||||||
atexit.register(lambda: scheduler.shutdown())
|
atexit.register(lambda: scheduler.shutdown())
|
||||||
|
BIN
instance/database.db
Normal file
BIN
instance/database.db
Normal file
Binary file not shown.
@ -4,5 +4,4 @@ Flask-Login
|
|||||||
APScheduler
|
APScheduler
|
||||||
librouteros
|
librouteros
|
||||||
requests
|
requests
|
||||||
gunicorn
|
waitress
|
||||||
requests
|
|
14
routeros_update.service
Normal file
14
routeros_update.service
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=RouterOS Update Waitress Service
|
||||||
|
After=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
#User=routeros
|
||||||
|
#Group=routeros
|
||||||
|
WorkingDirectory=/opt/routeros_update
|
||||||
|
ExecStart=/opt/routeros_update/venv/bin/python3 /opt/routeros_update/run_waitress.py
|
||||||
|
Restart=always
|
||||||
|
Environment=PYTHONUNBUFFERED=1
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
9
run_waitress.py
Normal file
9
run_waitress.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
from waitress import serve
|
||||||
|
from app import app, scheduler, clean_old_logs
|
||||||
|
import atexit
|
||||||
|
|
||||||
|
with app.app_context():
|
||||||
|
scheduler.add_job(func=clean_old_logs, trigger="interval", days=1)
|
||||||
|
atexit.register(lambda: scheduler.shutdown())
|
||||||
|
|
||||||
|
serve(app, host='0.0.0.0', port=5582, ident='', threads=4)
|
@ -2,6 +2,18 @@
|
|||||||
{% block title %}Logi - Aplikacja Updatera{% endblock %}
|
{% block title %}Logi - Aplikacja Updatera{% endblock %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h2>Logi</h2>
|
<h2>Logi</h2>
|
||||||
|
<!-- Formularz kasowania logów starszych niż podana liczba dni -->
|
||||||
|
<div class="mt-4">
|
||||||
|
<h4>Usuń logi starsze niż podana liczba dni</h4>
|
||||||
|
<form method="POST" action="{{ url_for('clean_logs') }}">
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="days" class="form-label">Liczba dni</label>
|
||||||
|
<input type="number" class="form-control" name="days" id="days" placeholder="Podaj liczbę dni">
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-danger">Usuń logi</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<hr>
|
||||||
<table class="table table-striped">
|
<table class="table table-striped">
|
||||||
<thead class="table-dark">
|
<thead class="table-dark">
|
||||||
<tr>
|
<tr>
|
||||||
@ -30,4 +42,5 @@
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
@ -46,12 +46,18 @@
|
|||||||
<input type="password" class="form-control" name="smtp_password" id="smtp_password" value="{{ settings.smtp_password or '' }}">
|
<input type="password" class="form-control" name="smtp_password" id="smtp_password" value="{{ settings.smtp_password or '' }}">
|
||||||
</div>
|
</div>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
<fieldset class="border p-3 mb-3">
|
||||||
|
<legend class="w-auto">E-mail odbiorcy powiadomień</legend>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="recipient_email" class="form-label">Adres e-mail do otrzymywania powiadomień</label>
|
||||||
|
<input type="email" class="form-control" name="recipient_email" id="recipient_email" value="{{ settings.recipient_email or '' }}">
|
||||||
|
</div>
|
||||||
|
</fieldset>
|
||||||
<fieldset class="border p-3 mb-3">
|
<fieldset class="border p-3 mb-3">
|
||||||
<legend class="w-auto">Interwał sprawdzania</legend>
|
<legend class="w-auto">Interwał sprawdzania</legend>
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="check_interval" class="form-label">Interwał (sekundy)</label>
|
<label for="check_interval" class="form-label">Interwał (sekundy) <code>21600 = 6 godzin</code></label>
|
||||||
<input type="number" class="form-control" name="check_interval" id="check_interval" value="{{ settings.check_interval or 60 }}">
|
<input type="number" class="form-control" name="check_interval" id="check_interval" value="{{ settings.check_interval or 21600 }}">
|
||||||
</div>
|
</div>
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="time_input" class="form-label">Czas (HH:MM:SS)</label>
|
<label for="time_input" class="form-label">Czas (HH:MM:SS)</label>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user