Example #1
0
def country_export(request, language):
    language = get_object_or_404(Language, language=language)
    headers = [
        # Front of scorecard
        "file",
        "TB2",
        "CD1",
        "CD2",
        "HSP1",
        "HSP2",
        "HSM1",
        "HSM2",
        "HSM3",
        "HSM4",
        "BC1",
        "BC2",
        "BC3",
        "BC4",
        "BC5",
        "BC6",
        "BC7",
        "BC8",
        "BC9",
        "BC10",
        "PC1",
        "PC2",
        "PC3",
        "PC4",
        "PF1",
        "PF2",
        "PFM1",
        "PFM2",
        "PR1",
        "PR2",
        "TA1",
        "TA2",
        "PHC1",
        "PHC2",
        "PHC3",
        "PHC4",
        "PHC5",
        "PHC6",
        "PHC7",
        "HRH1",
        "HRH2",
        "HRH3",
        "HRH4",
        "HRH5",
        "HRH6",
        "HRH7",
        "HS1",
        "HS2",
        "HS3",
        "HS4",
        "HS5",
        "HS6",
        "HS7",
        "RF1",
        "RF2",
        "RF3",
        "HMIS1",
        "HMIS2",
        "JAR1",
        "JAR2",
        "JAR3",
        "JAR4",
        "JAR5",
        "DBR1",
        "DBR2",
        "MDG1a",
        "MDG1b",
        "MDG1c",
        "MDG1d",
        "MDG1e",
        "MDG2a",
        "MDG2b",
        "MDG2c",
        "MDG2d",
        "MDG2e",
        "MDG3a",
        "MDG3b",
        "MDG3c",
        "MDG3d",
        "MDG3e",
        "MDG4a",
        "MDG4b",
        "MDG4c",
        "MDG4d",
        "MDG4e",
        "MDG5a1",
        "MDG5a2",
        "MDG5a3",
        "MDG5a4",
        "MDG5a5",
        "MDG5b1",
        "MDG5b2",
        "MDG5b3",
        "MDG5b4",
        "MDG5b5",
        "MDG6a1",
        "MDG6a2",
        "MDG6a3",
        "MDG6a4",
        "MDG6a5",
        "MDG6b1",
        "MDG6b2",
        "MDG6b3",
        "MDG6b4",
        "MDG6b5",
        "MDG6c1",
        "MDG6c2",
        "MDG6c3",
        "MDG6c4",
        "MDG6c5",
        "MDG7a1",
        "MDG7a2",
        "MDG7a3",
        "MDG7a4",
        "MDG7a5",
        "MDG7b1",
        "MDG7b2",
        "MDG7b3",
        "MDG7b4",
        "MDG7b5",
        # Back of scorecard
        "F1",
        "CN1",
        "GN1",
        "ER1a",
        "ER1b",
        "ER2a",
        "ER2b",
        "ER3a",
        "ER3b",
        "ER4a",
        "ER4b",
        "ER4c",
        "ER5a",
        "ER5b",
        "ER6a",
        "ER6b",
        "ER7a",
        "ER7b",
        "ER8a",
        "ER8b",
        "ER9a",
        "ER9b",
        "ER10a",
        "ER10b",
        "Header",
        "P1",
        "P2",
        "P3",
        "P4",
        "P5",
        "P6",
        "P7",
        "P8",
        "P9",
        "P10",
        "P11",
        "P12",
        "P13",
        "Header",
        "NP1",
        "NP2",
        "NP3",
        "NP4",
        "NP5",
        "NP6",
        "NP7",
        "NP8",
        "NP9",
        "NP10",
        "NP11",
        "NP12",
        "NP13",
        "workingdraft",
    ]

    special_formatting = {
        "HSM2": fformat_none,
        "BC2": fformat_front,
        "BC4": fformat_front,
        "PC1": fformat_front,
        "PC2": fformat_front,
        "PF1": fformat_none,
        "PHC1": fformat_front,
        "PHC3": fformat_front,
        "PHC5": fformat_front,
        "HRH1": fformat_front,
        "HRH3": fformat_front,
        "HRH5": fformat_front,
        "HS1": fformat_front,
        "HS3": fformat_front,
        "HS5": fformat_front,
        "RF2": fformat_front,
        "RF3": fformat_front,
    }

    def special_format(header, val):
        if header in special_formatting:
            return special_formatting[header](val)
        else:
            return val

    response = HttpResponse(mimetype="text/csv")
    response["Content-Disposition"] = "attachment; filename=country_export.csv"
    writer = csv.writer(response)
    writer.writerow(headers)

    for country in Country.objects.all():
        data = country_scorecard.get_country_export_data(country, language)
        writer.writerow([special_format(header, data.get(header, "")) for header in headers])
    return response
Example #2
0
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)