Skip to content

Commit

Permalink
✨ [#4908] Add config checks for JSON registration plugin
Browse files Browse the repository at this point in the history
Will check the connection to the configured service
  • Loading branch information
viktorvanwijk committed Dec 30, 2024
1 parent 295f464 commit c4a5834
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/openforms/fixtures/admin_index_unlisted.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions src/openforms/registrations/contrib/json/admin.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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",
},
),
]
Empty file.
30 changes: 30 additions & 0 deletions src/openforms/registrations/contrib/json/models.py
Original file line number Diff line number Diff line change
@@ -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")
33 changes: 28 additions & 5 deletions src/openforms/registrations/contrib/json/plugin.py
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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,)
)
)
]

0 comments on commit c4a5834

Please sign in to comment.