Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Moved to use EmailMessage #13

Closed
wants to merge 2 commits into from

1 participant

Vitaly Babiy
Vitaly Babiy

This allows us to support the reply-to header.

Vitaly Babiy vbabiy closed this
Vitaly Babiy

to many commits, going to recreate it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 25, 2012
  1. Vitaly Babiy

    Email template is configurable

    vbabiy authored
Commits on Jun 7, 2012
  1. Vitaly Babiy
This page is out of date. Refresh to see the latest.
Showing with 27 additions and 15 deletions.
  1. +8 −3 envelope/forms.py
  2. +13 −12 envelope/tests/forms.py
  3. +6 −0 envelope/views.py
11 envelope/forms.py
View
@@ -28,16 +28,21 @@ class BaseContactForm(forms.Form):
message = forms.CharField(label=_("Message"), max_length=1000,
widget=forms.Textarea())
+ def __init__(self, *args, **kwargs):
+ self.email_template = kwargs.pop('email_template')
+ super(BaseContactForm, self).__init__(*args, **kwargs)
+
def save(self):
u"""
Sends the message.
"""
subject = settings.ENVELOPE_SUBJECT_INTRO + self.cleaned_data['subject']
context = self.get_context()
- message = render_to_string('envelope/email_body.txt', context)
+ message = render_to_string(self.email_template, context)
try:
- mail.send_mail(subject, message, settings.ENVELOPE_FROM_EMAIL,
- settings.ENVELOPE_EMAIL_RECIPIENTS)
+ mail.EmailMessage(subject, message, settings.ENVELOPE_FROM_EMAIL,
+ settings.ENVELOPE_EMAIL_RECIPIENTS,
+ headers = {'Reply-To': self.cleaned_data['email']}).send()
logger.info(_("Contact form submitted and sent (from: %s)") %
self.cleaned_data['email'])
except SMTPException:
25 envelope/tests/forms.py
View
@@ -53,14 +53,14 @@ def test_all_fields_valid(self):
u"""
When all required fields are supplied, the form is valid.
"""
- form = BaseContactForm(self.form_data)
+ form = BaseContactForm(self.form_data, email_template='envelope/email_body.txt')
self.assertTrue(form.is_valid())
def test_get_context(self):
u"""
get_context() returns a copy of form's cleaned_data.
"""
- form = BaseContactForm(self.form_data)
+ form = BaseContactForm(self.form_data, email_template='envelope/email_body.txt')
self.assertTrue(form.is_valid())
context = form.get_context()
self.assertEqual(context, form.cleaned_data)
@@ -70,7 +70,8 @@ def test_save(self):
u"""
A call to save() on a valid form sends the message.
"""
- form = BaseContactForm(self.form_data)
+ form = BaseContactForm(self.form_data, email_template='envelope/email_body.txt')
+
self.assertTrue(form.is_valid())
result = form.save()
self.assertTrue(result)
@@ -81,27 +82,27 @@ def test_save_smtp_error(self):
u"""
If the email backend raised an error, the message is not sent.
"""
- form = BaseContactForm(self.form_data)
+ form = BaseContactForm(self.form_data, email_template='envelope/email_body.txt')
self.assertTrue(form.is_valid())
- old_send_mail = mail.send_mail
+ old_send_mail = mail.EmailMessage
- def new_send_mail(*args):
+ def new_send_mail(*args, **kwargs):
raise SMTPException
try:
- mail.send_mail = new_send_mail
+ mail.EmailMessage = new_send_mail
result = form.save()
self.assertFalse(result)
self.assertEqual(len(mail.outbox), 0)
finally:
- mail.send_mail = old_send_mail
+ mail.EmailMessage = old_send_mail
def _test_required_field(self, field_name):
u"""
Check that the form does not validate without a given field.
"""
del self.form_data[field_name]
- form = BaseContactForm(self.form_data)
+ form = BaseContactForm(self.form_data, email_template='envelope/email_body.txt')
self.assertFalse(form.is_valid())
self.assertIn(field_name, form.errors)
@@ -125,7 +126,7 @@ def test_category_field(self):
Message field is required.
"""
del self.form_data['category']
- form = ContactForm(self.form_data)
+ form = ContactForm(self.form_data, email_template='envelope/email_body.txt')
self.assertFalse(form.is_valid())
self.assertIn('category', form.errors)
@@ -133,7 +134,7 @@ def test_get_context(self):
u"""
get_context() is overridden and adds a 'category' variable.
"""
- form = ContactForm(self.form_data)
+ form = ContactForm(self.form_data, email_template='envelope/email_body.txt')
self.assertTrue(form.is_valid())
context = form.get_context()
self.assertIn('category', context)
@@ -143,7 +144,7 @@ def test_get_category_display(self):
A non-integer field value selects a category labeled "Other".
"""
self.form_data['category'] = 'not-an-integer'
- form = ContactForm(self.form_data)
+ form = ContactForm(self.form_data, email_template='envelope/email_body.txt')
self.assertFalse(form.is_valid())
self.assertEqual(form.get_category_display(), _("Other"))
6 envelope/views.py
View
@@ -52,6 +52,7 @@ class ContactView(FormView):
"""
form_class = ContactForm
template_name = 'envelope/contact.html'
+ email_template = 'envelope/email_body.txt'
success_url = None
def get_success_url(self):
@@ -81,6 +82,11 @@ def get_initial(self):
})
return initial
+ def get_form(self, form_class):
+ kwargs = self.get_form_kwargs()
+ kwargs['email_template'] = self.email_template
+ return form_class(**kwargs)
+
def form_valid(self, form):
u"""
Sends the message and redirects the user somewhere.
Something went wrong with that request. Please try again.