diff --git a/community/lms/doctype/exercise/exercise.py b/community/lms/doctype/exercise/exercise.py index ae9c44eb..b9e9246e 100644 --- a/community/lms/doctype/exercise/exercise.py +++ b/community/lms/doctype/exercise/exercise.py @@ -3,8 +3,12 @@ import frappe from frappe.model.document import Document +from ..lms_sketch.livecode import livecode_to_svg class Exercise(Document): + def before_save(self): + self.image = livecode_to_svg(None, self.answer) + def get_user_submission(self): """Returns the latest submission for this user. """ diff --git a/community/lms/doctype/lms_sketch/livecode.py b/community/lms/doctype/lms_sketch/livecode.py index 8ea05ddd..ab5d8dc5 100644 --- a/community/lms/doctype/lms_sketch/livecode.py +++ b/community/lms/doctype/lms_sketch/livecode.py @@ -4,6 +4,7 @@ import websocket import json from .svg import SVG import frappe +from urllib.parse import urlparse # Files to pass to livecode server # The same code is part of livecode-canvas.js @@ -60,9 +61,21 @@ def clear(): clear() ''' +def get_livecode_url(): + doc = frappe.get_cached_doc("LMS Settings") + return doc.livecode_url + +def get_livecode_ws_url(): + url = urlparse(get_livecode_url()) + protocol = "wss" if url.scheme == "https" else "ws" + return protocol + "://" + url.netloc + "/livecode" + def livecode_to_svg(livecode_ws_url, code, *, timeout=3): """Renders the code as svg. """ + if livecode_ws_url is None: + livecode_ws_url = get_livecode_ws_url() + try: ws = websocket.WebSocket() ws.settimeout(timeout) diff --git a/community/lms/widgets/Exercise.html b/community/lms/widgets/Exercise.html index af3ba383..7e075058 100644 --- a/community/lms/widgets/Exercise.html +++ b/community/lms/widgets/Exercise.html @@ -4,6 +4,10 @@