Files
cetana-backend/apps/core/views.py
T
2026-09-24 12:32:59 +03:30

63 lines
2.3 KiB
Python

from rest_framework.response import Response
from rest_framework.views import APIView
from apps.core.settings_registry import SCHEMA_BY_KEY
from apps.core.settings_service import SettingsService
from apps.rbac.permissions import HasPermission
class SettingsView(APIView):
"""Read/write runtime integration settings.
GET returns the schema (drives the panel) and current values (secrets
masked). PUT accepts ``{"values": {"key": value, ...}}`` and persists them.
"""
permission_classes = [HasPermission]
perm_code = "settings.manage"
def get(self, request):
service = SettingsService()
return Response({"schema": service.schema(), "values": service.all_values()})
def put(self, request):
service = SettingsService()
values = request.data.get("values") or {}
for key, value in values.items():
entry = SCHEMA_BY_KEY.get(key)
if entry is None:
continue
# Never overwrite a secret with an empty value (means "unchanged").
if entry.get("type") == "secret" and value in ("", None):
continue
service.set(key, value)
return Response({"values": service.all_values()})
class TestOdooView(APIView):
"""Attempt an XML-RPC connection using the currently saved Odoo settings."""
permission_classes = [HasPermission]
perm_code = "settings.manage"
def post(self, request):
from apps.integrations.odoo.client import OdooXmlRpcClient
service = SettingsService()
url = service.get("odoo.url", "")
if not url:
return Response({"ok": False, "error": "Odoo URL is not configured."})
client = OdooXmlRpcClient(
url=url,
db=service.get("odoo.database", ""),
username=service.get("odoo.username", ""),
password=service.get("odoo.password", ""),
api_key=service.get("odoo.api_key", ""),
auth_method=service.get("odoo.auth_method", "password"),
)
try:
result = client.test_connection()
return Response({"ok": True, "version": result["version"], "uid": str(result["uid"])})
except Exception as exc:
return Response({"ok": False, "error": str(exc)})