def abuse_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_attribute_value("gemabuse_cases", start_date=start_date, end_date=end_date, location=location)
    stats.append(("GEM reported abuse cases", location_values(user_location, values)))

    htabuse = (
        Poll.objects.get(name="emis_abuse")
        .responses.exclude(has_errors=True)
        .filter(date__range=(start_date, end_date))
        .filter(
            message__connection__contact__emisreporter__reporting_location__in=user_location.get_descendants(
                include_self=True
            ).all()
        )
        .values("eav_values__value_int")
        .annotate(Sum("eav_values__value_int"))
        .values_list("eav_values__value_int__sum", flat=True)
    )
    stats.append(("headteacher reported abuse cases", htabuse[0] if htabuse[0] else "-"))
    res = {}
    res["dates"] = dates
    res["stats"] = stats
    return res
def meals_stats(request, district_id=None):
    stats = []
    user_location = get_location(request, district_id)
    start_date, end_date = previous_calendar_month()
    dates = {"start": start_date, "end": end_date}
    expected_strs = ["none", "very few", "few", "less than half", "more than half", "very many"]

    meals = (
        Poll.objects.get(name="emis_meals")
        .responses.exclude(has_errors=True)
        .filter(date__range=(start_date, end_date))
        .filter(
            message__connection__contact__emisreporter__reporting_location__in=user_location.get_descendants(
                include_self=True
            ).all()
        )
        .values("eav_values__value_text")
        .annotate(Count("eav_values__value_text"))
    )
    for cat in expected_strs:
        num = 0
        for ct in meals:
            if ct["eav_values__value_text"]:
                if ct["eav_values__value_text"].lower() == cat:
                    num += ct["eav_values__value_text__count"]
        stats.append((cat, num if num else "-"))
    res = {}
    res["dates"] = dates
    res["stats"] = stats
    return res
    def test_should_calculate_first_day_of_previous_month_from_now(self):
        end_date = datetime.datetime.now().date()
        start_date = end_date - datetime.timedelta(days=30)

        returned_tuple = previous_calendar_month()
        returned_start_date = returned_tuple[0].date()
        returned_end_date = returned_tuple[1].date()

        self.assertEquals((returned_start_date, returned_end_date), (start_date, end_date))
    def test_should_calculate_first_day_of_previous_month_from_now(self):
        end_date = datetime.datetime.now().date()
        start_date = end_date - datetime.timedelta(days=30)

        returned_tuple = previous_calendar_month()
        returned_start_date = returned_tuple[0].date()
        returned_end_date = returned_tuple[1].date()

        self.assertEquals((returned_start_date, returned_end_date),
                          (start_date, end_date))
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