Skip to content

Commit

Permalink
👌 [#3005] PR Feedback part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
SilviaAmAm committed Nov 29, 2023
1 parent 146f7ff commit 56ee8d9
Show file tree
Hide file tree
Showing 28 changed files with 531 additions and 288 deletions.
5 changes: 5 additions & 0 deletions src/openforms/appointments/tasks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import warnings

from celery_once import QueueOnce

Expand Down Expand Up @@ -31,6 +32,10 @@ def maybe_register_appointment(submission_id: int) -> None | str:
be stored in the database. If appointment registration fails, this feedback
should find its way back to the end-user.
"""
warnings.warn(
"This task is deprecated because of the new appointment flow.",
PendingDeprecationWarning,
)
logger.info("Registering appointment for submission %d (if needed!)", submission_id)
submission = Submission.objects.select_related("form").get(id=submission_id)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from openforms.config.models import GlobalConfiguration
from openforms.forms.constants import SubmissionAllowedChoices
from openforms.submissions.constants import PostSubmissionEvents
from openforms.submissions.tests.factories import SubmissionFactory
from openforms.submissions.tests.mixins import SubmissionsMixin
from openforms.tests.search_strategies import json_values
Expand Down Expand Up @@ -147,7 +148,9 @@ def test_submission_is_completed(self, mock_on_completion):
self.assertIn("statusUrl", response.json())

# assert that the async celery task execution is scheduled
mock_on_completion.assert_called_once_with(self.submission.id)
mock_on_completion.assert_called_once_with(
self.submission.id, PostSubmissionEvents.on_completion
)

def test_retry_does_not_cause_integrity_error(self):
# When there are on_completion processing errors, the client will re-post the
Expand Down
3 changes: 2 additions & 1 deletion src/openforms/appointments/tests/test_confirmation_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from unittest.mock import patch

from django.core import mail
from django.test import TestCase
from django.test import TestCase, override_settings
from django.utils import timezone
from django.utils.html import escape
from django.utils.translation import gettext as _
Expand Down Expand Up @@ -94,6 +94,7 @@ def get_required_customer_fields(self, *args, **kwargs) -> list[Component]:
return [LAST_NAME, EMAIL]


@override_settings(CELERY_TASK_ALWAYS_EAGER=True)
class AppointmentCreationConfirmationMailTests(HTMLAssertMixin, TestCase):
@classmethod
def setUpTestData(cls):
Expand Down
9 changes: 7 additions & 2 deletions src/openforms/payments/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.test.client import RequestFactory

from openforms.config.models import GlobalConfiguration
from openforms.submissions.constants import PostSubmissionEvents
from openforms.submissions.tests.factories import SubmissionFactory

from ..base import BasePlugin, PaymentInfo
Expand Down Expand Up @@ -92,7 +93,9 @@ def test_views(self, on_post_submission_event_mock):
self.assertEqual(response.content, b"")
self.assertEqual(response.status_code, 302)

on_post_submission_event_mock.assert_called_once_with(submission.id)
on_post_submission_event_mock.assert_called_once_with(
submission.id, PostSubmissionEvents.on_payment_complete
)

with self.subTest("return bad method"):
on_post_submission_event_mock.reset_mock()
Expand Down Expand Up @@ -145,7 +148,9 @@ def test_views(self, on_post_submission_event_mock):
self.assertEqual(response.content, b"")
self.assertEqual(response.status_code, 200)

on_post_submission_event_mock.assert_called_once_with(submission.id)
on_post_submission_event_mock.assert_called_once_with(
submission.id, PostSubmissionEvents.on_payment_complete
)

with self.subTest("webhook bad method"):
on_post_submission_event_mock.reset_mock()
Expand Down
11 changes: 9 additions & 2 deletions src/openforms/payments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from openforms.api.serializers import ExceptionSerializer
from openforms.api.views import ERR_CONTENT_TYPE
from openforms.logging import logevent
from openforms.submissions.constants import PostSubmissionEvents
from openforms.submissions.models import Submission
from openforms.submissions.tasks import on_post_submission_event
from openforms.utils.redirect import allow_redirect_url
Expand Down Expand Up @@ -235,7 +236,11 @@ def _handle_return(self, request, uuid: str):
)
raise ParseError(detail="redirect not allowed")

transaction.on_commit(lambda: on_post_submission_event(payment.submission.pk))
transaction.on_commit(
lambda: on_post_submission_event(
payment.submission.pk, PostSubmissionEvents.on_payment_complete
)
)

return response

Expand Down Expand Up @@ -313,7 +318,9 @@ def _handle_webhook(self, request, *args, **kwargs):
if payment:
logevent.payment_flow_webhook(payment, plugin)
transaction.on_commit(
lambda: on_post_submission_event(payment.submission.pk)
lambda: on_post_submission_event(
payment.submission.pk, PostSubmissionEvents.on_payment_complete
)
)

return HttpResponse("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from openforms.config.models import GlobalConfiguration
from openforms.forms.tests.factories import FormRegistrationBackendFactory
from openforms.submissions.constants import RegistrationStatuses
from openforms.submissions.constants import PostSubmissionEvents, RegistrationStatuses
from openforms.submissions.tasks import on_post_submission_event
from openforms.submissions.tests.factories import SubmissionFactory
from stuf.stuf_zds.models import StufZDSConfig
Expand Down Expand Up @@ -115,7 +115,7 @@ def test_zaak_creation_fails_number_is_reserved(self):
additional_matcher=match_text("zakLk01"),
)

on_post_submission_event(self.submission.id)
on_post_submission_event(self.submission.id, PostSubmissionEvents.on_completion)
with self.subTest("Initial registration fails"):
self.submission.refresh_from_db()
self.assertEqual(
Expand Down Expand Up @@ -192,7 +192,7 @@ def test_doc_id_creation_fails_zaak_registration_succeeds(self):
additional_matcher=match_text("genereerDocumentIdentificatie_Di02"),
)

on_post_submission_event(self.submission.id)
on_post_submission_event(self.submission.id, PostSubmissionEvents.on_completion)
with self.subTest("Document id generation fails"):
self.submission.refresh_from_db()
self.assertEqual(
Expand Down Expand Up @@ -288,7 +288,7 @@ def test_doc_creation_failure_does_not_create_more_doc_ids(self):
additional_matcher=match_text("edcLk01"),
)

on_post_submission_event(self.submission.id)
on_post_submission_event(self.submission.id, PostSubmissionEvents.on_completion)
with self.subTest("Document id generation fails"):
self.submission.refresh_from_db()
self.assertEqual(
Expand Down
18 changes: 13 additions & 5 deletions src/openforms/registrations/contrib/zgw_apis/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from zgw_consumers.test import generate_oas_component

from openforms.authentication.tests.factories import RegistratorInfoFactory
from openforms.submissions.constants import RegistrationStatuses
from openforms.submissions.constants import PostSubmissionEvents, RegistrationStatuses
from openforms.submissions.models import SubmissionStep
from openforms.submissions.tasks import on_post_submission_event, pre_registration
from openforms.submissions.tests.factories import (
Expand Down Expand Up @@ -1469,7 +1469,9 @@ def test_failure_after_zaak_creation(self):
)

with self.subTest("Initial document creation fails"):
on_post_submission_event(self.submission.id)
on_post_submission_event(
self.submission.id, PostSubmissionEvents.on_completion
)

self.submission.refresh_from_db()
assert self.submission.registration_result
Expand All @@ -1484,7 +1486,9 @@ def test_failure_after_zaak_creation(self):

with self.subTest("New document creation attempt does not create zaak again"):
with self.assertRaises(RegistrationFailed):
on_post_submission_event(self.submission.id)
on_post_submission_event(
self.submission.id, PostSubmissionEvents.on_completion
)

self.submission.refresh_from_db()
self.assertEqual(
Expand Down Expand Up @@ -1632,7 +1636,9 @@ def test_attachment_document_create_fails(self):
)

with self.subTest("First try, attachment relation fails"):
on_post_submission_event(self.submission.id)
on_post_submission_event(
self.submission.id, PostSubmissionEvents.on_completion
)

self.submission.refresh_from_db()
assert self.submission.registration_result
Expand Down Expand Up @@ -1670,7 +1676,9 @@ def test_attachment_document_create_fails(self):

with self.subTest("New attempt - ensure existing data is not created again"):
with self.assertRaises(RegistrationFailed):
on_post_submission_event(self.submission.id)
on_post_submission_event(
self.submission.id, PostSubmissionEvents.on_completion
)

self.submission.refresh_from_db()
self.assertEqual(
Expand Down
24 changes: 16 additions & 8 deletions src/openforms/registrations/tests/test_registration_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

from openforms.config.models import GlobalConfiguration
from openforms.forms.models import FormRegistrationBackend
from openforms.submissions.constants import RegistrationStatuses
from openforms.logging.models import TimelineLogProxy
from openforms.submissions.constants import PostSubmissionEvents, RegistrationStatuses
from openforms.submissions.tasks import on_post_submission_event
from openforms.submissions.tests.factories import SubmissionFactory

from ...logging.models import TimelineLogProxy
from ..base import BasePlugin
from ..exceptions import RegistrationFailed
from ..registry import Registry
Expand Down Expand Up @@ -116,7 +116,9 @@ def get_reference_from_result(self, result: dict) -> None:
with patch_registry(model_field, register), self.assertLogs(
level="WARNING"
) as log:
on_post_submission_event(self.submission.id)
on_post_submission_event(
self.submission.id, PostSubmissionEvents.on_completion
)

# check we log a WARNING without stack
error_log = log.records[-2]
Expand Down Expand Up @@ -156,7 +158,9 @@ def get_reference_from_result(self, result: dict) -> None:
with patch_registry(model_field, register), self.assertLogs(
level="ERROR"
) as log:
on_post_submission_event(self.submission.id)
on_post_submission_event(
self.submission.id, PostSubmissionEvents.on_completion
)

# check we log an ERROR with stack
error_log = log.records[-1]
Expand Down Expand Up @@ -268,7 +272,9 @@ def get_reference_from_result(self, result: dict) -> None:
model_field = FormRegistrationBackend._meta.get_field("backend")
with patch_registry(model_field, register):
with self.assertRaises(RegistrationFailed):
on_post_submission_event(self.submission.id)
on_post_submission_event(
self.submission.id, PostSubmissionEvents.on_completion
)

self.submission.refresh_from_db()
self.assertTrue(self.submission.needs_on_completion_retry)
Expand Down Expand Up @@ -313,7 +319,7 @@ def get_reference_from_result(self, result: dict) -> None:
model_field = FormRegistrationBackend._meta.get_field("backend")
with patch_registry(model_field, register):
# first registration won't re-raise RegistrationFailed
on_post_submission_event(submission.id)
on_post_submission_event(submission.id, PostSubmissionEvents.on_completion)

submission.refresh_from_db()
self.assertEqual(
Expand All @@ -326,7 +332,9 @@ def get_reference_from_result(self, result: dict) -> None:
# second and next attempts will re-raise RegistrationFailed for Celery retry
for i in range(2, TEST_NUM_ATTEMPTS + 1):
with self.assertRaises(RegistrationFailed):
on_post_submission_event(submission.id)
on_post_submission_event(
submission.id, PostSubmissionEvents.on_retry
)

submission.refresh_from_db()
self.assertEqual(
Expand All @@ -340,7 +348,7 @@ def get_reference_from_result(self, result: dict) -> None:
self.assertEqual(submission.registration_attempts, TEST_NUM_ATTEMPTS)

# now make more attempts then limit
on_post_submission_event(submission.id)
on_post_submission_event(submission.id, PostSubmissionEvents.on_retry)

submission.refresh_from_db()
self.assertEqual(
Expand Down
6 changes: 3 additions & 3 deletions src/openforms/submissions/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from openforms.submissions.admin_views import LogsEvaluatedLogicView

from ..utils.admin import ReadOnlyAdminMixin
from .constants import IMAGE_COMPONENTS, RegistrationStatuses
from .constants import IMAGE_COMPONENTS, PostSubmissionEvents, RegistrationStatuses
from .exports import ExportFileTypes, export_submissions
from .models import (
Submission,
Expand Down Expand Up @@ -200,7 +200,7 @@ class SubmissionAdmin(admin.ModelAdmin):
"get_appointment_status",
"get_appointment_id",
"get_appointment_error_information",
"on_completion_task_ids",
"post_completion_task_ids",
"confirmation_email_sent",
"registration_attempts",
"pre_registration_completed",
Expand Down Expand Up @@ -338,7 +338,7 @@ def retry_processing_submissions(self, request, queryset):
submissions.update(registration_attempts=0)

for submission in submissions:
on_post_submission_event(submission.id)
on_post_submission_event(submission.id, PostSubmissionEvents.on_retry)

retry_processing_submissions.short_description = _(
"Retry processing %(verbose_name_plural)s."
Expand Down
7 changes: 6 additions & 1 deletion src/openforms/submissions/api/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from openforms.logging import logevent

from ..constants import PostSubmissionEvents
from ..models import Submission
from ..signals import submission_complete
from ..tasks import on_post_submission_event
Expand Down Expand Up @@ -47,7 +48,11 @@ def _complete_submission(self, submission: Submission) -> str:

# after committing the database transaction where the submissions completion is
# stored, start processing the completion.
transaction.on_commit(lambda: on_post_submission_event(submission.pk))
transaction.on_commit(
lambda: on_post_submission_event(
submission.pk, PostSubmissionEvents.on_completion
)
)

token = submission_status_token_generator.make_token(submission)
status_url = self.request.build_absolute_uri(
Expand Down
7 changes: 6 additions & 1 deletion src/openforms/submissions/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from openforms.utils.patches.rest_framework_nested.viewsets import NestedViewSetMixin

from ..attachments import attach_uploads_to_submission_step
from ..constants import PostSubmissionEvents
from ..exceptions import FormDeactivated, FormMaintenance
from ..form_logic import check_submission_logic, evaluate_form_logic
from ..models import Submission, SubmissionStep
Expand Down Expand Up @@ -253,7 +254,11 @@ def cosign(self, request, *args, **kwargs):

remove_submission_from_session(submission, self.request.session)

transaction.on_commit(lambda: on_post_submission_event(submission.id))
transaction.on_commit(
lambda: on_post_submission_event(
submission.id, PostSubmissionEvents.on_cosign_complete
)
)

out_serializer = SubmissionReportUrlSerializer(
instance=submission, context={"request": request}
Expand Down
7 changes: 7 additions & 0 deletions src/openforms/submissions/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,10 @@ class SubmissionValueVariableSources(models.TextChoices):
prefill = "prefill", _("Prefill")
logic = "logic", _("Logic")
dmn = "dmn", _("DMN")


class PostSubmissionEvents(models.TextChoices):
on_completion = "on_completion", _("On completion")
on_payment_complete = "on_payment_complete", _("On payment complete")
on_cosign_complete = "on_cosign_complete", _("On cosign complete")
on_retry = "on_retry", _("On retry")
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from rest_framework.request import Request

from ...constants import ProcessingStatuses
from ...constants import PostSubmissionEvents, ProcessingStatuses
from ...models import Submission
from ...status import SubmissionProcessingStatus
from ...tasks import on_post_submission_event
Expand Down Expand Up @@ -72,7 +72,7 @@ def handle(self, **options):

self.stdout.write(f"Submission: {submission.id}")
self.stdout.write("Entering on_completion flow...")
on_post_submission_event(submission.id)
on_post_submission_event(submission.id, PostSubmissionEvents.on_retry)

submission.refresh_from_db()
request = RequestFactory().get("/irrelevant")
Expand Down
Loading

0 comments on commit 56ee8d9

Please sign in to comment.