From 4660240395841f3ed2462e3741c501f8c137033f Mon Sep 17 00:00:00 2001 From: Jannat Patel Date: Wed, 9 Aug 2023 14:29:51 +0530 Subject: [PATCH 1/4] fix: certificate share --- lms/install.py | 26 ++++++++++++++++--- .../lms_certificate/lms_certificate.py | 18 ++++++------- lms/www/courses/certificate.py | 7 +++-- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/lms/install.py b/lms/install.py index fca317b0..3bd0539b 100644 --- a/lms/install.py +++ b/lms/install.py @@ -9,6 +9,7 @@ def after_install(): def after_sync(): create_lms_roles() set_default_home() + set_default_certificate_print_format() add_all_roles_to("Administrator") @@ -76,7 +77,7 @@ def create_course_creator_role(): "desk_access": 0, } ) - role.save(ignore_permissions=True) + role.save() def create_moderator_role(): @@ -89,7 +90,7 @@ def create_moderator_role(): "desk_access": 0, } ) - role.save(ignore_permissions=True) + role.save() def create_evaluator_role(): @@ -102,7 +103,26 @@ def create_evaluator_role(): "desk_access": 0, } ) - role.save(ignore_permissions=True) + role.save() + + +def set_default_certificate_print_format(): + filters = { + "doc_type": "LMS Certificate", + "property": "default_print_format", + } + if not frappe.db.exists("Property Setter", filters): + filters.update( + { + "doctype_or_field": "DocType", + "property_type": "Data", + "value": "Certificate", + } + ) + + doc = frappe.new_doc("Property Setter") + doc.update(filters) + doc.save() def delete_custom_fields(): diff --git a/lms/lms/doctype/lms_certificate/lms_certificate.py b/lms/lms/doctype/lms_certificate/lms_certificate.py index bf3fc1c0..daa400e4 100644 --- a/lms/lms/doctype/lms_certificate/lms_certificate.py +++ b/lms/lms/doctype/lms_certificate/lms_certificate.py @@ -24,17 +24,15 @@ class LMSCertificate(Document): _("{0} is already certified for the course {1}").format(full_name, course_name) ) - def after_insert(self): - share = frappe.get_doc( - { - "doctype": "DocShare", - "read": 1, - "share_doctype": "LMS Certificate", - "share_name": self.name, - "user": self.member, - } + def on_update(self): + frappe.share.add_docshare( + self.doctype, + self.name, + self.member, + write=1, + share=1, + flags={"ignore_share_permission": True}, ) - share.save(ignore_permissions=True) @frappe.whitelist() diff --git a/lms/www/courses/certificate.py b/lms/www/courses/certificate.py index 0784db9d..55144da6 100644 --- a/lms/www/courses/certificate.py +++ b/lms/www/courses/certificate.py @@ -30,7 +30,7 @@ def get_context(context): ) context.url = f"{get_url()}/courses/{context.course.name}/{context.doc.name}" - default_print_format = frappe.db.get_value( + print_format = frappe.db.get_value( "Property Setter", { "doc_type": "LMS Certificate", @@ -40,8 +40,11 @@ def get_context(context): as_dict=True, ) + if not print_format: + print_format = "Certificate" + template = frappe.db.get_value( - "Print Format", default_print_format.value, ["html", "css"], as_dict=True + "Print Format", print_format.value, ["html", "css"], as_dict=True ) merged_template = "" + template.html final_template = render_template(merged_template, context) From 551936e7c47576fb35f72d90caeb8bd0bffe4094 Mon Sep 17 00:00:00 2001 From: Jannat Patel Date: Wed, 9 Aug 2023 16:29:36 +0530 Subject: [PATCH 2/4] fix: show logo on certificate only if its present --- lms/lms/print_format/certificate/certificate.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lms/lms/print_format/certificate/certificate.json b/lms/lms/print_format/certificate/certificate.json index cea5f505..0040359b 100644 --- a/lms/lms/print_format/certificate/certificate.json +++ b/lms/lms/print_format/certificate/certificate.json @@ -10,14 +10,14 @@ "doctype": "Print Format", "font_size": 14, "format_data": "{\"header\":\"
\\n\\t

LMS Certificate

\\n\\t

{{ doc.name }}

\\n
\",\"sections\":[{\"label\":\"\",\"columns\":[{\"label\":\"\",\"fields\":[{\"label\":\"Course\",\"fieldname\":\"course\",\"fieldtype\":\"Link\",\"options\":\"LMS Course\"},{\"label\":\"Member\",\"fieldname\":\"member\",\"fieldtype\":\"Link\",\"options\":\"User\"},{\"label\":\"Member Name\",\"fieldname\":\"member_name\",\"fieldtype\":\"Data\"},{\"label\":\"Evaluator\",\"fieldname\":\"evaluator\",\"fieldtype\":\"Data\",\"options\":\"\"}]},{\"label\":\"\",\"fields\":[{\"label\":\"Issue Date\",\"fieldname\":\"issue_date\",\"fieldtype\":\"Date\"},{\"label\":\"Expiry Date\",\"fieldname\":\"expiry_date\",\"fieldtype\":\"Date\"},{\"label\":\"Version\",\"fieldname\":\"version\",\"fieldtype\":\"Select\",\"options\":\"V13\\nV14\"},{\"label\":\"Module Names for Certificate\",\"fieldname\":\"module_names_for_certificate\",\"fieldtype\":\"Data\"}]}],\"has_fields\":true}]}", - "html": "{% set certificate = frappe.db.get_value(\"LMS Certificate\", doc.name, [\"name\", \"member\", \"issue_date\", \"expiry_date\", \"course\"], as_dict=True) %}\n{% set member = frappe.db.get_value(\"User\", doc.member, [\"full_name\"], as_dict=True) %}\n{% set course = frappe.db.get_value(\"LMS Course\", doc.course, [\"title\", \"name\", \"image\"], as_dict=True) %}\n{% set logo = frappe.db.get_single_value(\"Website Settings\", \"banner_image\") %}\n{% set instructors = frappe.get_all(\"Course Instructor\", {\"parent\": doc.course}, pluck=\"instructor\", order_by=\"idx\") %}\n\n\n\n\n\n\n
\n
\n \n \n
\n {{ _(\"This certifies that\") }}\n
\n \n
\n {{ member.full_name }}\n
\n
\n {{ _(\"has successfully completed the course on\") }}\n {{ course.title }} \n on {{ frappe.utils.format_date(certificate.issue_date, \"medium\") }}.\n
\n \n \n \n {% if instructors %}\n \n {% endif %}\n \n {% if certificate.expiry_date %}\n \n \n \n {% endif %}\n \n
\n \n
\n
{{ _(\"Course Instructor\") }}
\n
\n \n
\n
{{ _(\"Expiry Date\") }}
\n
\n
\n
", + "html": "{% set certificate = frappe.db.get_value(\"LMS Certificate\", doc.name, [\"name\", \"member\", \"issue_date\", \"expiry_date\", \"course\"], as_dict=True) %}\n{% set member = frappe.db.get_value(\"User\", doc.member, [\"full_name\"], as_dict=True) %}\n{% set course = frappe.db.get_value(\"LMS Course\", doc.course, [\"title\", \"name\", \"image\"], as_dict=True) %}\n{% set logo = frappe.db.get_single_value(\"Website Settings\", \"banner_image\") %}\n{% set instructors = frappe.get_all(\"Course Instructor\", {\"parent\": doc.course}, pluck=\"instructor\", order_by=\"idx\") %}\n\n\n\n\n\n\n
\n
\n \n {% if logo %}\n \n {% endif %}\n
\n {{ _(\"This certifies that\") }}\n
\n \n
\n {{ member.full_name }}\n
\n
\n {{ _(\"has successfully completed the course on\") }}\n {{ course.title }} \n on {{ frappe.utils.format_date(certificate.issue_date, \"medium\") }}.\n
\n \n \n \n {% if instructors %}\n \n {% endif %}\n \n {% if certificate.expiry_date %}\n \n \n \n {% endif %}\n \n
\n \n
\n
{{ _(\"Course Instructor\") }}
\n
\n \n
\n
{{ _(\"Expiry Date\") }}
\n
\n
\n
", "idx": 0, "line_breaks": 0, "margin_bottom": 0.0, "margin_left": 0.0, "margin_right": 0.0, "margin_top": 0.0, - "modified": "2023-04-17 13:46:38.633751", + "modified": "2023-08-09 16:29:07.233894", "modified_by": "Administrator", "module": "LMS", "name": "Certificate", From 59f08ad4da691e1a1444df04864d1f6e295e3d05 Mon Sep 17 00:00:00 2001 From: Jannat Patel Date: Wed, 9 Aug 2023 16:57:25 +0530 Subject: [PATCH 3/4] fix: raise error if certificate is not found in any condition --- .../print_format/certificate/certificate.json | 4 +- lms/www/courses/certificate.py | 41 +++++++++++++------ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/lms/lms/print_format/certificate/certificate.json b/lms/lms/print_format/certificate/certificate.json index 0040359b..acf11b9e 100644 --- a/lms/lms/print_format/certificate/certificate.json +++ b/lms/lms/print_format/certificate/certificate.json @@ -1,7 +1,7 @@ { "absolute_value": 0, "align_labels_right": 0, - "creation": "2023-02-22 21:36:54.560420", + "creation": "2023-08-09 16:46:41.214175", "css": ".outer-border {\n font-family: \"Inter\" sans-serif;\n font-size: 16px;\n border-radius: 0.5rem;\n border: 1px solid #E2E6E9;\n padding: 1rem;\n}\n\n.inner-border {\n border: 10px solid #0089FF;\n border-radius: 8px;\n text-align: center;\n padding: 6rem 4rem;\n background-color: #FFFFFF;\n}\n\n.certificate-logo {\n height: 1.5rem;\n margin-bottom: 4rem;\n}\n\n.certificate-name {\n font-size: 2rem;\n font-weight: 500;\n color: #192734;\n margin-bottom: 0.5rem;\n}\n\n.certificate-footer {\n margin: 4rem auto 0;\n width: 70%;\n text-align: center;\n}\n\n.certificate-footer-item {\n color: #192734;\n}\n\n.cursive-font {\n font-family: cursive;\n font-weight: 600;\n}\n\n.certificate-divider {\n margin: 0.5rem 0;\n}\n\n.certificate-expiry {\n margin-left: 2rem;\n}", "custom_format": 1, "disabled": 0, @@ -17,7 +17,7 @@ "margin_left": 0.0, "margin_right": 0.0, "margin_top": 0.0, - "modified": "2023-08-09 16:29:07.233894", + "modified": "2023-08-09 16:46:41.214175", "modified_by": "Administrator", "module": "LMS", "name": "Certificate", diff --git a/lms/www/courses/certificate.py b/lms/www/courses/certificate.py index 55144da6..dccb9515 100644 --- a/lms/www/courses/certificate.py +++ b/lms/www/courses/certificate.py @@ -1,4 +1,5 @@ import frappe +from frappe import _ from frappe.utils.jinja import render_template from frappe.utils import get_url @@ -30,18 +31,7 @@ def get_context(context): ) context.url = f"{get_url()}/courses/{context.course.name}/{context.doc.name}" - print_format = frappe.db.get_value( - "Property Setter", - { - "doc_type": "LMS Certificate", - "property": "default_print_format", - }, - ["value"], - as_dict=True, - ) - - if not print_format: - print_format = "Certificate" + print_format = get_print_format() template = frappe.db.get_value( "Print Format", print_format.value, ["html", "css"], as_dict=True @@ -54,3 +44,30 @@ def get_context(context): def redirect_to_course_list(): frappe.local.flags.redirect_location = "/courses" raise frappe.Redirect + + +def get_print_format(): + print_format = None + default = frappe.db.get_value( + "Property Setter", + { + "doc_type": "LMS Certificate", + "property": "default_print_format", + }, + "value", + ) + + if frappe.db.exists("Print Format", default): + print_format = default + + if not print_format and frappe.db.exists("Print Format", "Certificate"): + print_format = "Certificate" + + if not print_format: + raise ValueError( + _( + "Default Print Format is not set for Certificate. Please contact the Administrator." + ) + ) + + return print_format From 066e2ddc69932884d056d66a4608b32418c4a92d Mon Sep 17 00:00:00 2001 From: Jannat Patel Date: Wed, 9 Aug 2023 17:03:10 +0530 Subject: [PATCH 4/4] fix: print format value --- lms/lms/print_format/certificate/certificate.json | 4 ++-- lms/www/courses/certificate.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lms/lms/print_format/certificate/certificate.json b/lms/lms/print_format/certificate/certificate.json index acf11b9e..47b27f51 100644 --- a/lms/lms/print_format/certificate/certificate.json +++ b/lms/lms/print_format/certificate/certificate.json @@ -1,7 +1,7 @@ { "absolute_value": 0, "align_labels_right": 0, - "creation": "2023-08-09 16:46:41.214175", + "creation": "2023-08-09 17:02:21.430320", "css": ".outer-border {\n font-family: \"Inter\" sans-serif;\n font-size: 16px;\n border-radius: 0.5rem;\n border: 1px solid #E2E6E9;\n padding: 1rem;\n}\n\n.inner-border {\n border: 10px solid #0089FF;\n border-radius: 8px;\n text-align: center;\n padding: 6rem 4rem;\n background-color: #FFFFFF;\n}\n\n.certificate-logo {\n height: 1.5rem;\n margin-bottom: 4rem;\n}\n\n.certificate-name {\n font-size: 2rem;\n font-weight: 500;\n color: #192734;\n margin-bottom: 0.5rem;\n}\n\n.certificate-footer {\n margin: 4rem auto 0;\n width: 70%;\n text-align: center;\n}\n\n.certificate-footer-item {\n color: #192734;\n}\n\n.cursive-font {\n font-family: cursive;\n font-weight: 600;\n}\n\n.certificate-divider {\n margin: 0.5rem 0;\n}\n\n.certificate-expiry {\n margin-left: 2rem;\n}", "custom_format": 1, "disabled": 0, @@ -17,7 +17,7 @@ "margin_left": 0.0, "margin_right": 0.0, "margin_top": 0.0, - "modified": "2023-08-09 16:46:41.214175", + "modified": "2023-08-09 17:02:21.430320", "modified_by": "Administrator", "module": "LMS", "name": "Certificate", diff --git a/lms/www/courses/certificate.py b/lms/www/courses/certificate.py index dccb9515..91e4de53 100644 --- a/lms/www/courses/certificate.py +++ b/lms/www/courses/certificate.py @@ -34,7 +34,7 @@ def get_context(context): print_format = get_print_format() template = frappe.db.get_value( - "Print Format", print_format.value, ["html", "css"], as_dict=True + "Print Format", print_format, ["html", "css"], as_dict=True ) merged_template = "" + template.html final_template = render_template(merged_template, context)