feat: add YAML settings management and admin settings page, add default_playlist_users

This commit is contained in:
Kamil
2024-12-13 08:27:55 +00:00
parent b7de39e501
commit 1b95f201be
5 changed files with 84 additions and 5 deletions

View File

@@ -1,6 +1,8 @@
from logging.handlers import RotatingFileHandler
import os
import threading
import time
import yaml
from flask_socketio import SocketIO
import sys
@@ -97,9 +99,20 @@ app = Flask(__name__, template_folder="../templates", static_folder='../static')
app.config.from_object(Config)
app.config['runtime_settings'] = {}
yaml_file = 'settings.yaml'
def load_yaml_settings():
with open(yaml_file, 'r') as f:
app.config['runtime_settings'] = yaml.safe_load(f)
def save_yaml_settings():
with open(yaml_file, 'w') as f:
yaml.dump(app.config['runtime_settings'], f)
for handler in app.logger.handlers:
app.logger.removeHandler(handler)
log_level = getattr(logging, app.config['LOG_LEVEL'], logging.INFO) # Default to DEBUG if invalid
app.logger.setLevel(log_level)
@@ -197,3 +210,28 @@ if app.config['LIDARR_API_KEY'] and app.config['LIDARR_URL']:
app.logger.info(f'Creating Lidarr Client with URL: {app.config["LIDARR_URL"]}')
from lidarr.client import LidarrClient
lidarr_client = LidarrClient(app.config['LIDARR_URL'], app.config['LIDARR_API_KEY'])
if os.path.exists(yaml_file):
app.logger.info('Loading runtime settings from settings.yaml')
load_yaml_settings()
# def watch_yaml_file(yaml_file, interval=30):
# last_mtime = os.path.getmtime(yaml_file)
# while True:
# time.sleep(interval)
# current_mtime = os.path.getmtime(yaml_file)
# if current_mtime != last_mtime:
# last_mtime = current_mtime
# yaml_settings = load_yaml_settings(yaml_file)
# app.config.update(yaml_settings)
# app.logger.info(f"Reloaded YAML settings from {yaml_file}")
# watcher_thread = threading.Thread(
# target=watch_yaml_file,
# args=('settings.yaml',),
# daemon=True
# )
# watcher_thread.start()

View File

@@ -3,7 +3,7 @@ import json
import os
import re
from flask import Flask, Response, jsonify, render_template, request, redirect, url_for, session, flash, Blueprint, g
from app import app, db, functions, jellyfin, read_dev_build_file, tasks
from app import app, db, functions, jellyfin, read_dev_build_file, tasks, save_yaml_settings
from app.classes import AudioProfile, CombinedPlaylistData
from app.models import JellyfinUser,Playlist,Track
from celery.result import AsyncResult
@@ -75,7 +75,6 @@ def task_manager():
lock_keys.append('full_update_jellyfin_ids_lock')
return render_template('admin/tasks.html', tasks=statuses,lock_keys = lock_keys)
@app.route('/admin')
@app.route('/admin/link_issues')
@functions.jellyfin_admin_required
def link_issues():
@@ -168,6 +167,23 @@ def get_logs_for_issue():
return jsonify({'logs': logs})
@app.route('/admin')
@app.route('/admin/settings')
@app.route('/admin/settings/save' , methods=['POST'])
@functions.jellyfin_admin_required
def admin_settings():
# if the request is a POST request, save the settings
if request.method == 'POST':
# from the form, get all values from default_playlist_users and join them to one array of strings
app.config['runtime_settings']['default_playlist_users'] = request.form.getlist('default_playlist_users')
save_yaml_settings()
flash('Settings saved', category='success')
return redirect('/admin/settings')
return render_template('admin/settings.html',jellyfin_users = jellyfin.get_users(session_token=functions._get_api_token()))
@app.route('/run_task/<task_name>', methods=['POST'])
@functions.jellyfin_admin_required
def run_task(task_name):