Modified fiew steps, added possibility to attach files
rodzic
b149b85f53
commit
46645bde17
|
@ -17,7 +17,10 @@ from wagtail.contrib.forms.models import (
|
||||||
AbstractFormSubmission
|
AbstractFormSubmission
|
||||||
)
|
)
|
||||||
|
|
||||||
from mailings.models import send_mail
|
from mailings.models import (
|
||||||
|
OutgoingEmail,
|
||||||
|
Attachment
|
||||||
|
)
|
||||||
from dynamic_forms.forms import DynamicForm
|
from dynamic_forms.forms import DynamicForm
|
||||||
|
|
||||||
|
|
||||||
|
@ -56,34 +59,27 @@ class Form(FormMixin, Page):
|
||||||
|
|
||||||
|
|
||||||
class EmailFormSubmission(AbstractFormSubmission):
|
class EmailFormSubmission(AbstractFormSubmission):
|
||||||
def render_email(self, data):
|
|
||||||
content = []
|
|
||||||
for field_name, value in data.items():
|
|
||||||
if isinstance(value, list):
|
|
||||||
value = ", ".join(value)
|
|
||||||
|
|
||||||
# Format dates and datetime(s) with SHORT_DATE(TIME)_FORMAT
|
|
||||||
if isinstance(value, datetime.datetime):
|
|
||||||
value = date_format(value, settings.SHORT_DATETIME_FORMAT)
|
|
||||||
elif isinstance(value, datetime.date):
|
|
||||||
value = date_format(value, settings.SHORT_DATE_FORMAT)
|
|
||||||
|
|
||||||
content.append("{}: {}".format(field_name, value))
|
|
||||||
|
|
||||||
return "\n".join(content)
|
|
||||||
|
|
||||||
def send_mail(self, data):
|
def send_mail(self, data):
|
||||||
|
# modify this, get proper template
|
||||||
to_addresses = data.pop("to_address").split(",")
|
to_addresses = data.pop("to_address").split(",")
|
||||||
subject = data.pop("subject")
|
attachments = [
|
||||||
|
Attachment(
|
||||||
|
file.name, file.file.read(), file.content_type
|
||||||
|
)
|
||||||
|
for file in data.pop("attachments", [])
|
||||||
|
]
|
||||||
|
subject = data.get("subject")
|
||||||
from_address = data.pop("from_address", settings.DEFAULT_FROM_EMAIL)
|
from_address = data.pop("from_address", settings.DEFAULT_FROM_EMAIL)
|
||||||
addresses = [x.strip() for x in to_addresses]
|
for address in to_addresses:
|
||||||
send_mail(
|
OutgoingEmail.objects.send(
|
||||||
addresses,
|
subject=subject,
|
||||||
[],
|
template_name="form_mail",
|
||||||
subject,
|
recipient=address,
|
||||||
self.render_email(data),
|
sender=from_address,
|
||||||
from_address
|
context=data,
|
||||||
)
|
attachments=attachments
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class CustomEmailForm(Form):
|
class CustomEmailForm(Form):
|
||||||
|
@ -106,6 +102,7 @@ class CustomEmailForm(Form):
|
||||||
return EmailFormSubmission
|
return EmailFormSubmission
|
||||||
|
|
||||||
def process_form_submission(self, form):
|
def process_form_submission(self, form):
|
||||||
|
attachments = form.cleaned_data.pop("attachments", [])
|
||||||
submission = self.get_submission_class().objects.create(
|
submission = self.get_submission_class().objects.create(
|
||||||
form_data=form.cleaned_data,
|
form_data=form.cleaned_data,
|
||||||
page=self,
|
page=self,
|
||||||
|
@ -114,7 +111,8 @@ class CustomEmailForm(Form):
|
||||||
mail_data.update({
|
mail_data.update({
|
||||||
"from_address": self.from_address,
|
"from_address": self.from_address,
|
||||||
"to_address": self.to_address,
|
"to_address": self.to_address,
|
||||||
"subject": self.subject
|
"subject": self.subject,
|
||||||
|
"attachments": attachments
|
||||||
})
|
})
|
||||||
submission.send_mail(data=mail_data)
|
submission.send_mail(data=mail_data)
|
||||||
return submission
|
return submission
|
||||||
|
|
|
@ -8,16 +8,16 @@
|
||||||
<p class="meta">{{ page.date }}</p>
|
<p class="meta">{{ page.date }}</p>
|
||||||
{% if form %}
|
{% if form %}
|
||||||
<div>{{ page.intro|richtext }}</div>
|
<div>{{ page.intro|richtext }}</div>
|
||||||
<form action="{% pageurl page %}" method="POST">
|
<form enctype="multipart/form-data" action="{% pageurl page %}" method="POST">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
{% for field in form %}
|
{% for field in form %}
|
||||||
<div class="form-group mt-3">
|
<div class="form-group mt-3">
|
||||||
<label for="{{field.id}}" class="form-label">
|
<label for="{{field.id}}" class="form-label">
|
||||||
{{field.label}}
|
{% trans field.label %}
|
||||||
</label>
|
</label>
|
||||||
{{field}}
|
{{field}}
|
||||||
<small id="emailHelp" class="form-text text-muted">
|
<small id="emailHelp" class="form-text text-muted">
|
||||||
{{form.help_text}}
|
{% trans field.help_text %}
|
||||||
</small>
|
</small>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
{{page.thank_you_text|richtext}}
|
{{page.thank_you_text|richtext}}
|
||||||
</div>
|
</div>
|
||||||
<a class="btn btn-outline-primary mt-3" href="/">
|
<a class="btn btn-outline-primary mt-3" href="/">
|
||||||
{% trans "Go to homepage" %}
|
{% trans "Homepage" %}
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
Ładowanie…
Reference in New Issue