From 943c8eabbfe24fd71f0c6c245cd9911ecdb36249 Mon Sep 17 00:00:00 2001 From: Anand Chitipothu Date: Thu, 9 Dec 2021 01:17:03 +0530 Subject: [PATCH] feat: added custom pages at subgroup level --- school/lms/doctype/cohort/cohort.py | 13 ++++++++++--- school/www/cohorts/cohort.html | 2 +- school/www/cohorts/cohort.py | 2 +- school/www/cohorts/subgroup.html | 6 ++++++ school/www/cohorts/subgroup.py | 29 ++++++++++++++++++++++++++--- 5 files changed, 44 insertions(+), 8 deletions(-) diff --git a/school/lms/doctype/cohort/cohort.py b/school/lms/doctype/cohort/cohort.py index 04dfe508..429baac5 100644 --- a/school/lms/doctype/cohort/cohort.py +++ b/school/lms/doctype/cohort/cohort.py @@ -37,10 +37,17 @@ class Cohort(Document): filters = {"cohort": self.name, **kw} return frappe.db.count(doctype, filters=filters) - def get_page_template(self, slug): + def get_page_template(self, slug, scope=None): + p = self.get_page(slug, scope=scope) + return p and p.get_template_html() + + def get_page(self, slug, scope=None): for p in self.pages: - if p.slug == slug: - return p.get_template_html() + if p.slug == slug and scope in [p.scope, None]: + return p + + def get_pages(self, scope=None): + return [p for p in self.pages if scope in [p.scope, None]] def get_stats(self): return { diff --git a/school/www/cohorts/cohort.html b/school/www/cohorts/cohort.html index 0fe3ae26..007e5d70 100644 --- a/school/www/cohorts/cohort.html +++ b/school/www/cohorts/cohort.html @@ -24,7 +24,7 @@ diff --git a/school/www/cohorts/cohort.py b/school/www/cohorts/cohort.py index 358e268f..7f890367 100644 --- a/school/www/cohorts/cohort.py +++ b/school/www/cohorts/cohort.py @@ -27,5 +27,5 @@ def get_context(context): # Function to render to custom page given the slug context.render_page = lambda page: frappe.render_template( - cohort.get_page_template(page), + cohort.get_page_template(page, scope="Cohort"), context) diff --git a/school/www/cohorts/subgroup.html b/school/www/cohorts/subgroup.html index 983d2d62..a7d69895 100644 --- a/school/www/cohorts/subgroup.html +++ b/school/www/cohorts/subgroup.html @@ -12,6 +12,10 @@ {{ render_navitem("Students", "/students", stats.students, page=="students")}} {% if is_mentor or is_admin %} {{ render_navitem("Join Requests", "/join-requests", stats.join_requests, page=="join-requests")}} + + {% for p in cohort.get_pages(scope="Subgroup") %} + {{ render_navitem(p.title, "/" + p.slug, -1, page==p.slug) }} + {% endfor %} {% endif %} {% if is_admin %} {{ render_navitem("Admin", "/admin", -1, page=="admin")}} @@ -28,6 +32,8 @@ {{ render_join_requests() }} {% elif page == "admin" %} {{ render_admin() }} + {% else %} + {{ render_page(page) }} {% endif %} {% endblock %} diff --git a/school/www/cohorts/subgroup.py b/school/www/cohorts/subgroup.py index e213396d..94c83764 100644 --- a/school/www/cohorts/subgroup.py +++ b/school/www/cohorts/subgroup.py @@ -16,9 +16,24 @@ def get_context(context): 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", "join-requests", "admin"] - or (page == "join-requests" and not (is_mentor or is_admin)) - or (page == "admin" and not is_admin)): + if is_admin: + role = "Admin" + elif is_mentor: + role = "Mentor" + else: + role = "Public" + + pages = [ + ("mentors", ["Admin", "Mentor", "Public"]), + ("students", ["Admin", "Mentor", "Public"]), + ("join-requests", ["Admin", "Mentor"]), + ("admin", ["Admin"]) + ] + pages += [(p.slug, ["Admin", "Mentor"]) for p in cohort.get_pages(scope="Subgroup")] + + page_names = [p for p, roles in pages if role in roles] + + if page not in page_names: frappe.local.flags.redirect_location = subgroup.get_url() + "/mentors" raise frappe.Redirect @@ -35,9 +50,17 @@ def get_context(context): context.is_admin = is_admin context.is_mentor = is_mentor + # Function to render to custom page given the slug + context.render_page = lambda page: frappe.render_template( + cohort.get_page_template(page), + context) + def get_stats(subgroup): return { "join_requests": len(subgroup.get_join_requests()), "students": len(subgroup.get_students()), "mentors": len(subgroup.get_mentors()) } + +def has_page(cohort, page): + return cohort.get_page(page, scope="Subgroup")