Skip to content

Commit

Permalink
🎨 [#4396] Only create the variables that are affected by prefill
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-maertens committed Oct 25, 2024
1 parent 881af98 commit b15f080
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
7 changes: 4 additions & 3 deletions src/openforms/submissions/models/submission_value_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,16 @@ def save_prefill_data(self, data: dict[str, Any]) -> None:
# variables at this point (the previous implementation with
# self.get_prefill_variables() gave us access to the component variables
# and not the user_defined ones).
variables = self.variables.values()
for variable in list(variables):
variables_to_create: list[SubmissionValueVariable] = []
for variable in self.variables.values():
if variable.key not in data:
continue

variable.value = data[variable.key]
variable.source = SubmissionValueVariableSources.prefill
variables_to_create.append(variable)

SubmissionValueVariable.objects.bulk_create(variables)
SubmissionValueVariable.objects.bulk_create(variables_to_create)

def set_values(self, data: DataMapping) -> None:
"""
Expand Down
23 changes: 9 additions & 14 deletions src/openforms/submissions/tests/test_start_submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
from rest_framework.test import APITestCase

from openforms.authentication.service import FORM_AUTH_SESSION_KEY, AuthAttribute
from openforms.forms.tests.factories import FormFactory, FormStepFactory
from openforms.forms.tests.factories import (
FormFactory,
FormStepFactory,
FormVariableFactory,
)

from ..constants import SUBMISSIONS_SESSION_KEY, SubmissionValueVariableSources
from ..models import Submission, SubmissionValueVariable
Expand Down Expand Up @@ -199,20 +203,11 @@ def test_start_submission_bad_form_url(self):

@patch("openforms.logging.logevent._create_log")
def test_start_submission_with_prefill(self, mock_logevent):
# we are creating a new step below-no need for two steps
self.form.formstep_set.get().delete()
FormStepFactory.create(
FormVariableFactory.create(
form=self.form,
form_definition__configuration={
"components": [
{
"type": "textfield",
"key": "test-key",
"label": "Test label",
"prefill": {"plugin": "demo", "attribute": "random_string"},
}
]
},
form_definition=self.step.form_definition,
prefill_plugin="demo",
prefill_attribute="random_string",
)
body = {
"form": f"http://testserver.com{self.form_url}",
Expand Down

0 comments on commit b15f080

Please sign in to comment.