Skip to content

Commit a14f140

Browse files
committed
✨ [#5074] Transform data for selectboxes based on openForms.transformData
1 parent 3f79ba2 commit a14f140

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/openforms/forms/models/form.py

+12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from openforms.authentication.registry import register as authentication_register
2626
from openforms.config.models import GlobalConfiguration
2727
from openforms.data_removal.constants import RemovalMethods
28+
from openforms.formio.datastructures import FormioConfigurationWrapper
2829
from openforms.formio.validators import variable_key_validator
2930
from openforms.payments.fields import PaymentBackendChoiceField
3031
from openforms.payments.registry import register as payment_register
@@ -609,6 +610,17 @@ def iter_components(self, recursive=True):
609610
for form_step in self.formstep_set.select_related("form_definition"):
610611
yield from form_step.iter_components(recursive=recursive)
611612

613+
def get_total_configuration(self) -> FormioConfigurationWrapper:
614+
"""
615+
Return the total joined FormIO configuration of all FormSteps belonging to this Form
616+
"""
617+
raw_configs = self.formstep_set.select_related("form_definition").values_list(
618+
"form_definition__configuration", flat=True
619+
)
620+
configs = [FormioConfigurationWrapper(config) for config in raw_configs]
621+
configuration = sum(configs, FormioConfigurationWrapper({"components": []}))
622+
return configuration
623+
612624
@transaction.atomic
613625
def restore_old_version(
614626
self, form_version_uuid: str, user: User | None = None

src/openforms/submissions/models/submission_value_variable.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import logging
34
from dataclasses import dataclass, field
45
from datetime import date, datetime, time
56
from typing import TYPE_CHECKING, Any, Literal, overload
@@ -11,6 +12,8 @@
1112
from django.utils.functional import empty
1213
from django.utils.translation import gettext_lazy as _
1314

15+
from glom import glom
16+
1417
from openforms.formio.service import FormioData
1518
from openforms.forms.models.form_variable import FormVariable
1619
from openforms.typing import DataMapping, JSONEncodable, JSONObject, JSONSerializable
@@ -19,11 +22,14 @@
1922
from openforms.variables.service import VariablesRegistry, get_static_variables
2023

2124
from ..constants import SubmissionValueVariableSources
25+
from ..transform_data import TRANSFORM_DATA_MAPPING
2226
from .submission import Submission
2327

2428
if TYPE_CHECKING:
2529
from .submission_step import SubmissionStep
2630

31+
logger = logging.getLogger(__name__)
32+
2733

2834
class ValueEncoder(DjangoJSONEncoder):
2935
def default(self, obj: JSONEncodable | JSONSerializable) -> JSONEncodable:
@@ -94,6 +100,8 @@ def get_data(
94100
)
95101

96102
formio_data = FormioData()
103+
configuration = self.submission.form.get_total_configuration()
104+
97105
for variable_key, variable in submission_variables.items():
98106
if (
99107
variable.value is None
@@ -104,7 +112,24 @@ def get_data(
104112
continue
105113

106114
if variable.source != SubmissionValueVariableSources.sensitive_data_cleaner:
107-
formio_data[variable_key] = variable.value
115+
component_configuration = configuration[variable.key]
116+
117+
if glom(
118+
component_configuration, "openForms.transformData", default=None
119+
):
120+
transform_function = TRANSFORM_DATA_MAPPING.get(
121+
component_configuration["type"]
122+
)
123+
if not transform_function:
124+
logger.warning(
125+
"Incorrect configuration, component of type `%s` has `openForms.transformData` "
126+
"set to true, but no transform function is defined in TRANSFORM_DATA_MAPPING",
127+
component_configuration["type"],
128+
)
129+
transform_function = lambda value: value
130+
formio_data[variable_key] = transform_function(variable.value)
131+
else:
132+
formio_data[variable_key] = variable.value
108133
return formio_data if as_formio_data else formio_data.data
109134

110135
def get_variables_in_submission_step(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def transform_selectboxes_data(value: dict[str, bool] | None) -> list[str] | None:
2+
if value:
3+
return sorted([key for key, value in value.items() if value])
4+
5+
6+
TRANSFORM_DATA_MAPPING = {
7+
"selectboxes": transform_selectboxes_data,
8+
}

0 commit comments

Comments
 (0)