From d4023e7dde422c2f43ef510d44572186dc5ac90d Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Sun, 29 Jul 2018 17:45:11 +0100 Subject: [PATCH] [Py2to3] More fixes for web ui --- deluge/tests/test_webserver.py | 1 + deluge/ui/web/common.py | 4 ++-- deluge/ui/web/json_api.py | 17 +++++++++-------- deluge/ui/web/server.py | 17 ++++++++++------- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/deluge/tests/test_webserver.py b/deluge/tests/test_webserver.py index c2f6da8da..2ddc99069 100644 --- a/deluge/tests/test_webserver.py +++ b/deluge/tests/test_webserver.py @@ -57,3 +57,4 @@ class WebServerTestCase(WebServerTestBase, WebServerMockBase): json = json_lib.loads(body) self.assertEqual(None, json['error']) + self.assertEqual('torrent_filehash', json['result']['name']) diff --git a/deluge/ui/web/common.py b/deluge/ui/web/common.py index 8e41feb7a..ee4f3cde5 100644 --- a/deluge/ui/web/common.py +++ b/deluge/ui/web/common.py @@ -16,7 +16,7 @@ from deluge import common def _(text): - return gettext.gettext(text).decode('utf-8') + return gettext.gettext(text) def escape(text): @@ -58,7 +58,7 @@ try: def render(self, *args, **data): data.update(self.builtins) rendered = MakoTemplate.render_unicode(self, *args, **data) - return rendered.encode('utf-8', 'replace') + return rendered.encode('utf-8') except ImportError: import warnings warnings.warn( diff --git a/deluge/ui/web/json_api.py b/deluge/ui/web/json_api.py index 066735043..28ce22e0c 100644 --- a/deluge/ui/web/json_api.py +++ b/deluge/ui/web/json_api.py @@ -134,16 +134,17 @@ class JSON(resource.Resource, component.Component): procedure calls and the request id. """ try: - request.json = json.loads(request.json) + request_data = json.loads(request.json.decode()) except (ValueError, TypeError): raise JSONException('JSON not decodable') try: - method = request.json['method'] - params = request.json['params'] - request_id = request.json['id'] + method = request_data['method'] + params = request_data['params'] + request_id = request_data['id'] 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) 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 _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') raise JSONException(message) @@ -220,7 +221,7 @@ class JSON(resource.Resource, component.Component): return '' response = json.dumps(response) request.setHeader(b'content-type', b'application/x-json') - request.write(compress(response, request)) + request.write(compress(response.encode(), request)) request.finish() 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. """ - if request.method != 'POST': + if request.method != b'POST': request.setResponseCode(http.NOT_ALLOWED) request.finish() return server.NOT_DONE_YET diff --git a/deluge/ui/web/server.py b/deluge/ui/web/server.py index 9ebe7515e..62691e335 100644 --- a/deluge/ui/web/server.py +++ b/deluge/ui/web/server.py @@ -107,11 +107,12 @@ class Upload(resource.Resource): """ # Block all other HTTP methods. - if request.method != 'POST': + if request.method != b'POST': request.setResponseCode(http.NOT_ALLOWED) return '' - if 'file' not in request.args: + print(request.args) + if b'file' not in request.args: request.setResponseCode(http.OK) return json.dumps({ 'success': True, @@ -150,19 +151,21 @@ class Render(resource.Resource): return self def render(self, request): + log.debug('Render template file: %s', request.render_file) if not hasattr(request, 'render_file'): request.setResponseCode(http.INTERNAL_SERVER_ERROR) 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) - filename = os.path.join('render', request.render_file) else: 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(filename)) + template = Template(filename=rpath(os.path.join('render', tpl_file))) return compress(template.render(), request)