settings.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. from db import Destination, RemoteInstance
  58. cfg = {k: _get_setting(k) for k in _SETTING_KEYS}
  59. cfg.setdefault("smtp_port", "587")
  60. cfg["smtp_tls"] = cfg.get("smtp_tls") or "1"
  61. cfg["smtp_ssl"] = cfg.get("smtp_ssl") or "0"
  62. cfg["notify_on_error"] = cfg.get("notify_on_error") or "1"
  63. api_token = current_app.config.get("API_TOKEN", "")
  64. instance_url = current_app.config.get("INSTANCE_URL", "")
  65. destinations = Destination.query.order_by(Destination.name).all()
  66. instances = RemoteInstance.query.order_by(RemoteInstance.name).all()
  67. return render_template("settings.html", cfg=cfg, api_token=api_token,
  68. instance_url=instance_url, destinations=destinations,
  69. instances=instances)
  70. @bp.route("/internal/databases/<db_type>")
  71. def internal_databases(db_type):
  72. """Liste les bases de données disponibles pour le formulaire job."""
  73. databases = []
  74. try:
  75. if db_type == "mysql":
  76. result = subprocess.run(
  77. ["sudo", "mysql", "--skip-column-names", "-e", "SHOW DATABASES;"],
  78. capture_output=True, text=True, timeout=10,
  79. )
  80. if result.returncode == 0:
  81. exclude = {"information_schema", "performance_schema", "mysql", "sys"}
  82. databases = [d.strip() for d in result.stdout.splitlines()
  83. if d.strip() and d.strip() not in exclude]
  84. elif db_type == "postgresql":
  85. result = subprocess.run(
  86. ["sudo", "-u", "postgres", "psql", "-Atc",
  87. "SELECT datname FROM pg_database WHERE datistemplate = false;"],
  88. capture_output=True, text=True, timeout=10,
  89. )
  90. if result.returncode == 0:
  91. databases = [d.strip() for d in result.stdout.splitlines() if d.strip()]
  92. except Exception:
  93. pass
  94. return jsonify(databases)