Fix mime types for JSON

This commit is contained in:
Tim Bentley 2013-09-28 06:10:44 +01:00
parent c05928c3d3
commit 6533fb5bb0

View File

@ -244,6 +244,14 @@ 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_json_header(self):
"""
Create a header for JSON messages
"""
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
def do_http_error(self): def do_http_error(self):
""" """
Create a error http header. Create a error http header.
@ -391,7 +399,7 @@ class HttpRouter(object):
'isSecure': Settings().value(self.settings_section + '/authentication enabled'), 'isSecure': Settings().value(self.settings_section + '/authentication enabled'),
'isAuthorised': self.authorised 'isAuthorised': self.authorised
} }
self.send_header('Content-type', 'application/json') self.do_json_header()
return json.dumps({'results': result}).encode() return json.dumps({'results': result}).encode()
def main_poll(self): def main_poll(self):
@ -401,7 +409,7 @@ class HttpRouter(object):
result = { result = {
'slide_count': self.live_controller.slide_count 'slide_count': self.live_controller.slide_count
} }
self.send_header('Content-type', 'application/json') self.do_json_header()
return json.dumps({'results': result}).encode() return json.dumps({'results': result}).encode()
def main_image(self): def main_image(self):
@ -411,7 +419,7 @@ class HttpRouter(object):
result = { result = {
'slide_image': 'data:image/png;base64,' + str(image_to_byte(self.live_controller.slide_image)) 'slide_image': 'data:image/png;base64,' + str(image_to_byte(self.live_controller.slide_image))
} }
self.send_header('Content-type', 'application/json') self.do_json_header()
return json.dumps({'results': result}).encode() return json.dumps({'results': result}).encode()
def display(self, action): def display(self, action):
@ -423,7 +431,7 @@ class HttpRouter(object):
This is the action, either ``hide`` or ``show``. This is the action, either ``hide`` or ``show``.
""" """
self.live_controller.emit(QtCore.SIGNAL('slidecontroller_toggle_display'), action) self.live_controller.emit(QtCore.SIGNAL('slidecontroller_toggle_display'), action)
self.send_header('Content-type', 'application/json') self.do_json_header()
return json.dumps({'results': {'success': True}}).encode() return json.dumps({'results': {'success': True}}).encode()
def alert(self): def alert(self):
@ -441,7 +449,7 @@ class HttpRouter(object):
success = True success = True
else: else:
success = False success = False
self.send_header('Content-type', 'application/json') self.do_json_header()
return json.dumps({'results': {'success': success}}).encode() return json.dumps({'results': {'success': success}}).encode()
def controller_text(self, var): def controller_text(self, var):
@ -469,7 +477,7 @@ 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
self.send_header('Content-type', 'application/json') self.do_json_header()
return json.dumps(json_data).encode() return json.dumps(json_data).encode()
def controller(self, display_type, action): def controller(self, display_type, action):
@ -494,7 +502,7 @@ class HttpRouter(object):
else: else:
self.live_controller.emit(QtCore.SIGNAL(event)) self.live_controller.emit(QtCore.SIGNAL(event))
json_data = {'results': {'success': True}} json_data = {'results': {'success': True}}
self.send_header('Content-type', 'application/json') self.do_json_header()
return json.dumps(json_data).encode() return json.dumps(json_data).encode()
def service_list(self): def service_list(self):
@ -504,7 +512,7 @@ class HttpRouter(object):
``action`` ``action``
The action to perform. The action to perform.
""" """
self.send_header('Content-type', 'application/json') self.do_json_header()
return json.dumps({'results': {'items': self._get_service_items()}}).encode() return json.dumps({'results': {'items': self._get_service_items()}}).encode()
def service(self, action): def service(self, action):
@ -523,7 +531,7 @@ class HttpRouter(object):
self.service_manager.emit(QtCore.SIGNAL(event), data) self.service_manager.emit(QtCore.SIGNAL(event), data)
else: else:
Registry().execute(event) Registry().execute(event)
self.send_header('Content-type', 'application/json') self.do_json_header()
return json.dumps({'results': {'success': True}}).encode() return json.dumps({'results': {'success': True}}).encode()
def plugin_info(self, action): def plugin_info(self, action):
@ -539,7 +547,7 @@ class HttpRouter(object):
for plugin in self.plugin_manager.plugins: for plugin in self.plugin_manager.plugins:
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:
searches.append([plugin.name, str(plugin.text_strings[StringContent.Name]['plural'])]) searches.append([plugin.name, str(plugin.text_strings[StringContent.Name]['plural'])])
self.send_header('Content-type', 'application/json') self.do_json_header()
return json.dumps({'results': {'items': searches}}).encode() return json.dumps({'results': {'items': searches}}).encode()
def search(self, plugin_name): def search(self, plugin_name):
@ -559,7 +567,7 @@ class HttpRouter(object):
results = plugin.media_item.search(text, False) results = plugin.media_item.search(text, False)
else: else:
results = [] results = []
self.send_header('Content-type', 'application/json') self.do_json_header()
return json.dumps({'results': {'items': results}}).encode() return json.dumps({'results': {'items': results}}).encode()
def go_live(self, plugin_name): def go_live(self, plugin_name):