Skip to content

Commit

Permalink
[#3718] Drop component_translations field from FormDefinition
Browse files Browse the repository at this point in the history
We removed the 'component_translations' field since it's deprecated. The
new formiobuilder handles the translations per component. The field is
still present in the FormDefinitionSerializer since we need it during import
of older forms, to process legacy format component translations.
  • Loading branch information
vaszig committed Mar 13, 2024
1 parent 78dea0c commit 2d5943c
Show file tree
Hide file tree
Showing 28 changed files with 349 additions and 275 deletions.
1 change: 0 additions & 1 deletion src/openforms/formio/rendering/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ def get_children(self) -> Iterator["ComponentNode"]:

def __post_init__(self):
self._base_label = self.component.get("groupLabel", self.default_label)
super().__post_init__()

def apply_to_labels(self, f: Callable[[str], str]) -> None:
super().apply_to_labels(f)
Expand Down
25 changes: 1 addition & 24 deletions src/openforms/formio/rendering/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from openforms.submissions.rendering.constants import RenderModes
from openforms.typing import DataMapping

from ..service import format_value, translate_function
from ..service import format_value
from ..typing import Component
from ..utils import (
is_layout_component,
Expand Down Expand Up @@ -50,7 +50,6 @@ class ComponentNode(Node):
"" # Path in the configuration tree, matching the path obtained with openforms/formio/utils.py `flatten_by_path`
)
parent_node: Node | None = None
translate: Callable[[str], str] | None = None

@staticmethod
def build_node(
Expand All @@ -62,7 +61,6 @@ def build_node(
configuration_path: str = "",
depth: int = 0,
parent_node: Node | None = None,
translate: Callable[[str], str] | None = None,
) -> "ComponentNode":
"""
Instantiate the most specific node type for a given component type.
Expand All @@ -80,17 +78,9 @@ def build_node(
json_renderer_path=json_renderer_path,
configuration_path=configuration_path,
parent_node=parent_node,
translate=translate,
)
return nested_node

def __post_init__(self):
# Value Formatters have no access to the translations; run all our
# labels through the translations, before further processing of logic
# etc.
if self.translate:
self.apply_to_labels(self.translate)

@property
def is_visible(self) -> bool:
"""
Expand Down Expand Up @@ -298,18 +288,6 @@ def get_children(self) -> Iterator[ComponentNode]:
assert self.step.form_step # nosec: intended use of B101
configuration = self.step.form_step.form_definition.configuration

# prepare for any potential translations to component definitions. The translate
# function is (for now) bound to the form step, as that's where the translations
# are stored. Possibly in the future we will store those in the component itself,
# but then the need to translate upfront is also gone as the value formatters do
# receive the component definition.
i18n_enabled = self.renderer.form.translation_enabled
do_translate = (
translate_function(self.renderer.submission, self.step)
if i18n_enabled
else None
)

for configuration_path, component in iterate_components_with_configuration_path(
configuration, recursive=False
):
Expand All @@ -318,6 +296,5 @@ def get_children(self) -> Iterator[ComponentNode]:
component=component,
renderer=self.renderer,
configuration_path=configuration_path,
translate=do_translate,
)
yield from child_node
18 changes: 2 additions & 16 deletions src/openforms/formio/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
submodules and only their 'public' API should be imported and used.
"""

from typing import Any, Callable
from typing import Any

import elasticapm
from rest_framework.request import Request

from openforms.prefill import inject_prefill
from openforms.submissions.models import Submission, SubmissionStep
from openforms.submissions.models import Submission
from openforms.typing import DataMapping

from .datastructures import FormioConfigurationWrapper, FormioData
Expand Down Expand Up @@ -60,20 +60,6 @@ def normalize_value_for_component(component: Component, value: Any) -> Any:
return register.normalize(component, value)


def translate_function(
submission: Submission,
step: SubmissionStep,
) -> Callable[[str], str]:
"Return a translate function for FormIO strings"

assert step.form_step # nosec: intended use of B101
lang = submission.language_code

translations = step.form_step.form_definition.component_translations.get(lang, {})

return lambda s: translations.get(s) or s


@elasticapm.capture_span(span_type="app.formio")
def get_dynamic_configuration(
config_wrapper: FormioConfigurationWrapper,
Expand Down
20 changes: 3 additions & 17 deletions src/openforms/formio/variables.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Callable, Iterator
from typing import Iterator

from django.template import TemplateSyntaxError

Expand Down Expand Up @@ -62,9 +62,7 @@ def validate_configuration(configuration: JSONObject) -> dict[str, str]:


def inject_variables(
configuration: FormioConfigurationWrapper,
values: DataMapping,
translate: Callable[[str], str] = str,
configuration: FormioConfigurationWrapper, values: DataMapping
) -> None:
"""
Inject the variable values into the Formio configuration.
Expand All @@ -88,20 +86,8 @@ def inject_variables(
continue

match property_value:
case str():
property_value = translate(property_value)
case [str(), *_]:
property_value = [
translate(s) for s in property_value if isinstance(s, str)
]
case [{"label": _}, *_]:
for item in property_value:
if "label" in item:
item["label"] = translate(item["label"])
case {"values": [*defined_values]}:
for item in defined_values:
if "label" in item:
item["label"] = translate(item["label"])
property_value = [s for s in property_value if isinstance(s, str)]

try:
templated_value = render(property_value, values)
Expand Down
4 changes: 2 additions & 2 deletions src/openforms/forms/api/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class IgnoreConfigurationFieldCamelCaseJSONParser(CamelCaseJSONParser):
# variant can sometimes overwrite the camelCase variant, which breaks the pre-fill
# functionality. This can happen because JSON objects DO NOT HAVE inherent ordering
# and the spec is non-deterministic.
json_underscoreize = {"ignore_fields": ("configuration", "component_translations")}
json_underscoreize = {"ignore_fields": ("configuration",)}


class FormVariableJSONParser(CamelCaseJSONParser):
Expand Down Expand Up @@ -42,7 +42,7 @@ class IgnoreConfigurationFieldCamelCaseJSONRenderer(CamelCaseJSONRenderer):
# This is needed for fields in the JSON configuration that have an underscore
# For example: time_24hr in the date component. See github issue
# https://github.com/open-formulieren/open-forms/issues/1255
json_underscoreize = {"ignore_fields": ("configuration", "component_translations")}
json_underscoreize = {"ignore_fields": ("configuration",)}


class FormCamelCaseJSONParser(FormCamelCaseMixin, CamelCaseJSONParser):
Expand Down
8 changes: 8 additions & 0 deletions src/openforms/forms/api/serializers/form_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ def validate(self, attrs):

return attrs

def save(self, **kwargs):
# field 'component_translations' has been deprecated and it's not part of
# the 'form_definition' model anymore. We use it only in the serializer.
if "component_translations" in self.validated_data:
del self.validated_data["component_translations"]

return super().save(**kwargs)


class FormDefinitionDetailSerializer(FormDefinitionSerializer):
used_in = UsedInFormSerializer(
Expand Down
8 changes: 1 addition & 7 deletions src/openforms/forms/forms/form_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,5 @@ class Meta:
"login_required",
"is_reusable",
"configuration",
"component_translations",
)
widgets = {
"uuid": forms.TextInput(attrs={"readonly": True}),
"component_translations": forms.HiddenInput(
attrs={"class": "form-builder__component-translations"}
),
}
widgets = {"uuid": forms.TextInput(attrs={"readonly": True})}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.10 on 2024-03-12 14:41

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("forms", "0093_fix_prefill_bis"),
]

operations = [
migrations.RemoveField(
model_name="formdefinition",
name="component_translations",
),
]
7 changes: 0 additions & 7 deletions src/openforms/forms/models/form_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ class FormDefinition(models.Model):
help_text=_("The total number of Formio components used in the configuration"),
)

component_translations = models.JSONField(
verbose_name=_("Component translations"),
help_text=_("Translations for literals used in components"),
blank=True,
default=dict,
)

class Meta:
verbose_name = _("Form definition")
verbose_name_plural = _("Form definitions")
Expand Down
1 change: 0 additions & 1 deletion src/openforms/forms/tests/e2e_tests/test_form_designer.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ def assertState():
textfield["openForms"]["translations"]["nl"]["label"],
"Veldlabel",
)
self.assertEqual(fd.component_translations, {})

await assertState()

Expand Down
Loading

0 comments on commit 2d5943c

Please sign in to comment.