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

Conversation

nonprofittechy
Copy link
Member

@nonprofittechy nonprofittechy commented Jul 30, 2024

Fix #263

This pull request introduces three new utility functions to enhance template handling and prevent unwanted placeholder text (e.g., "None" or "Other") from appearing in the output. These functions are designed to help write more readable template code.

New Functions

none_to_empty

def none_to_empty(val: Any):
    """If the value is None or "None", return a DAEmpty value. Otherwise return the value."""
  • Purpose: Replaces None or the string "None" with a DAEmpty value to ensure that these placeholders do not appear in the final output.
  • Use Case: Particularly useful when handling optional fields, such as radio buttons that may not be selected.

option_or_other

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."""
  • Purpose: Handles the scenario where a variable is set to "Other" by returning the corresponding "other" variable's value.
  • Use Case: Useful in dropdowns or radio buttons where "Other" is an option, ensuring the actual value provided by the user is displayed instead of the placeholder.

true_values_with_other

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."""
  • Purpose: Collects all True values from a variable, replacing any instances of "Other" with the actual value of the corresponding "other" variable.
  • Use Case: Ideal for handling multi-select fields where "Other" might be an option, ensuring the specific user input is included in the results.

Test case

test_new_functions.docx

---
modules:
  - .misc
---
mandatory: True
question: |
attachment:
  docx template file: test_new_functions.docx
---
question: |
  Answer some Qs
fields:
  - Pick one: selected_option
    choices:
      - Option A
      - Option B
      - Other
  - Other: selected_option_other
    show if:
      variable: selected_option
      is: Other
  - Pick a few: checkbox_test
    datatype: checkboxes
    choices:
      - A
      - B
      - C
      - other
  - Other: checkbox_test_other
    show if: checkbox_test["other"]
  - Pick yes no or leave blank: possibly_empty_value
    datatype: radio
    required: False
    choices:
      - Something
      - Other

@nonprofittechy nonprofittechy requested a review from samglover July 30, 2024 19:33
Copy link

@samglover samglover left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nonprofittechy nonprofittechy merged commit 88f9d9a into main Aug 2, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a "notnone" function?
2 participants