diff --git a/src/openforms/fixtures/admin_index_unlisted.json b/src/openforms/fixtures/admin_index_unlisted.json index cf01210cff..4ee11b504e 100644 --- a/src/openforms/fixtures/admin_index_unlisted.json +++ b/src/openforms/fixtures/admin_index_unlisted.json @@ -19,6 +19,7 @@ "haalcentraal.HaalCentraalConfig", "qmatic.QmaticConfig", "registrations_email.EmailConfig", + "registrations_json.JSONConfig", "registrations_microsoft_graph.MSGraphRegistrationConfig", "objects_api.ObjectsAPIGroupConfig", "registrations_objects_api.ObjectsAPIConfig", diff --git a/src/openforms/registrations/contrib/json/admin.py b/src/openforms/registrations/contrib/json/admin.py new file mode 100644 index 0000000000..9f4c2857a4 --- /dev/null +++ b/src/openforms/registrations/contrib/json/admin.py @@ -0,0 +1,10 @@ +from django.contrib import admin + +from solo.admin import SingletonModelAdmin + +from .models import JSONConfig + + +@admin.register(JSONConfig) +class JSONConfigAdmin(SingletonModelAdmin): + pass diff --git a/src/openforms/registrations/contrib/json/migrations/0001_initial.py b/src/openforms/registrations/contrib/json/migrations/0001_initial.py new file mode 100644 index 0000000000..dfd97b0092 --- /dev/null +++ b/src/openforms/registrations/contrib/json/migrations/0001_initial.py @@ -0,0 +1,44 @@ +# Generated by Django 4.2.17 on 2024-12-30 12:49 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ("zgw_consumers", "0022_set_default_service_slug"), + ] + + operations = [ + migrations.CreateModel( + name="JSONConfig", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "service", + models.OneToOneField( + help_text="Service for JSON registration plugin", + limit_choices_to={"api_type": "orc"}, + null=True, + on_delete=django.db.models.deletion.PROTECT, + to="zgw_consumers.service", + verbose_name="Service", + ), + ), + ], + options={ + "verbose_name": "JSON registration configuration", + }, + ), + ] diff --git a/src/openforms/registrations/contrib/json/migrations/__init__.py b/src/openforms/registrations/contrib/json/migrations/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/openforms/registrations/contrib/json/models.py b/src/openforms/registrations/contrib/json/models.py new file mode 100644 index 0000000000..f2ee73d7a3 --- /dev/null +++ b/src/openforms/registrations/contrib/json/models.py @@ -0,0 +1,30 @@ +from django.db import models +from django.utils.translation import gettext_lazy as _ + +from solo.models import SingletonModel +from zgw_consumers.constants import APITypes + + +class JSONConfigManager(models.Manager): + def get_queryset(self): + return super().get_queryset().select_related("service") + + +class JSONConfig(SingletonModel): + """ + Global configuration and defaults + """ + + service = models.OneToOneField( + "zgw_consumers.Service", + verbose_name=_("Service"), + on_delete=models.PROTECT, + limit_choices_to={"api_type": APITypes.orc}, + null=True, + help_text=_("Service for JSON registration plugin"), + ) + + objects = JSONConfigManager() + + class Meta: + verbose_name = _("JSON registration configuration") diff --git a/src/openforms/registrations/contrib/json/plugin.py b/src/openforms/registrations/contrib/json/plugin.py index a90e5ac946..b85eb45a38 100644 --- a/src/openforms/registrations/contrib/json/plugin.py +++ b/src/openforms/registrations/contrib/json/plugin.py @@ -1,15 +1,19 @@ import base64 from django.utils.translation import gettext_lazy as _ +from django.urls import reverse +from requests import RequestException from zgw_consumers.client import build_client +from openforms.plugins.exceptions import InvalidPluginConfiguration from openforms.submissions.models import Submission from openforms.variables.service import get_static_variables from ...base import BasePlugin # openforms.registrations.base from ...registry import register # openforms.registrations.registry from .config import JSONOptions, JSONOptionsSerializer +from .models import JSONConfig @register("json") @@ -58,8 +62,27 @@ def register_submission(self, submission: Submission, options: JSONOptions) -> d return result - def check_config(self): - # TODO-4908: check if it's possible to connect to the service - # (using the 'connection check endpoint' of the service) - # TODO-4908: check anything else? - pass + def check_config(self) -> None: + # Get service + config = JSONConfig.get_solo() + if (service := config.service) is None: + raise InvalidPluginConfiguration(_("Please configure a service")) + + # Check connection to service + with build_client(service) as client: + try: + res = client.get(service.api_connection_check_path) + res.raise_for_status() + except RequestException as exc: + raise InvalidPluginConfiguration(_(f"Invalid response: {exc}")) from exc + + def get_config_actions(self) -> list[tuple[str, str]]: + return [ + ( + _("Configuration"), + reverse( + "admin:registrations_json_jsonconfig_change", + args=(JSONConfig.singleton_instance_id,) + ) + ) + ]