Skip to content

Commit

Permalink
Fix #262 - more declarative method for defining navigation sections t…
Browse files Browse the repository at this point in the history
…hat is disabled by default
  • Loading branch information
nonprofittechy committed Aug 13, 2023
1 parent c88f618 commit be961d8
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
49 changes: 49 additions & 0 deletions docassemble/AssemblyLine/al_general.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from copy import deepcopy
from typing import Dict, List, Literal, Union, Optional
from docassemble.base.util import (
Address,
Expand Down Expand Up @@ -47,6 +48,7 @@
"ALPeopleList",
"Applicant",
"Applicant",
"get_visible_al_nav_items",
"github_modified_date",
"has_parsable_pronouns",
"HousingAuthority",
Expand Down Expand Up @@ -1515,3 +1517,50 @@ def parse_custom_pronouns(pronouns: str) -> Dict[str, str]:
"s": pronoun_list[1].lower(),
"p": pronoun_list[2].lower(),
}

def get_visible_al_nav_items(nav_items: List[Union[str, dict]]) -> List[Union[str, dict]]:
"""
Processes a list of nav items and returns only the ones that are not hidden.
Can be used to control the visible nav items in a more declarative way while keeping
the navigation dynamic.
Expects a list like this:
data = [
{"key": "value", "hidden": True},
"top level item",
{"key2": [{"subkey": "subvalue", "hidden": False}, {"subkey": "subvalue2", "hidden": True}]},
]
Args:
nav_items: a list of nav items
Returns:
a list of nav items with hidden items removed
"""
new_list: List[Union[str, dict]] = []

for item in nav_items:
# For strings at top level
if isinstance(item, str):
new_list.append(item)
continue

# For dictionaries at top level
item_copy = deepcopy(item)
if not item_copy.pop("hidden", False): # if not hidden
for key, val in item_copy.items():
if isinstance(val, list): # if value of a key is a list
new_sublist: List[Union[str, dict]] = []
for subitem in val:
# Add subitem strings as-is
if isinstance(subitem, str):
new_sublist.append(subitem)
# Add dictionaries if not hidden
elif isinstance(subitem, dict) and not subitem.pop("hidden", False):
new_sublist.append(subitem)
item_copy[key] = new_sublist
new_list.append(item_copy)
continue

return new_list
11 changes: 11 additions & 0 deletions docassemble/AssemblyLine/data/questions/al_visual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ event: al_start_over
code: |
command('new_session')
---
code: |
enable_al_nav_sections = False
---
code: |
al_nav_sections = []
---
initial: True
code: |
if enable_al_nav_sections:
nav.set_sections(get_visible_al_nav_items(al_nav_sections))
---
code: |
send_icon = "envelope"
---
Expand Down
61 changes: 59 additions & 2 deletions docassemble/AssemblyLine/test_al_general.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from .al_general import ALIndividual, ALAddress
from .al_general import ALIndividual, ALAddress, get_visible_al_nav_items
from unittest.mock import Mock
from docassemble.base.util import DADict, DAAttributeError

Expand Down Expand Up @@ -401,6 +401,63 @@ def test_custom_pronouns(self):
with self.assertRaises(DAAttributeError):
self.individual.pronoun_objective()


class test_get_visible_al_nav_items(unittest.TestCase):

def test_case_1(self):
data = [
"Selecting_Legal_Category",
{"personal_injury": "Gather Evidence", "hidden": False},
{"divorce": "Collect Financial Documents", "hidden": False},
{"business_dispute": ["Interview Witnesses", {"subtask": "Document Communication", "hidden": False}]}
]
expected = [
"Selecting_Legal_Category",
{"personal_injury": "Gather Evidence"},
{"divorce": "Collect Financial Documents"},
{"business_dispute": ["Interview Witnesses", {"subtask": "Document Communication"}]}
]
self.assertEqual(get_visible_al_nav_items(data), expected)

def test_case_2(self):
data = [
"Drafting_Documents",
{"pre_nup": "List Assets"},
"Finding_Legal_Representative",
{"business_formation": ["Choose Business Type", {"subtask": "Decide Ownership Structure", "hidden": True}]},
{"real_estate": "Draft Lease Agreement", "hidden": True}
]
expected = [
"Drafting_Documents",
{"pre_nup": "List Assets"},
"Finding_Legal_Representative",
{"business_formation": ["Choose Business Type"]}
]
self.assertEqual(get_visible_al_nav_items(data), expected)

def test_case_3(self):
data = [
"Negotiating_Terms",
"Reviewing_Contracts",
{"estate_planning": ["Draft Will", {"subtask": "Designate Beneficiaries", "hidden": False}, {"subtask": "Determine Power of Attorney", "hidden": False}]}
]
expected = [
"Negotiating_Terms",
"Reviewing_Contracts",
{"estate_planning": ["Draft Will", {"subtask": "Designate Beneficiaries"}, {"subtask": "Determine Power of Attorney"}]}
]
self.assertEqual(get_visible_al_nav_items(data), expected)

def test_case_4(self):
data = [
{"employment_law": "Understand Employee Rights", "hidden": False},
"Preparing_for_Court",
{"criminal_defense": ["Know Your Rights", {"subtask": "Hire Defense Attorney", "hidden": True}]}
]
expected = [
{"employment_law": "Understand Employee Rights"},
"Preparing_for_Court",
{"criminal_defense": ["Know Your Rights"]}
]
self.assertEqual(get_visible_al_nav_items(data), expected)
if __name__ == "__main__":
unittest.main()

0 comments on commit be961d8

Please sign in to comment.