diff --git a/school/lms/api.py b/school/lms/api.py index 8621a89c..7e60cbb7 100644 --- a/school/lms/api.py +++ b/school/lms/api.py @@ -129,3 +129,30 @@ def undo_reject_cohort_join_request(join_request): r.status = "Pending" r.save(ignore_permissions=True) return {"ok": True} + +@frappe.whitelist() +def add_mentor_to_subgroup(subgroup, email): + try: + sg = frappe.get_doc("Cohort Subgroup", subgroup) + except frappe.DoesNotExistError: + return { + "ok": False, + "error": f"Invalid subgroup: {subgroup}" + } + + if not sg.get_cohort().is_admin(frappe.session.user) and "System Manager" not in frappe.get_roles(): + return { + "ok": False, + "error": "Permission Deined" + } + + try: + user = frappe.get_doc("User", email) + except frappe.DoesNotExistError: + return { + "ok": False, + "error": f"Invalid user: {email}" + } + + sg.add_mentor(email) + return {"ok": True} diff --git a/school/lms/doctype/cohort_subgroup/cohort_subgroup.py b/school/lms/doctype/cohort_subgroup/cohort_subgroup.py index 064a8e92..f5b6dc01 100644 --- a/school/lms/doctype/cohort_subgroup/cohort_subgroup.py +++ b/school/lms/doctype/cohort_subgroup/cohort_subgroup.py @@ -79,5 +79,17 @@ class CohortSubgroup(Document): def get_cohort(self): return frappe.get_doc("Cohort", self.cohort) + def add_mentor(self, email): + d = { + "doctype": "Cohort Mentor", + "subgroup": self.name, + "cohort": self.cohort, + "email": email + } + if frappe.db.exists(d): + return + doc = frappe.get_doc(d) + doc.insert(ignore_permissions=True) + #def after_doctype_insert(): # frappe.db.add_unique("Cohort Subgroup", ("cohort", "slug")) diff --git a/school/www/cohorts/subgroup.html b/school/www/cohorts/subgroup.html index ae8d3fba..983d2d62 100644 --- a/school/www/cohorts/subgroup.html +++ b/school/www/cohorts/subgroup.html @@ -2,13 +2,19 @@ {% block title %} Subgroup {{subgroup.title}} - {{ course.title }} {% endblock %} {% block page_content %} -

{{subgroup.title}} Subgroup

+

{{subgroup.title}} Subgroup

@@ -18,15 +24,24 @@ {{ render_mentors() }} {% elif page == "students" %} {{ render_students() }} - {% elif page == "admin" %} + {% elif page == "join-requests" %} {{ render_join_requests() }} + {% elif page == "admin" %} + {{ render_admin() }} {% endif %}
{% endblock %} -{% macro render_info() %} - {% if is_admin %} - {% endif %} +{% macro render_admin() %} +
+
Add a new mentor
+
+
+ +
+ +
+
{% endmacro %} {% macro render_mentors() %} @@ -202,6 +217,24 @@ $(function() { } }); } + + $("#add-mentor").click(function() { + var subgroup = $("#page-title").data("subgroup"); + var title = $("#page-title").data("title"); + var email = $("#mentor-email").val(); + frappe.call("school.lms.api.add_mentor_to_subgroup", { + subgroup: subgroup, + email: email + }) + .then(r => { + if (r.message.ok) { + frappe.msgprint(`Successfully added ${email} as mentor to ${title}`); + } + else { + frappe.msgprint(r.message.error); + } + }); + }); }); {% endblock %} diff --git a/school/www/cohorts/subgroup.py b/school/www/cohorts/subgroup.py index 602fc218..e213396d 100644 --- a/school/www/cohorts/subgroup.py +++ b/school/www/cohorts/subgroup.py @@ -13,9 +13,12 @@ def get_context(context): return page = frappe.form_dict.get("page") - is_admin = subgroup.is_manager(frappe.session.user) or "System Manager" in frappe.get_roles() + is_mentor = subgroup.is_mentor(frappe.session.user) + is_admin = cohort.is_admin(frappe.session.user) or "System Manager" in frappe.get_roles() - if page not in ["mentors", "students", "admin"] or (page == "admin" and not is_admin): + if (page not in ["mentors", "students", "join-requests", "admin"] + or (page == "join-requests" and not (is_mentor or is_admin)) + or (page == "admin" and not is_admin)): frappe.local.flags.redirect_location = subgroup.get_url() + "/mentors" raise frappe.Redirect @@ -30,6 +33,7 @@ def get_context(context): context.stats = get_stats(subgroup) context.page = page context.is_admin = is_admin + context.is_mentor = is_mentor def get_stats(subgroup): return {