🐛 Update: Enhanced API for printer list retrieval with additional query parameters for filtering active and inactive printers. Improved response structure to include printer reachability and display status for better UI integration. Added granular permissions management in user creation process. 📚

This commit is contained in:
2025-06-12 21:14:08 +02:00
parent a212fcc8a3
commit bcb8f80415
129 changed files with 694 additions and 8 deletions

View File

@ -892,40 +892,69 @@ def api_finish_job(job_id):
@app.route("/api/printers", methods=["GET"]) @app.route("/api/printers", methods=["GET"])
@login_required @login_required
def api_get_printers(): def api_get_printers():
"""API-Endpunkt für Drucker-Liste mit konsistenter Response-Struktur""" """API-Endpunkt für Drucker-Liste mit konsistenter Response-Struktur
Query-Parameter:
- include_inactive: 'true' um auch inaktive Drucker anzuzeigen (default: 'true')
- show_all: 'true' um ALLE Drucker anzuzeigen, unabhängig vom Status
"""
try: try:
from models import get_db_session, Printer from models import get_db_session, Printer
# Query-Parameter auslesen
include_inactive = request.args.get('include_inactive', 'true').lower() == 'true'
show_all = request.args.get('show_all', 'true').lower() == 'true'
db_session = get_db_session() db_session = get_db_session()
# Alle Drucker für API-Abfragen anzeigen (unabhängig von active-Status)
printers = db_session.query(Printer).all() # Basis-Query - standardmäßig ALLE Drucker zeigen für Dropdown-Auswahl
query = db_session.query(Printer)
# Optional: Nur aktive Drucker filtern (wenn explizit angefordert)
if not include_inactive and not show_all:
query = query.filter(Printer.active == True)
printers = query.all()
printer_list = [] printer_list = []
for printer in printers: for printer in printers:
# Status-Bestimmung: Wenn nicht erreichbar, dann "offline"
status = printer.status or "offline"
# Zusätzliche Status-Informationen
is_reachable = status not in ["offline", "unreachable", "error"]
printer_dict = { printer_dict = {
"id": printer.id, "id": printer.id,
"name": printer.name, "name": printer.name,
"model": printer.model or "Unbekanntes Modell", "model": printer.model or "Unbekanntes Modell",
"location": printer.location or "Unbekannter Standort", "location": printer.location or "Unbekannter Standort",
"status": printer.status or "offline", "status": status,
"ip_address": printer.ip_address, "ip_address": printer.ip_address,
"plug_ip": printer.plug_ip, "plug_ip": printer.plug_ip,
"active": getattr(printer, 'active', True), "active": getattr(printer, 'active', True),
"is_reachable": is_reachable, # Zusätzliches Feld für UI
"is_selectable": True, # WICHTIG: Alle Drucker sind auswählbar!
"created_at": printer.created_at.isoformat() if printer.created_at else datetime.now().isoformat(), "created_at": printer.created_at.isoformat() if printer.created_at else datetime.now().isoformat(),
"last_checked": printer.last_checked.isoformat() if printer.last_checked else None "last_checked": printer.last_checked.isoformat() if printer.last_checked else None,
"display_status": f"{printer.name} - {status.title()}" # Für Dropdown-Anzeige
} }
printer_list.append(printer_dict) printer_list.append(printer_dict)
db_session.close() db_session.close()
app_logger.info(f"✅ API: {len(printer_list)} Drucker abgerufen") app_logger.info(f"✅ API: {len(printer_list)} Drucker abgerufen (include_inactive={include_inactive})")
# Konsistente Response-Struktur wie erwartet # Konsistente Response-Struktur wie erwartet
return jsonify({ return jsonify({
"success": True, "success": True,
"printers": printer_list, "printers": printer_list,
"count": len(printer_list), "count": len(printer_list),
"message": "Drucker erfolgreich geladen" "message": "Drucker erfolgreich geladen",
"filters": {
"include_inactive": include_inactive,
"show_all": show_all
}
}) })
except Exception as e: except Exception as e:

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -423,6 +423,24 @@ def create_user_api():
new_user.set_password(data['password']) new_user.set_password(data['password'])
db_session.add(new_user) db_session.add(new_user)
db_session.flush() # ID generieren für UserPermission
# Granulare Berechtigungen erstellen
from models import UserPermission
permissions = UserPermission(
user_id=new_user.id,
can_start_jobs=data.get('can_start_jobs', True), # Standard: kann Jobs starten
needs_approval=data.get('needs_approval', False), # Standard: keine Genehmigung nötig
can_approve_jobs=data.get('can_approve_jobs', False) # Standard: kann nicht genehmigen
)
# Administratoren bekommen automatisch Genehmigungsrechte
if new_user.role == 'admin':
permissions.can_approve_jobs = True
permissions.can_start_jobs = True
permissions.needs_approval = False
db_session.add(permissions)
db_session.commit() db_session.commit()
admin_logger.info(f"Neuer Benutzer erstellt: {new_user.username} von Admin {current_user.username}") admin_logger.info(f"Neuer Benutzer erstellt: {new_user.username} von Admin {current_user.username}")

Some files were not shown because too many files have changed in this diff Show More