diff options
author | 2018-05-18 16:00:31 +0100 | |
---|---|---|
committer | 2018-05-18 16:00:31 +0100 | |
commit | e1846928439aa2a7e660d870a083872c415c274d (patch) | |
tree | e716f3466ca3914f80b2ca102d5d345658af7bc8 /pysite/views/staff/render.py | |
parent | Update wiki footer in line with main site (diff) |
[Jams] Huge amount of work on code jam admin area
Diffstat (limited to 'pysite/views/staff/render.py')
-rw-r--r-- | pysite/views/staff/render.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/pysite/views/staff/render.py b/pysite/views/staff/render.py new file mode 100644 index 00000000..00c9a9f3 --- /dev/null +++ b/pysite/views/staff/render.py @@ -0,0 +1,63 @@ +import re + +from docutils.utils import SystemMessage +from flask import jsonify +from schema import Schema + +from pysite.base_route import APIView +from pysite.constants import EDITOR_ROLES, ValidationTypes +from pysite.decorators import api_params, csrf, require_roles +from pysite.rst import render + +SCHEMA = Schema([{ + "data": str +}]) + +MESSAGE_REGEX = re.compile(r"<string>:(\d+): \([A-Z]+/\d\) (.*)") + + +class RenderView(APIView): + path = "/render" # "path" means that it accepts slashes + name = "render" + + @csrf + @require_roles(*EDITOR_ROLES) + @api_params(schema=SCHEMA, validation_type=ValidationTypes.json) + def post(self, data): + if not len(data): + return jsonify({"error": "No data!"}) + + data = data[0]["data"] + try: + html = render(data, link_headers=False)["html"] + + return jsonify({"data": html}) + except SystemMessage as e: + lines = str(e) + data = { + "error": lines, + "error_lines": [] + } + + if "\n" in lines: + lines = lines.split("\n") + else: + lines = [lines] + + for message in lines: + match = MESSAGE_REGEX.match(message) + + if match: + data["error_lines"].append( + { + "row": int(match.group(1)) - 3, + "column": 0, + "type": "error", + "text": match.group(2) + } + ) + + print(data) + return jsonify(data) + except Exception as e: + return jsonify({"error": str(e)}) |