From 406244ff69635de5de51160156a6ea9c40f8cd84 Mon Sep 17 00:00:00 2001 From: Anand Chitipothu Date: Wed, 10 Mar 2021 12:25:14 +0000 Subject: [PATCH] Added the ability to save sketches Implemented by exposing an RPC method to save the sketch and calling from JS using `frappe.call`. Any logged-in user can create new sketches and the owner can edit his/her own sketches. --- .../lms/doctype/lms_sketch/lms_sketch.py | 33 +++++++- community/www/macros/livecode.html | 3 + community/www/sketches/sketch.html | 75 +++++++++++++++++-- community/www/sketches/sketch.py | 15 +++- 4 files changed, 112 insertions(+), 14 deletions(-) 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 @@
-

{{ sketch.title }}

-
By {{sketch.owner}}
+ {% if editable %} + +

+ +

+ {% else %} +

{{sketch.title}}

+ {% endif %} +
By {{sketch.get_owner_name()}}
- {% 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