From cb541e650d5253ea890a32f44a55ed483e3d6d9c Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Tue, 24 Jul 2018 10:22:52 +0100
Subject: [PATCH] Move the new submission messenger to a location with the
 request

---
 opentech/apply/funds/models.py | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/opentech/apply/funds/models.py b/opentech/apply/funds/models.py
index dbdf0819f..2b2febd7e 100644
--- a/opentech/apply/funds/models.py
+++ b/opentech/apply/funds/models.py
@@ -120,6 +120,17 @@ class WorkflowStreamForm(WorkflowHelpers, AbstractStreamForm):  # type: ignore
             form_index = self.workflow.stages.index(stage)
         return self.forms.all()[form_index].fields
 
+    def render_landing_page(self, request, form_submission=None, *args, **kwargs):
+        # We only reach this page after creation of a new submission
+        # Hook in to notify about new applications
+        messenger(
+            MESSAGES.NEW_APPLICATION,
+            request=request,
+            user=form_submission.user,
+            submission=form_submission,
+        )
+        return super().render_landing_page(request, form_submission=None, *args, **kwargs)
+
     content_panels = AbstractStreamForm.content_panels + [
         FieldPanel('workflow_name'),
         InlinePanel('forms', label="Forms"),
@@ -954,18 +965,6 @@ class ApplicationSubmission(WorkflowHelpers, BaseStreamForm, AbstractFormSubmiss
         return f'<{self.__class__.__name__}: {self.user}, {self.round}, {self.page}>'
 
 
-@receiver(post_save, sender=ApplicationSubmission)
-def log_submission_activity(sender, **kwargs):
-    if kwargs.get('created', False):
-        submission = kwargs.get('instance')
-
-        messenger(
-            MESSAGES.NEW_APPLICATION,
-            user=submission.user,
-            submission=submission,
-        )
-
-
 @receiver(post_transition, sender=ApplicationSubmission)
 def log_status_update(sender, **kwargs):
     instance = kwargs['instance']
-- 
GitLab