diff --git a/community/lms/doctype/lms_sketch/lms_sketch.py b/community/lms/doctype/lms_sketch/lms_sketch.py
index 9bf57313..29742edb 100644
--- a/community/lms/doctype/lms_sketch/lms_sketch.py
+++ b/community/lms/doctype/lms_sketch/lms_sketch.py
@@ -3,8 +3,37 @@
# For license information, please see license.txt
from __future__ import unicode_literals
-# import frappe
+import frappe
from frappe.model.document import Document
class LMSSketch(Document):
- pass
+ def get_owner_name(self):
+ return self.owner.split("@")[0]
+
+@frappe.whitelist()
+def save_sketch(name, title, code):
+ if not name or name == "new":
+ doc = frappe.new_doc('LMS Sketch')
+ doc.title = title
+ doc.code = code
+ doc.runtime = 'python-canvas'
+ doc.insert()
+ status = "created"
+ else:
+ doc = frappe.get_doc("LMS Sketch", name)
+
+ if doc.owner != frappe.session.user:
+ return {
+ "ok": False,
+ "error": "Permission Denied"
+ }
+ doc.title = title
+ doc.code = code
+ doc.save()
+ status = "updated"
+ return {
+ "ok": True,
+ "status": status,
+ "name": doc.name,
+ }
+
diff --git a/community/www/macros/livecode.html b/community/www/macros/livecode.html
index 21715887..6d9feff9 100644
--- a/community/www/macros/livecode.html
+++ b/community/www/macros/livecode.html
@@ -25,6 +25,8 @@
{% macro LiveCodeEditorJS(name, code) %}
diff --git a/community/www/sketches/sketch.html b/community/www/sketches/sketch.html
index 98a9274a..a0509ef1 100644
--- a/community/www/sketches/sketch.html
+++ b/community/www/sketches/sketch.html
@@ -29,22 +29,81 @@
- {% if not sketch.name %}
-
- Saving sketches is not yet implemented. Coming soon!
-
- {% endif %}
+ {% if sketch.is_new() and not editable %}
+
+ Please login to save this sketch.
+
+ {% endif %}
- {{LiveCodeEditor(sketch.name, sketch.code) }}
+
+ {{LiveCodeEditor(sketch.name, sketch.code) }}
+
{% endblock %}
{%- block script %}
{{ super() }}
{{ LiveCodeEditorJS() }}
+
+
{%- endblock %}
diff --git a/community/www/sketches/sketch.py b/community/www/sketches/sketch.py
index 691f55d0..4991e185 100644
--- a/community/www/sketches/sketch.py
+++ b/community/www/sketches/sketch.py
@@ -5,6 +5,15 @@ def get_context(context):
course_name = get_queryparam("sketch", '/sketches')
context.sketch = get_sketch(course_name)
context.livecode_url = get_livecode_url()
+ context.editable = is_editable(context.sketch, frappe.sesson.user)
+
+def is_editable(sketch, user):
+ if sketch.name == "new":
+ # new sketches can be editable by any logged in user
+ return user != "Guest"
+ else:
+ # existing sketches are editable by the owner
+ return sketch.owner == user
def get_livecode_url():
doc = frappe.get_doc("LMS Settings")
@@ -20,15 +29,13 @@ def get_queryparam(name, redirect_when_not_found):
def get_sketch(name):
if name == 'new':
sketch = frappe.new_doc('LMS Sketch')
+ sketch.name = "new"
sketch.title = "New Sketch"
sketch.code = "circle(100, 100, 50)"
- sketch.owner = frappe.session.user.split("@")[0]
return sketch
try:
- sketch = frappe.get_doc('LMS Sketch', name)
+ return frappe.get_doc('LMS Sketch', name)
except frappe.exceptions.DoesNotExistError:
raise frappe.NotFound
- sketch.owner = sketch.owner.split("@")[0]
- return sketch