Skip to content

Commit c99dc8e

Browse files
vaszigsergei-maertens
authored andcommitted
[#5037] Added check for non str to the DateFormatter
Backport-of: #5063
1 parent eb94ab1 commit c99dc8e

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/openforms/formio/formatters/custom.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# TODO implement: iban, bsn, postcode, licenseplate, npFamilyMembers, cosign
2+
from datetime import date
23
from typing import NotRequired, TypedDict
34

45
from django.template.defaultfilters import date as fmt_date, time as fmt_time
@@ -11,8 +12,10 @@
1112

1213

1314
class DateFormatter(FormatterBase):
14-
def format(self, component: Component, value: str) -> str:
15-
return fmt_date(parse_date(value))
15+
def format(self, component: Component, value: str | date | None) -> str:
16+
if isinstance(value, str):
17+
value = parse_date(value)
18+
return fmt_date(value)
1619

1720

1821
class DateTimeFormatter(FormatterBase):

src/openforms/submissions/tests/test_tasks_pdf.py

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22

3-
from django.test import TestCase, override_settings
3+
from django.test import TestCase, override_settings, tag
44
from django.utils.html import format_html
55
from django.utils.translation import gettext_lazy as _
66

@@ -10,7 +10,9 @@
1010
from testfixtures import LogCapture
1111

1212
from openforms.config.models import GlobalConfiguration
13+
from openforms.forms.tests.factories import FormLogicFactory
1314

15+
from ..form_logic import evaluate_form_logic
1416
from ..models import SubmissionReport
1517
from ..tasks.pdf import generate_submission_report
1618
from .factories import SubmissionFactory, SubmissionReportFactory
@@ -108,6 +110,45 @@ def test_hidden_output_not_included(self):
108110
# step has no visible children -> also not visible
109111
self.assertNotIn(step_title, html)
110112

113+
@tag("gh-5037")
114+
def test_date_object_is_converted_to_str_when_it_comes_from_logic_rule(self):
115+
submission = SubmissionFactory.from_components(
116+
[
117+
{
118+
"type": "date",
119+
"key": "date1",
120+
},
121+
{
122+
"type": "date",
123+
"key": "updatedDate",
124+
},
125+
],
126+
submitted_data={"date1": "2025-01-01"},
127+
completed=True,
128+
with_report=True,
129+
)
130+
131+
FormLogicFactory.create(
132+
form=submission.form,
133+
json_logic_trigger={"==": [{"var": "date1"}, "2025-01-01"]},
134+
actions=[
135+
{
136+
"variable": "updatedDate",
137+
"action": {
138+
"type": "variable",
139+
"value": {"+": [{"var": "date1"}, {"duration": "P1M"}]},
140+
},
141+
},
142+
],
143+
)
144+
evaluate_form_logic(
145+
submission, submission.submissionstep_set.get(), submission.data
146+
)
147+
148+
html = submission.report.generate_submission_report_pdf()
149+
150+
self.assertIn("31 januari 2025", html)
151+
111152
def test_visible_output_included(self):
112153
"""
113154
Assert that hidden components are not included in the report.

0 commit comments

Comments
 (0)