Skip to content

Commit

Permalink
Merge pull request #116 from SuffolkLITLab/view_answers_cleanup
Browse files Browse the repository at this point in the history
Improve appearance of interview list
  • Loading branch information
nonprofittechy authored Nov 21, 2023
2 parents 75e80af + 0518f18 commit 4153990
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 38 deletions.
75 changes: 46 additions & 29 deletions docassemble/ALDashboard/aldashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
restart_all,
install_pip_package,
get_package_info,
get_session_variables,
)
from docassemble.base.config import daconfig
from docassemble.webapp.backend import cloud
from docassemble.base.functions import serializable_dict
from docassemble.base.util import (
log,
DAFile,
Expand All @@ -31,7 +33,6 @@
word,
DAFileList,
get_config,
space_to_underscore,
)
from ruamel.yaml import YAML
from ruamel.yaml.compat import StringIO
Expand All @@ -54,6 +55,7 @@
"install_from_pypi",
"install_fonts",
"list_installed_fonts",
"dashboard_get_session_variables",
]


Expand Down Expand Up @@ -155,10 +157,12 @@ def get_users_and_name() -> List[Tuple[int, str, str, str]]:


def speedy_get_sessions(
user_id: Optional[int] = None, filename: Optional[str] = None
user_id: Optional[int] = None,
filename: Optional[str] = None,
filter_step1: bool = True,
) -> List[Tuple]:
"""
Return a lsit of the most recent 500 sessions, optionally tied to a specific user ID.
Return a list of the most recent 500 sessions, optionally tied to a specific user ID.
Each session is a tuple with named columns:
filename,
Expand All @@ -168,43 +172,56 @@ def speedy_get_sessions(
"""
get_sessions_query = text(
"""
SELECT userdict.filename as filename
,num_keys
,userdictkeys.user_id as user_id
,modtime
,userdict.key as key
FROM userdict
NATURAL JOIN
(
SELECT key
,MAX(modtime) AS modtime
,COUNT(key) AS num_keys
FROM userdict
GROUP BY key
) mostrecent
LEFT JOIN userdictkeys
ON userdictkeys.key = userdict.key
WHERE (userdict.user_id = :user_id OR :user_id is null)
AND
(userdict.filename = :filename OR :filename is null)
ORDER BY modtime desc
LIMIT 500;
"""
SELECT userdict.filename as filename,
num_keys,
userdictkeys.user_id as user_id,
modtime,
userdict.key as key
FROM userdict
NATURAL JOIN
(
SELECT key,
MAX(modtime) AS modtime,
COUNT(key) AS num_keys
FROM userdict
GROUP BY key
HAVING COUNT(key) > 1 OR :filter_step1 = False
) mostrecent
LEFT JOIN userdictkeys
ON userdictkeys.key = userdict.key
WHERE (userdict.user_id = :user_id OR :user_id is null)
AND (userdict.filename = :filename OR :filename is null)
ORDER BY modtime DESC
LIMIT 500;
"""
)
# Assuming `filename`, `user_id`, and `filter_step1` are provided elsewhere in your code
if not filename:
filename = None # Explicitly treat empty string as equivalent to None
if not user_id: # TODO: verify that 0 is not a valid value for user ID
user_id = None

# Ensure filter_step1 is a boolean
filter_step1 = bool(filter_step1)

with db.connect() as con:
rs = con.execute(get_sessions_query, {"user_id": user_id, "filename": filename})
sessions = []
for session in rs:
sessions.append(session)
rs = con.execute(
get_sessions_query,
{"user_id": user_id, "filename": filename, "filter_step1": filter_step1},
)
sessions = [session for session in rs]

return sessions


def dashboard_get_session_variables(session_id: str, filename: str):
"""
Return the variables and steps for a given session ID and YAML filename in serializable dictionary format.
"""
user_dict = get_session_variables(filename, session_id, secret=None, simplify=False)
return serializable_dict(user_dict, include_internal=False)


class ALPackageInstaller(DAObject):
"""Methods and state for installing AssemblyLine."""

Expand Down
46 changes: 37 additions & 9 deletions docassemble/ALDashboard/data/questions/list_sessions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ code: |
---
question: |
What interview do you want to view sessions for?
subquestion: |
Pick an interview from the list below, or type a filename like: `docassemble.playground1:data/questions/interview.yml`
fields:
- Filename: filename
required: False
datatype: combobox
code: |
sorted([{interview: interviews[interview].get('title')} for interview in interviews], key=lambda y: next(iter(y.values()), ''))
- User (leave blank to view all sessions): chosen_user
Expand All @@ -26,26 +29,51 @@ fields:
input type: combobox
code: |
speedy_get_users()
- Filter out interviews that are on the first page: filter_step1
required: False
datatype: yesno
default: True
---
# next((item.get('title') for item in interviews if item.get('filename') == interview['filename']), interview['filename'] )
---
code: |
sessions_list = speedy_get_sessions(user_id=chosen_user, filename=filename)
sessions_list = speedy_get_sessions(user_id=chosen_user, filename=filename, filter_step1=filter_step1)
---
mandatory: True
event: load_answer
question: |
Recently generated sessions for ${ filename }
Recently started sessions for ${ interviews.get(filename, {"title": filename}).get('title', filename) }
subquestion: |
<table class="table">
<thead>
<tr>
<th>Session ID</th>
<th>User</th>
<th>Modified</th>
<th>Page</th>
<th>Actions</th>
</tr>
</thead>
% for interview in sessions_list:
* <a href="${ interview_url(i=interview.filename, session=interview.key) }">
${ interviews.get(interview.filename,{'title': interview.filename}).get('title') }
</a>
(Modified ${ format_date(interview.modtime) }), on page **${ interview.num_keys }**
* [View variables](${ interview_url_action('view_session_variables', session_id=interview.key) })
<tr>
<td>
${ interview.key }
</td>
<td>${ interview.user_id }</td>
<td>${ format_date(interview.modtime, "MMM d YYYY") }</td>
<td>${ interview.num_keys }</td>
<td>
<a href="${ interview_url(i=interview.filename, session=interview.key) }">
<i class="fa-solid fa-folder-open"></i>&nbsp;Join</a>
<br/>
<a href="${ interview_url_action('view_session_variables', session_id=interview.key) }"><i class="fa-solid fa-eye"></i>&nbsp;Vars</a>
</td>
</tr>
% endfor
</table>
question back button: True
---
event: view_session_variables
code: |
json_response(get_session_variables(filename, action_argument('session_id'), simplify=True))
response(binaryresponse=json.dumps(dashboard_get_session_variables(session_id=action_argument('session_id'), filename=filename)).encode('utf-8'), content_type="application/json", response_code=200)

0 comments on commit 4153990

Please sign in to comment.