From c96e3ee2f96d9b3568da85f9f2b5fa155834a849 Mon Sep 17 00:00:00 2001 From: Anand Chitipothu Date: Thu, 2 Dec 2021 10:36:17 +0530 Subject: [PATCH] feat: show counts on cohort listing and view pages Issue #271 --- school/lms/doctype/cohort/cohort.py | 28 ++++++++++++++------- school/www/cohorts/cohort.html | 9 +++++++ school/www/cohorts/cohort.py | 2 +- school/www/cohorts/index.html | 38 ++++++++++++++++------------- school/www/cohorts/subgroup.py | 2 +- 5 files changed, 51 insertions(+), 28 deletions(-) diff --git a/school/lms/doctype/cohort/cohort.py b/school/lms/doctype/cohort/cohort.py index 6dbec93d..c2ace214 100644 --- a/school/lms/doctype/cohort/cohort.py +++ b/school/lms/doctype/cohort/cohort.py @@ -16,21 +16,31 @@ class Cohort(Document): if include_counts: mentors = self._get_subgroup_counts("Cohort Mentor") students = self._get_subgroup_counts("LMS Batch Membership") - join_requests = self._get_subgroup_counts("Cohort Join Request") + join_requests = self._get_subgroup_counts("Cohort Join Request", status="Pending") for s in subgroups: s.num_mentors = mentors.get(s.name, 0) s.num_students = students.get(s.name, 0) s.num_join_requests = join_requests.get(s.name, 0) return subgroups - def _get_subgroup_counts(self, doctype): - q = f""" - SELECT subgroup, count(*) as count - FROM `tab{doctype}` - WHERE cohort = %(cohort)s - GROUP BY subgroup""" - rows = frappe.db.sql(q, values={"cohort": self.name}) - return {subgroup: count for subgroup, count in rows} + def _get_subgroup_counts(self, doctype, **kw): + rows = frappe.get_list(doctype, + filters={"cohort": self.name, **kw}, + fields=['subgroup', 'count(*) as count'], + group_by='subgroup') + return {row['subgroup']: row['count'] for row in rows} + + def _get_count(self, doctype, **kw): + filters = {"cohort": self.name, **kw} + return frappe.db.count(doctype, filters=filters) + + def get_stats(self): + return { + "subgroups": self._get_count("Cohort Subgroup"), + "mentors": self._get_count("Cohort Mentor"), + "students": self._get_count("LMS Batch Membership"), + "join_requests": self._get_count("Cohort Join Request", status="Pending"), + } def get_subgroup(self, slug): q = dict(cohort=self.name, slug=slug) diff --git a/school/www/cohorts/cohort.html b/school/www/cohorts/cohort.html index 761a8406..e8c17abd 100644 --- a/school/www/cohorts/cohort.html +++ b/school/www/cohorts/cohort.html @@ -4,6 +4,15 @@ {% block page_content %}

{{cohort.title}} Cohort

+

+ {% set stats = cohort.get_stats() %} + + {{ stats.subgroups }} Subgroups + | {{ stats.mentors }} Mentors + | {{ stats.students }} students + | {{ stats.join_requests }} join requests +

+
Subgroups