Skip to content

Commit

Permalink
get email template from registry
Browse files Browse the repository at this point in the history
  • Loading branch information
davisagli committed Oct 2, 2024
1 parent b6a8e48 commit fce4dde
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 300 deletions.
12 changes: 0 additions & 12 deletions backend/src/collective/volto/formsupport/browser/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,6 @@
type="plone"
/>

<browser:page
name="send_mail_template"
for="*"
template="send_mail_template.pt"
permission="zope2.View"
/>
<browser:page
name="send_mail_template_table"
for="*"
template="send_mail_template_table.pt"
permission="zope2.View"
/>
<browser:page
name="email-confirm-view"
for="*"
Expand Down

This file was deleted.

This file was deleted.

21 changes: 21 additions & 0 deletions backend/src/collective/volto/formsupport/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from zope.interface import Attribute
from zope.interface import Interface
from zope.publisher.interfaces.browser import IDefaultBrowserLayer
from zope import schema
from ZPublisher.BaseRequest import BaseRequest

import dataclasses
Expand Down Expand Up @@ -95,3 +96,23 @@ def __init__(context: FormSubmissionContext):

def __call__():
"""Process the data."""


DEFAULT_TEMPLATE = """
${mail_header}
<hr />
${form_fields}
<hr />
${mail_footer}
"""

class IFormSettings(Interface):

mail_templates = schema.Dict(
title="Email templates",
key_type=schema.TextLine(),
value_type=schema.Text(),
default={
"default": DEFAULT_TEMPLATE
}
)
55 changes: 24 additions & 31 deletions backend/src/collective/volto/formsupport/processors/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,16 @@ def get_subject(self):
subject = self.substitute_variables(subject)
return subject

def substitute_variables(self, value):
def substitute_variables(self, value, context=None):
if context is None:
context = self.form_data

def replace(match):
name = match.group(1)
return context.get(name, "")

pattern = r"\$\{([^}]+)\}"
return re.sub(pattern, lambda match: self.get_value(match.group(1), ""), value)
return re.sub(pattern, replace, value)

def get_value(self, field_id, default=None):
return self.form_data.get(field_id, default)
Expand All @@ -148,36 +155,22 @@ def get_confirmation_recipients(self) -> str:
return confirmation_recipients

def prepare_message(self):
mail_header = self.block.get("mail_header", {}).get("data", "")
mail_footer = self.block.get("mail_footer", {}).get("data", "")

# Check if there is content
mail_header = BeautifulSoup(mail_header).get_text() if mail_header else None
mail_footer = BeautifulSoup(mail_footer).get_text() if mail_footer else None

# TODO
email_format_page_template_mapping = {
"list": "send_mail_template",
"table": "send_mail_template_table",
}
email_format = self.block.get("email_format", "")
template_name = email_format_page_template_mapping.get(
email_format, "send_mail_template"
)

message_template = api.content.get_view(
name=template_name,
context=self.context,
request=self.request,
)
parameters = {
"parameters": self.records,
"url": self.context.absolute_url(),
"title": self.context.Title(),
"mail_header": mail_header,
"mail_footer": mail_footer,
templates = api.portal.get_registry_record("schemaform.mail_templates")
template_name = self.block.get("email_template", "default")
template = templates[template_name]
template_vars = {
"mail_header": self.block.get("mail_header", {}).get("data", ""),
"mail_footer": self.block.get("mail_footer", {}).get("data", ""),
}
return message_template(**parameters)
form_fields = "<table>\n"
for record in self.records:
value = str(record["value"])
template_vars[record["field_id"]] = value
form_fields += f"<tr><th>{record['label']}</th><td>{value}</td></tr>"
form_fields += "\n</table>\n"
template_vars["form_fields"] = form_fields
message = self.substitute_variables(template, template_vars)
return message

def add_attachments(self, msg):
if not self.attachments:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<version>1300</version>
<version>1400</version>
<dependencies>
<dependency>profile-plone.restapi:default</dependency>
<dependency>profile-plone.volto:default</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
i18n:domain="collective.volto.formsupport"
>

<!-- -*- extra stuff goes here -*- -->
<records interface="collective.volto.formsupport.interfaces.IFormSettings"
prefix="schemaform" />

</registry>
8 changes: 8 additions & 0 deletions backend/src/collective/volto/formsupport/upgrades.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@
handler=".upgrades.to_1300"
/>

<genericsetup:upgradeDepends
title="Add form settings to registry"
profile="collective.volto.formsupport:default"
source="1300"
destination="1400"
import_steps="plone.app.registry"
/>

</configure>
Loading

0 comments on commit fce4dde

Please sign in to comment.