From 15b8b99ae27f4dde25609522f4ae09651dda2f33 Mon Sep 17 00:00:00 2001 From: Anand Chitipothu Date: Wed, 7 Apr 2021 01:19:42 +0530 Subject: [PATCH 1/3] fix: fixed the issue with dynamic routes. - The code to install regex controller was not always executed. Forced the exection of that by adding it to community/__init__.py - Renoved the that code from hooks.py --- community/__init__.py | 2 ++ community/hooks.py | 3 --- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/community/__init__.py b/community/__init__.py index 95d1338e..eb67ffe7 100644 --- a/community/__init__.py +++ b/community/__init__.py @@ -3,3 +3,5 @@ from __future__ import unicode_literals __version__ = '0.0.1' +from .routing import install_regex_converter +install_regex_converter() diff --git a/community/hooks.py b/community/hooks.py index 7669e2fe..bdc9e026 100644 --- a/community/hooks.py +++ b/community/hooks.py @@ -127,9 +127,6 @@ scheduler_events = { # # auto_cancel_exempted_doctypes = ["Auto Repeat"] -from .routing import install_regex_converter -install_regex_converter() - # Add all simple route rules here primary_rules = [ {"from_route": "/sketches", "to_route": "sketches"}, From f1508033a3499b8329b9338748db3ad582a87e38 Mon Sep 17 00:00:00 2001 From: Anand Chitipothu Date: Wed, 7 Apr 2021 01:32:19 +0530 Subject: [PATCH 2/3] feat: implemented autosave for sections Now the changes made to the code in each section will be autosaved and loaded back on next page load. --- community/__init__.py | 3 ++ community/lms/api.py | 23 +++++++++++++ .../lms/doctype/lms_section/lms_section.py | 20 ++++++++++- community/www/courses/topic.html | 33 +++++++++++++++++-- community/www/macros/livecode.html | 3 +- 5 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 community/lms/api.py diff --git a/community/__init__.py b/community/__init__.py index eb67ffe7..65fc9069 100644 --- a/community/__init__.py +++ b/community/__init__.py @@ -5,3 +5,6 @@ __version__ = '0.0.1' from .routing import install_regex_converter install_regex_converter() + +# load the methods from the lms api +from .lms import api # noqa diff --git a/community/lms/api.py b/community/lms/api.py new file mode 100644 index 00000000..ab69ede4 --- /dev/null +++ b/community/lms/api.py @@ -0,0 +1,23 @@ +"""API methods for the LMS. +""" + +import frappe + +@frappe.whitelist() +def autosave_section(section, code): + """Saves the code edited in one of the sections. + """ + doc = frappe.get_doc( + doctype="Code Revision", + section=section, + code=code, + author=frappe.session.user) + doc.insert() + return {"name": doc.name} + +@frappe.whitelist() +def get_section(name): + """Saves the code edited in one of the sections. + """ + doc = frappe.get_doc("LMS Section", name) + return doc and doc.as_dict() diff --git a/community/lms/doctype/lms_section/lms_section.py b/community/lms/doctype/lms_section/lms_section.py index ad40ece6..f51fec69 100644 --- a/community/lms/doctype/lms_section/lms_section.py +++ b/community/lms/doctype/lms_section/lms_section.py @@ -3,9 +3,27 @@ # For license information, please see license.txt from __future__ import unicode_literals -# import frappe +import frappe from frappe.model.document import Document class LMSSection(Document): def __repr__(self): return f"" + + def get_latest_code_for_user(self): + """Returns the latest code for the logged in user. + """ + if not frappe.session.user or frappe.session.user == "Guest": + return self.contents + result = frappe.get_all('Code Revision', + fields=["code"], + filters={ + "author": frappe.session.user, + "section": self.name + }, + order_by="creation desc", + page_length=1) + if result: + return result[0]['code'] + else: + return self.contents diff --git a/community/www/courses/topic.html b/community/www/courses/topic.html index 67df39dc..bf23c6d2 100644 --- a/community/www/courses/topic.html +++ b/community/www/courses/topic.html @@ -44,7 +44,7 @@ {% if s.type == "text" %} {{ render_section_text(s) }} {% elif s.type == "example" or s.type == "code" %} - {{ LiveCodeEditor(s.name, s.contents) }} + {{ LiveCodeEditor(s.name, s.get_latest_code_for_user()) }} {% else %}
Unknown section type: {{s.type}}
{% endif %} @@ -63,11 +63,40 @@