Skip to content

Commit

Permalink
🐛 [#3283] Display the submission registration_backend in the admin
Browse files Browse the repository at this point in the history
The submission admin display used to show the first registration_backend of the form. Due to form logic, the registration_backend on the submission could be different.

The admin now shows the exact registration_backend that was used by the submission
  • Loading branch information
robinmolen committed Dec 5, 2024
1 parent 08dfdbc commit af94e5c
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/openforms/submissions/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def successfully_processed(self, obj) -> bool | None:

@admin.display(description=_("Registration backend"))
def get_registration_backend(self, obj):
return backend if (backend := obj.form.registration_backends.first()) else "-"
return obj.registration_backend or "-"

@admin.display(description=_("Appointment status"))
def get_appointment_status(self, obj):
Expand Down
122 changes: 120 additions & 2 deletions src/openforms/submissions/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,24 @@
from maykin_2fa.test import disable_admin_mfa

from openforms.accounts.tests.factories import UserFactory
from openforms.forms.tests.factories import FormVariableFactory
from openforms.forms.tests.factories import (
FormFactory,
FormLogicFactory,
FormRegistrationBackendFactory,
FormStepFactory,
FormVariableFactory,
)
from openforms.logging.logevent import submission_start
from openforms.logging.models import TimelineLogProxy
from openforms.submissions.models.submission import Submission
from openforms.variables.constants import FormVariableDataTypes

from ...config.models import GlobalConfiguration
from ...forms.constants import LogicActionTypes
from ..admin import SubmissionAdmin, SubmissionTimeListFilter
from ..constants import PostSubmissionEvents, RegistrationStatuses
from .factories import SubmissionFactory
from ..form_logic import evaluate_form_logic
from .factories import SubmissionFactory, SubmissionStepFactory


@disable_admin_mfa()
Expand Down Expand Up @@ -248,6 +256,116 @@ def test_change_view_with_broken_price_variable_config(self):

self.assertEqual(change_page.status_code, 200)

def test_changing_registration_backend_with_form_logic_is_correctly_displayed_in_admin(
self,
):
form = FormFactory.create()
email = FormRegistrationBackendFactory.create(
form=form,
backend="email",
key="email",
name="Email",
)
objects_api = FormRegistrationBackendFactory.create(
form=form,
backend="objects_api",
key="objects_api",
name="Objects api",
)
form_step = FormStepFactory.create(
form=form,
form_definition__configuration={
"components": [
{
"type": "textfield",
"key": "text1",
}
]
},
)
FormLogicFactory.create(
form=form,
json_logic_trigger={"==": [{"var": "text1"}, "trigger-rule"]},
actions=[
{
"action": {
"type": LogicActionTypes.set_registration_backend,
"value": f"{objects_api.key}",
},
},
],
)

with self.subTest(
"Submission doesn't trigger logic and uses the default registration backend"
):
# Without triggering the form logic, the submission should use
# the first backend registration of the form
submission = SubmissionFactory.create(form=form, completed=True)
submission_step = SubmissionStepFactory.create(
submission=submission, form_step=form_step, data={"text1": "test"}
)

# Evaluate the logic, and save the changes
evaluate_form_logic(submission, submission_step, submission.data)
submission.save()

change_url = reverse(
"admin:submissions_submission_change",
kwargs={"object_id": submission.pk},
)

change_page = self.client.get(change_url)
self.assertEqual(change_page.status_code, 200)

# The email registration should be used
self.assertEqual(submission.registration_backend, email)

# The admin page should show the email registration as the one being used
registration_backend_field = change_page.pyquery.find(
".form-row.field-get_registration_backend > div > div"
)
self.assertEqual(
registration_backend_field.text(),
f"Registratie backend:\nEmail van {form.name}",
)

with self.subTest(
"Submission that does trigger the logic and gets the logic defined backend registration"
):
# When triggering the form logic, the submission should use
# the objects_api backend registration of the form
submission = SubmissionFactory.create(form=form, completed=True)
submission_step = SubmissionStepFactory.create(
submission=submission,
form_step=form_step,
data={"text1": "trigger-rule"},
)

# Evaluate the logic, and save the changes
evaluate_form_logic(submission, submission_step, submission.data)
submission.save()

change_url = reverse(
"admin:submissions_submission_change",
kwargs={"object_id": submission.pk},
)

change_page = self.client.get(change_url)
self.assertEqual(change_page.status_code, 200)

# The objects api registration should be used
self.assertEqual(submission.registration_backend, objects_api)

# The admin page should show the objects api registration as the one being used
registration_backend_field = change_page.pyquery.find(
".form-row.field-get_registration_backend > div > div"
)
self.assertEqual(
registration_backend_field.text(),
f"Registratie backend:\nObjects api van {form.name}",
)


class TestSubmissionTimeListFilterAdmin(TestCase):
def test_time_filtering(self):
Expand Down

0 comments on commit af94e5c

Please sign in to comment.