📝 🎉 Improved session management system with new backup database and enhanced logs 🌐
This commit is contained in:
166
backend/app.py
166
backend/app.py
@@ -20,6 +20,7 @@ from flask_wtf.csrf import CSRFError
|
||||
from sqlalchemy import event
|
||||
from contextlib import contextmanager
|
||||
import threading
|
||||
import uuid
|
||||
|
||||
# ===== MINIMALE SESSION-DATENKLASSE =====
|
||||
class MinimalSessionInterface:
|
||||
@@ -1563,12 +1564,25 @@ def api_admin_create_printer():
|
||||
|
||||
db_session = get_db_session()
|
||||
|
||||
# Prüfen ob Name bereits existiert
|
||||
existing_printer = db_session.query(Printer).filter(Printer.name == data["name"]).first()
|
||||
if existing_printer:
|
||||
db_session.close()
|
||||
return jsonify({"error": "Ein Drucker mit diesem Namen existiert bereits"}), 409
|
||||
|
||||
# MAC-Adresse generieren falls nicht vorhanden
|
||||
mac_address = data.get("mac_address")
|
||||
if not mac_address:
|
||||
# Generiere eine eindeutige MAC-Adresse
|
||||
mac_address = "00:50:56:" + ":".join([f"{uuid.uuid4().hex[:2]}" for _ in range(3)])
|
||||
|
||||
# Neuen Drucker erstellen
|
||||
new_printer = Printer(
|
||||
name=data["name"],
|
||||
model=data["model"],
|
||||
location=data.get("location", ""),
|
||||
ip_address=data.get("ip_address"),
|
||||
mac_address=mac_address,
|
||||
plug_ip=data.get("plug_ip"),
|
||||
plug_username=data.get("plug_username"),
|
||||
plug_password=data.get("plug_password"),
|
||||
@@ -1584,18 +1598,22 @@ def api_admin_create_printer():
|
||||
"name": new_printer.name,
|
||||
"model": new_printer.model,
|
||||
"location": new_printer.location,
|
||||
"ip_address": new_printer.ip_address,
|
||||
"mac_address": new_printer.mac_address,
|
||||
"plug_ip": new_printer.plug_ip,
|
||||
"status": new_printer.status,
|
||||
"active": new_printer.active
|
||||
"active": new_printer.active,
|
||||
"created_at": new_printer.created_at.isoformat() if new_printer.created_at else None
|
||||
}
|
||||
|
||||
db_session.close()
|
||||
|
||||
app_logger.info(f"✅ Admin API: Neuer Drucker '{new_printer.name}' erstellt")
|
||||
return jsonify({"printer": printer_dict}), 201
|
||||
return jsonify({"success": True, "printer": printer_dict, "message": "Drucker erfolgreich erstellt"}), 201
|
||||
|
||||
except Exception as e:
|
||||
app_logger.error(f"❌ Admin API-Fehler beim Erstellen des Druckers: {str(e)}")
|
||||
return jsonify({"error": "Fehler beim Erstellen des Druckers", "details": str(e)}), 500
|
||||
return jsonify({"success": False, "error": "Fehler beim Erstellen des Druckers", "details": str(e)}), 500
|
||||
|
||||
@app.route("/api/admin/printers/<int:printer_id>", methods=["PUT"])
|
||||
@login_required
|
||||
@@ -1616,6 +1634,16 @@ def api_admin_update_printer(printer_id):
|
||||
db_session.close()
|
||||
return jsonify({"error": "Drucker nicht gefunden"}), 404
|
||||
|
||||
# Prüfen ob neuer Name bereits existiert (falls Name geändert wird)
|
||||
if "name" in data and data["name"] != printer.name:
|
||||
existing_printer = db_session.query(Printer).filter(
|
||||
Printer.name == data["name"],
|
||||
Printer.id != printer_id
|
||||
).first()
|
||||
if existing_printer:
|
||||
db_session.close()
|
||||
return jsonify({"error": "Ein Drucker mit diesem Namen existiert bereits"}), 409
|
||||
|
||||
# Felder aktualisieren
|
||||
if "name" in data:
|
||||
printer.name = data["name"]
|
||||
@@ -1625,6 +1653,8 @@ def api_admin_update_printer(printer_id):
|
||||
printer.location = data["location"]
|
||||
if "ip_address" in data:
|
||||
printer.ip_address = data["ip_address"]
|
||||
if "mac_address" in data:
|
||||
printer.mac_address = data["mac_address"]
|
||||
if "plug_ip" in data:
|
||||
printer.plug_ip = data["plug_ip"]
|
||||
if "plug_username" in data:
|
||||
@@ -1645,24 +1675,70 @@ def api_admin_update_printer(printer_id):
|
||||
"name": printer.name,
|
||||
"model": printer.model,
|
||||
"location": printer.location,
|
||||
"ip_address": printer.ip_address,
|
||||
"mac_address": printer.mac_address,
|
||||
"plug_ip": printer.plug_ip,
|
||||
"status": printer.status,
|
||||
"active": printer.active
|
||||
"active": printer.active,
|
||||
"created_at": printer.created_at.isoformat() if printer.created_at else None,
|
||||
"updated_at": printer.updated_at.isoformat() if printer.updated_at else None,
|
||||
"last_checked": printer.last_checked.isoformat() if printer.last_checked else None
|
||||
}
|
||||
|
||||
db_session.close()
|
||||
|
||||
app_logger.info(f"✅ Admin API: Drucker {printer_id} aktualisiert")
|
||||
return jsonify({"printer": printer_dict})
|
||||
return jsonify({"success": True, "printer": printer_dict, "message": "Drucker erfolgreich aktualisiert"})
|
||||
|
||||
except Exception as e:
|
||||
app_logger.error(f"❌ Admin API-Fehler beim Aktualisieren des Druckers {printer_id}: {str(e)}")
|
||||
return jsonify({"error": "Fehler beim Aktualisieren des Druckers", "details": str(e)}), 500
|
||||
return jsonify({"success": False, "error": "Fehler beim Aktualisieren des Druckers", "details": str(e)}), 500
|
||||
|
||||
@app.route("/api/admin/printers/<int:printer_id>", methods=["DELETE"])
|
||||
@login_required
|
||||
@admin_required
|
||||
def api_admin_delete_printer(printer_id):
|
||||
"""API-Endpunkt für Drucker-Löschung (Admin only)"""
|
||||
try:
|
||||
from models import get_db_session, Printer, Job
|
||||
|
||||
db_session = get_db_session()
|
||||
printer = db_session.query(Printer).filter(Printer.id == printer_id).first()
|
||||
|
||||
if not printer:
|
||||
db_session.close()
|
||||
return jsonify({"error": "Drucker nicht gefunden"}), 404
|
||||
|
||||
# Prüfen ob der Drucker aktive Jobs hat
|
||||
active_jobs = db_session.query(Job).filter(
|
||||
Job.printer_id == printer_id,
|
||||
Job.status.in_(["scheduled", "running"])
|
||||
).count()
|
||||
|
||||
if active_jobs > 0:
|
||||
db_session.close()
|
||||
return jsonify({
|
||||
"error": f"Drucker hat {active_jobs} aktive Jobs und kann nicht gelöscht werden",
|
||||
"active_jobs": active_jobs
|
||||
}), 409
|
||||
|
||||
printer_name = printer.name
|
||||
db_session.delete(printer)
|
||||
db_session.commit()
|
||||
db_session.close()
|
||||
|
||||
app_logger.info(f"✅ Admin API: Drucker '{printer_name}' (ID: {printer_id}) gelöscht")
|
||||
return jsonify({"success": True, "message": f"Drucker '{printer_name}' erfolgreich gelöscht"})
|
||||
|
||||
except Exception as e:
|
||||
app_logger.error(f"❌ Admin API-Fehler beim Löschen des Druckers {printer_id}: {str(e)}")
|
||||
return jsonify({"success": False, "error": "Fehler beim Löschen des Druckers", "details": str(e)}), 500
|
||||
|
||||
@app.route("/api/admin/printers/<int:printer_id>", methods=["GET"])
|
||||
@login_required
|
||||
@admin_required
|
||||
def api_admin_get_printer(printer_id):
|
||||
"""API-Endpunkt für einzelnen Drucker (Admin only)"""
|
||||
try:
|
||||
from models import get_db_session, Printer
|
||||
|
||||
@@ -1673,17 +1749,81 @@ def api_admin_delete_printer(printer_id):
|
||||
db_session.close()
|
||||
return jsonify({"error": "Drucker nicht gefunden"}), 404
|
||||
|
||||
printer_name = printer.name
|
||||
db_session.delete(printer)
|
||||
db_session.commit()
|
||||
printer_dict = {
|
||||
"id": printer.id,
|
||||
"name": printer.name,
|
||||
"model": printer.model,
|
||||
"location": printer.location,
|
||||
"ip_address": printer.ip_address,
|
||||
"mac_address": printer.mac_address,
|
||||
"plug_ip": printer.plug_ip,
|
||||
"plug_username": printer.plug_username, # Für Admin sichtbar
|
||||
"status": printer.status,
|
||||
"active": printer.active,
|
||||
"created_at": printer.created_at.isoformat() if printer.created_at else None,
|
||||
"updated_at": printer.updated_at.isoformat() if hasattr(printer, 'updated_at') and printer.updated_at else None,
|
||||
"last_checked": printer.last_checked.isoformat() if printer.last_checked else None,
|
||||
"has_plug": bool(printer.plug_ip), # Hilfreich für Frontend
|
||||
"job_count": len(printer.jobs) if printer.jobs else 0
|
||||
}
|
||||
|
||||
db_session.close()
|
||||
|
||||
app_logger.info(f"✅ Admin API: Drucker '{printer_name}' (ID: {printer_id}) gelöscht")
|
||||
return jsonify({"success": True, "message": "Drucker erfolgreich gelöscht"})
|
||||
app_logger.info(f"✅ Admin API: Drucker {printer_id} abgerufen")
|
||||
return jsonify({"success": True, "printer": printer_dict})
|
||||
|
||||
except Exception as e:
|
||||
app_logger.error(f"❌ Admin API-Fehler beim Löschen des Druckers {printer_id}: {str(e)}")
|
||||
return jsonify({"error": "Fehler beim Löschen des Druckers", "details": str(e)}), 500
|
||||
app_logger.error(f"❌ Admin API-Fehler beim Abrufen des Druckers {printer_id}: {str(e)}")
|
||||
return jsonify({"success": False, "error": "Fehler beim Laden des Druckers", "details": str(e)}), 500
|
||||
|
||||
@app.route("/api/admin/printers", methods=["GET"])
|
||||
@login_required
|
||||
@admin_required
|
||||
def api_admin_get_printers():
|
||||
"""API-Endpunkt für alle Drucker (Admin only)"""
|
||||
try:
|
||||
from models import get_db_session, Printer
|
||||
|
||||
db_session = get_db_session()
|
||||
printers = db_session.query(Printer).all()
|
||||
|
||||
printer_list = []
|
||||
for printer in printers:
|
||||
printer_dict = {
|
||||
"id": printer.id,
|
||||
"name": printer.name,
|
||||
"model": printer.model,
|
||||
"location": printer.location,
|
||||
"ip_address": printer.ip_address,
|
||||
"mac_address": printer.mac_address,
|
||||
"plug_ip": printer.plug_ip,
|
||||
"status": printer.status,
|
||||
"active": printer.active,
|
||||
"created_at": printer.created_at.isoformat() if printer.created_at else None,
|
||||
"last_checked": printer.last_checked.isoformat() if printer.last_checked else None,
|
||||
"has_plug": bool(printer.plug_ip),
|
||||
"job_count": len(printer.jobs) if printer.jobs else 0
|
||||
}
|
||||
printer_list.append(printer_dict)
|
||||
|
||||
db_session.close()
|
||||
|
||||
app_logger.info(f"✅ Admin API: {len(printer_list)} Drucker abgerufen")
|
||||
return jsonify({
|
||||
"success": True,
|
||||
"printers": printer_list,
|
||||
"count": len(printer_list)
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
app_logger.error(f"❌ Admin API-Fehler beim Abrufen der Drucker: {str(e)}")
|
||||
return jsonify({
|
||||
"success": False,
|
||||
"error": "Fehler beim Laden der Drucker",
|
||||
"details": str(e),
|
||||
"printers": [],
|
||||
"count": 0
|
||||
}), 500
|
||||
|
||||
@app.route("/api/health", methods=["GET"])
|
||||
def api_health_check():
|
||||
|
Reference in New Issue
Block a user