From 157f6b45e9ff67085e5e06c9c1767faa20fcfa59 Mon Sep 17 00:00:00 2001 From: Jannat Patel Date: Fri, 11 Nov 2022 10:40:29 +0530 Subject: [PATCH] feat: add pages to nav after install --- lms/hooks.py | 1 + lms/install.py | 42 ++++++++++++++ lms/lms/utils.py | 23 +++----- lms/patches.txt | 1 + lms/patches/v0_0/add_pages_to_nav.py | 5 ++ lms/patches/v0_0/convert_progress_to_float.py | 5 +- lms/public/js/common_functions.js | 56 +++++++++++-------- lms/www/classes/__init__.py | 0 lms/www/courses/index.js | 3 - 9 files changed, 95 insertions(+), 41 deletions(-) create mode 100644 lms/patches/v0_0/add_pages_to_nav.py create mode 100644 lms/www/classes/__init__.py diff --git a/lms/hooks.py b/lms/hooks.py index 52e30375..8fb9cb6f 100644 --- a/lms/hooks.py +++ b/lms/hooks.py @@ -58,6 +58,7 @@ web_include_js = ["website.bundle.js"] # ------------ # before_install = "lms.install.before_install" +after_install = "lms.install.add_pages_to_nav" after_sync = "lms.install.after_sync" after_uninstall = "lms.install.after_uninstall" diff --git a/lms/install.py b/lms/install.py index 4adf68cc..03645ab9 100644 --- a/lms/install.py +++ b/lms/install.py @@ -2,6 +2,10 @@ import frappe from frappe.desk.page.setup_wizard.setup_wizard import add_all_roles_to +def after_install(): + add_pages_to_nav() + + def after_sync(): create_lms_roles() set_default_home() @@ -91,3 +95,41 @@ def delete_custom_fields(): for field in fields: frappe.db.delete("Custom Field", {"fieldname": field}) frappe.db.commit() + + +def add_pages_to_nav(): + pages = [ + {"label": "Courses", "url": "/courses", "parent": "Explore", "idx": 2}, + {"label": "Statistics", "url": "/statistics", "parent": "Explore", "idx": 3}, + {"label": "Jobs", "url": "/jobs", "parent": "Explore", "idx": 4}, + {"label": "People", "url": "/community", "parent": "Explore", "idx": 5}, + ] + + if not frappe.db.exists("Top Bar Item", {"label": "Explore"}): + frappe.get_doc( + { + "doctype": "Top Bar Item", + "label": "Explore", + "parent": "Website Settings", + "parenttype": "Website Settings", + "parentfield": "top_bar_items", + "idx": 1, + } + ).save() + + for page in pages: + if not frappe.db.exists( + "Top Bar Item", {"url": ["like", "%" + page.get("url") + "%"]} + ): + frappe.get_doc( + { + "doctype": "Top Bar Item", + "label": page.get("label"), + "url": page.get("url"), + "parent_label": page.get("parent"), + "idx": page.get("idx"), + "parent": "Website Settings", + "parenttype": "Website Settings", + "parentfield": "top_bar_items", + } + ).save() diff --git a/lms/lms/utils.py b/lms/lms/utils.py index cada6ea3..edb256b0 100644 --- a/lms/lms/utils.py +++ b/lms/lms/utils.py @@ -659,19 +659,14 @@ def get_chart_data(chart_name, timespan, timegrain, from_date, to_date): @frappe.whitelist(allow_guest=True) def get_course_completion_data(): all_membership = frappe.db.count("LMS Batch Membership") - completed = frappe.db.count("LMS Batch Membership", { - "progress": ["like", "%100%"] - }) + completed = frappe.db.count("LMS Batch Membership", {"progress": ["like", "%100%"]}) return { - "labels": ["Completed", "In Progress"], - "datasets": [ - { - "name": "Course Completion", - "values": [ - completed, - all_membership - completed - ], - } - ], - } + "labels": ["Completed", "In Progress"], + "datasets": [ + { + "name": "Course Completion", + "values": [completed, all_membership - completed], + } + ], + } diff --git a/lms/patches.txt b/lms/patches.txt index 9a0d5fec..012a7f4c 100644 --- a/lms/patches.txt +++ b/lms/patches.txt @@ -36,3 +36,4 @@ lms.patches.v0_0.set_dashboard #11-10-2022 lms.patches.v0_0.set_courses_page_as_home lms.patches.v0_0.set_member_in_progress #09-11-2022 lms.patches.v0_0.convert_progress_to_float +lms.patches.v0_0.add_pages_to_nav #11-11-2022 diff --git a/lms/patches/v0_0/add_pages_to_nav.py b/lms/patches/v0_0/add_pages_to_nav.py new file mode 100644 index 00000000..3546e3dc --- /dev/null +++ b/lms/patches/v0_0/add_pages_to_nav.py @@ -0,0 +1,5 @@ +import frappe +from lms.install import add_pages_to_nav + +def execute(): + add_pages_to_nav() diff --git a/lms/patches/v0_0/convert_progress_to_float.py b/lms/patches/v0_0/convert_progress_to_float.py index 62489f45..28ba459a 100644 --- a/lms/patches/v0_0/convert_progress_to_float.py +++ b/lms/patches/v0_0/convert_progress_to_float.py @@ -1,8 +1,11 @@ import frappe from frappe.utils import flt + def execute(): frappe.reload_doc("lms", "doctype", "lms_course_progress") progress_records = frappe.get_all("LMS Batch Membership", fields=["name", "progress"]) for progress in progress_records: - frappe.db.set_value("LMS Batch Membership", progress.name, "progress", flt(progress.progress)) + frappe.db.set_value( + "LMS Batch Membership", progress.name, "progress", flt(progress.progress) + ) diff --git a/lms/public/js/common_functions.js b/lms/public/js/common_functions.js index f8f9c2ef..f60b4721 100644 --- a/lms/public/js/common_functions.js +++ b/lms/public/js/common_functions.js @@ -23,7 +23,6 @@ frappe.ready(() => { generate_graph("Lesson Completion", "#lesson-completion"); generate_course_completion_graph(); } - }); const setup_file_size = () => { @@ -59,10 +58,13 @@ const join_course = (e) => { callback: (data) => { if (data.message == "OK") { $(".no-preview-modal").modal("hide"); - frappe.show_alert({ - message: __("Enrolled successfully"), - indicator: "green", - }, 3); + frappe.show_alert( + { + message: __("Enrolled successfully"), + indicator: "green", + }, + 3 + ); setTimeout(function () { window.location.href = `/courses/${course}/learn/1.1`; }, 1000); @@ -86,12 +88,15 @@ const notify_user = (e) => { }, callback: (data) => { $(".no-preview-modal").modal("hide"); - frappe.show_alert({ - message: __( - "You have opted to be notified for this course. You will receive an email when the course becomes available." - ), - indicator: "green", - }, 3); + frappe.show_alert( + { + message: __( + "You have opted to be notified for this course. You will receive an email when the course becomes available." + ), + indicator: "green", + }, + 3 + ); setTimeout(() => { window.location.reload(); }, 3000); @@ -123,15 +128,16 @@ const add_chapter = (e) => { scroll_to_chapter_container(); }; - const scroll_to_chapter_container = () => { - $([document.documentElement, document.body]).animate({ - scrollTop: $(".new-chapter").offset().top, - }, 1000); + $([document.documentElement, document.body]).animate( + { + scrollTop: $(".new-chapter").offset().top, + }, + 1000 + ); $(".new-chapter").find(".chapter-title-main").focus(); }; - const save_chapter = (e) => { let target = $(e.currentTarget); let parent = target.closest(".chapter-parent"); @@ -157,7 +163,7 @@ const save_chapter = (e) => { }); }; -const generate_graph = (chart_name, element, type="line") => { +const generate_graph = (chart_name, element, type = "line") => { let date = frappe.datetime; frappe.call({ @@ -167,7 +173,7 @@ const generate_graph = (chart_name, element, type="line") => { timespan: "Select Date Range", timegrain: "Daily", from_date: date.add_days(date.get_today(), -30), - to_date: date.add_days(date.get_today(), +1) + to_date: date.add_days(date.get_today(), +1), }, callback: (data) => { render_chart(data.message, chart_name, element, type); @@ -191,12 +197,16 @@ const render_chart = (data, chart_name, element, type) => { }); }; - const generate_course_completion_graph = () => { frappe.call({ method: "lms.lms.utils.get_course_completion_data", callback: (data) => { - render_chart(data.message, "Course Completion", "#course-completion", "pie") - } - }) -} + render_chart( + data.message, + "Course Completion", + "#course-completion", + "pie" + ); + }, + }); +}; diff --git a/lms/www/classes/__init__.py b/lms/www/classes/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/lms/www/courses/index.js b/lms/www/courses/index.js index 7d75a3d0..f3bc44ec 100644 --- a/lms/www/courses/index.js +++ b/lms/www/courses/index.js @@ -1,5 +1,4 @@ frappe.ready(() => { - $(".nav-link").click((e) => { change_hash(e); }); @@ -7,10 +6,8 @@ frappe.ready(() => { if (window.location.hash) { open_tab(); } - }); - const change_hash = (e) => { window.location.hash = $(e.currentTarget).attr("href"); };