gva/gnuviechadmin/contact_form/forms.py

74 lines
2.4 KiB
Python
Raw Normal View History

"""
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
2015-02-01 20:58:53 +01:00
from django.utils.translation import ugettext_lazy 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()
)