Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API endpoint for default fixed metadata variables of JSON dump plugin #5094

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3800,6 +3800,34 @@ paths:
$ref: '#/components/headers/X-Is-Form-Designer'
Content-Language:
$ref: '#/components/headers/Content-Language'
/api/v2/registration/plugins/json-dump/fixed-metadata-variables:
get:
operationId: registration_plugins_json_dump_fixed_metadata_variables_retrieve
description: Return a list of fixed metadata variables for the JSON dump registration
backend that are needed by the frontend as default options.
summary: Fixed metadata variables JSON dump registration backend
tags:
- registration
security:
- tokenAuth: []
- cookieAuth: []
- {}
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/FixedMetadataVariable'
description: ''
headers:
X-Session-Expires-In:
$ref: '#/components/headers/X-Session-Expires-In'
X-CSRFToken:
$ref: '#/components/headers/X-CSRFToken'
X-Is-Form-Designer:
$ref: '#/components/headers/X-Is-Form-Designer'
Content-Language:
$ref: '#/components/headers/Content-Language'
/api/v2/registration/plugins/objects-api/target-paths:
post:
operationId: registration_plugins_objects_api_target_paths_create
Expand Down Expand Up @@ -7477,6 +7505,27 @@ components:
- code
- name
- reason
FixedMetadataVariable:
type: object
properties:
variables:
type: array
items:
type: string
pattern: ^(\w|\w[\w.\-]*\w)$
readOnly: true
default:
- public_reference
- form_name
- form_version
- form_id
- registration_timestamp
- auth_type
title: Fixed metadata variable key list
description: A list of fixed variables to use in the metadata. These include
the registration variables of the JSON dump plugin.
required:
- variables
Form:
type: object
description: |-
Expand Down
2 changes: 2 additions & 0 deletions src/openforms/js/components/admin/form_design/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export const REGISTRATION_BACKENDS_ENDPOINT = '/api/v2/registration/plugins';
export const OBJECTS_API_OBJECTTYPES_ENDPOINT = '/api/v2/objects-api/object-types';
export const REGISTRATION_OBJECTS_TARGET_PATHS =
'/api/v2/registration/plugins/objects-api/target-paths';
export const REGISTRATION_JSON_DUMP_FIXED_METADATA_VARIABLES =
'/api/v2/registration/plugins/json-dump/fixed-metadata-variables';
export const AUTH_PLUGINS_ENDPOINT = '/api/v2/authentication/plugins';
export const PREFILL_PLUGINS_ENDPOINT = '/api/v2/prefill/plugins';
export const DMN_PLUGINS_ENDPOINT = '/api/v2/dmn/plugins';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import PropTypes from 'prop-types';
import React, {useContext} from 'react';
import {FormattedMessage} from 'react-intl';
import {useAsync} from 'react-use';

import {REGISTRATION_JSON_DUMP_FIXED_METADATA_VARIABLES} from 'components/admin/form_design/constants';
import Fieldset from 'components/admin/forms/Fieldset';
import ModalOptionsConfiguration from 'components/admin/forms/ModalOptionsConfiguration';
import {
ValidationErrorContext,
ValidationErrorsProvider,
filterErrors,
} from 'components/admin/forms/ValidationErrors';
import {get} from 'utils/fetch';
import {getChoicesFromSchema} from 'utils/json-schema';

