def agency_table_by_indicator(request, indicator, language="English", template_name="submissions/agency_table_by_indicator.html", extra_context=None): dp_gov_map = { "1DP" : "1G", "2DPa" : "2Ga", "2DPb" : "2Gb", "3DP" : "3G", "4DP" : "4G", "5DPa" : "5Gb", "5DPb" : "5Ga", "6DP" : "6G", "7DP" : "7G", "8DP" : "8G", } extra_context = extra_context or {} extra_context["translation"] = request.translation country_calcs = None countries = models.Country.objects.all().order_by("country") if indicator in dp_gov_map: gov_indicator = dp_gov_map[indicator] country_calcs = [(c, target.calc_country_ratings(c)[gov_indicator]) for c in countries] agencies = [] for agency in models.Agency.objects.all(): agency_values = [] for country in countries: if country in agency.countries: inds = indicators.calc_agency_country_indicators(agency, country, indicators.positive_funcs) ratings = target.country_agency_indicator_ratings(country, agency) base_val, base_year, latest_val, cur_year = inds[indicator][0] country_abs_values = { "baseline_value" : tbl_float_format(base_val), "base_year" : base_year, "latest_value" : tbl_float_format(latest_val), "cur_year" : cur_year, "rating" : ratings[indicator], "cellclass" : "", } else: country_abs_values = { "baseline_value" : "", "base_year" : "", "latest_value" : "", "cur_year" : "", "rating" : "", "cellclass" : "notactive", } agency_values.append((country, country_abs_values)) agency_values = sorted(agency_values, key=lambda x: x[0].country) agencies.append((agency, agency_values)) agencies = sorted(agencies, key=lambda x: x[0].agency) extra_context["agencies"] = agencies extra_context["countries"] = countries extra_context["country_calcs"] = country_calcs return direct_to_template(request, template=template_name, extra_context=extra_context)
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 country_response_breakdown( request, template_name="submissions/country_response_breakdown.html", extra_context=None ): """ Return a histogram of responses for each country indicator """ extra_context = extra_context or {} is_na = lambda r: r["target"] == Rating.NONE is_question = lambda r: r["target"] == Rating.QUESTION is_response = lambda r: not (is_na(r) or is_question(r)) countries = Country.objects.all() counts = defaultdict(int, {}) for country in countries: results = calc_country_ratings(country) for indicator in g_indicators: counts["%s_na" % indicator] += 1 if is_na(results[indicator]) else 0 counts["%s_question" % indicator] += 1 if is_question(results[indicator]) else 0 counts["%s_response" % indicator] += 1 if is_response(results[indicator]) else 0 counts["%s_total" % indicator] += 1 extra_context["counts"] = counts return direct_to_template(request, template=template_name, extra_context=extra_context)
def get_country_export_data(country, language=None): """ Return all the data that is required for the country scorecard """ language = language or models.Language.objects.get(language="English") translation = translations.get_translation(language) data = target.calc_country_ratings(country, language) data["questions"] = get_country_questions(country) data["agencies"] = get_agency_values(country) data["indicators"] = get_country_indicators(country, data["questions"], data["agencies"]) data["np"], data["p"] = target.get_agency_progress(country) ratings, _ = models.GovScorecardRatings.objects.get_or_create(country=country) comments_override, _ = models.CountryScorecardOverrideComments.objects.get_or_create( country=country, language=language ) try: data["ER1a"] = data["1G"]["target"] data["ER1b"] = data["1G"]["commentary"] data["ER2a"] = data["2Ga"]["target"] data["ER2b"] = data["2Ga"]["commentary"] data["ER3a"] = data["2Gb"]["target"] data["ER3b"] = data["2Gb"]["commentary"] data["ER4a"] = data["3G"]["target"] data["ER4b"] = data["3G"]["commentary"] data["ER4c"] = country.country data["ER5a"] = data["4G"]["target"] data["ER5b"] = data["4G"]["commentary"] data["ER6a"] = data["5Ga"]["target"] data["ER6b"] = data["5Ga"]["commentary"] data["ER7a"] = data["5Gb"]["target"] data["ER7b"] = data["5Gb"]["commentary"] data["ER8a"] = data["6G"]["target"] data["ER8b"] = data["6G"]["commentary"] data["ER9a"] = data["7G"]["target"] data["ER9b"] = data["7G"]["commentary"] data["ER10a"] = data["8G"]["target"] data["ER10b"] = data["8G"]["commentary"] data["file"] = country.country data["TB2"] = translation.gov_tb2 % country.country.upper() data["CD1"] = data["ER1a"] data["CD2"] = comments_override.cd2 or data["questions"]["1"]["comments"] data["HSP1"] = ratings.hsp1 or data["Q2G"]["target"] data["HSP2"] = ratings.hsp2 or data["Q3G"]["target"] data["HSM1"] = ratings.hsm1 or data["Q12G"]["target"] data["HSM2"] = data["questions"]["15"]["latest_value"] data["HSM3"] = data["ER10a"] data["HSM4"] = ratings.hsm4 data["BC1"] = data["questions"]["5"]["baseline_year"] data["BC2"] = data["questions"]["6"]["baseline_value"] data["BC3"] = data["questions"]["5"]["latest_year"] data["BC4"] = data["questions"]["6"]["latest_value"] data["BC5"] = "?????" data["BC6"] = "?????" data["BC7"] = "?????" data["BC8"] = "?????" data["BC9"] = "?????" data["BC10"] = "?????" data["PC1"] = data["indicators"]["3G"]["latest_value"] data["PC2"] = data["indicators"]["3G"]["hs_budget_gap"] data["PC3"] = translation.gov_pc3 % data["PC1"] data["PC4"] = translation.gov_pc4 % data["PC2"] data["PF1"] = data["questions"]["16"]["latest_value"] data["PF2"] = comments_override.pf2 or data["questions"]["16"]["comments"] data["PFM1"] = data["ER6a"] data["PFM2"] = comments_override.pfm2 or data["questions"]["9"]["comments"] data["PR1"] = data["ER7a"] data["PR2"] = comments_override.pr2 or data["questions"]["10"]["comments"] data["TA1"] = data["indicators"]["other"]["coordinated_programmes"] data["TA2"] = "" for agency in data["agencies"]: aqs = data["agencies"][agency] if "4" in aqs: data["TA2"] += "%s %s" % (agency, aqs["4"]["comments"].replace("%", "%%")) data["TA2"] += "\n" data["TA2"] = comments_override.ta2 or data["TA2"] data["PHC1"] = data["indicators"]["other"]["outpatient_visits_baseline"] data["PHC2"] = data["questions"]["19"]["baseline_year"] data["PHC3"] = data["indicators"]["other"]["outpatient_visits_latest"] data["PHC4"] = data["questions"]["19"]["latest_year"] data["PHC5"] = data["indicators"]["other"]["outpatient_visits_change"] data["PHC6"] = data["indicators"]["other"]["outpatient_visits_change_dir"] data["PHC7"] = "" data["HRH1"] = data["indicators"]["other"]["skilled_personnel_baseline"] data["HRH2"] = data["questions"]["17"]["baseline_year"] data["HRH3"] = data["indicators"]["other"]["skilled_personnel_latest"] data["HRH4"] = data["questions"]["17"]["latest_year"] data["HRH5"] = data["indicators"]["other"]["skilled_personnel_change"] data["HRH6"] = data["indicators"]["other"]["skilled_personnel_change_dir"] data["HRH7"] = "" data["HS1"] = data["questions"]["20"]["baseline_value"] data["HS2"] = data["questions"]["20"]["baseline_year"] data["HS3"] = data["questions"]["20"]["latest_value"] data["HS4"] = data["questions"]["20"]["latest_year"] data["HS5"] = data["indicators"]["other"]["health_workforce_spent_change"] data["HS6"] = data["indicators"]["other"]["health_workforce_spent_change_dir"] data["HS7"] = "" data["RF1"] = data["ER8a"] data["RF2"] = comments_override.rf2 or data["questions"]["22"]["latest_value"] data["RF3"] = comments_override.rf3 or data["questions"]["23"]["latest_value"] data["HMIS1"] = ratings.hmis1 or data["Q21G"]["target"] data["HMIS2"] = comments_override.hmis2 or data["questions"]["21"]["comments"] data["JAR1"] = ratings.jar1 or data["Q12G"]["target"] data["JAR2"] = "Field no longer used" data["JAR3"] = "Field no longer used" data["JAR4"] = comments_override.jar4 or data["questions"]["24"]["comments"] data["JAR5"] = "Field no longer used" data["DBR1"] = data["ER8a"] data["DBR2"] = comments_override.dbr2 or data["questions"]["11"]["comments"] group1 = ["MDG1", "MDG2", "MDG3", "MDG4"] group2 = ["MDG5a", "MDG5b", "MDG6a", "MDG6b", "MDG6c", "MDG7a", "MDG7b"] group1_index = "abcde" group2_index = "12345" needs_percent = ["MDG1", "MDG2", "MDG6a", "MDG6b", "MDG7a", "MDG7b"] add_perc = lambda ind: "%" if ind in needs_percent else "" for mdg in group1 + group2: # import pdb; pdb.set_trace() index = group1_index if mdg in group1 else group2_index mdgdata = models.MDGData.objects.get(mdg_target=mdg, country=country) if not mdgdata.latest_value: data[mdg + index[0]] = "" data[mdg + index[1]] = "" data[mdg + index[2]] = "questionmdg" data[mdg + index[3]] = "" data[mdg + index[4]] = "" elif not mdgdata.baseline_value: data[mdg + index[0]] = str(fformat_front(mdgdata.latest_value)) + add_perc(mdg) data[mdg + index[1]] = mdgdata.latest_year data[mdg + index[2]] = "questionmdg" data[mdg + index[3]] = "" data[mdg + index[4]] = "" else: fmt = fformat_two if mdg == "MDG3" else fformat_front data[mdg + index[0]] = str(fmt(mdgdata.latest_value)) + add_perc(mdg) data[mdg + index[1]] = mdgdata.latest_year data[mdg + index[2]] = mdgdata.arrow data[mdg + index[3]] = str(fmt(mdgdata.change)) + add_perc(mdg) data[mdg + index[4]] = mdgdata.baseline_year data["F1"] = country.country data["CN1"] = data["TB2"] data["GN1"] = country.country data["Header"] = country.country for i in range(1, 14): data["P%d" % i] = data["p"].get(i - 1, "pwhite") data["NP%d" % i] = data["np"].get(i - 1, "npwhite") working_draft, _ = models.CountryWorkingDraft.objects.get_or_create(country=country) data["workingdraft"] = "workingdraft" if working_draft.is_draft else "" except Exception, e: traceback.print_exc()