Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Email template is configurable #12

Closed
wants to merge 1 commit into from

2 participants

@vbabiy

No description provided.

@zsiciarz
Owner

This has been done by Erik in d4f3cab, also for other form class attributes. Thanks anyway!

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

    Email template is configurable

    vbabiy authored
This page is out of date. Refresh to see the latest.
View
6 envelope/forms.py
@@ -28,13 +28,17 @@ 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)
View
17 envelope/tests/forms.py
@@ -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,7 +82,7 @@ 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
@@ -101,7 +102,7 @@ def _test_required_field(self, field_name):
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"))
View
6 envelope/views.py
@@ -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.