From 20ccc09869f107879b061f385f3e0337bc4180f0 Mon Sep 17 00:00:00 2001 From: Anand Chitipothu Date: Thu, 29 Apr 2021 10:44:05 +0530 Subject: [PATCH] refactor: sketches page - Added new widget SketchTeaser - Moved get_recent_sketches as static method in LMSSketch - Added `models.py` to make it easy to import Course and Sketch class - Updated the sketches template to use the SketchTeaser widget --- .../lms/doctype/lms_sketch/lms_sketch.py | 56 +++++++------------ community/lms/models.py | 5 ++ community/lms/widgets/SketchTeaser.html | 15 +++++ community/public/css/style.css | 24 +++++++- community/www/sketches/index.html | 34 ++--------- community/www/sketches/index.py | 4 +- 6 files changed, 70 insertions(+), 68 deletions(-) create mode 100644 community/lms/models.py create mode 100644 community/lms/widgets/SketchTeaser.html diff --git a/community/lms/doctype/lms_sketch/lms_sketch.py b/community/lms/doctype/lms_sketch/lms_sketch.py index 2f0120fe..e26ff490 100644 --- a/community/lms/doctype/lms_sketch/lms_sketch.py +++ b/community/lms/doctype/lms_sketch/lms_sketch.py @@ -10,9 +10,6 @@ from frappe.model.document import Document from . import livecode class LMSSketch(Document): - def get_owner_name(self): - return get_userinfo(self.owner)['full_name'] - @property def sketch_id(self): """Returns the numeric part of the name. @@ -21,6 +18,14 @@ class LMSSketch(Document): """ return self.name.replace("SKETCH-", "") + def get_owner(self): + """Returns the owner of this sketch as a document. + """ + return frappe.get_doc("User", self.owner) + + def get_owner_name(self): + return self.get_owner().full_name + def get_livecode_url(self): doc = frappe.get_cached_doc("LMS Settings") return doc.livecode_url @@ -46,6 +51,18 @@ class LMSSketch(Document): cache.set(key, value) return value + @staticmethod + def get_recent_sketches(limit=100): + """Returns the recent sketches. + """ + sketches = frappe.get_all( + "LMS Sketch", + fields='*', + order_by='modified desc', + page_length=limit + ) + return [frappe.get_doc(doctype='LMS Sketch', **doc) for doc in sketches] + def __repr__(self): return f"" @@ -76,36 +93,3 @@ def save_sketch(name, title, code): "status": status, "name": doc.name, } - -def get_recent_sketches(): - """Returns the recent sketches. - - The return value will be a list of dicts with each entry containing - the following fields: - - name - - title - - owner - - owner_name - - modified - """ - sketches = frappe.get_all( - "LMS Sketch", - fields='*', - order_by='modified desc', - page_length=100 - ) - for s in sketches: - s['owner_name'] = get_userinfo(s['owner'])['full_name'] - return [frappe.get_doc(doctype='LMS Sketch', **doc) for doc in sketches] - -def get_userinfo(email): - """Returns the username and fullname of a user. - - Please note that the email could be "Administrator" or "Guest" - as a special case to denote the system admin and guest user respectively. - """ - user = frappe.get_doc("User", email) - return { - "full_name": user.full_name, - "username": user.username - } diff --git a/community/lms/models.py b/community/lms/models.py new file mode 100644 index 00000000..00c910c3 --- /dev/null +++ b/community/lms/models.py @@ -0,0 +1,5 @@ +"""Handy module to make access to all doctypes from a single place. +""" +from .doctype.lms_course.lms_course import LMSCourse as Course +from .doctype.lms_sketch.lms_sketch import LMSSketch as Sketch + diff --git a/community/lms/widgets/SketchTeaser.html b/community/lms/widgets/SketchTeaser.html new file mode 100644 index 00000000..b827b59c --- /dev/null +++ b/community/lms/widgets/SketchTeaser.html @@ -0,0 +1,15 @@ +
+ + +
diff --git a/community/public/css/style.css b/community/public/css/style.css index bf157299..971e2db0 100644 --- a/community/public/css/style.css +++ b/community/public/css/style.css @@ -291,4 +291,26 @@ nav.navbar { .message-section { margin-left: 5%; display: inline-block; -} \ No newline at end of file +} + +.sketch-preview { + background: white; + border-radius: 10px; + border: 1px solid #ddd; + width: 220px; +} + +.sketch-preview svg { + width: 200px; + height: 200px; +} + +.sketch-image { + background: white; + padding: 10px; +} + +.sketch-footer { + padding: 10px; + background: #eee; +} diff --git a/community/www/sketches/index.html b/community/www/sketches/index.html index dd1a61de..71ad9996 100644 --- a/community/www/sketches/index.html +++ b/community/www/sketches/index.html @@ -16,37 +16,13 @@ Create a New Sketch
-
- {% for sketch in sketches %} -
-
- - - {% endfor %} + {% endfor %}
{% endblock %} - -{% block style %} - {{super()}} - -{% endblock %} diff --git a/community/www/sketches/index.py b/community/www/sketches/index.py index 3ab688e1..88bcd47e 100644 --- a/community/www/sketches/index.py +++ b/community/www/sketches/index.py @@ -1,7 +1,7 @@ import frappe -from ...lms.doctype.lms_sketch.lms_sketch import get_recent_sketches +from community.lms.models import Sketch def get_context(context): context.no_cache = 1 - context.sketches = get_recent_sketches() + context.sketches = Sketch.get_recent_sketches()