Skip to content

Commit

Permalink
#142 added script title to the webpage title
Browse files Browse the repository at this point in the history
  • Loading branch information
bugy committed Apr 29, 2019
1 parent dee6234 commit 8f1757d
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 11 deletions.
8 changes: 7 additions & 1 deletion src/model/external_model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
from datetime import timezone

from utils import date_utils
Expand Down Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion src/model/server_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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 = []
Expand Down Expand Up @@ -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:
Expand Down
23 changes: 22 additions & 1 deletion src/tests/external_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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'))
22 changes: 22 additions & 0 deletions src/tests/server_conf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
9 changes: 4 additions & 5 deletions src/web/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down
20 changes: 17 additions & 3 deletions web-src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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;
}
}

0 comments on commit 8f1757d

Please sign in to comment.