Skip to content

Commit

Permalink
Merge pull request #4307 from open-formulieren/fix/1906-duplicates-in…
Browse files Browse the repository at this point in the history
…-admin-form-definition-list-after-import

[#1906] Fix duplicate form definitions after importing a form
  • Loading branch information
sergei-maertens authored May 22, 2024
2 parents 21bbe45 + 04b1957 commit 9232522
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/openforms/conf/locale/nl/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ msgid ""
msgstr ""
"Project-Id-Version: Open Forms\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-05-21 10:26+0200\n"
"POT-Creation-Date: 2024-05-21 13:43+0200\n"
"PO-Revision-Date: 2024-05-17 15:22+0200\n"
"Last-Translator: Sergei Maertens <sergei+local@maykinmedia.nl>\n"
"Language-Team: Dutch <support@maykinmedia.nl>\n"
Expand Down Expand Up @@ -4920,11 +4920,11 @@ msgstr "Form.io configuratie"
msgid "The form definition as Form.io JSON schema"
msgstr "De formulier definitie als Form.io JSON schema"

#: openforms/forms/api/serializers/form_definition.py:168
#: openforms/forms/api/serializers/form_definition.py:172
msgid "Used in forms"
msgstr "Gebruikt in formulieren"

#: openforms/forms/api/serializers/form_definition.py:170
#: openforms/forms/api/serializers/form_definition.py:174
msgid ""
"The collection of forms making use of this definition. This includes both "
"active and inactive forms."
Expand Down
12 changes: 8 additions & 4 deletions src/openforms/forms/api/serializers/form_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,14 @@ def to_representation(self, instance):
# for the dynamic formio configuration in the context of a submission.
# 2. The serializers/API endpoints of :module:`openforms.forms.api` for
# 'standalone' use/introspection.
rewrite_formio_components_for_request(
instance.configuration_wrapper,
request=self.context["request"],
)
is_export = self.context.get("is_export", False)

if not is_export:
rewrite_formio_components_for_request(
instance.configuration_wrapper,
request=self.context["request"],
)

representation["configuration"] = instance.configuration_wrapper.configuration

return representation
Expand Down
61 changes: 61 additions & 0 deletions src/openforms/forms/tests/test_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,43 @@ def test_export(self):
FormVariableSources.user_defined, form_variables[0]["source"]
)

@tag("gh-1906")
def test_export_reusable_form_definition_uuid_already_exists(self):
form = FormFactory.create()
form_definition = FormDefinitionFactory.create(
configuration={"components": [{"key": "test-key", "type": "file"}]},
is_reusable=True,
)
FormStepFactory.create(form=form, form_definition=form_definition)

call_command("export", form.pk, self.filepath)

with zipfile.ZipFile(self.filepath, "r") as f:
self.assertEqual(
f.namelist(),
[
"forms.json",
"formSteps.json",
"formDefinitions.json",
"formLogic.json",
"formVariables.json",
f"{EXPORT_META_KEY}.json",
],
)

form_definitions = json.loads(f.read("formDefinitions.json"))
self.assertEqual(len(form_definitions), 1)
self.assertEqual(form_definitions[0]["uuid"], str(form_definition.uuid))
self.assertEqual(form_definitions[0]["name"], form_definition.name)
self.assertEqual(
form_definitions[0]["internal_name"], form_definition.internal_name
)
self.assertEqual(form_definitions[0]["slug"], form_definition.slug)
self.assertEqual(
form_definitions[0]["configuration"],
form_definition.configuration,
)

def test_import(self):
product = ProductFactory.create()
merchant = OgoneMerchantFactory.create()
Expand Down Expand Up @@ -438,6 +475,30 @@ def test_import_form_definition_uuid_already_exists_configuration_different(self
self.assertNotEqual(form_logic_2.pk, form_logic_pk)
self.assertEqual(imported_form.pk, form_logic_2.form.pk)

@tag("gh-1906")
def test_import_reusable_form_definition_uuid_already_exists(self):
form = FormFactory.create()
form_definition = FormDefinitionFactory.create(
configuration={"components": [{"key": "test-key", "type": "file"}]},
is_reusable=True,
)
FormStepFactory.create(form=form, form_definition=form_definition)

call_command("export", form.pk, self.filepath)

call_command("import", import_file=self.filepath)

form_definitions = FormDefinition.objects.all()
fd2 = form_definitions.last()
self.assertEqual(form_definitions.count(), 1)
self.assertEqual(fd2.pk, form_definition.pk)
self.assertEqual(fd2.uuid, form_definition.uuid)
self.assertEqual(fd2.configuration, form_definition.configuration)
self.assertEqual(fd2.login_required, form_definition.login_required)
self.assertEqual(fd2.name, form_definition.name)
self.assertEqual(fd2.internal_name, form_definition.internal_name)
self.assertEqual(fd2.slug, form_definition.slug)

def test_import_form_with_category(self):
"""
Assert that the category reference is ignored during import.
Expand Down
2 changes: 1 addition & 1 deletion src/openforms/forms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def form_to_json(form_id: int) -> dict:
form_definitions = FormDefinitionSerializer(
instance=form_definitions,
many=True,
context={"request": request},
context={"request": request, "is_export": True},
).data
form_steps = FormStepSerializer(
instance=form_steps, many=True, context={"request": request}
Expand Down

0 comments on commit 9232522

Please sign in to comment.