From 7001ddc96f85379e927dee2810cdef2811238ea8 Mon Sep 17 00:00:00 2001 From: Anand Chitipothu Date: Sun, 5 Dec 2021 01:04:46 +0530 Subject: [PATCH] feat: made all the cohort pages public There is some info on the page that is only accessible to mentors and admins and not shown to other users. --- school/hooks.py | 5 ++--- school/lms/doctype/cohort/cohort.py | 2 +- school/www/cohorts/cohort.html | 17 +++++++--------- school/www/cohorts/cohort.py | 7 ------- school/www/cohorts/subgroup.html | 31 +++++++++++++++++++---------- school/www/cohorts/subgroup.py | 16 ++++++++++----- 6 files changed, 41 insertions(+), 37 deletions(-) diff --git a/school/hooks.py b/school/hooks.py index d73c18cf..21f83c9c 100644 --- a/school/hooks.py +++ b/school/hooks.py @@ -144,9 +144,8 @@ website_route_rules = [ {"from_route": "/courses//manage", "to_route": "cohorts"}, {"from_route": "/courses//cohorts/", "to_route": "cohorts/cohort"}, {"from_route": "/courses//cohorts//", "to_route": "cohorts/cohort"}, - {"from_route": "/courses//subgroups//", "to_route": "cohorts/subgroup", "defaults": {"page": "info"}}, - {"from_route": "/courses//subgroups///students", "to_route": "cohorts/subgroup", "defaults": {"page": "students"}}, - {"from_route": "/courses//subgroups///join-requests", "to_route": "cohorts/subgroup", "defaults": {"page": "join-requests"}}, + {"from_route": "/courses//subgroups//", "to_route": "cohorts/subgroup"}, + {"from_route": "/courses//subgroups///", "to_route": "cohorts/subgroup"}, {"from_route": "/courses//join///", "to_route": "cohorts/join"}, {"from_route": "/users", "to_route": "profiles/profile"} ] diff --git a/school/lms/doctype/cohort/cohort.py b/school/lms/doctype/cohort/cohort.py index c0d9a191..04dfe508 100644 --- a/school/lms/doctype/cohort/cohort.py +++ b/school/lms/doctype/cohort/cohort.py @@ -10,7 +10,7 @@ class Cohort(Document): def get_subgroups(self, include_counts=False, sort_by=None): names = frappe.get_all("Cohort Subgroup", filters={"cohort": self.name}, pluck="name") - subgroups = [frappe.get_doc("Cohort Subgroup", name) for name in names] + subgroups = [frappe.get_cached_doc("Cohort Subgroup", name) for name in names] subgroups = sorted(subgroups, key=lambda sg: sg.title) if include_counts: diff --git a/school/www/cohorts/cohort.html b/school/www/cohorts/cohort.html index f785c4fb..0fe3ae26 100644 --- a/school/www/cohorts/cohort.html +++ b/school/www/cohorts/cohort.html @@ -22,7 +22,8 @@ {% endif %}
{% if page == "info" %} {{ render_info() }} + {% elif page == "mentors" %} + {{ render_mentors() }} {% elif page == "students" %} {{ render_students() }} - {% elif page == "join-requests" %} + {% elif page == "admin" %} {{ render_join_requests() }} {% endif %}
{% endblock %} {% macro render_info() %} -
Invite Link
- {% set link = subgroup.get_invite_link() %} -

{{link}} -
- Copy to Clipboard -

+ {% if is_admin %} + {% endif %} +{% endmacro %} +{% macro render_mentors() %}
Mentors
{% set mentors = subgroup.get_mentors() %} {% if mentors %} @@ -57,7 +59,14 @@ {% macro render_join_requests() %} - {% set join_requests = subgroup.get_join_requests() %} +
Invite Link
+ {% set link = subgroup.get_invite_link() %} +

{{link}} +
+ Copy to Clipboard +

+ +{% set join_requests = subgroup.get_join_requests() %}
Pending Requests
{% if join_requests %} diff --git a/school/www/cohorts/subgroup.py b/school/www/cohorts/subgroup.py index 7a4794b1..602fc218 100644 --- a/school/www/cohorts/subgroup.py +++ b/school/www/cohorts/subgroup.py @@ -4,9 +4,6 @@ from . import utils def get_context(context): context.no_cache = 1 course = utils.get_course() - if frappe.session.user == "Guest": - frappe.local.flags.redirect_location = "/login?redirect-to=" + frappe.request.path - raise frappe.Redirect() cohort = utils.get_cohort(course, frappe.form_dict['cohort']) subgroup = utils.get_subgroup(cohort, frappe.form_dict['subgroup']) @@ -15,6 +12,13 @@ def get_context(context): context.template = "www/404.html" return + page = frappe.form_dict.get("page") + is_admin = subgroup.is_manager(frappe.session.user) or "System Manager" in frappe.get_roles() + + if page not in ["mentors", "students", "admin"] or (page == "admin" and not is_admin): + frappe.local.flags.redirect_location = subgroup.get_url() + "/mentors" + raise frappe.Redirect + utils.add_nav(context, "All Courses", "/courses") utils.add_nav(context, course.title, f"/courses/{course.name}") utils.add_nav(context, "Cohorts", f"/courses/{course.name}/manage") @@ -24,10 +28,12 @@ def get_context(context): context.cohort = cohort context.subgroup = subgroup context.stats = get_stats(subgroup) - context.page = frappe.form_dict["page"] + context.page = page + context.is_admin = is_admin def get_stats(subgroup): return { "join_requests": len(subgroup.get_join_requests()), - "students": len(subgroup.get_students()) + "students": len(subgroup.get_students()), + "mentors": len(subgroup.get_mentors()) }