def agency_table_by_agency(request, agency_id, language="English", template_name="submissions/agency_table.html", extra_context=None):
    extra_context = extra_context or {} 
    agency = get_object_or_404(models.Agency, pk=agency_id)

    extra_context["translation"] = translation = request.translation
    abs_values = {}
    for country in agency.countries:
        country_abs_values = {}
        inds = indicators.calc_agency_country_indicators(agency, country, indicators.positive_funcs)
        ratings = target.country_agency_indicator_ratings(country, agency)
        for indicator in inds:
            base_val, base_year, latest_val, latest_year = inds[indicator][0]
            country_abs_values[indicator] = {
                "base_val" : tbl_float_format(base_val), 
                "latest_val" : tbl_float_format(latest_val), 
                "perc_change" : tbl_float_format(perc_change(base_val, latest_val)),
                "base_year" : base_year,
                "latest_year" : latest_year,
                "rating" : ratings[indicator]
            } 
        abs_values[country.country] = country_abs_values
    extra_context["abs_values"] = sorted(abs_values.items())
    extra_context["spm_map"] = translation.spm_map
    extra_context["institution_name"] = translation.by_agency_title % agency.agency
    
    return direct_to_template(request, template=template_name, extra_context=extra_context)
Exemple #2
0
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)
Exemple #3
0
def agency_country_ratings(request, template_name="submissions/agency_country_ratings.html", extra_context=None):
    extra_context = extra_context or {}
    data = []
    for agency in Agency.objects.all().order_by("agency"):
        for country in agency.countries:
            ratings = country_agency_indicator_ratings(country, agency)
            making_progress = country_agency_progress(country, agency)

            data.append(
                {"agency": agency, "country": country, "indicators": ratings, "making_progress": making_progress}
            )

    extra_context["data"] = data
    return direct_to_template(request, template=template_name, extra_context=extra_context)
Exemple #4
0
def agency_response_breakdown(request, template_name="submissions/agency_response_breakdown.html", extra_context=None):
    """
    Return a histogram of responses for each agency indicator 
    """

    extra_context = extra_context or {}
    is_na = lambda r: r == Rating.NONE
    is_question = lambda r: r == Rating.QUESTION
    is_response = lambda r: not (is_na(r) or is_question(r))

    agencies = Agency.objects.all()
    counts = defaultdict(int, {})
    for agency in agencies:
        for country in agency.countries:
            results = country_agency_indicator_ratings(country, agency)
            for indicator in dp_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)