epicyon/webapp_accesskeys.py

118 lines
4.3 KiB
Python
Raw Normal View History

2021-04-22 21:46:02 +00:00
__filename__ = "webapp_accesskeys.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.2.0"
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2021-04-22 21:46:02 +00:00
__status__ = "Production"
2021-06-26 11:27:14 +00:00
__module_group__ = "Accessibility"
2021-04-22 21:46:02 +00:00
import os
2021-12-26 18:46:43 +00:00
from utils import is_account_dir
2021-12-26 15:13:34 +00:00
from utils import load_json
2021-12-26 14:08:58 +00:00
from utils import get_config_param
2021-12-26 12:02:29 +00:00
from utils import acct_dir
2021-04-22 21:46:02 +00:00
from webapp_utils import htmlHeaderWithExternalStyle
from webapp_utils import htmlFooter
2021-12-25 16:17:53 +00:00
def loadAccessKeysForAccounts(base_dir: str, keyShortcuts: {},
2021-04-23 17:29:18 +00:00
accessKeysTemplate: {}) -> None:
2021-04-22 21:46:02 +00:00
"""Loads key shortcuts for each account
"""
2021-12-25 16:17:53 +00:00
for subdir, dirs, files in os.walk(base_dir + '/accounts'):
2021-04-22 21:46:02 +00:00
for acct in dirs:
2021-12-26 18:46:43 +00:00
if not is_account_dir(acct):
2021-04-22 21:46:02 +00:00
continue
2021-12-25 16:17:53 +00:00
accountDir = os.path.join(base_dir + '/accounts', acct)
2021-04-22 21:46:02 +00:00
accessKeysFilename = accountDir + '/accessKeys.json'
if not os.path.isfile(accessKeysFilename):
continue
nickname = acct.split('@')[0]
2021-12-26 15:13:34 +00:00
accessKeys = load_json(accessKeysFilename)
2021-04-22 21:46:02 +00:00
if accessKeys:
2021-04-23 17:29:18 +00:00
keyShortcuts[nickname] = accessKeysTemplate.copy()
for variableName, key in accessKeysTemplate.items():
if accessKeys.get(variableName):
keyShortcuts[nickname][variableName] = \
accessKeys[variableName]
2021-04-22 21:46:02 +00:00
break
2021-12-25 16:17:53 +00:00
def htmlAccessKeys(cssCache: {}, base_dir: str,
2021-04-22 21:46:02 +00:00
nickname: str, domain: str,
translate: {}, accessKeys: {},
2021-04-22 22:28:16 +00:00
defaultAccessKeys: {},
defaultTimeline: str) -> str:
2021-04-22 21:46:02 +00:00
"""Show and edit key shortcuts
"""
accessKeysFilename = \
2021-12-26 12:02:29 +00:00
acct_dir(base_dir, nickname, domain) + '/accessKeys.json'
2021-04-22 21:46:02 +00:00
if os.path.isfile(accessKeysFilename):
2021-12-26 15:13:34 +00:00
accessKeysFromFile = load_json(accessKeysFilename)
2021-04-22 21:46:02 +00:00
if accessKeysFromFile:
accessKeys = accessKeysFromFile
accessKeysForm = ''
2021-12-25 16:17:53 +00:00
cssFilename = base_dir + '/epicyon-profile.css'
if os.path.isfile(base_dir + '/epicyon.css'):
cssFilename = base_dir + '/epicyon.css'
2021-04-22 21:46:02 +00:00
instanceTitle = \
2021-12-26 14:08:58 +00:00
get_config_param(base_dir, 'instanceTitle')
accessKeysForm = \
htmlHeaderWithExternalStyle(cssFilename, instanceTitle, None)
2021-04-22 21:46:02 +00:00
accessKeysForm += '<div class="container">\n'
accessKeysForm += \
' <h1>' + translate['Key Shortcuts'] + '</h1>\n'
2021-04-23 19:59:55 +00:00
accessKeysForm += \
'<p>' + translate['These access keys may be used'] + \
'<label class="labels"></label></p>'
2021-04-22 21:46:02 +00:00
accessKeysForm += ' <form method="POST" action="' + \
'/users/' + nickname + '/changeAccessKeys">\n'
2021-04-23 09:15:53 +00:00
timelineKey = accessKeys['menuTimeline']
submitKey = accessKeys['submitButton']
2021-04-22 21:46:02 +00:00
accessKeysForm += \
2021-04-22 22:28:16 +00:00
' <center>\n' + \
2021-04-22 22:37:52 +00:00
' <button type="submit" class="button" ' + \
2021-04-23 09:15:53 +00:00
'name="submitAccessKeysCancel" accesskey="' + timelineKey + '">' + \
2021-04-22 22:37:52 +00:00
translate['Go Back'] + '</button>\n' + \
2021-04-23 09:15:53 +00:00
' <button type="submit" class="button" ' + \
'name="submitAccessKeys" accesskey="' + submitKey + '">' + \
2021-04-22 22:28:16 +00:00
translate['Submit'] + '</button>\n </center>\n'
2021-04-22 21:46:02 +00:00
accessKeysForm += ' <table class="accesskeys">\n'
accessKeysForm += ' <colgroup>\n'
accessKeysForm += ' <col span="1" class="accesskeys-left">\n'
accessKeysForm += ' <col span="1" class="accesskeys-center">\n'
accessKeysForm += ' </colgroup>\n'
accessKeysForm += ' <tbody>\n'
for variableName, key in defaultAccessKeys.items():
if not translate.get(variableName):
continue
keyStr = '<tr>'
keyStr += \
'<td><label class="labels">' + \
translate[variableName] + '</label></td>'
if accessKeys.get(variableName):
key = accessKeys[variableName]
2021-04-23 14:21:57 +00:00
if len(key) > 1:
key = key[0]
2021-04-22 21:46:02 +00:00
keyStr += \
2021-04-23 14:21:57 +00:00
'<td><input type="text" ' + \
'name="' + variableName.replace(' ', '_') + '" ' + \
'value="' + key + '">'
2021-04-22 22:15:25 +00:00
keyStr += '</td></tr>\n'
2021-04-22 22:01:25 +00:00
accessKeysForm += keyStr
2021-04-22 21:46:02 +00:00
accessKeysForm += ' </tbody>\n'
accessKeysForm += ' </table>\n'
accessKeysForm += ' </form>\n'
accessKeysForm += '</div>\n'
accessKeysForm += htmlFooter()
return accessKeysForm