summaryrefslogtreecommitdiff
path: root/app/lib/contact/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'app/lib/contact/views.py')
-rw-r--r--app/lib/contact/views.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/app/lib/contact/views.py b/app/lib/contact/views.py
new file mode 100644
index 0000000..9e2c9c7
--- /dev/null
+++ b/app/lib/contact/views.py
@@ -0,0 +1,45 @@
+"""
+View which can render and send email from a contact form.
+
+"""
+
+from django import http
+from django.urls import reverse_lazy
+from django.views.generic.edit import FormView
+
+from .forms import ContactForm, StringKeyedDict
+
+
+class ContactFormView(FormView):
+ form_class = ContactForm
+ recipient_list = None
+ success_url = reverse_lazy("contact_form_sent")
+ template_name = "contact/contact_form.html"
+
+ def get_context_data(self, **kwargs):
+ '''
+ Adds breadcrumb path to every view
+ '''
+ # Call the base implementation first to get a context
+
+ context = super().get_context_data(**kwargs)
+ # special case for pages:
+ context['breadcrumbs'] = ('contact',)
+ context['crumb_url'] = None
+ return context
+
+ def form_valid(self, form) -> http.HttpResponse:
+ form.save()
+ return super().form_valid(form)
+
+ def get_form_kwargs(self) -> StringKeyedDict:
+ # ContactForm instances require instantiation with an
+ # HttpRequest.
+ kwargs = super().get_form_kwargs()
+ kwargs.update({"request": self.request})
+
+ # We may also have been given a recipient list when
+ # instantiated.
+ if self.recipient_list is not None:
+ kwargs.update({"recipient_list": self.recipient_list})
+ return kwargs