Repository: liqd/a4-meinberlin

Issue:
Send wagtail form emails asynchronously 
Currently wagtail form emails are sent synchronously because the `send` attributes are not serializable. see #671 for the temporary fix.
Rather then sending sync we should fix the problem and provide the required data as a serializable type


Discussion:

Resolving patch:
diff --git a/meinberlin/apps/cms/emails.py b/meinberlin/apps/cms/emails.py
--- a/meinberlin/apps/cms/emails.py
+++ b/meinberlin/apps/cms/emails.py
@@ -6,15 +6,14 @@
 from django.utils import timezone
 from django.utils.translation import ugettext as _
 
-from adhocracy4.emails.mixins import SyncEmailMixin
 from meinberlin.apps.contrib.emails import Email
 
 
-class FormEmail(SyncEmailMixin, Email):
+class FormEmail(Email):
     template_name = 'meinberlin_cms/emails/form_submission'
 
     def get_receivers(self):
-        return [x.strip() for x in self.object.to_address.split(',')]
+        return [x.strip() for x in self.kwargs.get('to_addresses')]
 
 
 class XlsxFormEmail(FormEmail):
@@ -27,8 +26,8 @@ def _generate_xlsx(self):
         worksheet.write(0, 0, _('Form Field'))
         worksheet.write(0, 1, _('Response'))
 
-        rows = self.object.field_values.items()
-        for rownum, row in enumerate(rows, start=1):
+        field_values = self.kwargs.get('field_values')
+        for rownum, row in enumerate(field_values.items(), start=1):
             worksheet.write(rownum, 0, row[0])
             worksheet.write(rownum, 1, self._fix_newline_if_string(row[1]))
 
@@ -47,8 +46,8 @@ def get_attachments(self):
             _data=xlsx_data,
             _subtype='vnd.openxmlformats-officedocument.spreadsheetml.sheet')
         timestamp = timezone.now().strftime("%Y-%m-%d")
-        form_title = self.object.title.replace(' ', '_')
-        submission_pk = self.object.get_submission_class().objects.last().pk
+        form_title = self.kwargs.get('title')
+        submission_pk = self.kwargs.get('submission_pk')
         filename = '{}_{}_{}.xlsx'.format(timestamp, form_title, submission_pk)
         mime_doc.add_header(
             'Content-Disposition',
@@ -62,7 +61,8 @@ class TextFormEmail(FormEmail):
     def get_attachments(self):
         attachments = super().get_attachments()
         text = ''
-        for field, value in self.object.field_values.items():
+        field_values = self.kwargs.get('field_values')
+        for field, value in field_values.items():
             text += '{}:\n{}\n\n'.format(field, value)
         mime_doc = MIMEText(_text=text, _charset='utf-8')
         return attachments + [mime_doc]
diff --git a/meinberlin/apps/cms/models.py b/meinberlin/apps/cms/models.py
--- a/meinberlin/apps/cms/models.py
+++ b/meinberlin/apps/cms/models.py
@@ -130,16 +130,20 @@ class EmailFormPage(AbstractEmailForm):
     ]
 
     def send_mail(self, form):
-        self.form = form
+        kwargs = {
+            'title': self.title.replace(' ', '_'),
+            'to_addresses': self.to_address.split(','),
+            'field_values': self.get_field_values(form),
+            'submission_pk': self.get_submission_class().objects.last().pk
+        }
         if self.attach_as == 'xls':
-            emails.XlsxFormEmail.send(self)
+            emails.XlsxFormEmail.send(self, **kwargs)
         elif self.attach_as == 'txt':
-            emails.TextFormEmail.send(self)
+            emails.TextFormEmail.send(self, **kwargs)
 
-    @property
-    def field_values(self):
+    def get_field_values(self, form):
         fields = {}
-        for field in self.form:
+        for field in form:
             value = field.value()
             if isinstance(value, list):
                 value = ', '.join(value)


Test patch: