diff --git a/isso/utils/__init__.py b/isso/utils/__init__.py index 4eb71e5..5167bdb 100644 --- a/isso/utils/__init__.py +++ b/isso/utils/__init__.py @@ -8,7 +8,7 @@ werkzeug = pkg_resources.get_distribution("werkzeug") import json import hashlib -from werkzeug.wrappers import Request +from werkzeug.wrappers import Request, Response from werkzeug.exceptions import BadRequest try: @@ -112,6 +112,14 @@ class JSONRequest(Request): raise BadRequest('Unable to read JSON request') +class JSONResponse(Response): + + def __init__(self, obj, *args, **kwargs): + kwargs["content_type"] = "application/json" + return super(JSONResponse, self).__init__( + json.dumps(obj).encode("utf-8"), *args, **kwargs) + + def markdown(text): return misaka.html(text, extensions= misaka.EXT_STRIKETHROUGH | misaka.EXT_SUPERSCRIPT | misaka.EXT_AUTOLINK diff --git a/isso/views/comments.py b/isso/views/comments.py index f6420a6..492e99c 100644 --- a/isso/views/comments.py +++ b/isso/views/comments.py @@ -16,7 +16,7 @@ from werkzeug.exceptions import BadRequest, Forbidden, NotFound from isso.compat import text_type as str from isso import utils, local -from isso.utils import http, parse, markdown +from isso.utils import http, parse, markdown, JSONResponse as JSON from isso.utils.crypto import pbkdf2 from isso.views import requires @@ -25,12 +25,6 @@ def sha1(text): return hashlib.sha1(text.encode('utf-8')).hexdigest() -class JSON(Response): - - def __init__(self, *args): - return super(JSON, self).__init__(*args, content_type='application/json') - - def xhr(func): """A decorator to check for CSRF on POST/PUT/DELETE using a