🎉 Improved cursor rules and expanded logging system across backend files. 🖥️📝
This commit is contained in:
139
backend/blueprints/api.py
Normal file
139
backend/blueprints/api.py
Normal file
@@ -0,0 +1,139 @@
|
||||
"""
|
||||
API-Blueprint für das 3D-Druck-Management-System
|
||||
|
||||
Dieses Modul enthält allgemeine API-Endpunkte und WebSocket-Fallback-Funktionalität.
|
||||
"""
|
||||
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from flask import Blueprint, jsonify, request, session
|
||||
from flask_login import login_required, current_user
|
||||
from models import get_db_session, User, Notification
|
||||
from utils.logging_config import get_logger
|
||||
|
||||
# Blueprint erstellen
|
||||
api_blueprint = Blueprint('api', __name__, url_prefix='/api')
|
||||
|
||||
# Logger initialisieren
|
||||
api_logger = get_logger("api")
|
||||
|
||||
@api_blueprint.route('/ws-fallback', methods=['GET'])
|
||||
@login_required
|
||||
def ws_fallback():
|
||||
"""WebSocket-Fallback für Browser ohne WebSocket-Unterstützung"""
|
||||
try:
|
||||
# Einfache Polling-Antwort für Clients ohne WebSocket
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'timestamp': datetime.now().isoformat(),
|
||||
'user_id': current_user.id,
|
||||
'message': 'WebSocket-Fallback aktiv'
|
||||
})
|
||||
except Exception as e:
|
||||
api_logger.error(f"Fehler im WebSocket-Fallback: {str(e)}")
|
||||
return jsonify({'error': 'WebSocket-Fallback-Fehler'}), 500
|
||||
|
||||
@api_blueprint.route('/notifications', methods=['GET'])
|
||||
@login_required
|
||||
def get_notifications():
|
||||
"""Abrufen der Benutzer-Benachrichtigungen"""
|
||||
try:
|
||||
db_session = get_db_session()
|
||||
|
||||
# Benutzer-spezifische Benachrichtigungen
|
||||
notifications = db_session.query(Notification).filter(
|
||||
Notification.user_id == current_user.id,
|
||||
Notification.is_read == False
|
||||
).order_by(Notification.created_at.desc()).limit(20).all()
|
||||
|
||||
notification_list = []
|
||||
for notification in notifications:
|
||||
notification_list.append({
|
||||
'id': notification.id,
|
||||
'title': notification.title,
|
||||
'message': notification.message,
|
||||
'type': notification.type,
|
||||
'created_at': notification.created_at.isoformat(),
|
||||
'is_read': notification.is_read
|
||||
})
|
||||
|
||||
db_session.close()
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'notifications': notification_list,
|
||||
'count': len(notification_list)
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
api_logger.error(f"Fehler beim Abrufen der Benachrichtigungen: {str(e)}")
|
||||
return jsonify({'error': 'Fehler beim Laden der Benachrichtigungen'}), 500
|
||||
|
||||
@api_blueprint.route('/notifications/<int:notification_id>/read', methods=['POST'])
|
||||
@login_required
|
||||
def mark_notification_read(notification_id):
|
||||
"""Markiert eine Benachrichtigung als gelesen"""
|
||||
try:
|
||||
db_session = get_db_session()
|
||||
|
||||
notification = db_session.query(Notification).filter(
|
||||
Notification.id == notification_id,
|
||||
Notification.user_id == current_user.id
|
||||
).first()
|
||||
|
||||
if not notification:
|
||||
db_session.close()
|
||||
return jsonify({'error': 'Benachrichtigung nicht gefunden'}), 404
|
||||
|
||||
notification.is_read = True
|
||||
notification.read_at = datetime.now()
|
||||
db_session.commit()
|
||||
db_session.close()
|
||||
|
||||
api_logger.info(f"Benachrichtigung {notification_id} als gelesen markiert")
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'message': 'Benachrichtigung als gelesen markiert'
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
api_logger.error(f"Fehler beim Markieren der Benachrichtigung: {str(e)}")
|
||||
return jsonify({'error': 'Fehler beim Markieren der Benachrichtigung'}), 500
|
||||
|
||||
@api_blueprint.route('/system/status', methods=['GET'])
|
||||
@login_required
|
||||
def system_status():
|
||||
"""Gibt den System-Status zurück"""
|
||||
try:
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'status': 'online',
|
||||
'timestamp': datetime.now().isoformat(),
|
||||
'user': {
|
||||
'id': current_user.id,
|
||||
'username': current_user.username,
|
||||
'is_admin': current_user.is_admin
|
||||
}
|
||||
})
|
||||
except Exception as e:
|
||||
api_logger.error(f"Fehler beim Abrufen des System-Status: {str(e)}")
|
||||
return jsonify({'error': 'System-Status nicht verfügbar'}), 500
|
||||
|
||||
@api_blueprint.route('/heartbeat', methods=['POST'])
|
||||
@login_required
|
||||
def heartbeat():
|
||||
"""Heartbeat-Endpunkt für Frontend-Verbindungsmonitoring"""
|
||||
try:
|
||||
# Session-Aktivität aktualisieren
|
||||
session['last_heartbeat'] = datetime.now().strftime('%H:%M:%S')
|
||||
session.permanent = True
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'timestamp': datetime.now().isoformat(),
|
||||
'user_id': current_user.id
|
||||
})
|
||||
except Exception as e:
|
||||
api_logger.error(f"Fehler im Heartbeat: {str(e)}")
|
||||
return jsonify({'error': 'Heartbeat-Fehler'}), 500
|
Reference in New Issue
Block a user