05/24 Enhance functionalities
This commit is contained in:
@@ -3,8 +3,10 @@ routes/admin_settings.py — Application settings (email, Groq API, etc.)
|
||||
"""
|
||||
|
||||
import logging
|
||||
from flask import Blueprint, render_template, request, redirect, url_for, flash, session
|
||||
from config import get_settings_dict, set_setting
|
||||
import os
|
||||
import requests as http_requests
|
||||
from flask import Blueprint, render_template, request, redirect, url_for, flash, session, jsonify
|
||||
from config import get_settings_dict, set_setting, get_setting
|
||||
from models import log_action
|
||||
from utils.decorators import admin_required
|
||||
|
||||
@@ -54,3 +56,49 @@ def save_groq():
|
||||
flash("Groq settings saved successfully.", "success")
|
||||
logger.info(f"Groq settings updated by admin_id={admin['id']}.")
|
||||
return redirect(url_for("admin_settings.settings"))
|
||||
|
||||
|
||||
@admin_settings_bp.route("/test-email", methods=["POST"])
|
||||
@admin_required
|
||||
def test_email():
|
||||
from utils.email import send_email
|
||||
admin = session["user"]
|
||||
to = (admin.get("email") or "").strip()
|
||||
if not to:
|
||||
return jsonify({"error": "Your admin account has no email address. Add one in User Management."}), 400
|
||||
try:
|
||||
send_email(
|
||||
to,
|
||||
"Website Checker — SMTP Test",
|
||||
"This is a test email from Website Checker. Your SMTP configuration is working correctly.",
|
||||
)
|
||||
log_action(admin["id"], "TEST_EMAIL", "app_settings", None,
|
||||
f"SMTP test email sent to {to}.")
|
||||
return jsonify({"ok": True, "to": to})
|
||||
except Exception as e:
|
||||
logger.error(f"SMTP test failed: {e}")
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
@admin_settings_bp.route("/test-groq", methods=["POST"])
|
||||
@admin_required
|
||||
def test_groq():
|
||||
admin = session["user"]
|
||||
api_key = (os.environ.get("GROQ_API_KEY") or "").strip() or get_setting("groq.api_key", "").strip()
|
||||
if not api_key:
|
||||
return jsonify({"error": "No Groq API key is configured. Save one first."}), 400
|
||||
model = get_setting("groq.model", "llama-3.3-70b-versatile")
|
||||
try:
|
||||
resp = http_requests.post(
|
||||
"https://api.groq.com/openai/v1/chat/completions",
|
||||
headers={"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"},
|
||||
json={"model": model, "messages": [{"role": "user", "content": "Hi"}], "max_tokens": 5},
|
||||
timeout=15,
|
||||
)
|
||||
if resp.ok:
|
||||
log_action(admin["id"], "TEST_GROQ", "app_settings", None, "Groq API test successful.")
|
||||
return jsonify({"ok": True, "model": model})
|
||||
return jsonify({"error": f"Groq API returned {resp.status_code}: {resp.text[:200]}"}), 400
|
||||
except Exception as e:
|
||||
logger.error(f"Groq test failed: {e}")
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
Reference in New Issue
Block a user