Jan Dittberner
385838580b
- implement contact_form.forms.ContactForm - implement contact_form.views.ContactFormView and contact_form.views.ContactSuccessView - add new URL patterns 'contact_form' and 'contact_success' in contact_form.urls - add contact_form templates base.html, contact_form.html, contact_form.txt, contact_form_subject.txt and contact_success.html - add german translation for new strings - add contact_form to .coveragerc - add generated code documentation for contact_form app - add changelog entry
73 lines
2.4 KiB
Python
73 lines
2.4 KiB
Python
"""
|
|
This module contains the form class for the contact_form app.
|
|
|
|
"""
|
|
from __future__ import absolute_import, unicode_literals
|
|
|
|
from django import forms
|
|
from django.conf import settings
|
|
from django.core.mail import send_mail
|
|
from django.core.urlresolvers import reverse
|
|
from django.template import RequestContext
|
|
from django.template import loader
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from django.contrib.sites.models import RequestSite
|
|
from django.contrib.sites.models import Site
|
|
|
|
from crispy_forms.helper import FormHelper
|
|
from crispy_forms.layout import Submit
|
|
|
|
|
|
class ContactForm(forms.Form):
|
|
"""
|
|
This is the contact form class.
|
|
|
|
"""
|
|
name = forms.CharField(max_length=100, label=_('Your name'))
|
|
email = forms.EmailField(max_length=200, label=_('Your email address'))
|
|
body = forms.CharField(widget=forms.Textarea, label=_('Your message'))
|
|
|
|
subject_template_name = "contact_form/contact_form_subject.txt"
|
|
template_name = 'contact_form/contact_form.txt'
|
|
from_email = settings.DEFAULT_FROM_EMAIL
|
|
recipient_list = [mail_tuple[1] for mail_tuple in settings.MANAGERS]
|
|
|
|
def __init__(self, **kwargs):
|
|
self.request = kwargs.pop('request')
|
|
super(ContactForm, self).__init__(**kwargs)
|
|
self.helper = FormHelper()
|
|
self.helper.form_action = reverse('contact_form')
|
|
self.helper.add_input(Submit('submit', _('Send message')))
|
|
|
|
def get_context(self):
|
|
if not self.is_valid():
|
|
raise ValueError(
|
|
'Cannot generate context from invalid contact form')
|
|
if Site._meta.installed:
|
|
site = Site.objects.get_current()
|
|
else:
|
|
site = RequestSite(self.request)
|
|
return RequestContext(
|
|
self.request, dict(self.cleaned_data, site=site))
|
|
|
|
def message(self):
|
|
return loader.render_to_string(self.template_name, self.get_context())
|
|
|
|
def subject(self):
|
|
subject = loader.render_to_string(
|
|
self.subject_template_name, self.get_context())
|
|
return ''.join(subject.splitlines())
|
|
|
|
def save(self, fail_silently=False):
|
|
"""
|
|
Build and send the email.
|
|
|
|
"""
|
|
send_mail(
|
|
fail_silently=fail_silently,
|
|
from_email=self.from_email,
|
|
recipient_list=self.recipient_list,
|
|
subject=self.subject(),
|
|
message=self.message()
|
|
)
|