From 5cf9097ced00d67ad06df49a9ef2eb35b6ee262f Mon Sep 17 00:00:00 2001 From: Quinten Steenhuis Date: Fri, 14 Nov 2025 17:05:31 -0500 Subject: [PATCH 1/7] Fix #986 - make it possible to use a DALazyTemplate to allow a single field to combine multiple values in a PDF --- docassemble/AssemblyLine/al_document.py | 22 ++++++++++++++++++-- docassemble/AssemblyLine/test_al_document.py | 12 +++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/docassemble/AssemblyLine/al_document.py b/docassemble/AssemblyLine/al_document.py index b9d021d0..fc15cf1d 100644 --- a/docassemble/AssemblyLine/al_document.py +++ b/docassemble/AssemblyLine/al_document.py @@ -12,6 +12,7 @@ DAFile, DAFileCollection, DAFileList, + DALazyTemplate, defined, pdf_concatenate, zip_file, @@ -145,7 +146,10 @@ def safeattr(object: Any, key: str) -> str: """ try: if isinstance(object, dict) or isinstance(object, DADict): - return str(object.get(key, "")) + val = object.get(key, "") + if isinstance(val, DALazyTemplate): + return str(val) + return str(val) elif isinstance(object, DAObject): # `location` is not an attribute people usually want shown in the table of people's attributes if key == "location" and isinstance(object, Address): @@ -155,7 +159,10 @@ def safeattr(object: Any, key: str) -> str: object.location, LatitudeLongitude ): return "" - return str(getattr(object, key)) + val = getattr(object, key) + if isinstance(val, DALazyTemplate): + return str(val) + return str(val) else: return "" except: @@ -315,6 +322,9 @@ def overflow_value( # If trigger is not a boolean value, overflow value is the value that starts at the end of the safe value. original_value = self.value_if_defined() + # Ensure a DALazyTemplate is rendered to a string for downstream string ops + if isinstance(original_value, DALazyTemplate): + original_value = str(original_value) safe_text = self.safe_value( overflow_message=overflow_message, input_width=input_width, @@ -409,6 +419,9 @@ def has_overflow( val = _original_value else: val = self.value_if_defined() + # If this is a template block, render it now so comparisons work correctly + if isinstance(val, DALazyTemplate): + val = str(val) return ( self.safe_value( @@ -508,6 +521,11 @@ def safe_value( value = _original_value else: value = self.value_if_defined() + + # Convert template blocks to strings for purposes of evaluating overflow + if isinstance(value, DALazyTemplate): + value = str(value) + if ( isinstance(value, str) and len(value) <= self.overflow_trigger diff --git a/docassemble/AssemblyLine/test_al_document.py b/docassemble/AssemblyLine/test_al_document.py index 7d7d7c93..e781172d 100644 --- a/docassemble/AssemblyLine/test_al_document.py +++ b/docassemble/AssemblyLine/test_al_document.py @@ -76,6 +76,18 @@ def test_safe_value(self): overflow_value = field_with_weird_spaces.overflow_value(overflow_message="") self.assertTrue(overflow_value.startswith("very"), msg=f"{ overflow_value }") + def test_lazy_template_overflow(self): + from docassemble.base.util import DALazyTemplate + + tmpl = DALazyTemplate("x" * 50) + field = ALAddendumField(field_name="lazy_field") + field.overflow_trigger = 10 + # monkeypatch the value to be a template block + field.value_if_defined = lambda: tmpl + self.assertTrue(field.has_overflow()) + safe_value = field.safe_value(overflow_message="") + self.assertTrue(len(safe_value) <= 10) + class TestOriginalOrOverflowMessage(unittest.TestCase): def test_original_value_shorter_than_trigger(self): From e5d5d1973ed66d64af84ddb7e799e87866be921c Mon Sep 17 00:00:00 2001 From: Quinten Steenhuis Date: Sun, 16 Nov 2025 12:27:08 -0500 Subject: [PATCH 2/7] Define content in test properly --- docassemble/AssemblyLine/test_al_document.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docassemble/AssemblyLine/test_al_document.py b/docassemble/AssemblyLine/test_al_document.py index e781172d..86a7aa53 100644 --- a/docassemble/AssemblyLine/test_al_document.py +++ b/docassemble/AssemblyLine/test_al_document.py @@ -79,7 +79,7 @@ def test_safe_value(self): def test_lazy_template_overflow(self): from docassemble.base.util import DALazyTemplate - tmpl = DALazyTemplate("x" * 50) + tmpl = DALazyTemplate(content="x" * 50) field = ALAddendumField(field_name="lazy_field") field.overflow_trigger = 10 # monkeypatch the value to be a template block From 0e360d4d3c44efe01cb65477a92dd4e65aa16c63 Mon Sep 17 00:00:00 2001 From: Quinten Steenhuis Date: Sun, 16 Nov 2025 12:47:54 -0500 Subject: [PATCH 3/7] Use proper keyword param --- docassemble/AssemblyLine/test_al_document.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docassemble/AssemblyLine/test_al_document.py b/docassemble/AssemblyLine/test_al_document.py index 86a7aa53..28dc22f8 100644 --- a/docassemble/AssemblyLine/test_al_document.py +++ b/docassemble/AssemblyLine/test_al_document.py @@ -79,7 +79,7 @@ def test_safe_value(self): def test_lazy_template_overflow(self): from docassemble.base.util import DALazyTemplate - tmpl = DALazyTemplate(content="x" * 50) + tmpl = DALazyTemplate(source_content="x" * 50) field = ALAddendumField(field_name="lazy_field") field.overflow_trigger = 10 # monkeypatch the value to be a template block From cd169a6d57927c1b14ac3fc130cb27c56e1ee5d0 Mon Sep 17 00:00:00 2001 From: Quinten Steenhuis Date: Sun, 16 Nov 2025 13:03:44 -0500 Subject: [PATCH 4/7] one more time - this test might not be possible --- docassemble/AssemblyLine/test_al_document.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docassemble/AssemblyLine/test_al_document.py b/docassemble/AssemblyLine/test_al_document.py index 28dc22f8..ddc9ab7d 100644 --- a/docassemble/AssemblyLine/test_al_document.py +++ b/docassemble/AssemblyLine/test_al_document.py @@ -79,7 +79,7 @@ def test_safe_value(self): def test_lazy_template_overflow(self): from docassemble.base.util import DALazyTemplate - tmpl = DALazyTemplate(source_content="x" * 50) + tmpl = DALazyTemplate("tmpl", source_content="x" * 50) field = ALAddendumField(field_name="lazy_field") field.overflow_trigger = 10 # monkeypatch the value to be a template block From 97f234a305bc7ef61f756cd8348af1947852326b Mon Sep 17 00:00:00 2001 From: Quinten Steenhuis Date: Sun, 16 Nov 2025 13:11:18 -0500 Subject: [PATCH 5/7] Testing DALazyTemplate isn't really possible without so much monkeypatching to make it useless --- docassemble/AssemblyLine/test_al_document.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/docassemble/AssemblyLine/test_al_document.py b/docassemble/AssemblyLine/test_al_document.py index ddc9ab7d..7d7d7c93 100644 --- a/docassemble/AssemblyLine/test_al_document.py +++ b/docassemble/AssemblyLine/test_al_document.py @@ -76,18 +76,6 @@ def test_safe_value(self): overflow_value = field_with_weird_spaces.overflow_value(overflow_message="") self.assertTrue(overflow_value.startswith("very"), msg=f"{ overflow_value }") - def test_lazy_template_overflow(self): - from docassemble.base.util import DALazyTemplate - - tmpl = DALazyTemplate("tmpl", source_content="x" * 50) - field = ALAddendumField(field_name="lazy_field") - field.overflow_trigger = 10 - # monkeypatch the value to be a template block - field.value_if_defined = lambda: tmpl - self.assertTrue(field.has_overflow()) - safe_value = field.safe_value(overflow_message="") - self.assertTrue(len(safe_value) <= 10) - class TestOriginalOrOverflowMessage(unittest.TestCase): def test_original_value_shorter_than_trigger(self): From f33a289e117ef63feb404f6e61df54f68f4388b2 Mon Sep 17 00:00:00 2001 From: Quinten Steenhuis Date: Tue, 18 Nov 2025 09:10:29 -0500 Subject: [PATCH 6/7] Update docassemble/AssemblyLine/al_document.py Co-authored-by: Bryce Willey --- docassemble/AssemblyLine/al_document.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/docassemble/AssemblyLine/al_document.py b/docassemble/AssemblyLine/al_document.py index fc15cf1d..346d835b 100644 --- a/docassemble/AssemblyLine/al_document.py +++ b/docassemble/AssemblyLine/al_document.py @@ -146,10 +146,7 @@ def safeattr(object: Any, key: str) -> str: """ try: if isinstance(object, dict) or isinstance(object, DADict): - val = object.get(key, "") - if isinstance(val, DALazyTemplate): - return str(val) - return str(val) + return str(object.get(key, "")) elif isinstance(object, DAObject): # `location` is not an attribute people usually want shown in the table of people's attributes if key == "location" and isinstance(object, Address): From c3242f56ee3aa1837260d06218691fc00b84bba9 Mon Sep 17 00:00:00 2001 From: Quinten Steenhuis Date: Tue, 18 Nov 2025 09:10:47 -0500 Subject: [PATCH 7/7] Update docassemble/AssemblyLine/al_document.py Co-authored-by: Bryce Willey --- docassemble/AssemblyLine/al_document.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/docassemble/AssemblyLine/al_document.py b/docassemble/AssemblyLine/al_document.py index 346d835b..8f8d1c9e 100644 --- a/docassemble/AssemblyLine/al_document.py +++ b/docassemble/AssemblyLine/al_document.py @@ -156,10 +156,7 @@ def safeattr(object: Any, key: str) -> str: object.location, LatitudeLongitude ): return "" - val = getattr(object, key) - if isinstance(val, DALazyTemplate): - return str(val) - return str(val) + return str(getattr(object, key)) else: return "" except: