Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XML Sending and custom field mapping #22

Merged
merged 13 commits into from
May 23, 2023
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

setup(
name="collective.volto.formsupport",
version="2.6.3.dev0",
version="2.6.3.dev1",
description="Add support for customizable forms in Volto",
long_description=long_description,
# Get more from https://pypi.org/classifiers/
Expand Down
15 changes: 13 additions & 2 deletions src/collective/volto/formsupport/datamanager/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@ def get_form_fields(self):
form_block = deepcopy(block)
if not form_block:
return {}
return form_block.get("subblocks", [])

subblocks = form_block.get("subblocks", [])

# Add the 'custom_field_id' field back in as this isn't stored with each subblock
for index, field in enumerate(subblocks):
if form_block.get(field["field_id"]):
subblocks[index]["custom_field_id"] = form_block.get(field["field_id"])

return subblocks

def add(self, data):
form_fields = self.get_form_fields()
Expand All @@ -69,7 +77,10 @@ def add(self, data):
)
return None

fields = {x["field_id"]: x.get("label", x["field_id"]) for x in form_fields}
fields = {
x["field_id"]: x.get("custom_field_id", x.get("label", x["field_id"]))
for x in form_fields
}
record = Record()
fields_labels = {}
fields_order = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
from collective.volto.formsupport.interfaces import ICaptchaSupport
from collective.volto.formsupport.interfaces import IFormDataStore
from collective.volto.formsupport.interfaces import IPostEvent
from datetime import datetime
from email.message import EmailMessage
from plone import api
from plone.protect.interfaces import IDisableCSRFProtection
from plone.registry.interfaces import IRegistry
from plone.restapi.deserializer import json_body
from plone.restapi.services import Service
from Products.CMFPlone.interfaces.controlpanel import IMailSchema
from xml.etree.ElementTree import ElementTree, Element, SubElement
from zExceptions import BadRequest
from zope.component import getMultiAdapter
from zope.component import getUtility
Expand Down Expand Up @@ -301,6 +303,10 @@ def send_mail(self, msg, encoding):

def manage_attachments(self, msg):
attachments = self.form_data.get("attachments", {})

if self.block.get("attachXml", False):
self.attach_xml(msg=msg)

if not attachments:
return []
for key, value in attachments.items():
Expand All @@ -320,13 +326,40 @@ def manage_attachments(self, msg):
file_data = file_data.encode("utf-8")
else:
file_data = value
maintype, subtype = content_type.split("/")
msg.add_attachment(
file_data,
maintype=content_type,
subtype=content_type,
maintype=maintype,
subtype=subtype,
filename=filename,
)

def attach_xml(self, msg):
now = (
datetime.now()
.isoformat(timespec="seconds")
.replace(" ", "-")
.replace(":", "")
)
filename = f"formdata_{now}.xml"
output = six.BytesIO()
xmlRoot = Element("form")

for field in self.filter_parameters():
SubElement(
xmlRoot, "field", name=field.get("custom_field_id", field["label"])
).text = str(field["value"])

doc = ElementTree(xmlRoot)
doc.write(output, encoding="utf-8", xml_declaration=True)
xmlstr = output.getvalue()
msg.add_attachment(
xmlstr,
maintype="application",
subtype="xml",
filename=filename,
)

def store_data(self):
store = getMultiAdapter((self.context, self.request), IFormDataStore)
res = store.add(data=self.filter_parameters())
Expand Down