From b15f080399f62c5c492f9b529ce70ffbe933c356 Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Fri, 25 Oct 2024 17:56:51 +0200 Subject: [PATCH] :art: [#4396] Only create the variables that are affected by prefill --- .../models/submission_value_variable.py | 7 +++--- .../tests/test_start_submission.py | 23 ++++++++----------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/openforms/submissions/models/submission_value_variable.py b/src/openforms/submissions/models/submission_value_variable.py index 859eee8e9c..b5ca07acae 100644 --- a/src/openforms/submissions/models/submission_value_variable.py +++ b/src/openforms/submissions/models/submission_value_variable.py @@ -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: """ diff --git a/src/openforms/submissions/tests/test_start_submission.py b/src/openforms/submissions/tests/test_start_submission.py index 29d3f157d0..ec46588ad2 100644 --- a/src/openforms/submissions/tests/test_start_submission.py +++ b/src/openforms/submissions/tests/test_start_submission.py @@ -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 @@ -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}",