From c97458a8fe25dba8bab2bf8502275c111602414e Mon Sep 17 00:00:00 2001 From: jcass Date: Sun, 4 Dec 2016 12:19:21 +0200 Subject: [PATCH] Show server information in loader and in navigation pane (hostname, ip address, port). Fixes #67. Fixes #209. --- README.rst | 2 ++ .../static/css/webclient.css | 10 ++++++ mopidy_musicbox_webclient/static/index.html | 17 +++++++--- .../static/js/controls.js | 1 - .../static/js/functionsvars.js | 7 ++-- mopidy_musicbox_webclient/static/js/gui.js | 2 -- mopidy_musicbox_webclient/static/mb.appcache | 2 +- mopidy_musicbox_webclient/web.py | 25 ++++++++++---- mopidy_musicbox_webclient/webclient.py | 6 ++-- tests/test_web.py | 33 +++++++++++++++++-- 10 files changed, 83 insertions(+), 22 deletions(-) diff --git a/README.rst b/README.rst index 169ea95..a329e75 100644 --- a/README.rst +++ b/README.rst @@ -110,6 +110,8 @@ v2.4.0 (UNRELEASED) - Only show 'Show Album' or 'Show Artist' options in popup menus if URI's for those resources are available. (Fixes: `#213 `_). +- Now shows correct hostname information in loader popup. (Fixes: `#209 `_). +- Now shows server name/IP address and port number at the bottom of the navigation pane. (Fixes: `#67 `_). v2.3.0 (2016-05-15) ------------------- diff --git a/mopidy_musicbox_webclient/static/css/webclient.css b/mopidy_musicbox_webclient/static/css/webclient.css index 48d4e5a..57ad59f 100644 --- a/mopidy_musicbox_webclient/static/css/webclient.css +++ b/mopidy_musicbox_webclient/static/css/webclient.css @@ -160,6 +160,16 @@ display: inline; } +div.hostInfo { + width: 100%; + text-align: center; +} + +span.hostInfo { + font-weight: normal; + font-size: 0.75em; +} + /******************** * Pages, content * ********************/ diff --git a/mopidy_musicbox_webclient/static/index.html b/mopidy_musicbox_webclient/static/index.html index db57490..3ac2ada 100644 --- a/mopidy_musicbox_webclient/static/index.html +++ b/mopidy_musicbox_webclient/static/index.html @@ -16,7 +16,7 @@ - + @@ -36,8 +36,8 @@ - -
+ +
+
  • +
    + {% if hostname == serverIP %} + {{programName}} running on {{ hostname }}:{{ serverPort}} + {% else %} + {{programName}} running on {{ hostname }}/{{ serverIP }}:{{ serverPort}} + {% end %} +
    +
  • @@ -216,7 +225,7 @@ diff --git a/mopidy_musicbox_webclient/static/js/controls.js b/mopidy_musicbox_webclient/static/js/controls.js index 41d5471..f1a032a 100644 --- a/mopidy_musicbox_webclient/static/js/controls.js +++ b/mopidy_musicbox_webclient/static/js/controls.js @@ -652,7 +652,6 @@ window.history.back() }, 10000) } - } return controls })) diff --git a/mopidy_musicbox_webclient/static/js/functionsvars.js b/mopidy_musicbox_webclient/static/js/functionsvars.js index a2d7dfc..3803bbd 100644 --- a/mopidy_musicbox_webclient/static/js/functionsvars.js +++ b/mopidy_musicbox_webclient/static/js/functionsvars.js @@ -49,7 +49,8 @@ var isMobile = /Mobile/.test(ua) var isWebkit = /WebKit/.test(ua) // constants -PROGRAM_NAME = 'MusicBox' +PROGRAM_NAME = $(document.body).data('program-name') +HOSTNAME = $(document.body).data('hostname') ARTIST_TABLE = '#artiststable' ALBUM_TABLE = '#albumstable' BROWSE_TABLE = '#browsetable' @@ -456,7 +457,7 @@ function showLoading (on) { if (on) { $('body').css('cursor', 'progress') $.mobile.loading('show', { - text: 'Loading data from ' + PROGRAM_NAME + '. Please wait...', + text: 'Loading data from ' + PROGRAM_NAME + ' on ' + HOSTNAME + '. Please wait...', textVisible: true, theme: 'a' }) @@ -469,7 +470,7 @@ function showLoading (on) { function showOffline (on) { if (on) { $.mobile.loading('show', { - text: 'Trying to reach ' + PROGRAM_NAME + '. Please wait...', + text: 'Trying to reach ' + PROGRAM_NAME + ' on ' + HOSTNAME + '. Please wait...', textVisible: true, theme: 'a' }) diff --git a/mopidy_musicbox_webclient/static/js/gui.js b/mopidy_musicbox_webclient/static/js/gui.js index 5c15d08..dec8810 100644 --- a/mopidy_musicbox_webclient/static/js/gui.js +++ b/mopidy_musicbox_webclient/static/js/gui.js @@ -449,8 +449,6 @@ function locationHashChanged () { $('#normalFooter').show() $('#nowPlayingFooter').hide() } - // Set the page title based on the hash. - document.title = PROGRAM_NAME return false } diff --git a/mopidy_musicbox_webclient/static/mb.appcache b/mopidy_musicbox_webclient/static/mb.appcache index 529ba4a..fa52763 100644 --- a/mopidy_musicbox_webclient/static/mb.appcache +++ b/mopidy_musicbox_webclient/static/mb.appcache @@ -1,6 +1,6 @@ CACHE MANIFEST -# 2016-05-15:v1 +# 2016-12-04:v1 NETWORK: * diff --git a/mopidy_musicbox_webclient/web.py b/mopidy_musicbox_webclient/web.py index 3ee687b..72f62fd 100644 --- a/mopidy_musicbox_webclient/web.py +++ b/mopidy_musicbox_webclient/web.py @@ -3,7 +3,9 @@ from __future__ import absolute_import, division, print_function, unicode_litera import json import logging +import socket import string +import urlparse import tornado.web @@ -33,23 +35,34 @@ class IndexHandler(tornado.web.RequestHandler): webclient = mmw.Webclient(config) + if webclient.is_music_box(): + program_name = 'MusicBox' + else: + program_name = 'Mopidy' + + url = urlparse.urlparse('%s://%s' % (self.request.protocol, self.request.host)) + port = url.port or 80 + self.__dict = { 'isMusicBox': json.dumps(webclient.is_music_box()), 'websocketUrl': webclient.get_websocket_url(self.request), 'hasAlarmClock': json.dumps(webclient.has_alarm_clock()), - 'onTrackClick': webclient.get_default_click_action() + 'onTrackClick': webclient.get_default_click_action(), + 'programName': program_name, + 'hostname': url.hostname, + 'serverIP': socket.gethostbyname(url.hostname), + 'serverPort': port + } self.__path = path - self.__title = string.Template('MusicBox on $hostname') + self.__title = string.Template('{} on $hostname'.format(program_name)) def get(self, path): return self.render(path, title=self.get_title(), **self.__dict) def get_title(self): - hostname, sep, port = self.request.host.rpartition(':') - if not sep or not port.isdigit(): - hostname, port = self.request.host, '80' - return self.__title.safe_substitute(hostname=hostname, port=port) + url = urlparse.urlparse('%s://%s' % (self.request.protocol, self.request.host)) + return self.__title.safe_substitute(hostname=url.hostname) def get_template_path(self): return self.__path diff --git a/mopidy_musicbox_webclient/webclient.py b/mopidy_musicbox_webclient/webclient.py index e4c6e79..e185b3a 100644 --- a/mopidy_musicbox_webclient/webclient.py +++ b/mopidy_musicbox_webclient/webclient.py @@ -26,16 +26,16 @@ class Webclient(object): if host or port: if not host: host = request.host.partition(':')[0] - logger.warning('Musicbox websocket_host not specified, ' + logger.warning('Mopidy websocket_host not specified, ' 'using %s', host) elif not port: port = self.config['http']['port'] - logger.warning('Musicbox websocket_port not specified, ' + logger.warning('Mopidy websocket_port not specified, ' 'using %s', port) protocol = 'ws' if request.protocol == 'https': protocol = 'wss' - ws_url = "%s://%s:%d/mopidy/ws" % (protocol, host, port) + ws_url = '%s://%s:%d/mopidy/ws' % (protocol, host, port) return ws_url diff --git a/tests/test_web.py b/tests/test_web.py index a77484c..e50d64e 100644 --- a/tests/test_web.py +++ b/tests/test_web.py @@ -46,13 +46,13 @@ class RedirectHandlerTest(BaseTest): response.headers['Location'].endswith('index.html') -class IndexHandlerTest(BaseTest): +class IndexHandlerTestMusicBox(BaseTest): def test_index_handler(self): response = self.fetch('/index.html', method='GET') assert response.code == 200 - def test_get_title(self): + def test_get_title_musicbox(self): response = self.fetch('/index.html', method='GET') body = tornado.escape.to_unicode(response.body) @@ -65,3 +65,32 @@ class IndexHandlerTest(BaseTest): assert 'data-is-musicbox="true"' in body assert 'data-has-alarmclock="false"' in body assert 'data-websocket-url=""' in body + assert 'data-on-track-click="' in body + assert 'data-program-name="' in body + assert 'data-hostname="' in body + + +class IndexHandlerTestMopidy(BaseTest): + + def get_app(self): + extension = Extension() + self.config = config.Proxy({'musicbox_webclient': { + 'enabled': True, + 'musicbox': False, + 'websocket_host': '', + 'websocket_port': '', + } + }) + return tornado.web.Application(extension.factory(self.config, mock.Mock())) + + def test_initialize_sets_dictionary_objects(self): + response = self.fetch('/index.html', method='GET') + body = tornado.escape.to_unicode(response.body) + + assert 'data-is-musicbox="false"' in body + + def test_get_title_mopidy(self): + response = self.fetch('/index.html', method='GET') + body = tornado.escape.to_unicode(response.body) + + assert 'Mopidy on localhost' in body