settings.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import subprocess
  2. from flask import (
  3. Blueprint,
  4. current_app,
  5. flash,
  6. jsonify,
  7. redirect,
  8. render_template,
  9. request,
  10. url_for,
  11. )
  12. from db import db, Setting
  13. bp = Blueprint("cfg", __name__)
  14. _SETTING_KEYS = [
  15. "smtp_host", "smtp_port", "smtp_user", "smtp_password",
  16. "smtp_from", "smtp_to", "smtp_tls", "smtp_ssl",
  17. "notify_on_success", "notify_on_error",
  18. ]
  19. def _get_setting(key, default=""):
  20. s = Setting.query.filter_by(key=key).first()
  21. return s.value if s else default
  22. @bp.route("/settings", methods=["GET", "POST"])
  23. def settings():
  24. if request.method == "POST":
  25. action = request.form.get("action")
  26. if action == "test_smtp":
  27. from notifications import send_test_email
  28. try:
  29. send_test_email(
  30. host=request.form.get("smtp_host", "").strip(),
  31. port=int(request.form.get("smtp_port", 587) or 587),
  32. user=request.form.get("smtp_user", "").strip(),
  33. password=request.form.get("smtp_password", ""),
  34. from_addr=request.form.get("smtp_from", "").strip(),
  35. to_addr=request.form.get("smtp_to", "").strip(),
  36. use_ssl=request.form.get("smtp_ssl") == "1",
  37. use_tls=request.form.get("smtp_tls") == "1",
  38. )
  39. flash("Email de test envoyé avec succès.", "success")
  40. except Exception as exc:
  41. flash(f"Échec du test SMTP : {exc}", "error")
  42. else:
  43. for key in _SETTING_KEYS:
  44. if key in ("smtp_tls", "smtp_ssl", "notify_on_success", "notify_on_error"):
  45. value = "1" if request.form.get(key) == "1" else "0"
  46. else:
  47. value = request.form.get(key, "").strip()
  48. s = Setting.query.filter_by(key=key).first()
  49. if s is None:
  50. s = Setting(key=key, value=value)
  51. db.session.add(s)
  52. else:
  53. s.value = value
  54. db.session.commit()
  55. flash("Paramètres enregistrés.", "success")
  56. return redirect(url_for("cfg.settings"))
  57. cfg = {k: _get_setting(k) for k in _SETTING_KEYS}
  58. cfg.setdefault("smtp_port", "587")
  59. cfg["smtp_tls"] = cfg.get("smtp_tls") or "1"
  60. cfg["smtp_ssl"] = cfg.get("smtp_ssl") or "0"
  61. cfg["notify_on_error"] = cfg.get("notify_on_error") or "1"
  62. api_token = current_app.config.get("API_TOKEN", "")
  63. instance_url = current_app.config.get("INSTANCE_URL", "")
  64. return render_template("settings.html", cfg=cfg, api_token=api_token,
  65. instance_url=instance_url)
  66. @bp.route("/internal/databases/<db_type>")
  67. def internal_databases(db_type):
  68. """Liste les bases de données disponibles pour le formulaire job."""
  69. databases = []
  70. try:
  71. if db_type == "mysql":
  72. result = subprocess.run(
  73. ["sudo", "mysql", "--skip-column-names", "-e", "SHOW DATABASES;"],
  74. capture_output=True, text=True, timeout=10,
  75. )
  76. if result.returncode == 0:
  77. exclude = {"information_schema", "performance_schema", "mysql", "sys"}
  78. databases = [d.strip() for d in result.stdout.splitlines()
  79. if d.strip() and d.strip() not in exclude]
  80. elif db_type == "postgresql":
  81. result = subprocess.run(
  82. ["sudo", "-u", "postgres", "psql", "-Atc",
  83. "SELECT datname FROM pg_database WHERE datistemplate = false;"],
  84. capture_output=True, text=True, timeout=10,
  85. )
  86. if result.returncode == 0:
  87. databases = [d.strip() for d in result.stdout.splitlines() if d.strip()]
  88. except Exception:
  89. pass
  90. return jsonify(databases)