Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 16 additions & 43 deletions docassemble/AssemblyLine/al_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -2558,7 +2558,11 @@ def address_block(
show_impounded=False,
) -> str:
"""
Generate a formatted address block for mailings.
Generate a mailing block containing the full name followed by the address.

Uses Word line breaks in DOCX output and Docassemble line-break markers
elsewhere. Address confidentiality and formatting are delegated to the
address object. Use ``person.address.block()`` for an address without a name.

Args:
language (Optional): The language in which the address is written.
Expand All @@ -2583,49 +2587,18 @@ def address_block(
{{ users[0].address_block() }}
```
"""
name = self.name_full()
options = dict(
language=language,
international=international,
show_country=show_country,
)
if isinstance(self.address, ALAddress):
options.update(bare=bare, show_impounded=show_impounded)
address = self.address.block(**options)
if docassemble.base.functions.this_thread.evaluation_context == "docx":
if isinstance(self.address, ALAddress):
return self.address.block(
language=language,
international=international,
show_country=show_country,
bare=bare,
show_impounded=show_impounded,
)
else:
# bare parameter is ignored for plain Address objects
return self.address.block(
language=language,
international=international,
show_country=show_country,
)
else:
if isinstance(self.address, ALAddress):
return (
"[FLUSHLEFT] " + self.name_full()
if isinstance(self, ALIndividual)
else self.name.full(middle="full")
+ " [NEWLINE] "
+ self.address.block(
language=language,
international=international,
show_country=show_country,
bare=bare,
show_impounded=show_impounded,
)
)
else:
return (
"[FLUSHLEFT] " + self.name_full()
if isinstance(self, ALIndividual)
else self.name.full(middle="full")
+ " [NEWLINE] "
+ self.address.block(
language=language,
international=international,
show_country=show_country,
)
)
return name + '</w:t><w:br/><w:t xml:space="preserve">' + address
return "[FLUSHLEFT] " + name + " [NEWLINE] " + address

def pronoun(self, **kwargs) -> str:
"""
Expand Down
48 changes: 48 additions & 0 deletions docassemble/AssemblyLine/test_al_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -1108,3 +1108,51 @@ def test_hides_section_with_no_visible_subsections(self):

if __name__ == "__main__":
unittest.main()


class TestNameAndAddressBlock(unittest.TestCase):
def test_both_rendering_contexts_and_confidentiality(self):
import docassemble.base.functions

person = ALIndividual("person")
person.name.first = "Alex"
person.name.middle = ""
person.name.last = "Rivera"
person.name.suffix = ""
person.address.address = "123 Main Street"
person.address.unit = ""
person.address.city = "Boston"
person.address.state = "MA"
person.address.zip = "02108"
person.address.country = "US"
for context, separator in [("docx", "<w:br/>"), ("pdf", "[NEWLINE]")]:
with self.subTest(context=context), patch.object(
docassemble.base.functions.this_thread, "evaluation_context", context
):
result = person.address_block()
self.assertIn("Alex Rivera", result)
self.assertIn("123 Main Street", result)
self.assertIn(separator, result)
self.assertNotIn("Alex Rivera", person.address.block())
person.address.impounded = True
person.address.impounded_output_label = "Confidential"
self.assertIn("Confidential", person.address_block())
self.assertNotIn("123 Main Street", person.address_block())
self.assertIn(
"123 Main Street", person.address_block(show_impounded=True)
)
person.address.impounded = False

def test_plain_address_does_not_receive_al_options(self):
from docassemble.base.util import Address

person = ALIndividual("person")
person.name_full = Mock(return_value="Alex Rivera")
person.address = Address("plain_address")
person.address.block = Mock(return_value="123 Main Street")
self.assertIn(
"Alex Rivera", person.address_block(bare=True, show_impounded=True)
)
person.address.block.assert_called_once_with(
language=None, international=False, show_country=False
)
Loading