Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a few utility functions for DOCX templates - handle None values and Other for checkboxes and radio buttons #264

Merged
merged 1 commit into from
Aug 2, 2024
Merged
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
104 changes: 88 additions & 16 deletions docassemble/ALToolbox/misc.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
from typing import Dict, List, Optional, TypedDict, Union
from typing import Any, Dict, List, Optional, TypedDict, Union

from base64 import b64encode
from decimal import Decimal
import docassemble.base.functions
from docassemble.base.util import (
action_button_html,
Address,
DADict,
DAEmpty,
defined,
value,
showifdef,
space_to_underscore,
action_button_html,
Address,
word,
user_has_privilege,
value,
word,
)
import re

__all__ = [
"shortenMe",
"thousands",
"tel",
"add_records",
"button_array",
"collapse_template",
"fa_icon",
"space",
"yes_no_unknown",
"nice_county_name",
"none_to_empty",
"number_to_letter",
"collapse_template",
"tabbed_templates_html",
"option_or_other",
"output_checkbox",
"review_widget",
"shortenMe",
"space",
"sum_if_defined",
"add_records",
"output_checkbox",
"nice_county_name",
"button_array",
"tabbed_templates_html",
"tel",
"thousands",
"true_values_with_other",
"yes_no_unknown",
]


Expand Down Expand Up @@ -387,3 +392,70 @@ def button_array(
</a>"""
output += "</div>"
return output


def none_to_empty(val: Any):
"""If the value is None or "None", return a DAEmpty value. Otherwise return the value.

This is useful for filling in a template and to prevent the word None from appearing in the output. For example,
when handling a radio button that is not required and left unanswered.

A DAEmpty value appears as an empty string in the output. You can also safely transform it or use any method on it
without raising an error.

Args:
val: the value to check
Returns:
a DAEmpty if the value is None, otherwise the value
"""
if val is None or val == "None":
return DAEmpty()
return val


def option_or_other(
variable_name: str, other_variable_name: Optional[str] = None
) -> str:
"""If the variable is set to 'Other', return the value of the 'other' variable. Otherwise return the value of the variable.

This is useful for filling in a template and to prevent the word 'Other' from appearing in the output.

Args:
variable_name: the name of the variable to check
other_variable_name: the name of the variable to return if the value of the first variable is 'Other'
Returns:
the value of the variable if it is not 'Other', otherwise the value of the other variable
"""
if not other_variable_name:
other_variable_name = variable_name + "_other"

if str(value(variable_name)).lower() == "other":
return value(other_variable_name)
return value(variable_name)


def true_values_with_other(
variable_name: str, other_variable_name: Optional[str] = None
) -> List[str]:
"""Return a list of values that are True, with the value of the 'other' variable appended to the end of the list.

This is useful for filling in a template and to prevent the word 'Other' from appearing in the output.

Args:
variable: the dictionary of variables to check
other_variable_name: the name of the variable (as a string) to return if the value of the first variable is 'Other'
Returns:
a list of values that are True, with the value of the 'other' variable appended to the end of the list.
"""
if not other_variable_name:
other_variable_name = variable_name + "_other"

true_values = value(variable_name).true_values()
if "other" in true_values:
true_values.remove("other")
true_values.append(value(other_variable_name))
if "Other" in true_values:
true_values.remove("Other")
true_values.append(value(other_variable_name))

return true_values
2 changes: 1 addition & 1 deletion docassemble/ALToolbox/test_misc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
import unittest
from unittest.mock import patch
from .misc import button_array, ButtonDict
from .misc import button_array, ButtonDict, true_values_with_other
import xml.etree.ElementTree as ET


Expand Down