forked from feincms/feincms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
78 lines (61 loc) · 2.45 KB
/
Copy pathmodels.py
File metadata and controls
78 lines (61 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Simple contact form for FeinCMS. The default form class has name, email,
subject and content fields, content being the only one which is not required.
You can provide your own comment form by passing an additional
``form=YourClass`` argument to the ``create_content_type`` call.
"""
from django import forms
from django.core.mail import send_mail
from django.db import models
from django.http import HttpResponseRedirect
from django.template.loader import render_to_string
from django.utils.translation import gettext_lazy as _
class ContactForm(forms.Form):
name = forms.CharField(label=_("name"))
email = forms.EmailField(label=_("email"))
subject = forms.CharField(label=_("subject"))
content = forms.CharField(widget=forms.Textarea, required=False, label=_("content"))
class ContactFormContent(models.Model):
form = ContactForm
email = models.EmailField()
subject = models.CharField(max_length=200)
class Meta:
abstract = True
verbose_name = _("contact form")
verbose_name_plural = _("contact forms")
@classmethod
def initialize_type(cls, form=None):
if form:
cls.form = form
def process(self, request, **kwargs):
if request.GET.get("_cf_thanks"):
self.rendered_output = render_to_string(
"content/contactform/thanks.html", {"content": self}, request=request
)
return
if request.method == "POST":
form = self.form(request.POST)
if form.is_valid():
send_mail(
form.cleaned_data["subject"],
render_to_string(
"content/contactform/email.txt", {"data": form.cleaned_data}
),
form.cleaned_data["email"],
[self.email],
fail_silently=True,
)
return HttpResponseRedirect("?_cf_thanks=1")
else:
initial = {"subject": self.subject}
if request.user.is_authenticated():
initial["email"] = request.user.email
initial["name"] = request.user.get_full_name()
form = self.form(initial=initial)
self.rendered_output = render_to_string(
"content/contactform/form.html",
{"content": self, "form": form},
request=request,
)
def render(self, **kwargs):
return getattr(self, "rendered_output", "")