Skip to content

Commit

Permalink
Merge branch 'custom-label-mapping' into deploy
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/collective/volto/formsupport/restapi/services/submit_form/post.py
  • Loading branch information
JeffersonBledsoe committed Jan 9, 2024
2 parents 050acdb + 3785922 commit 8a5a50f
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 32 deletions.
4 changes: 4 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
[run]
relative_files = True

omit =
src/collective/volto/formsupport/tests/*
src/collective/volto/formsupport/upgrades.py

[report]
include =
*/src/collective/*
Expand Down
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,13 @@ This add-on can be seen in action at the following sites:

- https://www.comune.modena.it/form/contatti

Custom label mapping
=========================

In some cases, the text that is displayed for a field on the page and in the sent email may need to be different from the value that is stored internally. For example, you may want your "Yes/ No" widget to show "Accept" and "Decline" as the labels, but internally still store `True` and `False`.

By storing a `display_values` dictionary for each field in the block data, you can perform these mappings.


Translations
============
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ def __init__(self, field_data):
def _attribute(attribute_name):
setattr(self, attribute_name, field_data.get(attribute_name))

_attribute("field_id")
_attribute("field_type")
_attribute("id")
_attribute("show_when_when")
Expand All @@ -14,15 +13,16 @@ def _attribute(attribute_name):
_attribute("widget")
_attribute("use_as_reply_to")
_attribute("use_as_reply_bcc")
self._dislpay_value_mapping = field_data.get("dislpay_value_mapping")
self._value = field_data.get("value")
self._display_value_mapping = field_data.get("display_value_mapping")
self._value = field_data.get("value", "")
self._custom_field_id = field_data.get("custom_field_id")
self._label = field_data.get("label")
self._label = field_data.get("label", "")
self._field_id = field_data.get("field_id", "")

@property
def value(self):
if self._dislpay_value_mapping:
return self._dislpay_value_mapping.get(self._value, self._value)
if self._display_value_mapping:
return self._display_value_mapping.get(self._value, self._value)
return self._value

@value.setter
Expand All @@ -31,14 +31,22 @@ def value(self, value):

@property
def label(self):
if self._custom_field_id:
return self._custom_field_id
return self._label if self._label else self.field_id

@label.setter
def label(self, label):
self._label = label

@property
def field_id(self):
if self._custom_field_id:
return self._custom_field_id
return self._field_id if self._field_id else self._label

@field_id.setter
def field_id(self, field_id):
self._field_id = field_id

@property
def send_in_email(self):
return True
Expand All @@ -47,11 +55,11 @@ def send_in_email(self):
class YesNoField(Field):
@property
def value(self):
if self._dislpay_value_mapping:
if self._display_value_mapping:
if self._value is True:
return self._dislpay_value_mapping.get("yes")
return self._display_value_mapping.get("yes")
elif self._value is False:
return self._dislpay_value_mapping.get("no")
return self._display_value_mapping.get("no")
return self._value

@property
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-


import codecs
import logging
import math
Expand Down Expand Up @@ -81,7 +82,7 @@ def reply(self):
{
**field,
**submitted_field,
"dislpay_value_mapping": field.get("display_values"),
"display_value_mapping": field.get("display_values"),
}
)
self.fields = construct_fields(fields_data)
Expand Down Expand Up @@ -306,13 +307,12 @@ def send_data(self):

self.manage_attachments(msg=msg)

if should_send:
if isinstance(should_send, list):
if "recipient" in self.block.get("send", []):
self.send_mail(msg=msg, charset=charset)
# Backwards compatibility for forms before 'acknowledgement' sending
else:
if isinstance(should_send, list):
if "recipient" in self.block.get("send", []):
self.send_mail(msg=msg, charset=charset)
# Backwards compatibility for forms before 'acknowledgement' sending
else:
self.send_mail(msg=msg, charset=charset)

# send a copy also to the fields with bcc flag
for bcc in self.get_bcc():
Expand Down Expand Up @@ -411,7 +411,7 @@ def attach_xml(self, msg):
xmlRoot = Element("form")

for field in self.filter_parameters():
SubElement(xmlRoot, "field", name=field.label).text = str(field._value)
SubElement(xmlRoot, "field", name=field.field_id).text = str(field._value)

doc = ElementTree(xmlRoot)
doc.write(output, encoding="utf-8", xml_declaration=True)
Expand Down
69 changes: 56 additions & 13 deletions src/collective/volto/formsupport/tests/test_send_action_form.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
# -*- coding: utf-8 -*-
from collective.volto.formsupport.testing import ( # noqa: E501,
VOLTO_FORMSUPPORT_API_FUNCTIONAL_TESTING,
)
import base64
import os
import unittest
import xml.etree.ElementTree as ET
from email.parser import Parser

import transaction
from plone import api
from plone.app.testing import setRoles
from plone.app.testing import SITE_OWNER_NAME
from plone.app.testing import SITE_OWNER_PASSWORD
from plone.app.testing import TEST_USER_ID
from plone.app.testing import (
SITE_OWNER_NAME,
SITE_OWNER_PASSWORD,
TEST_USER_ID,
setRoles,
)
from plone.registry.interfaces import IRegistry
from plone.restapi.testing import RelativeSession
from Products.MailHost.interfaces import IMailHost
from six import StringIO
from zope.component import getUtility

import base64
import os
import transaction
import unittest
import xml.etree.ElementTree as ET
from collective.volto.formsupport.testing import ( # noqa: E501,
VOLTO_FORMSUPPORT_API_FUNCTIONAL_TESTING,
)


class TestMailSend(unittest.TestCase):
Expand Down Expand Up @@ -806,7 +809,7 @@ def test_field_custom_display_value(
"subblocks": [
{
"field_id": "12345678",
"internal_value": {"John": "Paul"},
"display_values": {"John": "Paul"},
},
],
}
Expand Down Expand Up @@ -841,6 +844,46 @@ def test_field_custom_display_value(
self.assertIn("<strong>Message:</strong> just want to say hi", msg)
self.assertIn("<strong>Name:</strong> Paul", msg)

def test_send_custom_field_id(self):
"""Custom field IDs should still appear as their friendly names in the email"""
self.document.blocks = {
"form-id": {"@type": "form", "send": True},
}
transaction.commit()

form_data = [
{"label": "Name", "value": "John"},
{
"label": "Other name",
"value": "Test",
"custom_field_id": "My custom field id",
},
]

response = self.submit_form(
data={
"from": "john@doe.com",
"data": form_data,
"subject": "test subject",
"block_id": "form-id",
},
)
transaction.commit()
self.assertEqual(response.status_code, 204)
msg = self.mailhost.messages[0]
if isinstance(msg, bytes) and bytes is not str:
# Python 3 with Products.MailHost 4.10+
msg = msg.decode("utf-8")

parsed_msgs = Parser().parse(StringIO(msg))
body = parsed_msgs.get_payload()

self.assertIn("Name", body)
self.assertIn("John", body)
self.assertNotIn("My custom field id", body)
self.assertIn("Other name", body)
self.assertIn("Test", body)

def test_send_xml(self):
self.document.blocks = {
"form-id": {"@type": "form", "send": True, "attachXml": True},
Expand Down

0 comments on commit 8a5a50f

Please sign in to comment.