Skip to content

Commit

Permalink
Fix #119 - Add an interview to browse all of the YAML files on the se…
Browse files Browse the repository at this point in the history
…rver by package
  • Loading branch information
nonprofittechy committed May 17, 2024
1 parent ad682ce commit 0761ec4
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
55 changes: 55 additions & 0 deletions docassemble/ALDashboard/aldashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@
get_config,
user_has_privilege,
)
from docassemble.webapp.server import get_package_info

from ruamel.yaml import YAML
from ruamel.yaml.compat import StringIO
import re
import werkzeug
import pkg_resources


db = init_sqlalchemy()

Expand All @@ -58,6 +62,8 @@
"list_installed_fonts",
"dashboard_get_session_variables",
"nicer_interview_filename",
"list_question_files_in_package",
"list_question_files_in_docassemble_packages",
]


Expand Down Expand Up @@ -367,3 +373,52 @@ def nicer_interview_filename(filename: str) -> str:
return f"{filename_parts[0]}:{filename_parts[1].replace('.yml', '')}"

return filename_parts[0]

def list_question_files_in_package(package_name:str) -> List[str]:
"""
List all the files in the 'data/questions' directory of a package.
Args:
package_name (str): The name of the package to list files from.
Returns:
List[str]: A list of filenames in the 'data/questions' directory of the package.
"""
try:
# Locate the directory within the package
directory_path = pkg_resources.resource_filename(package_name, 'data/questions')

# List all files in the directory
if os.path.isdir(directory_path):
files = os.listdir(directory_path)
# Filter out directories, only keep files
files = [f for f in files if os.path.isfile(os.path.join(directory_path, f))]
return files
else:
return []
except Exception as e:
log(f"An error occurred with package '{package_name}': {e}")
return None

def list_question_files_in_docassemble_packages():
"""
List all the files in the 'data/questions' directory of all docassemble packages.
Returns:
Dict[str, List[str]]: A dictionary where the keys are package names and the values are lists of filenames in the 'data/questions' directory of the package.
"""
packages = get_package_info()[0] # get_package_info returns a tuple, the packages are in index 0

filtered_packages = [pkg for pkg in packages if pkg.package.name.startswith('docassemble.')]

result = {}

# Iterate over each filtered package and list files in 'data/questions'
for package in filtered_packages:
package_name = package.package.name

files = list_question_files_in_package(package_name)
if files:
result[package_name] = files

return result
34 changes: 34 additions & 0 deletions docassemble/ALDashboard/data/questions/browse_interviews.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
modules:
- .aldashboard
- docassemble.demo.accordion
---
default screen parts:
right: |
${ action_button_html(interview_url(i=f"{user_info().package}:menu.yml"), label="Back to Dashboard") }
---
mandatory: True
id: browse packages
question: |
Interviews on this server
subquestion: |
Below is a list of every interview YAML file in every Docassemble package on this server.
Not all YAMLs are intended to be run directly as interviews.
% for package_name, question_files in sorted(list_question_files_in_docassemble_packages().items()):
% if loop.index == 0:
${ start_accordion(package_name.replace("docassemble.", "")) }
% else:
${ next_accordion(package_name.replace("docassemble.", "")) }
% endif
% if question_files:
<ul>
% for question_file in question_files:
<li>
<a href="${ interview_url(i=package_name + ":" + question_file, reset=1) }">${ question_file }</a>
</li>
% endfor
</ul>
% endif
% endfor
${ end_accordion() }
3 changes: 3 additions & 0 deletions docassemble/ALDashboard/data/questions/menu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ data:
- name: Package scanner
url: ${ interview_url(i=user_info().package + ":package_scanner.yml", reset=1) }
image: search
- name: View installed interviews
url: ${ interview_url(i=user_info().package + ":browse_interviews.yml", reset=1) }
image: list
- name: View answer files
url: ${ interview_url(i=user_info().package + ":list_sessions.yml", reset=1) }
image: file-alt
Expand Down

0 comments on commit 0761ec4

Please sign in to comment.