Skip to content

Commit

Permalink
♻️ [#5044] Add constant for dataSrc options
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Feb 4, 2025
1 parent 3f79ba2 commit 58b3212
Show file tree
Hide file tree
Showing 22 changed files with 124 additions and 88 deletions.
7 changes: 4 additions & 3 deletions src/openforms/formio/components/vanilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from csp_post_processor import post_process_html
from openforms.config.constants import UploadFileType
from openforms.config.models import GlobalConfiguration
from openforms.formio.constants import DataSrcOptions
from openforms.submissions.attachments import temporary_upload_from_url
from openforms.submissions.models import EmailVerification
from openforms.typing import DataMapping, JSONObject
Expand Down Expand Up @@ -700,7 +701,7 @@ def as_json_schema(component: SelectBoxesComponent) -> JSONObject:
data_src = component.get("openForms", {}).get("dataSrc")

base = {"title": label, "type": "object"}
if data_src != "variable":
if data_src != DataSrcOptions.variable:
# Only add properties if the data source IS NOT another variable, because
# component[values] is not updated when it IS. So it does not make sense to
# add properties in that case.
Expand Down Expand Up @@ -775,7 +776,7 @@ def as_json_schema(component: SelectComponent) -> JSONObject:
data_src = component.get("openForms", {}).get("dataSrc")

base = {"type": "string"}
if data_src != "variable":
if data_src != DataSrcOptions.variable:
# Only add properties if the data source IS NOT another variable, because
# component[data][values] is not updated when it IS. So it does not make
# sense to add properties in that case.
Expand Down Expand Up @@ -863,7 +864,7 @@ def as_json_schema(component: RadioComponent) -> JSONObject:
data_src = component.get("openForms", {}).get("dataSrc")

base = {"title": label, "type": "string"}
if data_src != "variable":
if data_src != DataSrcOptions.variable:
# Only add enum if the data source IS NOT another variable, because
# component[values] is not updated when it IS. So it does not make sense to
# add a list of choices to the enum in that case.
Expand Down
8 changes: 8 additions & 0 deletions src/openforms/formio/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from enum import StrEnum

COMPONENT_DATATYPES = {
"date": "date",
"time": "time",
Expand All @@ -11,3 +13,9 @@
"editgrid": "array",
"datetime": "datetime",
}


class DataSrcOptions(StrEnum):
manual = "manual"
variable = "variable"
referentielijsten = "referentielijsten"
5 changes: 3 additions & 2 deletions src/openforms/formio/dynamic_config/dynamic_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from json_logic import jsonLogic

from openforms.api.exceptions import ServiceUnavailable
from openforms.formio.constants import DataSrcOptions
from openforms.logging import logevent
from openforms.submissions.models import Submission
from openforms.typing import DataMapping, JSONValue
Expand Down Expand Up @@ -108,13 +109,13 @@ def add_options_to_config(
) -> None:
data_src = glom(component, "openForms.dataSrc", default=None)
match data_src:
case "referentielijsten":
case DataSrcOptions.referentielijsten:
items_array = fetch_options_from_referentielijsten(component, submission)
if not items_array:
raise ServiceUnavailable(
_("Could not retrieve options from Referentielijsten API."),
)
case "variable":
case DataSrcOptions.variable:
items_array = get_options_from_variable(component, data, submission)
if items_array is None:
return
Expand Down
51 changes: 26 additions & 25 deletions src/openforms/formio/dynamic_config/tests/test_dynamic_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from rest_framework.test import APITestCase

from openforms.accounts.tests.factories import SuperUserFactory
from openforms.formio.constants import DataSrcOptions
from openforms.formio.datastructures import FormioConfigurationWrapper
from openforms.formio.dynamic_config import rewrite_formio_components
from openforms.forms.tests.factories import (
Expand All @@ -30,7 +31,7 @@ def test_manual_options_not_updated(self):
{"label": "A", "value": "a"},
{"label": "B", "value": "b"},
],
"dataSrc": "manual",
"dataSrc": DataSrcOptions.manual,
},
{
"label": "Select",
Expand All @@ -40,7 +41,7 @@ def test_manual_options_not_updated(self):
{"label": "A", "value": "a"},
{"label": "B", "value": "b"},
],
"dataSrc": "manual",
"dataSrc": DataSrcOptions.manual,
"json": "",
"url": "",
"resource": "",
Expand All @@ -56,7 +57,7 @@ def test_manual_options_not_updated(self):
{"label": "A", "value": "a"},
{"label": "B", "value": "b"},
],
"dataSrc": "manual",
"dataSrc": DataSrcOptions.manual,
},
]
}
Expand Down Expand Up @@ -105,7 +106,7 @@ def test_variable_options_repeating_group(self):
{"label": "", "value": ""},
],
"openForms": {
"dataSrc": "variable",
"dataSrc": DataSrcOptions.variable,
"itemsExpression": {
"map": [{"var": "repeatingGroup"}, {"var": "name"}]
},
Expand All @@ -120,7 +121,7 @@ def test_variable_options_repeating_group(self):
],
},
"openForms": {
"dataSrc": "variable",
"dataSrc": DataSrcOptions.variable,
"itemsExpression": {
"map": [{"var": "repeatingGroup"}, {"var": "name"}]
},
Expand All @@ -138,7 +139,7 @@ def test_variable_options_repeating_group(self):
"itemsExpression": {
"map": [{"var": "repeatingGroup"}, {"var": "name"}]
},
"dataSrc": "variable",
"dataSrc": DataSrcOptions.variable,
},
},
]
Expand Down Expand Up @@ -189,7 +190,7 @@ def test_variable_options_repeating_group_empty_data(self):
"values": [
{"label": "", "value": ""},
],
"dataSrc": "variable",
"dataSrc": DataSrcOptions.variable,
"data": {
"itemsExpression": {
"map": [{"var": "repeatingGroup"}, {"var": "name"}]
Expand All @@ -203,7 +204,7 @@ def test_variable_options_repeating_group_empty_data(self):
"values": [
{"label": "", "value": ""},
],
"dataSrc": "variable",
"dataSrc": DataSrcOptions.variable,
"itemsExpression": {
"map": [{"var": "repeatingGroup"}, {"var": "name"}]
},
Expand All @@ -217,7 +218,7 @@ def test_variable_options_repeating_group_empty_data(self):
"values": [
{"label": "", "value": ""},
],
"dataSrc": "variable",
"dataSrc": DataSrcOptions.variable,
"data": {
"itemsExpression": {
"map": [{"var": "repeatingGroup"}, {"var": "name"}]
Expand Down Expand Up @@ -264,7 +265,7 @@ def test_variable_options_multiple_component(self):
{"label": "", "value": ""},
],
"openForms": {
"dataSrc": "variable",
"dataSrc": DataSrcOptions.variable,
"itemsExpression": {"var": "textField"},
},
},
Expand All @@ -277,7 +278,7 @@ def test_variable_options_multiple_component(self):
],
},
"openForms": {
"dataSrc": "variable",
"dataSrc": DataSrcOptions.variable,
"itemsExpression": {"var": "textField"},
},
"type": "select",
Expand All @@ -290,7 +291,7 @@ def test_variable_options_multiple_component(self):
{"label": "", "value": ""},
],
"openForms": {
"dataSrc": "variable",
"dataSrc": DataSrcOptions.variable,
"itemsExpression": {"var": "textField"},
},
},
Expand Down Expand Up @@ -342,7 +343,7 @@ def test_variable_options_multiple_empty_data(self):
"values": [
{"label": "", "value": ""},
],
"dataSrc": "variable",
"dataSrc": DataSrcOptions.variable,
"data": {
"itemsExpression": {"var": "textField"},
},
Expand All @@ -354,7 +355,7 @@ def test_variable_options_multiple_empty_data(self):
"values": [
{"label": "", "value": ""},
],
"dataSrc": "variable",
"dataSrc": DataSrcOptions.variable,
"itemsExpression": {"var": "textField"},
},
"type": "select",
Expand All @@ -366,7 +367,7 @@ def test_variable_options_multiple_empty_data(self):
"values": [
{"label": "", "value": ""},
],
"dataSrc": "variable",
"dataSrc": DataSrcOptions.variable,
"data": {
"itemsExpression": {"var": "textField"},
},
Expand Down Expand Up @@ -411,7 +412,7 @@ def test_variable_options_repeating_group_missing_map(self):
{"label": "", "value": ""},
],
"openForms": {
"dataSrc": "variable",
"dataSrc": DataSrcOptions.variable,
"itemsExpression": {
"var": "repeatingGroup"
}, # No map operation to transform dict into str
Expand All @@ -426,7 +427,7 @@ def test_variable_options_repeating_group_missing_map(self):
],
},
"openForms": {
"dataSrc": "variable",
"dataSrc": DataSrcOptions.variable,
"itemsExpression": {
"var": "repeatingGroup"
}, # No map operation to transform dict into str
Expand All @@ -444,7 +445,7 @@ def test_variable_options_repeating_group_missing_map(self):
"itemsExpression": {
"var": "repeatingGroup"
}, # No map operation to transform dict into str
"dataSrc": "variable",
"dataSrc": DataSrcOptions.variable,
},
},
]
Expand Down Expand Up @@ -506,7 +507,7 @@ def test_escaped_html(self):
{"label": "", "value": ""},
],
"openForms": {
"dataSrc": "variable",
"dataSrc": DataSrcOptions.variable,
"itemsExpression": {"var": "textField"},
},
},
Expand Down Expand Up @@ -547,7 +548,7 @@ def test_wrong_type_variable(self):
{"label": "", "value": ""},
],
"openForms": {
"dataSrc": "variable",
"dataSrc": DataSrcOptions.variable,
"itemsExpression": {"var": "textField"},
},
},
Expand Down Expand Up @@ -593,7 +594,7 @@ def test_duplicate_options_with_multiple_field(self):
{"label": "", "value": ""},
],
"openForms": {
"dataSrc": "variable",
"dataSrc": DataSrcOptions.variable,
"itemsExpression": {"var": "textField"},
},
},
Expand Down Expand Up @@ -628,7 +629,7 @@ def test_duplicate_options_with_repeating_group(self):
{"label": "", "value": ""},
],
"openForms": {
"dataSrc": "variable",
"dataSrc": DataSrcOptions.variable,
"itemsExpression": {
"map": [{"var": "repeatingGroup"}, {"var": "name"}]
},
Expand Down Expand Up @@ -660,7 +661,7 @@ def test_badly_formatted_items(self):
{"label": "", "value": ""},
],
"openForms": {
"dataSrc": "variable",
"dataSrc": DataSrcOptions.variable,
"itemsExpression": {
"map": [{"var": "externalData"}, {"var": "id"}]
},
Expand Down Expand Up @@ -722,7 +723,7 @@ def test_different_label_key_options(self):
{"label": "", "value": ""},
],
"openForms": {
"dataSrc": "variable",
"dataSrc": DataSrcOptions.variable,
"itemsExpression": {
"map": [
{"var": "repeatingGroup"},
Expand Down Expand Up @@ -781,7 +782,7 @@ def test_overwrite_html_in_content_component(self):
<div>
<style nonce="my-malicious-and-bad-nonce"></style>
<script>alert('xss')</script>
</div>
</div>
""",
},
{
Expand Down
Loading

0 comments on commit 58b3212

Please sign in to comment.