def gem_htpresent_stats(request, district_id=None): stats = [] user_location = get_location(request, district_id) location = Location.tree.root_nodes()[0] start_date, end_date = previous_calendar_month() dates = {"start": start_date, "end": end_date} values = total_submissions( "gemteachers", start_date=start_date, end_date=end_date, location=location, extra_filters={"eav__gemteachers_htpresent": 1}, ) gem_htpresent = location_values(user_location, values) stats.append(("head teachers reported present", gem_htpresent)) values = total_submissions( "gemteachers", start_date=start_date, end_date=end_date, location=location, extra_filters={"eav__gemteachers_htpresent": 0}, ) gem_htabsent = location_values(user_location, values) stats.append(("head teachers reported absent", gem_htabsent)) if type(gem_htpresent) == int and type(gem_htabsent) == int: tot = gem_htpresent + gem_htabsent else: tot = gem_htpresent if type(gem_htpresent) == int else gem_htabsent stats.append(("total reports received", tot)) num_schools = School.objects.filter(location__in=user_location.get_descendants(include_self=True)).count() if num_schools > 0 and type(gem_htpresent) == int: gem_htpresent /= float(num_schools) perc_present = "%0.1f%%" % (gem_htpresent * 100) perc_present = "-" if type(gem_htpresent) == str else perc_present if num_schools > 0 and type(gem_htabsent) == int: gem_htabsent /= float(num_schools) perc_absent = "%0.1f%%" % (gem_htabsent * 100) perc_absent = "-" if type(gem_htabsent) == str else perc_absent stats.append(("% present", perc_present)) stats.append(("% absent", perc_absent)) res = {} res["dates"] = dates res["stats"] = stats return res
def add_to_report(self, report, key, dictionary): val = total_submissions(self.keyword, report.start_date, report.end_date, report.location, self.extra_filters) for rdict in val: rdict["value"] = ( rdict["value"] / Location.objects.get(pk=rdict["location_id"]) .get_descendants(include_self=True) .aggregate(Count("schools"))["schools__count"] ) reorganize_location(key, val, dictionary)