basic working model

This commit is contained in:
Tim Bentley 2013-09-14 19:46:49 +01:00
parent cca76a90bd
commit 2531513ec5
4 changed files with 75 additions and 67 deletions

View File

@ -40,6 +40,8 @@ window.OpenLP = {
// defeat Safari bug // defeat Safari bug
targ = targ.parentNode; targ = targ.parentNode;
} }
var isSecure = false;
var isAuthorised = false;
return $(targ); return $(targ);
}, },
getSearchablePlugins: function () { getSearchablePlugins: function () {
@ -147,11 +149,13 @@ window.OpenLP = {
}, },
pollServer: function () { pollServer: function () {
$.getJSON( $.getJSON(
"/stage/poll", "/api/poll",
function (data, status) { function (data, status) {
var prevItem = OpenLP.currentItem; var prevItem = OpenLP.currentItem;
OpenLP.currentSlide = data.results.slide; OpenLP.currentSlide = data.results.slide;
OpenLP.currentItem = data.results.item; OpenLP.currentItem = data.results.item;
OpenLP.isSecure = data.results.isSecure;
OpenLP.isAuthorised = data.results.isAuthorised;
if ($("#service-manager").is(":visible")) { if ($("#service-manager").is(":visible")) {
if (OpenLP.currentService != data.results.service) { if (OpenLP.currentService != data.results.service) {
OpenLP.currentService = data.results.service; OpenLP.currentService = data.results.service;

View File

@ -26,7 +26,7 @@
window.OpenLP = { window.OpenLP = {
loadService: function (event) { loadService: function (event) {
$.getJSON( $.getJSON(
"/stage/service/list", "/api/service/list",
function (data, status) { function (data, status) {
OpenLP.nextSong = ""; OpenLP.nextSong = "";
$("#notes").html(""); $("#notes").html("");
@ -46,7 +46,7 @@ window.OpenLP = {
}, },
loadSlides: function (event) { loadSlides: function (event) {
$.getJSON( $.getJSON(
"/stage/controller/live/text", "/api/controller/live/text",
function (data, status) { function (data, status) {
OpenLP.currentSlides = data.results.slides; OpenLP.currentSlides = data.results.slides;
OpenLP.currentSlide = 0; OpenLP.currentSlide = 0;
@ -137,7 +137,7 @@ window.OpenLP = {
}, },
pollServer: function () { pollServer: function () {
$.getJSON( $.getJSON(
"/stage/poll", "/api/poll",
function (data, status) { function (data, status) {
OpenLP.updateClock(data); OpenLP.updateClock(data);
if (OpenLP.currentItem != data.results.item || if (OpenLP.currentItem != data.results.item ||

View File

@ -151,13 +151,12 @@ class HttpRouter(object):
('^/(main)$', {'function': self.serve_file, 'secure': False}), ('^/(main)$', {'function': self.serve_file, 'secure': False}),
(r'^/files/(.*)$', {'function': self.serve_file, 'secure': False}), (r'^/files/(.*)$', {'function': self.serve_file, 'secure': False}),
(r'^/api/poll$', {'function': self.poll, 'secure': False}), (r'^/api/poll$', {'function': self.poll, 'secure': False}),
(r'^/stage/poll$', {'function': self.poll, 'secure': False}),
(r'^/main/poll$', {'function': self.poll, 'secure': False}), (r'^/main/poll$', {'function': self.poll, 'secure': False}),
(r'^/main/image$', {'function': self.main_poll, 'secure': False}), (r'^/main/image$', {'function': self.main_poll, 'secure': False}),
(r'^/api/controller/(live|preview)/text$', {'function': self.controller_text, 'secure': False}),
(r'^/api/controller/(live|preview)/(.*)$', {'function': self.controller, 'secure': True}), (r'^/api/controller/(live|preview)/(.*)$', {'function': self.controller, 'secure': True}),
(r'^/stage/controller/(live|preview)/(.*)$', {'function': self.controller, 'secure': False}), (r'^/api/service/list$', {'function': self.service_list, 'secure': False}),
(r'^/api/service/(.*)$', {'function':self.service, 'secure': False}), (r'^/api/service/(.*)$', {'function': self.service, 'secure': True}),
(r'^/stage/service/(.*)$', {'function': self.service, 'secure': False}),
(r'^/api/display/(hide|show|blank|theme|desktop)$', {'function': self.display, 'secure': True}), (r'^/api/display/(hide|show|blank|theme|desktop)$', {'function': self.display, 'secure': True}),
(r'^/api/alert$', {'function': self.alert, 'secure': True}), (r'^/api/alert$', {'function': self.alert, 'secure': True}),
(r'^/api/plugin/(search)$', {'function': self.plugin_info, 'secure': False}), (r'^/api/plugin/(search)$', {'function': self.plugin_info, 'secure': False}),
@ -165,6 +164,7 @@ class HttpRouter(object):
(r'^/api/(.*)/live$', {'function': self.go_live, 'secure': True}), (r'^/api/(.*)/live$', {'function': self.go_live, 'secure': True}),
(r'^/api/(.*)/add$', {'function': self.add_to_service, 'secure': True}) (r'^/api/(.*)/add$', {'function': self.add_to_service, 'secure': True})
] ]
self.settings_section = 'remotes'
self.translate() self.translate()
self.html_dir = os.path.join(AppLocation.get_directory(AppLocation.PluginsDir), 'remotes', 'html') self.html_dir = os.path.join(AppLocation.get_directory(AppLocation.PluginsDir), 'remotes', 'html')
@ -215,7 +215,7 @@ class HttpRouter(object):
self.send_header('Content-type', 'text/html') self.send_header('Content-type', 'text/html')
self.end_headers() self.end_headers()
def do_notfound(self): def do_not_found(self):
self.send_response(404) self.send_response(404)
self.send_header('Content-type', 'text/html') self.send_header('Content-type', 'text/html')
self.end_headers() self.end_headers()
@ -289,7 +289,7 @@ class HttpRouter(object):
file_name = 'main.html' file_name = 'main.html'
path = os.path.normpath(os.path.join(self.html_dir, file_name)) path = os.path.normpath(os.path.join(self.html_dir, file_name))
if not path.startswith(self.html_dir): if not path.startswith(self.html_dir):
return self.http_not_found() return self.do_not_found()
ext = os.path.splitext(file_name)[1] ext = os.path.splitext(file_name)[1]
html = None html = None
if ext == '.html': if ext == '.html':
@ -320,7 +320,7 @@ class HttpRouter(object):
content = file_handle.read() content = file_handle.read()
except IOError: except IOError:
log.exception('Failed to open %s' % path) log.exception('Failed to open %s' % path)
return self._http_not_found() return self.do_not_found()
finally: finally:
if file_handle: if file_handle:
file_handle.close() file_handle.close()
@ -337,7 +337,10 @@ class HttpRouter(object):
'twelve': Settings().value('remotes/twelve hour'), 'twelve': Settings().value('remotes/twelve hour'),
'blank': self.live_controller.blank_screen.isChecked(), 'blank': self.live_controller.blank_screen.isChecked(),
'theme': self.live_controller.theme_screen.isChecked(), 'theme': self.live_controller.theme_screen.isChecked(),
'display': self.live_controller.desktop_screen.isChecked() 'display': self.live_controller.desktop_screen.isChecked(),
'version': 2,
'isSecure': Settings().value(self.settings_section + '/authentication enabled'),
'isAuthorised': self.authorised
} }
return json.dumps({'results': result}).encode() return json.dumps({'results': result}).encode()
@ -379,7 +382,7 @@ class HttpRouter(object):
try: try:
text = json.loads(self.request_data)['request']['text'] text = json.loads(self.request_data)['request']['text']
except KeyError as ValueError: except KeyError as ValueError:
return self._http_bad_request() return self.do_http_error()
text = urllib.parse.unquote(text) text = urllib.parse.unquote(text)
self.alerts_manager.emit(QtCore.SIGNAL('alerts_text'), [text]) self.alerts_manager.emit(QtCore.SIGNAL('alerts_text'), [text])
success = True success = True
@ -387,18 +390,10 @@ class HttpRouter(object):
success = False success = False
return json.dumps({'results': {'success': success}}).encode() return json.dumps({'results': {'success': success}}).encode()
def controller(self, display_type, action): def controller_text(self, var):
""" """
Perform an action on the slide controller. Perform an action on the slide controller.
``display_type``
This is the type of slide controller, either ``preview`` or ``live``.
``action``
The action to perform.
""" """
event = 'slidecontroller_%s_%s' % (display_type, action)
if action == 'text':
current_item = self.live_controller.service_item current_item = self.live_controller.service_item
data = [] data = []
if current_item: if current_item:
@ -420,12 +415,24 @@ class HttpRouter(object):
json_data = {'results': {'slides': data}} json_data = {'results': {'slides': data}}
if current_item: if current_item:
json_data['results']['item'] = self.live_controller.service_item.unique_identifier json_data['results']['item'] = self.live_controller.service_item.unique_identifier
else: return json.dumps(json_data).encode()
def controller(self, display_type, action):
"""
Perform an action on the slide controller.
``display_type``
This is the type of slide controller, either ``preview`` or ``live``.
``action``
The action to perform.
"""
event = 'slidecontroller_%s_%s' % (display_type, action)
if self.request_data: if self.request_data:
try: try:
data = json.loads(self.request_data)['request']['id'] data = json.loads(self.request_data)['request']['id']
except KeyError as ValueError: except KeyError as ValueError:
return self._http_bad_request() return self.do_http_error()
log.info(data) log.info(data)
# This slot expects an int within a list. # This slot expects an int within a list.
self.live_controller.emit(QtCore.SIGNAL(event), [data]) self.live_controller.emit(QtCore.SIGNAL(event), [data])
@ -434,6 +441,15 @@ class HttpRouter(object):
json_data = {'results': {'success': True}} json_data = {'results': {'success': True}}
return json.dumps(json_data).encode() return json.dumps(json_data).encode()
def service_list(self):
"""
Handles requests for service items in the service manager
``action``
The action to perform.
"""
return json.dumps({'results': {'items': self._get_service_items()}}).encode()
def service(self, action): def service(self, action):
""" """
Handles requests for service items in the service manager Handles requests for service items in the service manager
@ -441,17 +457,12 @@ class HttpRouter(object):
``action`` ``action``
The action to perform. The action to perform.
""" """
event = 'servicemanager_%s' % action event = 'servicemanager_%s_item' % action
if action == 'list':
return json.dumps({'results': {'items': self._get_service_items()}}).encode()
event += '_item'
if self.request_data: if self.request_data:
try: try:
# print(json.loads(self.request_data['data']))
print(json.loads(self.request_data))
data = json.loads(self.request_data)['request']['id'] data = json.loads(self.request_data)['request']['id']
except KeyError: except KeyError:
return self._http_bad_request() return self.do_http_error()
self.service_manager.emit(QtCore.SIGNAL(event), data) self.service_manager.emit(QtCore.SIGNAL(event), data)
else: else:
Registry().execute(event) Registry().execute(event)
@ -482,7 +493,7 @@ class HttpRouter(object):
try: try:
text = json.loads(self.request_data)['request']['text'] text = json.loads(self.request_data)['request']['text']
except KeyError as ValueError: except KeyError as ValueError:
return self._http_bad_request() return self.do_http_error()
text = urllib.parse.unquote(text) text = urllib.parse.unquote(text)
plugin = self.plugin_manager.get_plugin_by_name(plugin_name) plugin = self.plugin_manager.get_plugin_by_name(plugin_name)
if plugin.status == PluginStatus.Active and plugin.media_item and plugin.media_item.has_search: if plugin.status == PluginStatus.Active and plugin.media_item and plugin.media_item.has_search:
@ -498,11 +509,11 @@ class HttpRouter(object):
try: try:
id = json.loads(self.request_data)['request']['id'] id = json.loads(self.request_data)['request']['id']
except KeyError as ValueError: except KeyError as ValueError:
return self._http_bad_request() return self.do_http_error()
plugin = self.plugin_manager.get_plugin_by_name(plugin_name) plugin = self.plugin_manager.get_plugin_by_name(plugin_name)
if plugin.status == PluginStatus.Active and plugin.media_item: if plugin.status == PluginStatus.Active and plugin.media_item:
plugin.media_item.emit(QtCore.SIGNAL('%s_go_live' % plugin_name), [id, True]) plugin.media_item.emit(QtCore.SIGNAL('%s_go_live' % plugin_name), [id, True])
return self._http_success() return self.do_http_success()
def add_to_service(self, plugin_name): def add_to_service(self, plugin_name):
""" """
@ -511,12 +522,12 @@ class HttpRouter(object):
try: try:
id = json.loads(self.request_data)['request']['id'] id = json.loads(self.request_data)['request']['id']
except KeyError as ValueError: except KeyError as ValueError:
return self._http_bad_request() return self.do_http_error()
plugin = self.plugin_manager.get_plugin_by_name(plugin_name) plugin = self.plugin_manager.get_plugin_by_name(plugin_name)
if plugin.status == PluginStatus.Active and plugin.media_item: if plugin.status == PluginStatus.Active and plugin.media_item:
item_id = plugin.media_item.create_item_from_id(id) item_id = plugin.media_item.create_item_from_id(id)
plugin.media_item.emit(QtCore.SIGNAL('%s_add_to_service' % plugin_name), [item_id, True]) plugin.media_item.emit(QtCore.SIGNAL('%s_add_to_service' % plugin_name), [item_id, True])
self._http_success() self.do_http_success()
def _get_service_manager(self): def _get_service_manager(self):
""" """
@ -557,4 +568,3 @@ class HttpRouter(object):
return self._alerts_manager return self._alerts_manager
alerts_manager = property(_get_alerts_manager) alerts_manager = property(_get_alerts_manager)

View File

@ -72,20 +72,14 @@ class CustomHandler(BaseHTTPRequestHandler, HttpRouter):
""" """
if self.path == '/favicon.ico': if self.path == '/favicon.ico':
return return
#print(self.headers['content-type'],self.headers['content-length'])
if self.headers['content-type'] == 'application/text':
length = int(self.headers['content-length'])
postvars = parse_qs(self.rfile.read(length), keep_blank_values=1)
for var in postvars:
print(var)
#{"request": {"id": 1}}
if not hasattr(self, 'auth'): if not hasattr(self, 'auth'):
self.initialise() self.initialise()
function, args = self.process_http_request(self.path) function, args = self.process_http_request(self.path)
if not function: if not function:
self.do_http_error() self.do_http_error()
return return
if function['secure']: self.authorised = self.headers['Authorization'] is None
if function['secure'] and Settings().value(self.settings_section + '/authentication enabled'):
if self.headers['Authorization'] is None: if self.headers['Authorization'] is None:
self.do_authorisation() self.do_authorisation()
self.wfile.write(bytes('no auth header received', 'UTF-8')) self.wfile.write(bytes('no auth header received', 'UTF-8'))