epicyon/webapp_accesskeys.py

134 lines
5.0 KiB
Python
Raw Normal View History

2021-04-22 21:46:02 +00:00
__filename__ = "webapp_accesskeys.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2022-02-03 13:58:20 +00:00
__version__ = "1.3.0"
2021-04-22 21:46:02 +00:00
__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-12-29 21:55:09 +00:00
from webapp_utils import html_header_with_external_style
from webapp_utils import html_footer
2022-05-14 12:15:26 +00:00
from webapp_utils import get_banner_file
2021-04-22 21:46:02 +00:00
2022-01-01 15:11:42 +00:00
def load_access_keys_for_accounts(base_dir: str, key_shortcuts: {},
2022-01-03 22:00:38 +00:00
access_keys_template: {}) -> None:
2021-04-22 21:46:02 +00:00
"""Loads key shortcuts for each account
"""
2022-01-03 22:00:38 +00:00
for _, dirs, _ 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
2022-01-03 22:00:38 +00:00
account_dir = os.path.join(base_dir + '/accounts', acct)
access_keys_filename = account_dir + '/access_keys.json'
if not os.path.isfile(access_keys_filename):
2021-04-22 21:46:02 +00:00
continue
nickname = acct.split('@')[0]
2022-01-03 22:00:38 +00:00
access_keys = load_json(access_keys_filename)
2021-12-31 21:18:12 +00:00
if access_keys:
2022-01-03 22:00:38 +00:00
key_shortcuts[nickname] = access_keys_template.copy()
for variable_name, _ in access_keys_template.items():
if access_keys.get(variable_name):
key_shortcuts[nickname][variable_name] = \
access_keys[variable_name]
2021-04-22 21:46:02 +00:00
break
2022-07-12 19:03:30 +00:00
def html_access_keys(base_dir: str,
2021-12-29 21:55:09 +00:00
nickname: str, domain: str,
2021-12-31 21:18:12 +00:00
translate: {}, access_keys: {},
2022-01-03 22:00:38 +00:00
default_access_keys: {},
2022-05-14 12:15:26 +00:00
default_timeline: str, theme: str) -> str:
2021-04-22 21:46:02 +00:00
"""Show and edit key shortcuts
"""
2022-01-03 22:00:38 +00:00
access_keys_filename = \
2021-12-31 21:18:12 +00:00
acct_dir(base_dir, nickname, domain) + '/access_keys.json'
2022-01-03 22:00:38 +00:00
if os.path.isfile(access_keys_filename):
access_keys_from_file = load_json(access_keys_filename)
if access_keys_from_file:
access_keys = access_keys_from_file
2021-04-22 21:46:02 +00:00
2022-05-14 12:15:26 +00:00
timeline_key = access_keys['menuTimeline']
submit_key = access_keys['submitButton']
2022-01-03 22:00:38 +00:00
access_keys_form = ''
2021-12-31 21:18:12 +00:00
css_filename = base_dir + '/epicyon-profile.css'
2021-12-25 16:17:53 +00:00
if os.path.isfile(base_dir + '/epicyon.css'):
2021-12-31 21:18:12 +00:00
css_filename = base_dir + '/epicyon.css'
2021-04-22 21:46:02 +00:00
2022-01-03 22:00:38 +00:00
instance_title = \
2021-12-26 14:08:58 +00:00
get_config_param(base_dir, 'instanceTitle')
2022-01-03 22:00:38 +00:00
access_keys_form = \
html_header_with_external_style(css_filename, instance_title, None)
2022-05-14 12:15:26 +00:00
access_keys_form += \
'<header>\n' + \
'<a href="/users/' + nickname + '/' + \
default_timeline + '" title="' + \
translate['Switch to timeline view'] + '" alt="' + \
2022-05-14 12:19:27 +00:00
translate['Switch to timeline view'] + '">\n'
2022-05-14 12:15:26 +00:00
banner_file, _ = \
get_banner_file(base_dir, nickname, domain, theme)
access_keys_form += '<img loading="lazy" decoding="async" ' + \
'class="timeline-banner" alt="" ' + \
'src="/users/' + nickname + '/' + banner_file + '" /></a>\n' + \
'</header>\n'
2022-01-03 22:00:38 +00:00
access_keys_form += '<div class="container">\n'
2021-04-22 21:46:02 +00:00
2022-01-03 22:00:38 +00:00
access_keys_form += \
2021-04-22 21:46:02 +00:00
' <h1>' + translate['Key Shortcuts'] + '</h1>\n'
2022-01-03 22:00:38 +00:00
access_keys_form += \
2021-04-23 19:59:55 +00:00
'<p>' + translate['These access keys may be used'] + \
'<label class="labels"></label></p>'
2021-04-22 21:46:02 +00:00
2022-01-03 22:00:38 +00:00
access_keys_form += ' <form method="POST" action="' + \
2021-04-22 21:46:02 +00:00
'/users/' + nickname + '/changeAccessKeys">\n'
2022-01-03 22:00:38 +00:00
access_keys_form += \
2021-04-22 22:28:16 +00:00
' <center>\n' + \
2021-04-22 22:37:52 +00:00
' <button type="submit" class="button" ' + \
2022-01-03 22:00:38 +00:00
'name="submitAccessKeysCancel" accesskey="' + timeline_key + '">' + \
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" ' + \
2022-01-03 22:00:38 +00:00
'name="submitAccessKeys" accesskey="' + submit_key + '">' + \
2022-05-23 18:48:45 +00:00
translate['Publish'] + '</button>\n </center>\n'
2021-04-22 21:46:02 +00:00
2022-01-03 22:00:38 +00:00
access_keys_form += ' <table class="accesskeys">\n'
access_keys_form += ' <colgroup>\n'
access_keys_form += ' <col span="1" class="accesskeys-left">\n'
access_keys_form += ' <col span="1" class="accesskeys-center">\n'
access_keys_form += ' </colgroup>\n'
access_keys_form += ' <tbody>\n'
2021-04-22 21:46:02 +00:00
2022-01-03 22:00:38 +00:00
for variable_name, key in default_access_keys.items():
if not translate.get(variable_name):
2021-04-22 21:46:02 +00:00
continue
2022-01-03 22:00:38 +00:00
key_str = '<tr>'
key_str += \
2021-04-22 21:46:02 +00:00
'<td><label class="labels">' + \
2022-01-03 22:00:38 +00:00
translate[variable_name] + '</label></td>'
if access_keys.get(variable_name):
key = access_keys[variable_name]
2021-04-23 14:21:57 +00:00
if len(key) > 1:
key = key[0]
2022-01-03 22:00:38 +00:00
key_str += \
2021-04-23 14:21:57 +00:00
'<td><input type="text" ' + \
2022-01-03 22:00:38 +00:00
'name="' + variable_name.replace(' ', '_') + '" ' + \
2021-04-23 14:21:57 +00:00
'value="' + key + '">'
2022-01-03 22:00:38 +00:00
key_str += '</td></tr>\n'
access_keys_form += key_str
2021-04-22 21:46:02 +00:00
2022-01-03 22:00:38 +00:00
access_keys_form += ' </tbody>\n'
access_keys_form += ' </table>\n'
access_keys_form += ' </form>\n'
access_keys_form += '</div>\n'
access_keys_form += html_footer()
return access_keys_form