refactor(decorators): simplify require_admin decorator by reusing require_role

This commit is contained in:
JSC
2025-07-04 19:13:33 +02:00
parent 5c29fa1a4c
commit 4375718c2f
3 changed files with 107 additions and 92 deletions

View File

@@ -1,10 +1,15 @@
"""Soundboard routes."""
from flask import Blueprint, jsonify, request
from app.models.sound import Sound, SoundType
from app.models.sound_played import SoundPlayed
from app.services.decorators import (
get_current_user,
require_auth,
require_credits,
)
from app.services.vlc_service import vlc_service
from app.services.decorators import require_auth, get_current_user
bp = Blueprint("soundboard", __name__, url_prefix="/api/soundboard")
@@ -40,17 +45,18 @@ def get_sounds():
@bp.route("/sounds/<int:sound_id>/play", methods=["POST"])
@require_auth
@require_credits(1)
def play_sound(sound_id: int):
"""Play a specific sound."""
try:
# Get current user for tracking
user = get_current_user()
user_id = int(user["id"]) if user else None
# Get client information
ip_address = request.remote_addr
user_agent = request.headers.get("User-Agent")
success = vlc_service.play_sound(
sound_id=sound_id,
user_id=user_id,
@@ -61,9 +67,10 @@ def play_sound(sound_id: int):
if success:
return jsonify({"message": "Sound playing", "sound_id": sound_id})
else:
return jsonify(
{"error": "Sound not found or cannot be played"}
), 404
return (
jsonify({"error": "Sound not found or cannot be played"}),
404,
)
except Exception as e:
return jsonify({"error": str(e)}), 500
@@ -75,19 +82,22 @@ def stop_all_sounds():
try:
# Try normal stop first
vlc_service.stop_all()
# Wait a moment and check if any are still playing
import time
time.sleep(0.2)
# If there are still instances, force stop them
if vlc_service.get_playing_count() > 0:
stopped_count = vlc_service.force_stop_all()
return jsonify({
"message": f"Force stopped {stopped_count} sounds",
"forced": True
})
return jsonify(
{
"message": f"Force stopped {stopped_count} sounds",
"forced": True,
}
)
return jsonify({"message": "All sounds stopped"})
except Exception as e:
return jsonify({"error": str(e)}), 500
@@ -99,10 +109,12 @@ def force_stop_all_sounds():
"""Force stop all sounds with aggressive cleanup."""
try:
stopped_count = vlc_service.force_stop_all()
return jsonify({
"message": f"Force stopped {stopped_count} sound instances",
"stopped_count": stopped_count
})
return jsonify(
{
"message": f"Force stopped {stopped_count} sound instances",
"stopped_count": stopped_count,
}
)
except Exception as e:
return jsonify({"error": str(e)}), 500
@@ -113,17 +125,19 @@ def get_status():
"""Get current playback status."""
try:
playing_count = vlc_service.get_playing_count()
# Get detailed process information
with vlc_service.lock:
processes = []
for process_id, process in vlc_service.processes.items():
processes.append({
"id": process_id,
"pid": process.pid,
"running": process.poll() is None,
})
processes.append(
{
"id": process_id,
"pid": process.pid,
"running": process.poll() is None,
}
)
return jsonify(
{
"playing_count": playing_count,
@@ -143,14 +157,18 @@ def get_play_history():
page = int(request.args.get("page", 1))
per_page = min(int(request.args.get("per_page", 50)), 100)
offset = (page - 1) * per_page
recent_plays = SoundPlayed.get_recent_plays(limit=per_page, offset=offset)
return jsonify({
"plays": [play.to_dict() for play in recent_plays],
"page": page,
"per_page": per_page,
})
recent_plays = SoundPlayed.get_recent_plays(
limit=per_page, offset=offset
)
return jsonify(
{
"plays": [play.to_dict() for play in recent_plays],
"page": page,
"per_page": per_page,
}
)
except Exception as e:
return jsonify({"error": str(e)}), 500
@@ -163,20 +181,24 @@ def get_my_play_history():
user = get_current_user()
if not user:
return jsonify({"error": "User not found"}), 404
user_id = int(user["id"])
page = int(request.args.get("page", 1))
per_page = min(int(request.args.get("per_page", 50)), 100)
offset = (page - 1) * per_page
user_plays = SoundPlayed.get_user_plays(user_id=user_id, limit=per_page, offset=offset)
return jsonify({
"plays": [play.to_dict() for play in user_plays],
"page": page,
"per_page": per_page,
"user_id": user_id,
})
user_plays = SoundPlayed.get_user_plays(
user_id=user_id, limit=per_page, offset=offset
)
return jsonify(
{
"plays": [play.to_dict() for play in user_plays],
"page": page,
"per_page": per_page,
"user_id": user_id,
}
)
except Exception as e:
return jsonify({"error": str(e)}), 500
@@ -189,10 +211,10 @@ def get_my_stats():
user = get_current_user()
if not user:
return jsonify({"error": "User not found"}), 404
user_id = int(user["id"])
stats = SoundPlayed.get_user_stats(user_id)
return jsonify(stats)
except Exception as e:
return jsonify({"error": str(e)}), 500
@@ -206,13 +228,15 @@ def get_popular_sounds():
limit = min(int(request.args.get("limit", 10)), 50)
days = request.args.get("days")
days = int(days) if days and days.isdigit() else None
popular_sounds = SoundPlayed.get_popular_sounds(limit=limit, days=days)
return jsonify({
"popular_sounds": popular_sounds,
"limit": limit,
"days": days,
})
return jsonify(
{
"popular_sounds": popular_sounds,
"limit": limit,
"days": days,
}
)
except Exception as e:
return jsonify({"error": str(e)}), 500