Skip to content

Commit

Permalink
🎨 [#4908] Fix imports and black code
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorvanwijk committed Jan 2, 2025
1 parent 707ef37 commit bd2af0e
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ export default {
properties: {
service: {
enum: [1, 2],
enumNames: ['Service 1', 'Service 2'],
enumNames: ['Service 1', 'Service 2'],
},
relativeApiEndpoint: {
minLength: 1,
Expand Down Expand Up @@ -1015,7 +1015,6 @@ export const STUFZDS = {
},
};


export const JSON = {
args: {
configuredBackends: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const BACKEND_OPTIONS_FORMS = {
form: StufZDSOptionsForm,
},
'microsoft-graph': {form: MSGraphOptionsForm},
'json': {
json: {
form: JSONOptionsForm,
configurableFromVariables: true,
summaryHandler: JSONSummaryHandler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ import FormVariablesSelect from './fields/FormVariablesSelect';
import RelativeAPIEndpoint from './fields/RelativeAPIEndpoint';
import ServiceSelect from './fields/ServiceSelect';


const JSONOptionsForm = ({name, label, schema, formData, onChange}) => {
const validationErrors = useContext(ValidationErrorContext);
const relevantErrors = filterErrors(name, validationErrors);

// Get form variables and create form variable options
const formContext = useContext(FormContext)
const formContext = useContext(FormContext);
const formVariables = formContext.formVariables ?? [];
const staticVariables = formContext.staticVariables ?? [];
const allFormVariables = staticVariables.concat(formVariables);
Expand All @@ -37,9 +36,9 @@ const JSONOptionsForm = ({name, label, schema, formData, onChange}) => {

// Create service options
const {service} = schema.properties;
const serviceOptions = getChoicesFromSchema(
service.enum, service.enumNames
).map(([value, label]) => ({value, label}));
const serviceOptions = getChoicesFromSchema(service.enum, service.enumNames).map(
([value, label]) => ({value, label})
);

return (
<ModalOptionsConfiguration
Expand All @@ -58,9 +57,9 @@ const JSONOptionsForm = ({name, label, schema, formData, onChange}) => {
>
<ValidationErrorsProvider errors={relevantErrors}>
<Fieldset>
<ServiceSelect options={serviceOptions}/>
<ServiceSelect options={serviceOptions} />
<RelativeAPIEndpoint />
<FormVariablesSelect options={formVariableOptions}/>
<FormVariablesSelect options={formVariableOptions} />
</Fieldset>
</ValidationErrorsProvider>
</ModalOptionsConfiguration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@ import React from 'react';
import {FormattedMessage} from 'react-intl';

const JSONSummaryHandler = ({variable, backendOptions}) => {

const isIncluded = backendOptions.formVariables.includes(variable.key);

if (isIncluded) {
return (
<FormattedMessage
description="JSON registration summary message"
defaultMessage="Included"
/>
<FormattedMessage description="JSON registration summary message" defaultMessage="Included" />
);
}
else {
} else {
return (
<FormattedMessage
description="JSON registration summary message"
Expand All @@ -24,7 +19,6 @@ const JSONSummaryHandler = ({variable, backendOptions}) => {
}
};


JSONSummaryHandler.propTypes = {
variable: PropTypes.shape({
key: PropTypes.string.isRequired,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import {useField} from 'formik';
import PropTypes from 'prop-types';
import React from 'react';

import {FormattedMessage} from 'react-intl';

import Field from 'components/admin/forms/Field';
import FormRow from 'components/admin/forms/FormRow';
import {Checkbox} from 'components/admin/forms/Inputs';


const JSONVariableConfigurationEditor = ({variable}) => {
const [fieldProps, , {setValue}] = useField('formVariables');

const formVariables = fieldProps.value
const formVariables = fieldProps.value;
const isIncluded = formVariables.includes(variable.key);

return (
Expand All @@ -33,34 +32,37 @@ const JSONVariableConfigurationEditor = ({variable}) => {
}
checked={isIncluded}
onChange={event => {
const formVariablesNew = formVariables.slice()
const formVariablesNew = formVariables.slice();
const index = formVariablesNew.indexOf(variable.key);
if (event.target.checked) {
if (index !== -1) {throw new Error(
"This form variable is already on the list of " +
"form variables to include. This shouldn't happen."
);}
if (index !== -1) {
throw new Error(
'This form variable is already on the list of ' +
"form variables to include. This shouldn't happen."
);
}
formVariablesNew.push(variable.key);
} else {
if (index === -1) {throw new Error(
"This form variable is not yet on the list of " +
"form variables to include. This shouldn't happen."
);}
if (index === -1) {
throw new Error(
'This form variable is not yet on the list of ' +
"form variables to include. This shouldn't happen."
);
}
formVariablesNew.splice(index, 1);
}
setValue(formVariablesNew);
}}
/>
</Field>
</FormRow>
)
}
);
};

JSONVariableConfigurationEditor.propTypes = {
variable: PropTypes.shape({
key: PropTypes.string.isRequired,
}).isRequired,
};


export default JSONVariableConfigurationEditor
export default JSONVariableConfigurationEditor;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import Field from 'components/admin/forms/Field';
import FormRow from 'components/admin/forms/FormRow';
import ReactSelect from 'components/admin/forms/ReactSelect';


// TODO-4908: the select box does not change size when you add more form variables, which causes
// selected form variables to be hidden
const FormVariablesSelect = ({options}) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Field from 'components/admin/forms/Field';
import FormRow from 'components/admin/forms/FormRow';
import {TextInput} from 'components/admin/forms/Inputs';


const RelativeAPIEndpoint = () => {
const [fieldProps] = useField('relativeApiEndpoint');
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ const ServiceSelect = ({options}) => {
/>
}
>
<ReactSelect
name="service"
options={options}
required
/>
<ReactSelect name="service" options={options} required />
</Field>
</FormRow>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ export const WithJSONRegistrationBackend = {

const saveButton = canvas.getByRole('button', {name: 'Opslaan'});
await userEvent.click(saveButton);
})
});

await step('aSingleFile checkbox checked', async () => {
await userEvent.click(editIcons[1]);
Expand All @@ -635,20 +635,18 @@ export const WithJSONRegistrationBackend = {

const saveButton = canvas.getByRole('button', {name: 'Opslaan'});
await userEvent.click(saveButton);
})
});

await step('now checkbox checked', async () => {
const staticVariables = canvas.getByRole('tab', {name: 'Vaste variabelen'});
await userEvent.click(staticVariables);

const editIcon = canvas.getByTitle('Registratie-instellingen bewerken')
await userEvent.click(editIcon)
const editIcon = canvas.getByTitle('Registratie-instellingen bewerken');
await userEvent.click(editIcon);

const checkbox = await canvas.findByRole('checkbox');
await expect(checkbox).toBeChecked();
})


});
},
};

Expand Down
9 changes: 7 additions & 2 deletions src/openforms/registrations/contrib/json/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ class JSONOptionsSerializer(JsonSchemaSerializerMixin, serializers.Serializer):
relative_api_endpoint = serializers.CharField(
max_length=255,
label=_("Relative API endpoint"),
help_text=_("The API endpoint to send the data to (relative to the service API root)."),
help_text=_(
"The API endpoint to send the data to (relative to the service API root)."
),
allow_blank=True,
required=False,
)
form_variables = serializers.ListField(
child=FormioVariableKeyField(max_length=50),
label=_("Form variable key list"),
help_text=_("A list of form variables (can also include static variables) to use."),
help_text=_(
"A list of form variables (can also include static variables) to use."
),
required=True,
min_length=1,
)
Expand All @@ -41,6 +45,7 @@ class JSONOptions(TypedDict):
This describes the shape of :attr:`JSONOptionsSerializer.validated_data`, after
the input data has been cleaned/validated.
"""

service: Required[Service]
relative_api_endpoint: str
form_variables: Required[list[str]]
26 changes: 8 additions & 18 deletions src/openforms/registrations/contrib/json/plugin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import base64

from django.utils.translation import gettext_lazy as _
from django.urls import reverse
from django.utils.translation import gettext_lazy as _

from requests import RequestException
from zgw_consumers.client import build_client
Expand All @@ -26,7 +26,7 @@ def register_submission(self, submission: Submission, options: JSONOptions) -> d
# Encode (base64) and add attachments to values dict if their form keys were specified in the
# form variables list
for attachment in submission.attachments:
if not attachment.form_key in options["form_variables"]:
if attachment.form_key not in options["form_variables"]:
continue

Check warning on line 30 in src/openforms/registrations/contrib/json/plugin.py

View check run for this annotation

Codecov / codecov/patch

src/openforms/registrations/contrib/json/plugin.py#L30

Added line #L30 was not covered by tests
options["form_variables"].remove(attachment.form_key)
with attachment.content.open("rb") as f:
Expand Down Expand Up @@ -54,20 +54,10 @@ def register_submission(self, submission: Submission, options: JSONOptions) -> d
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"static_var_1": {
"type": "string",
"pattern": "^cool_pattern$"
},
"form_var_1": {
"type": "string"
},
"form_var_2": {
"type": "string"
},
"attachment": {
"type": "string",
"contentEncoding": "base64"
},
"static_var_1": {"type": "string", "pattern": "^cool_pattern$"},
"form_var_1": {"type": "string"},
"form_var_2": {"type": "string"},
"attachment": {"type": "string", "contentEncoding": "base64"},
},
"required": ["static_var_1", "form_var_1", "form_var_2"],
"additionalProperties": False,
Expand Down Expand Up @@ -107,7 +97,7 @@ def get_config_actions(self) -> list[tuple[str, str]]:
_("Configuration"),
reverse(
"admin:registrations_json_jsonconfig_change",
args=(JSONConfig.singleton_instance_id,)
)
args=(JSONConfig.singleton_instance_id,),
),
)
]
21 changes: 5 additions & 16 deletions src/openforms/registrations/contrib/json/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from pathlib import Path

from django.test import TestCase
from requests import RequestException

from requests import RequestException
from zgw_consumers.test.factories import ServiceFactory

from openforms.submissions.public_references import set_submission_reference
Expand All @@ -15,7 +15,6 @@

from ..plugin import JSONRegistration


VCR_TEST_FILES = Path(__file__).parent / "files"


Expand Down Expand Up @@ -75,20 +74,10 @@ def test_submission_with_json_backend(self):
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"static_var_1": {
"type": "string",
"pattern": "^cool_pattern$"
},
"form_var_1": {
"type": "string"
},
"form_var_2": {
"type": "string"
},
"attachment": {
"type": "string",
"contentEncoding": "base64"
},
"static_var_1": {"type": "string", "pattern": "^cool_pattern$"},
"form_var_1": {"type": "string"},
"form_var_2": {"type": "string"},
"attachment": {"type": "string", "contentEncoding": "base64"},
},
"required": ["static_var_1", "form_var_1", "form_var_2"],
"additionalProperties": False,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from ..models import JSONConfig
from ..plugin import JSONRegistration


VCR_TEST_FILES = Path(__file__).parent / "files"


Expand All @@ -31,7 +30,7 @@ def test_config_check_happy_flow(self):

with patch(
"openforms.registrations.contrib.json.plugin.JSONConfig.get_solo",
return_value=config
return_value=config,
):
json_plugin.check_config()

Expand All @@ -41,7 +40,7 @@ def test_no_service_configured(self):

with patch(
"openforms.registrations.contrib.json.plugin.JSONConfig.get_solo",
return_value=config
return_value=config,
):
self.assertRaises(InvalidPluginConfiguration, json_plugin.check_config)

Expand All @@ -57,6 +56,6 @@ def test_invalid_response_from_api_test_connection_endpoint(self):

with patch(
"openforms.registrations.contrib.json.plugin.JSONConfig.get_solo",
return_value=config
return_value=config,
):
self.assertRaises(InvalidPluginConfiguration, json_plugin.check_config)

0 comments on commit bd2af0e

Please sign in to comment.