2015-02-01 18:55:24 +01:00
|
|
|
"""
|
|
|
|
This module contains the form class for the contact_form app.
|
|
|
|
|
|
|
|
"""
|
|
|
|
from __future__ import absolute_import, unicode_literals
|
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
from crispy_forms.helper import FormHelper
|
|
|
|
from crispy_forms.layout import Submit
|
2015-02-01 18:55:24 +01:00
|
|
|
from django import forms
|
2023-07-08 19:21:02 +02:00
|
|
|
from django.apps import apps
|
2015-02-01 18:55:24 +01:00
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.sites.models import Site
|
2015-10-13 22:31:20 +02:00
|
|
|
from django.contrib.sites.requests import RequestSite
|
2023-02-18 22:46:48 +01:00
|
|
|
from django.core.mail import send_mail
|
|
|
|
from django.template import RequestContext, loader
|
|
|
|
from django.urls import reverse
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2015-02-01 18:55:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ContactForm(forms.Form):
|
|
|
|
"""
|
|
|
|
This is the contact form class.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
|
|
|
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"))
|
2015-02-01 18:55:24 +01:00
|
|
|
|
|
|
|
subject_template_name = "contact_form/contact_form_subject.txt"
|
2023-02-18 22:46:48 +01:00
|
|
|
template_name = "contact_form/contact_form.txt"
|
2015-02-01 18:55:24 +01:00
|
|
|
from_email = settings.DEFAULT_FROM_EMAIL
|
|
|
|
recipient_list = [mail_tuple[1] for mail_tuple in settings.MANAGERS]
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2023-02-18 22:46:48 +01:00
|
|
|
self.request = kwargs.pop("request")
|
2015-02-01 18:55:24 +01:00
|
|
|
super(ContactForm, self).__init__(**kwargs)
|
|
|
|
self.helper = FormHelper()
|
2023-02-18 22:46:48 +01:00
|
|
|
self.helper.form_action = reverse("contact_form")
|
|
|
|
self.helper.add_input(Submit("submit", _("Send message")))
|
2015-02-01 18:55:24 +01:00
|
|
|
|
|
|
|
def get_context(self):
|
|
|
|
if not self.is_valid():
|
2023-02-18 22:46:48 +01:00
|
|
|
raise ValueError("Cannot generate context from invalid contact form")
|
2023-07-08 19:21:02 +02:00
|
|
|
if apps.is_installed("django.contrib.sites"):
|
2015-02-01 18:55:24 +01:00
|
|
|
site = Site.objects.get_current()
|
|
|
|
else:
|
|
|
|
site = RequestSite(self.request)
|
2023-02-18 22:46:48 +01:00
|
|
|
return RequestContext(self.request, dict(self.cleaned_data, site=site))
|
2015-02-01 18:55:24 +01:00
|
|
|
|
|
|
|
def message(self):
|
2016-01-09 15:46:04 +01:00
|
|
|
context = self.get_context()
|
|
|
|
template_context = context.flatten()
|
2023-02-18 22:46:48 +01:00
|
|
|
template_context.update({"remote_ip": context.request.META["REMOTE_ADDR"]})
|
2016-01-09 15:46:04 +01:00
|
|
|
return loader.render_to_string(self.template_name, template_context)
|
2015-02-01 18:55:24 +01:00
|
|
|
|
|
|
|
def subject(self):
|
2016-01-09 15:46:04 +01:00
|
|
|
context = self.get_context().flatten()
|
|
|
|
subject = loader.render_to_string(self.subject_template_name, context)
|
2023-02-18 22:46:48 +01:00
|
|
|
return "".join(subject.splitlines())
|
2015-02-01 18:55:24 +01:00
|
|
|
|
|
|
|
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(),
|
2023-02-18 22:46:48 +01:00
|
|
|
message=self.message(),
|
2015-02-01 18:55:24 +01:00
|
|
|
)
|