Skip to content

Commit

Permalink
Add button_array function, fix #218
Browse files Browse the repository at this point in the history
  • Loading branch information
nonprofittechy committed Dec 5, 2023
1 parent cb68fa8 commit 9067ccf
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion docassemble/ALToolbox/misc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Union
from typing import Dict, List, Optional, Union

from base64 import b64encode
from decimal import Decimal
Expand All @@ -11,6 +11,7 @@
action_button_html,
Address,
word,
user_has_privilege,
)
import re

Expand All @@ -29,6 +30,7 @@
"add_records",
"output_checkbox",
"nice_county_name",
"button_array",
]


Expand Down Expand Up @@ -341,3 +343,33 @@ def nice_county_name(address: Address) -> str:
return address.county[: -len(" County")]
else:
return address.county


def button_array(buttons:List[Dict[str, Union[str, List[str]]]], custom_container_class = "", custom_link_class="") -> str:
"""Create a grid of da-buttons from a dictionary of links and icons
This mimics the look and feel of Docassemble's `buttons` field type, but
doesn't have the limits of that particular input method. This is meant to appear
on any page of an interview in the `subquestion` area.
Optionally, you can limit access to paricular buttons by specifying a privilege or a list
of privileges.
Args:
button_list: a dictionary of dictionaries with the following keys:
- `name`: the text to display on the button
- `image`: the name of a fontawesome icon to display on the button
- `url`: the name of the page to link to
- `privilege`: optional, the name of a Docassemble privilege that the user must have to see the button. Can be a list or a single string.
"""
buttons = [button for button in buttons if user_has_privilege(button.get("privilege")) or not button.get("privilege") ]

# create the grid of buttons
output = f"""<div class="da-button-set da-field-buttons {custom_container_class}">"""
for button in buttons:
output += f"""
<a class="btn btn-da btn-light btn-da btn-da-custom {custom_link_class}" href="{button.get("link")}">
{fa_icon(button.get("icon", "globe")) } {button.get("label", button.get("link"))}
</a>"""
output += "</div>"
return output

0 comments on commit 9067ccf

Please sign in to comment.