From 8f1757d51e3f2bcd2ca2387eef298f7c764acddc Mon Sep 17 00:00:00 2001 From: bugy Date: Mon, 29 Apr 2019 21:46:35 +0200 Subject: [PATCH] #142 added script title to the webpage title --- src/model/external_model.py | 8 +++++++- src/model/server_conf.py | 4 +++- src/tests/external_model_test.py | 23 ++++++++++++++++++++++- src/tests/server_conf_test.py | 22 ++++++++++++++++++++++ src/web/server.py | 9 ++++----- web-src/js/index.js | 20 +++++++++++++++++--- 6 files changed, 75 insertions(+), 11 deletions(-) diff --git a/src/model/external_model.py b/src/model/external_model.py index 6e369387..bf6895ad 100644 --- a/src/model/external_model.py +++ b/src/model/external_model.py @@ -1,4 +1,3 @@ -import json from datetime import timezone from utils import date_utils @@ -105,3 +104,10 @@ def to_execution_info(request_parameters): info.param_values = param_values return info + + +def server_conf_to_external(server_config): + return { + 'title': server_config.title, + 'enableScriptTitles': server_config.enable_script_titles + } diff --git a/src/model/server_conf.py b/src/model/server_conf.py index 57cddbae..0941ebc0 100644 --- a/src/model/server_conf.py +++ b/src/model/server_conf.py @@ -5,7 +5,7 @@ import utils.file_utils as file_utils from auth.authorization import ANY_USER from model import model_helper -from model.model_helper import read_list, read_int_from_config +from model.model_helper import read_list, read_int_from_config, read_bool_from_config from utils.string_utils import strip LOGGER = logging.getLogger('server_conf') @@ -24,6 +24,7 @@ def __init__(self) -> None: self.logging_config = None self.admin_config = None self.title = None + self.enable_script_titles = None self.trusted_ips = [] self.user_groups = None self.admin_users = [] @@ -81,6 +82,7 @@ def from_json(conf_path, temp_folder): if json_object.get('title'): config.title = json_object.get('title') + config.enable_script_titles = read_bool_from_config('enable_script_titles', json_object, default=True) access_config = json_object.get('access') if access_config: diff --git a/src/tests/external_model_test.py b/src/tests/external_model_test.py index 1215bb43..2472d943 100644 --- a/src/tests/external_model_test.py +++ b/src/tests/external_model_test.py @@ -3,7 +3,8 @@ from datetime import datetime, timezone from execution.logging import HistoryEntry -from model.external_model import to_short_execution_log, to_long_execution_log +from model.external_model import to_short_execution_log, to_long_execution_log, server_conf_to_external +from model.server_conf import ServerConfig class TestHistoryEntry(unittest.TestCase): @@ -140,3 +141,23 @@ def _validate_translated_entry(self, def setUp(self): self.random_instance = random.seed(a=123) + + +class TestServerConf(unittest.TestCase): + def test_full_config(self): + config = ServerConfig() + config.title = 'test title' + config.enable_script_titles = False + + external_config = server_conf_to_external(config) + self.assertEqual('test title', external_config.get('title')) + self.assertIs(False, external_config.get('enableScriptTitles')) + + def test_config_with_none_values(self): + config = ServerConfig() + config.title = None + config.enable_script_titles = None + + external_config = server_conf_to_external(config) + self.assertIsNone(external_config.get('title')) + self.assertIsNone(external_config.get('enableScriptTitles')) diff --git a/src/tests/server_conf_test.py b/src/tests/server_conf_test.py index 52eb6dc7..10dc444f 100644 --- a/src/tests/server_conf_test.py +++ b/src/tests/server_conf_test.py @@ -119,6 +119,28 @@ def test_default_value(self): self.assertEqual(10, config.max_request_size_mb) +class TestSimpleConfigs(unittest.TestCase): + def test_server_title(self): + config = _from_json({'title': 'my server'}) + self.assertEqual('my server', config.title) + + def test_server_title_default(self): + config = _from_json({}) + self.assertIsNone(config.title) + + def test_enable_script_titles_enabled(self): + config = _from_json({'enable_script_titles': 'true'}) + self.assertIs(True, config.enable_script_titles) + + def test_enable_script_titles_disabled(self): + config = _from_json({'enable_script_titles': 'false'}) + self.assertIs(False, config.enable_script_titles) + + def test_enable_script_titles_default(self): + config = _from_json({}) + self.assertIs(True, config.enable_script_titles) + + def _from_json(content): json_obj = json.dumps(content) conf_path = os.path.join(test_utils.temp_folder, 'conf.json') diff --git a/src/web/server.py b/src/web/server.py index 1f8c6457..98d9be49 100755 --- a/src/web/server.py +++ b/src/web/server.py @@ -172,10 +172,9 @@ def set_default_headers(self): self.set_header('X-Frame-Options', 'DENY') -class GetServerTitle(BaseRequestHandler): +class GetServerConf(BaseRequestHandler): def get(self): - if self.application.server_title: - self.write(self.application.server_title) + self.write(external_model.server_conf_to_external(self.application.server_config)) class GetScripts(BaseRequestHandler): @@ -788,7 +787,7 @@ def init(server_config: ServerConfig, downloads_folder = file_download_feature.get_result_files_folder() - handlers = [(r'/conf/title', GetServerTitle), + handlers = [(r'/conf', GetServerConf), (r'/scripts', GetScripts), (r'/scripts/([^/]*)', ScriptConfigSocket), (r'/scripts/([^/]*)/([^/]*)/list-files', ScriptParameterListFiles), @@ -825,7 +824,7 @@ def init(server_config: ServerConfig, application.auth = auth - application.server_title = server_config.title + application.server_config = server_config application.authorizer = authorizer application.downloads_folder = downloads_folder application.file_download_feature = file_download_feature diff --git a/web-src/js/index.js b/web-src/js/index.js index d26819a8..0f51a35a 100644 --- a/web-src/js/index.js +++ b/web-src/js/index.js @@ -28,15 +28,21 @@ const scriptSelectionListeners = []; const scriptMenuItems = new Map(); const runningScriptExecutors = []; let activeScriptController = null; +let enableScriptTitles = false; +let mainTitle = document.title; window.onload = onLoad; function onLoad() { - authorizedCallHttp('conf/title', null, 'GET', function (result) { - if (result) { - document.title = result; + authorizedCallHttp('conf', null, 'GET', function (result) { + const config = JSON.parse(result); + if (!isNull(config.title)) { + mainTitle = config.title; } + enableScriptTitles = isNull(config.enableScriptTitles) || config.enableScriptTitles; + updateTitle(); }); + scriptSelectionListeners.push(updateTitle); var response = authorizedCallHttp('scripts'); @@ -504,4 +510,12 @@ function hideErrorPanel() { removeClass(scriptPanelContainer, 'collapsed'); hide(errorPanel); +} + +function updateTitle() { + if ((enableScriptTitles) && (!isNull(selectedScript))) { + document.title = selectedScript + ' - ' + mainTitle; + } else { + document.title = mainTitle; + } } \ No newline at end of file