From 5f8de7612b689a336442608009e2e201d03eefd6 Mon Sep 17 00:00:00 2001 From: Anand Chitipothu Date: Thu, 11 Mar 2021 11:53:06 +0000 Subject: [PATCH] Using the user fullname when rendering a sketch. Also refactored the portal page for sketches and moved the common code to the lms_sketch doctype module. --- .../lms/doctype/lms_sketch/lms_sketch.py | 34 ++++++++++++++++++- community/www/sketches/index.html | 2 +- community/www/sketches/index.py | 13 ++----- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/community/lms/doctype/lms_sketch/lms_sketch.py b/community/lms/doctype/lms_sketch/lms_sketch.py index 29742edb..2ef8f989 100644 --- a/community/lms/doctype/lms_sketch/lms_sketch.py +++ b/community/lms/doctype/lms_sketch/lms_sketch.py @@ -8,7 +8,7 @@ from frappe.model.document import Document class LMSSketch(Document): def get_owner_name(self): - return self.owner.split("@")[0] + return get_userinfo(self.owner)['full_name'] @frappe.whitelist() def save_sketch(name, title, code): @@ -37,3 +37,35 @@ def save_sketch(name, title, code): "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=['name', 'title', 'owner', 'modified'], + order_by='modified desc', + page_length=100 + ) + for s in sketches: + s['owner_name'] = get_userinfo(s['owner'])['full_name'] + return 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/www/sketches/index.html b/community/www/sketches/index.html index c9db0f4d..f67ef2a7 100644 --- a/community/www/sketches/index.html +++ b/community/www/sketches/index.html @@ -20,7 +20,7 @@
{{ sketch.modified }} {{sketch.title}} - By {{sketch.owner}} + By {{sketch.owner_name}}
{% endfor %} diff --git a/community/www/sketches/index.py b/community/www/sketches/index.py index 156604e9..3ab688e1 100644 --- a/community/www/sketches/index.py +++ b/community/www/sketches/index.py @@ -1,16 +1,7 @@ import frappe +from ...lms.doctype.lms_sketch.lms_sketch import get_recent_sketches def get_context(context): context.no_cache = 1 - context.sketches = get_sketches() + context.sketches = get_recent_sketches() -def get_sketches(): - sketches = frappe.get_all( - "LMS Sketch", - fields=['name', 'title', 'owner', 'modified'], - order_by='modified desc', - page_length=100 - ) - for s in sketches: - s['owner'] = s['owner'].split("@")[0] - return sketches