From 16953c79f33c6a5a7264056e99515ef607f8bd86 Mon Sep 17 00:00:00 2001 From: Serafeim Papastefanos Date: Fri, 27 May 2016 19:05:24 +0300 Subject: [PATCH] Allow multiple, comma seperated email addresses... to be used in the `to_address field` in the `AbstractEmailForm`. --- CHANGELOG.txt | 1 + docs/releases/1.6.rst | 1 + .../tests/testapp/migrations/0001_initial.py | 2 +- wagtail/wagtailforms/models.py | 5 +-- wagtail/wagtailforms/tests.py | 33 +++++++++++++++++++ 5 files changed, 39 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 659110fee..3e38a5a69 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -19,6 +19,7 @@ Changelog * Wagtail version number is now shown on the settings menu (Chris Rogers) * Added a system check to validate that fields listed in `search_fields` are defined on the model (Josh Schneier) * Added formal APIs for customising the display of StructBlock forms within the page editor (Matt Westcott) + * `wagtailforms.models.AbstractEmailForm` now supports multiple email recipients (Serafeim Papastefanos) * Fix: Email templates and document uploader now support custom `STATICFILES_STORAGE` (Jonny Scholes) * Fix: Removed alignment options (deprecated in HTML and not rendered by Wagtail) from `TableBlock` context menu (Moritz Pfeiffer) * Fix: Fixed incorrect CSS path on ModelAdmin's "choose a parent page" view diff --git a/docs/releases/1.6.rst b/docs/releases/1.6.rst index 7fac1b586..401c98416 100644 --- a/docs/releases/1.6.rst +++ b/docs/releases/1.6.rst @@ -34,6 +34,7 @@ Minor features * Wagtail version number is now shown on the settings menu (Chris Rogers) * Added a system check to validate that fields listed in ``search_fields`` are defined on the model (Josh Schneier) * Added formal APIs for customising the display of StructBlock forms within the page editor - see :ref:`custom_editing_interfaces_for_structblock` (Matt Westcott) + * ``wagtailforms.models.AbstractEmailForm`` now supports multiple email recipients (Serafeim Papastefanos) Bug fixes diff --git a/wagtail/tests/testapp/migrations/0001_initial.py b/wagtail/tests/testapp/migrations/0001_initial.py index c43dbddcd..a00770678 100644 --- a/wagtail/tests/testapp/migrations/0001_initial.py +++ b/wagtail/tests/testapp/migrations/0001_initial.py @@ -293,7 +293,7 @@ class Migration(migrations.Migration): name='FormPage', fields=[ ('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')), - ('to_address', models.CharField(blank=True, help_text='Optional - form submissions will be emailed to this address', max_length=255, verbose_name='to address')), + ('to_address', models.CharField(blank=True, help_text='Optional - form submissions will be emailed to these addresses. Separate multiple addresses by comma.', max_length=255, verbose_name='to address')), ('from_address', models.CharField(blank=True, max_length=255, verbose_name='from address')), ('subject', models.CharField(blank=True, max_length=255, verbose_name='subject')), ], diff --git a/wagtail/wagtailforms/models.py b/wagtail/wagtailforms/models.py index 9e11ecc8d..16bdc8fe0 100644 --- a/wagtail/wagtailforms/models.py +++ b/wagtail/wagtailforms/models.py @@ -210,7 +210,7 @@ class AbstractEmailForm(AbstractForm): to_address = models.CharField( verbose_name=_('to address'), max_length=255, blank=True, - help_text=_("Optional - form submissions will be emailed to this address") + help_text=_("Optional - form submissions will be emailed to these addresses. Separate multiple addresses by comma.") ) from_address = models.CharField(verbose_name=_('from address'), max_length=255, blank=True) subject = models.CharField(verbose_name=_('subject'), max_length=255, blank=True) @@ -219,8 +219,9 @@ class AbstractEmailForm(AbstractForm): super(AbstractEmailForm, self).process_form_submission(form) if self.to_address: + addresses = [x.strip() for x in self.to_address.split(',')] content = '\n'.join([x[1].label + ': ' + text_type(form.data.get(x[0])) for x in form.fields.items()]) - send_mail(self.subject, content, [self.to_address], self.from_address,) + send_mail(self.subject, content, addresses, self.from_address,) class Meta: abstract = True diff --git a/wagtail/wagtailforms/tests.py b/wagtail/wagtailforms/tests.py index 8bb650862..962f07196 100644 --- a/wagtail/wagtailforms/tests.py +++ b/wagtail/wagtailforms/tests.py @@ -159,6 +159,39 @@ class TestFormSubmission(TestCase): self.assertIn("Your choices: None", mail.outbox[0].body) +class TestFormSubmissionWithMultipleRecipients(TestCase): + def setUp(self): + # Create a form page + self.form_page = make_form_page(to_address='to@email.com, another@email.com') + + def test_post_valid_form(self): + response = self.client.post('/contact-us/', { + 'your-email': 'bob@example.com', + 'your-message': 'hello world', + 'your-choices': {'foo': '', 'bar': '', 'baz': ''} + }) + + # Check response + self.assertContains(response, "Thank you for your feedback.") + self.assertTemplateNotUsed(response, 'tests/form_page.html') + self.assertTemplateUsed(response, 'tests/form_page_landing.html') + + # check that variables defined in get_context are passed through to the template (#1429) + self.assertContains(response, "

hello world

") + + # Check that one email was sent, but to two recipients + self.assertEqual(len(mail.outbox), 1) + + self.assertEqual(mail.outbox[0].subject, "The subject") + self.assertIn("Your message: hello world", mail.outbox[0].body) + self.assertEqual(mail.outbox[0].from_email, 'from@email.com') + self.assertEqual(set(mail.outbox[0].to), {'to@email.com', 'another@email.com'}) + + # Check that form submission was saved correctly + form_page = Page.objects.get(url_path='/home/contact-us/') + self.assertTrue(FormSubmission.objects.filter(page=form_page, form_data__contains='hello world').exists()) + + class TestFormBuilder(TestCase): def setUp(self): # Create a form page