Skip to content

Commit

Permalink
⚰️ [#5058] Clean up code that has become obsolete
Browse files Browse the repository at this point in the history
We can express everything now in terms of synchronize_for form_definition.
  • Loading branch information
sergei-maertens committed Jan 31, 2025
1 parent d86a1bb commit d1fdb80
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 69 deletions.
64 changes: 1 addition & 63 deletions src/openforms/forms/models/form_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from django.db.models import CheckConstraint, Q
from django.utils.translation import gettext_lazy as _

from attr import attributes
from glom import Path, glom
from glom import glom

from openforms.formio.utils import (
component_in_editgrid,
Expand All @@ -31,7 +30,6 @@

if TYPE_CHECKING:
from .form import Form
from .form_step import FormStep


EMPTY_PREFILL_PLUGIN = Q(prefill_plugin="")
Expand All @@ -50,66 +48,6 @@ def create_for_form(self, form: "Form") -> None:
for form_step in form_steps:
self.synchronize_for(form_step.form_definition)

def create_for_formstep(self, form_step: FormStep) -> list[FormVariable]:
form_definition_configuration = form_step.form_definition.configuration
component_keys = [
component["key"]
for component in iter_components(
configuration=form_definition_configuration, recursive=True
)
]
existing_form_variables_keys = form_step.form.formvariable_set.filter(
key__in=component_keys,
form_definition=form_step.form_definition,
).values_list("key", flat=True)

form_variables = []
for component in iter_components(
configuration=form_definition_configuration, recursive=True
):
if (
is_layout_component(component)
or component["type"] in ("content", "softRequiredErrors")
or component["key"] in existing_form_variables_keys
or component_in_editgrid(form_definition_configuration, component)
):
continue

form_variables.append(
self.model(
form=form_step.form,
form_definition=form_step.form_definition,
prefill_plugin=glom(
component,
Path("prefill", "plugin"),
default="",
skip_exc=KeyError,
)
or "",
prefill_attribute=glom(
component,
Path("prefill", "attribute"),
default="",
skip_exc=KeyError,
)
or "",
prefill_identifier_role=glom(
component,
Path("prefill", "identifierRole"),
default=IdentifierRoles.main,
skip_exc=KeyError,
),
key=component["key"],
name=component.get("label") or component["key"],
is_sensitive_data=component.get("isSensitiveData", False),
source=FormVariableSources.component,
data_type=get_component_datatype(component),
initial_value=get_component_default_value(component),
)
)

return self.bulk_create(form_variables)

@transaction.atomic
def synchronize_for(self, form_definition: FormDefinition):
"""
Expand Down
4 changes: 2 additions & 2 deletions src/openforms/forms/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,14 @@ def create(
**kwargs,
) -> FormStep:
form_step = super().create(**kwargs)
FormVariable.objects.create_for_formstep(form_step)
FormVariable.objects.synchronize_for(form_step.form_definition)
return form_step

@classmethod
def _create(cls, *args, **kwargs):
# This method is called instead of create() from the FormFactory with `generte_minimal_setup`
form_step = super()._create(*args, **kwargs)
FormVariable.objects.create_for_formstep(form_step)
FormVariable.objects.synchronize_for(form_step.form_definition)
return form_step


Expand Down
10 changes: 6 additions & 4 deletions src/openforms/forms/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,10 @@ class RegressionTests(HypothesisTestCase):
)
@tag("gh-3922")
def test_copy_form_with_corrupt_prefill(self, component_type):
form = FormFactory.create(
generate_minimal_setup=True,
formstep__form_definition__configuration={
# bypass the factories since those enforce DB constraints
form = FormFactory.create()
fd = FormDefinitionFactory.create(
configuration={
"components": [
{
"type": component_type,
Expand All @@ -313,8 +314,9 @@ def test_copy_form_with_corrupt_prefill(self, component_type):
},
}
]
},
}
)
FormStep.objects.create(form=form, form_definition=fd, order=0)

form.copy()

Expand Down

0 comments on commit d1fdb80

Please sign in to comment.