import {
Expand All @@ -29,6 +32,15 @@ const JSONDumpOptionsForm = ({name, label, schema, formData, onChange}) => {
([value, label]) => ({value, label})
);

// Get fixed metadata variables
const {value: fixedMetadataVariables} = useAsync(async () => {
const response = await get(REGISTRATION_JSON_DUMP_FIXED_METADATA_VARIABLES);
if (!response.ok) {
throw new Error(response.statusText);
}
return response.data;
}, []);

return (
<ModalOptionsConfiguration
name={name}
Expand All @@ -44,7 +56,7 @@ const JSONDumpOptionsForm = ({name, label, schema, formData, onChange}) => {
service: null,
path: '',
variables: [],
fixedMetadataVariables: [],
fixedMetadataVariables: fixedMetadataVariables || [],
additionalMetadataVariables: [],
...formData,
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {FormattedMessage} from 'react-intl';
import {IconNo, IconYes} from 'components/admin/BooleanIcons';

const JSONDumpSummaryHandler = ({variable, backendOptions}) => {
const isIncludedInVariables = backendOptions.variables.includes(variable.key);
const isIncludedInVariables = backendOptions.variables?.includes(variable.key);
const isIncludedInMetadata =
backendOptions.fixedMetadataVariables.includes(variable.key) ||
backendOptions.additionalMetadataVariables.includes(variable.key);
backendOptions.fixedMetadataVariables?.includes(variable.key) ||
backendOptions.additionalMetadataVariables?.includes(variable.key);

return (
<>
Expand Down
4 changes: 4 additions & 0 deletions src/openforms/registrations/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@
"plugins/zgw-api/",
include("openforms.registrations.contrib.zgw_apis.api.urls"),
),
path(
"plugins/json-dump/",
include("openforms.registrations.contrib.json_dump.api.urls"),
),
]
Empty file.
25 changes: 25 additions & 0 deletions src/openforms/registrations/contrib/json_dump/api/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django.utils.translation import gettext_lazy as _

from rest_framework import serializers

from openforms.formio.api.fields import FormioVariableKeyField


class FixedMetadataVariableSerializer(serializers.Serializer):
variables = serializers.ListField(
child=FormioVariableKeyField(),
label=_("Fixed metadata variable key list"),
help_text=_(
"A list of fixed variables to use in the metadata. These include the "
"registration variables of the JSON dump plugin."
),
default=[
"public_reference",
"form_name",
"form_version",
"form_id",
"registration_timestamp",
"auth_type",
],
read_only=True,
)
13 changes: 13 additions & 0 deletions src/openforms/registrations/contrib/json_dump/api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.urls import path

from .views import FixedMetadataVariablesView

app_name = "registrations_json_dump"

urlpatterns = [
path(
"fixed-metadata-variables",
FixedMetadataVariablesView.as_view(),
name="fixed_metadata_variables",
)
]
24 changes: 24 additions & 0 deletions src/openforms/registrations/contrib/json_dump/api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.utils.translation import gettext_lazy as _

from drf_spectacular.utils import extend_schema
from rest_framework.response import Response
from rest_framework.views import APIView

from .serializers import FixedMetadataVariableSerializer


class FixedMetadataVariablesView(APIView):

serializer_class = FixedMetadataVariableSerializer

@extend_schema(
summary=_("Fixed metadata variables JSON dump registration backend"),
description=_(
"Return a list of fixed metadata variables for the JSON dump registration "
"backend that are needed by the frontend as default options."
),
)
def get(self, request, *args, **kwargs):
serializer = self.serializer_class(data={})
serializer.is_valid()
return Response(serializer.data["variables"])
19 changes: 6 additions & 13 deletions src/openforms/registrations/contrib/json_dump/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,19 @@ class JSONDumpOptionsSerializer(JsonSchemaSerializerMixin, serializers.Serialize
required=True,
min_length=1,
)
# Note: this field cannot be updated in the frontend, and is used as read-only.
# Adding a read_only=True argument to it, though, will make it not available in the
# backend options of the plugin, as they are not added to the validated_data
# property of the serializer.
# Note: the defaults for this field are listed in
# `.api.views.FixedMetadataVariableSerializer`. They are in a separate API to make
# them available in the frontend as default fixed options.
# TODO: this list should be validated against the available FormVariable records
# that exist for the form.
fixed_metadata_variables = serializers.ListField(
child=FormioVariableKeyField(),
default=[
"public_reference",
"form_name",
"form_version",
"form_id",
"registration_timestamp",
"auth_type",
],
label=_("Fixed metadata variable key list"),
help_text=_(
"A list of required variables to use in the metadata. These include "
"the registration variables of the JSON dump plugin."
),
required=False,
required=True,
)
# TODO: this list should be validated against the available FormVariable records
# that exist for the form.
Expand Down
13 changes: 13 additions & 0 deletions src/openforms/registrations/contrib/json_dump/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.test import TestCase
from django.urls import reverse_lazy


class JSONDumpAPITests(TestCase):

endpoint = reverse_lazy("api:registrations_json_dump:fixed_metadata_variables")

def test_get(self):
response = self.client.get(self.endpoint)

self.assertEqual(response.status_code, 200)
self.assertTrue(isinstance(response.json(), list))
Loading