def country_table(request, language="English", template_name="submissions/country_table.html", extra_context=None): extra_context = extra_context or {} extra_context["translation"] = translation = request.translation abs_values = {} for country in models.Country.objects.all().order_by("country"): country_abs_values = {} country_ratings = target.calc_country_ratings(country) inds = indicators.calc_country_indicators(country, indicators.positive_funcs) for indicator in inds: tpl = inds[indicator][0] base_val, base_year, latest_val, latest_year = tpl rating = country_ratings[indicator]["target"] if type(base_val) == str: base_val = base_val.upper() if type(latest_val) == str: latest_val = latest_val.upper() if indicator == "2Gb": # The indicator turns this into 100/0 # This code turns it back - to much effort # to figure out why it does this base_val = "Y" if base_val == 100 else "N" latest_val = "Y" if latest_val == 100 else "N" if indicator == "2Ga": base_val1 = base_val[0] if base_val else None base_val2 = base_val[1] if base_val else None latest_val1 = latest_val[0] if latest_val else None latest_val2 = latest_val[1] if latest_val else None country_abs_values["2Ga1"] = ( tbl_float_format(base_val1), tbl_float_format(latest_val1), None, base_year, rating ) country_abs_values["2Ga2"] = ( tbl_float_format(base_val2), tbl_float_format(latest_val2), None, base_year, rating, ) else: decimal_places = { "5Ga" : 1 } places = decimal_places.get(indicator, 0) country_abs_values[indicator] = ( tbl_float_format(base_val, places), tbl_float_format(latest_val, places), tbl_float_format(perc_change(base_val, latest_val), places), base_year, rating ) abs_values[country.country] = country_abs_values extra_context["abs_values"] = sorted(abs_values.items()) extra_context["spm_map"] = translation.gov_spm_map return direct_to_template(request, template=template_name, extra_context=extra_context)
def get_country_indicators(country, questions, agencies_data): indicator_data = calc_country_indicators(country) indicators = {} for indicator in indicator_data: ind = indicators[indicator] = {} data = indicator_data[indicator][0] ind["baseline_value"] = none_num(data[0]) ind["baseline_year"] = data[1] ind["latest_value"] = none_num(data[2]) ind["latest_year"] = data[3] if indicators["3G"]["latest_value"] != NA_STR: indicators["3G"]["hs_budget_gap"] = 15 - indicators["3G"]["latest_value"] else: indicators["3G"]["hs_budget_gap"] = None indicators["other"] = {} other_indicators = indicators["other"] baseline_denom = questions["18"]["baseline_value"] / 10000.0 latest_denom = questions["18"]["latest_value"] / 10000.0 # Outpatient Visits other_indicators["outpatient_visits_baseline"] = questions["19"]["baseline_value"] / baseline_denom other_indicators["outpatient_visits_latest"] = questions["19"]["latest_value"] / latest_denom other_indicators["outpatient_visits_change"], other_indicators["outpatient_visits_change_dir"] = calc_change(other_indicators["outpatient_visits_latest"], other_indicators["outpatient_visits_baseline"]) # Skilled Personnel other_indicators["skilled_personnel_baseline"] = questions["17"]["baseline_value"] / baseline_denom other_indicators["skilled_personnel_latest"] = questions["17"]["latest_value"] / latest_denom other_indicators["skilled_personnel_change"], other_indicators["skilled_personnel_change_dir"] = calc_change(other_indicators["skilled_personnel_latest"], other_indicators["skilled_personnel_baseline"]) # Health Workforce other_indicators["health_workforce_perc_of_budget_baseline"] = questions["20"]["baseline_value"] / questions["7"]["baseline_value"] other_indicators["health_workforce_perc_of_budget_latest"] = questions["20"]["latest_value"] / questions["7"]["latest_value"] other_indicators["health_workforce_spent_change"], other_indicators["health_workforce_spent_change_dir"] = calc_change(questions["20"]["latest_value"], questions["20"]["baseline_value"]) other_indicators["pfm_diff"] = questions["9"]["latest_value"] - questions["9"]["baseline_value"] def sum_agency_values(question_number, field): sum = 0 for agency in agencies_data: if question_number in agencies_data[agency]: try: sum += float(agencies_data[agency][question_number][field]) except ValueError: pass return sum coordinated_programmes = sum_agency_values("5", "latest_value") - sum_agency_values("4", "latest_value") if coordinated_programmes > 0.51: other_indicators["coordinated_programmes"] = models.Rating.TICK elif coordinated_programmes >= 0.11: other_indicators["coordinated_programmes"] = models.Rating.ARROW else: other_indicators["coordinated_programmes"] = models.Rating.CROSS return indicators
def calc_country_ratings(country, language=None): """ Returns information for all indicators for the given country in a dict with the following form { "1G" : { "base_val" : ..., "cur_val" : ..., "comments" : ..., "target" : ..., }, "2Ga" : { "base_val" : ..., "cur_val" : ..., "comments" : ..., "target" : ..., }, . . . } """ language = language or models.Language.objects.get(language="English") translation = translations.get_translation(language) gov_commentary_text = translation.gov_commentary_text rating_question_text = translation.rating_question_text rating_none_text = translation.rating_none_text % country.country targets = get_country_targets(country, g_indicators) indicators = calc_country_indicators(country) results = {} ratings, _ = models.GovScorecardRatings.objects.get_or_create(country=country) comment_override, _ = models.GovScorecardComments.objects.get_or_create(country=country, language=language) def ratings_val(obj, tmpl): def _func(indicator): h = indicator.replace("G", "").replace("Q", "") d = obj.__dict__ return d.get(tmpl % h, None) return _func ratings_comments = ratings_val(comment_override, "er%s") ratings_target = ratings_val(ratings, "r%s") for indicator in indicators: (base_val, base_year, cur_val, cur_year), comments = indicators[indicator] target = targets[indicator] result = { "base_val" : base_val, "base_year" : base_year, "cur_val" : cur_val, "cur_year" : cur_year, "comments" : comments, "commentary" : "", "country_name" : country, } result["one_minus_base_val"] = base_val if isinstance(base_val, numbers.Real): result["one_minus_base_val"] = 100 - base_val result["one_minus_cur_val"] = cur_val if isinstance(cur_val, numbers.Real): result["one_minus_cur_val"] = 100 - cur_val result["target"] = ratings_target(indicator) or evaluate_indicator(target, base_val, cur_val) if ratings_comments(indicator): result["commentary"] = ratings_comments(indicator) else: if indicator in gov_commentary_text: target_value = result["target"] if target_value == Rating.QUESTION: commentary = rating_question_text elif target_value == Rating.NONE: commentary = rating_none_text elif target_value == None: raise Exception("This shouldn't really be happening") commentary = "Missing Data" elif "all" in gov_commentary_text[indicator]: commentary = gov_commentary_text[indicator]["all"] else: commentary = gov_commentary_text[indicator][target_value] try: result["commentary"] = commentary % result except TypeError, e: result["commentary"] = ["This text couldn't be generated, possibly because the rating was overriden. Please override the text appropriately as well"] results[indicator] = result
def government_graphs(request, language, template_name="submission/country_graphs_by_indicator.html", extra_context=None): extra_context = extra_context or {} translation = request.translation countries = sorted(Country.objects.all(), key=lambda x: x.country) data_3G = dict([(c, calc_country_indicators(c)["3G"]) for c in countries]) data_4G = dict([(c, calc_country_indicators(c)["4G"]) for c in countries]) country_data = dict([(country, country_scorecard.get_country_export_data(country)) for country in Country.objects.all()]) # TODO # Request from James to zero negative values neg_to_zero = lambda x : 0 if x < 0 else x # TODO # Request from James to remove overly large values remove_large = lambda x : 0 if x > 100 else x # Nepal needs to be shown at the end of the list and with an asterix nepal = Country.objects.get(country="Nepal") nepal.country = nepal.country + "*" countries_3g = list(countries) + [nepal] countries_3g.remove(nepal) extra_context["graph_3G"] = TargetCountryBarGraph( countries_3g, "graph_3G", [data_3G[country][0][0] for country in countries_3g], [data_3G[country][0][2] for country in countries_3g], translation.target_language["target"], 15, title=translation.government_graphs["3G"]["title"], ) extra_context["graph_3G"].subtitle = { "text": translation.government_graphs["3G"]["subtitle"], "align": 'left', "x": 50, "y": 388, "floating" : "true", } extra_context["graph_4G"] = CountryBarGraph( countries, "graph_4G", [neg_to_zero(data_4G[country][0][0]) for country in countries], [neg_to_zero(data_4G[country][0][2]) for country in countries], title=translation.government_graphs["4G"]["title"], ) extra_context["graph_hw"] = CountryBarGraph( countries, "graph_hw", [remove_large(country_data[country]["indicators"]["other"]["health_workforce_perc_of_budget_baseline"] * 100) for country in countries], [remove_large(country_data[country]["indicators"]["other"]["health_workforce_perc_of_budget_latest"] * 100) for country in countries], title=translation.government_graphs["health_workforce"]["title"], ) extra_context["graph_outpatient_visits"] = CountryBarGraph( countries, "graph_outpatient_visits", [country_data[country]["indicators"]["other"]["outpatient_visits_baseline"] for country in countries], [country_data[country]["indicators"]["other"]["outpatient_visits_latest"] for country in countries], title=translation.government_graphs["outpatient_visits"]["title"], ) extra_context["graph_outpatient_visits"].yAxis = {"title" : {"text" : ""}} extra_context["graph_skilled_medical"] = TargetCountryBarGraph( countries, "graph_skilled_medical", [country_data[country]["indicators"]["other"]["skilled_personnel_baseline"] for country in countries], [country_data[country]["indicators"]["other"]["skilled_personnel_latest"] for country in countries], translation.target_language["who"], 23, title=translation.government_graphs["skilled_medical"]["title"], ) extra_context["graph_skilled_medical"].yAxis = {"title" : {"text" : ""}} extra_context["graph_health_budget"] = TargetCountryBarGraph( countries, "graph_health_budget", [country_data[country]["indicators"]["3G"]["baseline_value"] for country in countries], [country_data[country]["indicators"]["3G"]["latest_value"] for country in countries], translation.target_language["target"], 15, title=translation.government_graphs["health_budget"]["title"], ) return direct_to_template(request, template=template_name, extra_context=extra_context)