Skip to content

Commit

Permalink
[#5037] Added check for non str to the DateFormatter
Browse files Browse the repository at this point in the history
Backport-of: #5063
  • Loading branch information
vaszig authored and sergei-maertens committed Jan 31, 2025
1 parent fc3bacd commit 8352995
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/openforms/formio/formatters/custom.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# TODO implement: iban, bsn, postcode, licenseplate, npFamilyMembers, cosign
from datetime import date
from typing import NotRequired, TypedDict

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


class DateFormatter(FormatterBase):
def format(self, component: Component, value: str) -> str:
return fmt_date(parse_date(value))
def format(self, component: Component, value: str | date | None) -> str:
if isinstance(value, str):
value = parse_date(value)
return fmt_date(value)


class DateTimeFormatter(FormatterBase):
Expand Down
44 changes: 43 additions & 1 deletion src/openforms/submissions/tests/test_tasks_pdf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from django.test import TestCase, override_settings
from django.test import TestCase, override_settings, tag
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _

Expand All @@ -9,6 +9,9 @@
from pyquery import PyQuery as pq
from testfixtures import LogCapture

from openforms.forms.tests.factories import FormLogicFactory

from ..form_logic import evaluate_form_logic
from ..models import SubmissionReport
from ..tasks.pdf import generate_submission_report
from .factories import SubmissionFactory, SubmissionReportFactory
Expand Down Expand Up @@ -106,6 +109,45 @@ def test_hidden_output_not_included(self):
# step has no visible children -> also not visible
self.assertNotIn(step_title, html)

@tag("gh-5037")
def test_date_object_is_converted_to_str_when_it_comes_from_logic_rule(self):
submission = SubmissionFactory.from_components(
[
{
"type": "date",
"key": "date1",
},
{
"type": "date",
"key": "updatedDate",
},
],
submitted_data={"date1": "2025-01-01"},
completed=True,
with_report=True,
)

FormLogicFactory.create(
form=submission.form,
json_logic_trigger={"==": [{"var": "date1"}, "2025-01-01"]},
actions=[
{
"variable": "updatedDate",
"action": {
"type": "variable",
"value": {"+": [{"var": "date1"}, {"duration": "P1M"}]},
},
},
],
)
evaluate_form_logic(
submission, submission.submissionstep_set.get(), submission.data
)

html = submission.report.generate_submission_report_pdf()

self.assertIn("31 januari 2025", html)

def test_visible_output_included(self):
"""
Assert that hidden components are not included in the report.
Expand Down

0 comments on commit 8352995

Please sign in to comment.