Skip to content

Commit

Permalink
Use field class where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffersonBledsoe committed Jan 20, 2025
1 parent 7d2cfcc commit f8787cd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 54 deletions.
55 changes: 28 additions & 27 deletions src/collective/volto/formsupport/adapters/post.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from collective.volto.formsupport import _
from collective.volto.formsupport.interfaces import ICaptchaSupport
from collective.volto.formsupport.interfaces import IPostAdapter
from collective.volto.formsupport.restapi.services.submit_form.field import (
construct_fields,
)
from collective.volto.formsupport.utils import get_blocks
from collective.volto.otp.utils import validate_email_token
from copy import deepcopy
Expand Down Expand Up @@ -31,6 +34,8 @@ def __init__(self, context, request):
if self.block_id:
self.block = self.get_block_data(block_id=self.block_id)

self.fields = self.format_fields()

def __call__(self):
"""
Avoid XSS injections and other attacks.
Expand All @@ -41,6 +46,9 @@ def __call__(self):

self.validate_form()

for field in self.fields:
field.validate(request=self.request)

return self.form_data

def extract_data_from_request(self):
Expand Down Expand Up @@ -219,32 +227,25 @@ def filter_parameters(self):
"""
do not send attachments fields.
"""
result = []

for field in self.block.get("subblocks", []):
if field.get("field_type", "") == "attachment":
continue

for item in self.form_data.get("data", []):
if item.get("field_id", "") == field.get("field_id", ""):
result.append(item)

return result
return [field for field in self.fields if field.send_in_email]

def format_fields(self):
fields = self.filter_parameters()
formatted_fields = []
field_ids = [field.get("field_id") for field in self.block.get("subblocks", [])]

for field in fields:
field_id = field.get("field_id", "")

if field_id:
field_index = field_ids.index(field_id)

if self.block["subblocks"][field_index].get("field_type") == "date":
field["value"] = api.portal.get_localized_time(field["value"])

formatted_fields.append(field)

return formatted_fields
fields_data = []
for submitted_field in self.form_data.get("data", []):
# TODO: Review if fields submitted without a field_id should be included. Is breaking change if we remove it
if submitted_field.get("field_id") is None:
fields_data.append(submitted_field)
continue
for field in self.block.get("subblocks", []):
if field.get("id", field.get("field_id")) == submitted_field.get(
"field_id"
):
fields_data.append(
{
**field,
**submitted_field,
"display_value_mapping": field.get("display_values"),
"custom_field_id": self.block.get(field["field_id"]),
}
)
return construct_fields(fields_data)
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
from collective.volto.formsupport.interfaces import IFormDataStore
from collective.volto.formsupport.interfaces import IPostAdapter
from collective.volto.formsupport.interfaces import IPostEvent
from collective.volto.formsupport.restapi.services.submit_form.field import (
construct_fields,
)
from collective.volto.formsupport.utils import get_blocks
from plone.protect.interfaces import IDisableCSRFProtection
from plone.registry.interfaces import IRegistry
Expand Down Expand Up @@ -76,29 +73,6 @@ def reply(self):

notify(PostEventService(self.context, self.form_data))

# Construct self.fieldss
fields_data = []
for submitted_field in self.form_data.get("data", []):
# TODO: Review if fields submitted without a field_id should be included. Is breaking change if we remove it
if submitted_field.get("field_id") is None:
fields_data.append(submitted_field)
continue
for field in self.block.get("subblocks", []):
if field.get("id", field.get("field_id")) == submitted_field.get(
"field_id"
):
fields_data.append(
{
**field,
**submitted_field,
"display_value_mapping": field.get("display_values"),
"custom_field_id": self.block.get(field["field_id"]),
}
)
self.fields = construct_fields(fields_data)
for field in self.fields:
field.validate(request=self.request)

if send_action or self.get_bcc():
try:
self.send_data()
Expand Down Expand Up @@ -396,7 +370,7 @@ def attach_xml(self, msg):
output = BytesIO()
xmlRoot = Element("form")

for field in self.form_data_adapter.filter_parameters():
for field in self.form_data_adapter.format_fields():
SubElement(xmlRoot, "field", name=field.field_id).text = str(
field.internal_value
)
Expand Down

0 comments on commit f8787cd

Please sign in to comment.