Files
Nexusphp-Panel/routes/settings.py
DengDai ad2c65affb init
2025-12-08 14:31:21 +08:00

162 lines
5.5 KiB
Python

from flask import Blueprint, render_template, session, redirect, url_for, jsonify, request
import sqlite3
settings_bp = Blueprint('settings', __name__)
def get_db_connection():
conn = sqlite3.connect('pt_manager.db')
conn.row_factory = sqlite3.Row
return conn
@settings_bp.route('/settings')
def settings_index():
if 'user_id' not in session:
return redirect(url_for('auth.login'))
if session['role'] != 'admin':
return redirect(url_for('main.index'))
conn = get_db_connection()
# Get NexusPHP settings
nexusphp_site_url = conn.execute(
"SELECT value FROM settings WHERE key = 'nexusphp_site_url'"
).fetchone()
nexusphp_api_token = conn.execute(
"SELECT value FROM settings WHERE key = 'nexusphp_api_token'"
).fetchone()
# Get client settings
qbittorrent_config = conn.execute(
"SELECT * FROM clients WHERE name = 'qbittorrent'"
).fetchone()
transmission_config = conn.execute(
"SELECT * FROM clients WHERE name = 'transmission'"
).fetchone()
conn.close()
return render_template('settings/index.html',
nexusphp_site_url=nexusphp_site_url['value'] if nexusphp_site_url else '',
nexusphp_api_token=nexusphp_api_token['value'] if nexusphp_api_token else '',
qbittorrent_config=qbittorrent_config,
transmission_config=transmission_config)
@settings_bp.route('/settings/nexusphp', methods=['POST'])
def save_nexusphp_settings():
if 'user_id' not in session:
return jsonify({'error': 'Authentication required'}), 401
if session['role'] != 'admin':
return jsonify({'error': 'Admin access required'}), 403
site_url = request.form.get('site_url')
api_token = request.form.get('api_token')
conn = get_db_connection()
try:
# Save or update site URL
conn.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)",
('nexusphp_site_url', site_url)
)
# Save or update API token
conn.execute(
"INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)",
('nexusphp_api_token', api_token)
)
print(api_token)#262|wEGbxaqybJ6ZfLZAAtxX0oiQTymFcelHpc6YHims27a70898
conn.commit()
conn.close()
return jsonify({'success': True})
except Exception as e:
conn.close()
return jsonify({'error': str(e)}), 500
@settings_bp.route('/settings/qbittorrent', methods=['POST'])
def save_qbittorrent_settings():
if 'user_id' not in session:
return jsonify({'error': 'Authentication required'}), 401
if session['role'] != 'admin':
return jsonify({'error': 'Admin access required'}), 403
host = request.form.get('host')
port = request.form.get('port')
username = request.form.get('username')
password = request.form.get('password')
enabled = bool(request.form.get('enabled'))
conn = get_db_connection()
try:
# Check if config exists
existing = conn.execute(
"SELECT id FROM clients WHERE name = 'qbittorrent'"
).fetchone()
if existing:
# Update existing config
conn.execute(
"""UPDATE clients SET host = ?, port = ?, username = ?, password = ?, enabled = ?
WHERE name = 'qbittorrent'""",
(host, port, username, password, enabled)
)
else:
# Insert new config
conn.execute(
"""INSERT INTO clients (name, host, port, username, password, enabled)
VALUES (?, ?, ?, ?, ?, ?)""",
('qbittorrent', host, port, username, password, enabled)
)
conn.commit()
conn.close()
return jsonify({'success': True})
except Exception as e:
conn.close()
return jsonify({'error': str(e)}), 500
@settings_bp.route('/settings/transmission', methods=['POST'])
def save_transmission_settings():
if 'user_id' not in session:
return jsonify({'error': 'Authentication required'}), 401
if session['role'] != 'admin':
return jsonify({'error': 'Admin access required'}), 403
host = request.form.get('host')
port = request.form.get('port')
username = request.form.get('username')
password = request.form.get('password')
enabled = bool(request.form.get('enabled'))
conn = get_db_connection()
try:
# Check if config exists
existing = conn.execute(
"SELECT id FROM clients WHERE name = 'transmission'"
).fetchone()
if existing:
# Update existing config
conn.execute(
"""UPDATE clients SET host = ?, port = ?, username = ?, password = ?, enabled = ?
WHERE name = 'transmission'""",
(host, port, username, password, enabled)
)
else:
# Insert new config
conn.execute(
"""INSERT INTO clients (name, host, port, username, password, enabled)
VALUES (?, ?, ?, ?, ?, ?)""",
('transmission', host, port, username, password, enabled)
)
conn.commit()
conn.close()
return jsonify({'success': True})
except Exception as e:
conn.close()
return jsonify({'error': str(e)}), 500