Exemple #1
0
def get_focus_area_data(financial_year, sphere_slug):
    dataset = get_expenditure_time_series_dataset(sphere_slug)
    if not dataset:
        return None, None
    openspending_api = dataset.get_openspending_api()
    year_ref = openspending_api.get_financial_year_ref()
    dept_ref = openspending_api.get_department_name_ref()
    function_ref = openspending_api.get_function_ref()
    phase_ref = openspending_api.get_phase_ref()
    government_ref = openspending_api.get_geo_ref()

    cuts = [
        year_ref + ":" + "{}".format(financial_year.get_starting_year()),
        phase_ref + ":" + "{}".format("Main appropriation"),
    ]

    drilldowns = [function_ref, dept_ref]

    if sphere_slug == "provincial":
        drilldowns.append(government_ref)

    results = openspending_api.aggregate(cuts=cuts, drilldowns=drilldowns)
    cells = results["cells"]
    cells = filter(lambda c: c[function_ref] != "", cells)

    return cells, openspending_api
Exemple #2
0
def get_prov_eq_share(financial_year):
    dataset = get_expenditure_time_series_dataset("national")
    if not dataset:
        return None, None
    openspending_api = dataset.get_openspending_api()
    year_ref = openspending_api.get_financial_year_ref()
    dept_ref = openspending_api.get_department_name_ref()
    function_ref = openspending_api.get_function_ref()
    phase_ref = openspending_api.get_phase_ref()
    subprogramme_ref = openspending_api.get_subprogramme_name_ref()

    cuts = [
        year_ref + ":" + "{}".format(financial_year.get_starting_year()),
        phase_ref + ":" + "{}".format("Main appropriation"),
        dept_ref + ":" + "{}".format(PROV_EQ_SHARE_DEPT),
        subprogramme_ref + ":" + "{}".format(PROV_EQ_SHARE_SUBPROG),
    ]

    drilldowns = [function_ref]

    results = openspending_api.aggregate(cuts=cuts, drilldowns=drilldowns)
    count = len(results["cells"])
    if count != 1:
        raise Exception(
            "Expected Provincial Equitable Share once but found it %d times" % count
        )
    cell = results["cells"][0]
    return (cell[function_ref], cell["value.sum"])
Exemple #3
0
def get_preview_page(financial_year_id, phase_slug, government_slug, sphere_slug):
    try:
        selected_phase = EXPENDITURE_TIME_SERIES_PHASE_MAPPING[phase_slug]
    except KeyError:
        raise Exception("An invalid phase was provided: {}".format(phase_slug))

    expenditure = []
    dataset = get_expenditure_time_series_dataset(sphere_slug)
    if not dataset:
        return None
    openspending_api = dataset.get_openspending_api()
    programme_ref = openspending_api.get_programme_name_ref()
    geo_ref = openspending_api.get_geo_ref()
    department_ref = openspending_api.get_department_name_ref()
    financial_year = FinancialYear.objects.get(slug=financial_year_id)
    function_ref = openspending_api.get_function_ref()

    # Expenditure data
    expenditure_cuts = [
        openspending_api.get_adjustment_kind_ref() + ":" + '"Total"',
        openspending_api.get_financial_year_ref()
        + ":"
        + "{}".format(FinancialYear.start_from_year_slug(financial_year_id)),
        openspending_api.get_phase_ref() + ":" + '"{}"'.format(selected_phase),
    ]
    expenditure_drilldowns = [department_ref, geo_ref, programme_ref, function_ref]
    expenditure_results = openspending_api.aggregate(
        cuts=expenditure_cuts, drilldowns=expenditure_drilldowns
    )

    # We do a separate call to always build focus area data from the main
    # appropriation phase
    focus_cuts = [
        openspending_api.get_adjustment_kind_ref() + ":" + '"Total"',
        openspending_api.get_financial_year_ref()
        + ":"
        + "{}".format(FinancialYear.start_from_year_slug(financial_year_id)),
        openspending_api.get_phase_ref()
        + ":"
        + '"{}"'.format(EXPENDITURE_TIME_SERIES_PHASE_MAPPING["original"]),
    ]
    focus_drilldowns = [department_ref, geo_ref, function_ref]

    focus_results = openspending_api.aggregate(
        cuts=focus_cuts, drilldowns=focus_drilldowns
    )

    # Filter departments that belong to the selected government
    expenditure_results_filter_government_complete_breakdown = filter(
        lambda x: slugify(x[geo_ref]) == government_slug, expenditure_results["cells"]
    )
    focus_results_filter_government = filter(
        lambda x: slugify(x[geo_ref]) == government_slug, focus_results["cells"]
    )

    # Used to determine programmes for departments
    expenditure_results_filter_government_programme_breakdown = openspending_api.aggregate_by_refs(
        [department_ref, geo_ref, programme_ref],
        expenditure_results_filter_government_complete_breakdown,
    )

    # Used to iterate over unique departments and build department objects
    expenditure_results_filter_government_department_breakdown = openspending_api.aggregate_by_refs(
        [department_ref, geo_ref],
        expenditure_results_filter_government_complete_breakdown,
    )

    total_budget = 0
    filtered_result_cells = []
    sphere_depts = Department.objects.filter(
        government__sphere__financial_year__slug=financial_year_id,
        government__sphere__slug=sphere_slug,
        government__slug=government_slug,
        is_vote_primary=True,
    )

    for cell in expenditure_results_filter_government_department_breakdown:
        try:
            dept = sphere_depts.get(
                slug=slugify(cell[department_ref]),
                government__slug=slugify(cell[geo_ref]),
            )
        except Department.DoesNotExist:
            logger.warning(
                "Excluding: {} {} {} {}".format(
                    sphere_slug, financial_year_id, cell[geo_ref], cell[department_ref]
                )
            )
            continue

        total_budget += float(cell["value.sum"])
        cell["url"] = dept.get_url_path() if dept else None
        cell["description"] = dept.intro if dept else None
        filtered_result_cells.append(cell)

    for cell in filtered_result_cells:
        percentage_of_total = float(cell["value.sum"]) / total_budget * 100

        department_programmes = filter(
            lambda x: x[department_ref] == cell[department_ref],
            expenditure_results_filter_government_programme_breakdown,
        )
        programmes = []

        department_functions = filter(
            lambda x: x[department_ref] == cell[department_ref],
            focus_results_filter_government,
        )
        functions = []

        for programme in department_programmes:
            percentage = float(programme["value.sum"]) / cell["value.sum"] * 100
            programmes.append(
                {
                    "title": programme[programme_ref].title(),
                    "slug": slugify(programme[programme_ref]),
                    "percentage": percentage,
                    "amount": float(programme["value.sum"]),
                }
            )

        for function in department_functions:
            name = function[function_ref]
            slug = slugify(name)
            functions.append(
                {
                    "title": name,
                    "slug": slug,
                    "url": get_focus_area_url_path(financial_year.slug, name),
                }
            )

        expenditure.append(
            {
                "title": cell[department_ref],
                "slug": slugify(cell[department_ref]),
                "total": float(cell["value.sum"]),
                "percentage_of_budget": percentage_of_total,
                "description": cell["description"],
                "programmes": programmes,
                "focus_areas": functions,
            }
        )

    return {"data": {"items": expenditure}} if expenditure else None