def mortality_register(request): if not request.datespan.is_valid(): messages.error(request, request.datespan.get_validation_reason()) return render_to_response(request, "reports/mortality_register.html", {"show_dates": True, "report": None}) clinic_id = request.GET.get("clinic", None) main_clinic = Location.objects.get(slug=clinic_id) if clinic_id else None cause_of_death_report = MortalityReport() place_of_death_report = MortalityReport() global_map = defaultdict(lambda: 0) global_display = CauseOfDeathDisplay("Total", AGGREGATE_OPTIONS) hhs = 0 if main_clinic: startkey = [clinic_id, request.datespan.startdate.year, request.datespan.startdate.month - 1] endkey = [clinic_id, request.datespan.enddate.year, request.datespan.enddate.month - 1, {}] results = get_db().view("centralreports/nhc_mortality_report", group=True, group_level=7, startkey=startkey, endkey=endkey).all() for row in results: # key: ["5010", 2010,8,"adult","f","cause","heart_problem"] clinic_id_back, year, jsmonth, agegroup, gender, type, val = row["key"] count = row["value"] group = MortalityGroup(main_clinic, agegroup, gender) if type == "global": global_map[val] = count if type == "cause": cause_of_death_report.add_data(group, val, count) elif type == "place": place_of_death_report.add_data(group, val, count) if "num_households" in global_map: hhs = global_map.pop("num_households") global_display.add_data(global_map) return render_to_response(request, "reports/mortality_register.html", {"show_dates": True, "cause_report": cause_of_death_report, "place_report": place_of_death_report, "districts": districts_for_view(), "clinics": clinics_for_view(), "global_display": global_display, "hhs": hhs, "main_clinic": main_clinic, })
def render_mortality_report(report, type): """ Convert a Mortality Report object into a displayable report. This is a big hunk of template tagging. """ if report is None or len(report.groups) == 0: return "<h3>Sorry, there's no data for the report and parameters you selected. " \ "Try running the report over a different range.</h3>" if type == "cause": female_data = CauseOfDeathDisplay("Females > 14 who died", ADULT_FEMALE_CAUSE_OPTIONS) male_data = CauseOfDeathDisplay("Males > 14 who died", ADULT_MALE_CAUSE_OPTIONS) child_data = CauseOfDeathDisplay("Children < 14 who died", CHILD_CAUSE_OPTIONS) title = "Causes of death" elif type == "place": female_data = CauseOfDeathDisplay("Females > 14 who died", PLACE_OPTIONS) male_data = CauseOfDeathDisplay("Males > 14 who died", PLACE_OPTIONS) child_data = CauseOfDeathDisplay("Children < 14 who died", PLACE_OPTIONS) title = "Places of death" else: raise Exception("Invalid mortality type: %s, valid options are 'place' and 'cause'" % type) for group in report.groups: if group.is_adult() and group.is_female(): female_data.add_data(report.get_data(group)) elif group.is_adult() and group.is_male(): male_data.add_data(report.get_data(group)) elif group.is_child(): child_data.add_data(report.get_data(group)) return render_to_string("reports/partials/mortality_cause_of_death_partial.html", {"title": title, "adult_male_data": male_data, "adult_female_data": female_data, "child_data": child_data})