[Py2to3] More fixes for web ui

This commit is contained in:
Calum Lind 2018-07-29 17:45:11 +01:00
parent 0fd3c25684
commit d4023e7dde
4 changed files with 22 additions and 17 deletions

View File

@ -57,3 +57,4 @@ class WebServerTestCase(WebServerTestBase, WebServerMockBase):
json = json_lib.loads(body) json = json_lib.loads(body)
self.assertEqual(None, json['error']) self.assertEqual(None, json['error'])
self.assertEqual('torrent_filehash', json['result']['name'])

View File

@ -16,7 +16,7 @@ from deluge import common
def _(text): def _(text):
return gettext.gettext(text).decode('utf-8') return gettext.gettext(text)
def escape(text): def escape(text):
@ -58,7 +58,7 @@ try:
def render(self, *args, **data): def render(self, *args, **data):
data.update(self.builtins) data.update(self.builtins)
rendered = MakoTemplate.render_unicode(self, *args, **data) rendered = MakoTemplate.render_unicode(self, *args, **data)
return rendered.encode('utf-8', 'replace') return rendered.encode('utf-8')
except ImportError: except ImportError:
import warnings import warnings
warnings.warn( warnings.warn(

View File

@ -134,16 +134,17 @@ class JSON(resource.Resource, component.Component):
procedure calls and the request id. procedure calls and the request id.
""" """
try: try:
request.json = json.loads(request.json) request_data = json.loads(request.json.decode())
except (ValueError, TypeError): except (ValueError, TypeError):
raise JSONException('JSON not decodable') raise JSONException('JSON not decodable')
try: try:
method = request.json['method'] method = request_data['method']
params = request.json['params'] params = request_data['params']
request_id = request.json['id'] request_id = request_data['id']
except KeyError as ex: except KeyError as ex:
message = 'Invalid JSON request, missing param %s in %s' % (ex, request.json) message = 'Invalid JSON request, missing param %s in %s' % (
ex, request_data)
raise JSONException(message) raise JSONException(message)
result = None result = None
@ -185,7 +186,7 @@ class JSON(resource.Resource, component.Component):
Handler to take the json data as a string and pass it on to the Handler to take the json data as a string and pass it on to the
_handle_request method for further processing. _handle_request method for further processing.
""" """
if request.getHeader('content-type') != 'application/json': if request.getHeader(b'content-type') != b'application/json':
message = 'Invalid JSON request content-type: %s' % request.getHeader('content-type') message = 'Invalid JSON request content-type: %s' % request.getHeader('content-type')
raise JSONException(message) raise JSONException(message)
@ -220,7 +221,7 @@ class JSON(resource.Resource, component.Component):
return '' return ''
response = json.dumps(response) response = json.dumps(response)
request.setHeader(b'content-type', b'application/x-json') request.setHeader(b'content-type', b'application/x-json')
request.write(compress(response, request)) request.write(compress(response.encode(), request))
request.finish() request.finish()
return server.NOT_DONE_YET return server.NOT_DONE_YET
@ -228,7 +229,7 @@ class JSON(resource.Resource, component.Component):
""" """
Handles all the POST requests made to the /json controller. Handles all the POST requests made to the /json controller.
""" """
if request.method != 'POST': if request.method != b'POST':
request.setResponseCode(http.NOT_ALLOWED) request.setResponseCode(http.NOT_ALLOWED)
request.finish() request.finish()
return server.NOT_DONE_YET return server.NOT_DONE_YET

View File

@ -107,11 +107,12 @@ class Upload(resource.Resource):
""" """
# Block all other HTTP methods. # Block all other HTTP methods.
if request.method != 'POST': if request.method != b'POST':
request.setResponseCode(http.NOT_ALLOWED) request.setResponseCode(http.NOT_ALLOWED)
return '' return ''
if 'file' not in request.args: print(request.args)
if b'file' not in request.args:
request.setResponseCode(http.OK) request.setResponseCode(http.OK)
return json.dumps({ return json.dumps({
'success': True, 'success': True,
@ -150,19 +151,21 @@ class Render(resource.Resource):
return self return self
def render(self, request): def render(self, request):
log.debug('Render template file: %s', request.render_file)
if not hasattr(request, 'render_file'): if not hasattr(request, 'render_file'):
request.setResponseCode(http.INTERNAL_SERVER_ERROR) request.setResponseCode(http.INTERNAL_SERVER_ERROR)
return '' return ''
if request.render_file in self.template_files: request.setHeader(b'content-type', b'text/html')
tpl_file = request.render_file.decode()
if tpl_file in self.template_files:
request.setResponseCode(http.OK) request.setResponseCode(http.OK)
filename = os.path.join('render', request.render_file)
else: else:
request.setResponseCode(http.NOT_FOUND) request.setResponseCode(http.NOT_FOUND)
filename = os.path.join('render', '404.html') tpl_file = '404.html'
request.setHeader(b'content-type', b'text/html') template = Template(filename=rpath(os.path.join('render', tpl_file)))
template = Template(filename=rpath(filename))
return compress(template.render(), request) return compress(template.render(), request)