Exemplo n.º 1
0
 def make(cls, data, prefix=''):
     return cls(
         id=data[f"{prefix}id"],
         period_start=data.get(f"{prefix}period_start", None),
         period_end=data.get(f"{prefix}period_end", None),
         target_value=maybe_decimal(data.get(f"{prefix}target_value",
                                             None)),
         target_comment=data.get(f"{prefix}target_comment", ''),
         actual_value=maybe_decimal(data.get(f"{prefix}actual_value",
                                             None)),
         actual_comment=data.get(f"{prefix}actual_comment", ''),
         narrative=data.get(f"{prefix}narrative", ''),
         indicator_type=data.get(f"{prefix}indicator__type", QUANTITATIVE),
         indicator_measure=data.get(f"{prefix}indicator__measure", ''),
     )
Exemplo n.º 2
0
 def test_maybe_decimal(self):
     for expected, actual in [
         (Decimal(10), '10'),
         (Decimal(0), '0'),
         (None, ''),
         (None, None),
     ]:
         self.assertEqual(expected, maybe_decimal(actual))
Exemplo n.º 3
0
def render_contributor(ws,
                       row,
                       result,
                       indicator,
                       period,
                       contributor,
                       aggregate_targets=False,
                       use_indicator_target=False,
                       disaggregations={},
                       level=1):
    long_text_style = Style(alignment=Alignment(wrap_text=True))
    ws.set_cell_style(row, 1, long_text_style)
    ws.set_cell_value(row, 1, result.title)
    ws.set_cell_style(row, 2, long_text_style)
    ws.set_cell_value(row, 2, result.description)
    ws.set_cell_style(row, 3, long_text_style)
    ws.set_cell_value(row, 3, indicator.title)
    ws.set_cell_value(row, 4, f"{period.period_start} - {period.period_end}")
    ws.set_cell_value(row, 5, level)
    ws.set_cell_style(row, 6, long_text_style)
    ws.set_cell_value(row, 6, contributor.project.title)
    ws.set_cell_style(row, 7, long_text_style)
    ws.set_cell_value(row, 7, contributor.project.subtitle)
    ws.set_cell_style(row, 8, long_text_style)
    ws.set_cell_value(row, 8, contributor.project.country)
    ws.set_cell_style(row, 9, long_text_style)
    ws.set_cell_value(
        row, 9, ', '.join(contributor.project.sectors)
        if contributor.project.sectors else '')
    ws.set_cell_value(row, 10,
                      maybe_decimal(contributor.indicator_baseline_value))
    col = get_dynamic_column_start(aggregate_targets)
    ws.set_cell_value(
        row, col, contributor.indicator_target_value
        if use_indicator_target else ensure_decimal(contributor.target_value))
    col += 2
    ws.set_cell_value(row, col, contributor.actual_value)
    col += 1
    if period.is_quantitative:
        contribution = calculate_percentage(
            ensure_decimal(contributor.updates_value),
            ensure_decimal(period.aggregated_value))
        ws.set_cell_style(row, col,
                          Style(alignment=Alignment(horizontal='right')))
        ws.set_cell_value(row, col, f"{contribution}%")
        col += 1
        for category, types in disaggregations.items():
            for type in [t for t in types.keys()]:
                ws.set_cell_value(
                    row, col,
                    contributor.get_disaggregation_value(category, type) or '')
                col += 1
                ws.set_cell_value(
                    row, col,
                    contributor.get_disaggregation_target_value(
                        category, type) or '')
                col += 1
    return row + 1
Exemplo n.º 4
0
 def get_disaggregation_targets(self, obj):
     return [{
         'id': t.id,
         'category': t.dimension_value.name.name,
         'category_id': t.dimension_value.name.id,
         'type': t.dimension_value.value,
         'type_id': t.dimension_value.id,
         'value': maybe_decimal(t.value),
     } for t in obj.disaggregation_targets.all()]
Exemplo n.º 5
0
 def make(cls, data, prefix=''):
     return cls(
         id=data[f"{prefix}id"],
         title=data.get(f"{prefix}title", ''),
         type=data.get(f"{prefix}type", QUANTITATIVE),
         measure=data.get(f"{prefix}measure", ''),
         description=data.get(f"{prefix}description", ''),
         baseline_year=data.get(f"{prefix}baseline_year", None),
         baseline_value=maybe_decimal(
             data.get(f"{prefix}baseline_value", None)),
         baseline_comment=data.get(f"{prefix}baseline_comment", ''),
         target_value=data.get(f"{prefix}target_value", None),
         target_comment=data.get(f"{prefix}target_comment", None),
     )
Exemplo n.º 6
0
 def make(cls, data, prefix=''):
     return cls(
         id=data[f"{prefix}id"],
         parent=data.get(f"{prefix}parent_period", None),
         indicator_type=data.get(f"{prefix}indicator__type", QUANTITATIVE),
         indicator_measure=data.get(f"{prefix}indicator__measure", ''),
         target_value=maybe_decimal(data.get(f"{prefix}target_value",
                                             None)),
         indicator_baseline_value=data.get(
             f"{prefix}indicator__baseline_value", None),
         indicator_target_value=data.get(f"{prefix}indicator__target_value",
                                         None),
         project=ContributorProjectData.make(
             data, 'indicator__result__project__'))
Exemplo n.º 7
0
def render_period(ws,
                  row,
                  result,
                  indicator,
                  period,
                  aggregate_targets=False,
                  use_indicator_target=False,
                  disaggregations={}):
    long_text_style = Style(alignment=Alignment(wrap_text=True))
    ws.set_cell_style(row, 1, long_text_style)
    ws.set_cell_value(row, 1, result.title)
    ws.set_cell_style(row, 2, long_text_style)
    ws.set_cell_value(row, 2, result.description)
    ws.set_cell_style(row, 3, long_text_style)
    ws.set_cell_value(row, 3, indicator.title)
    ws.set_cell_value(row, 4, f"{period.period_start} - {period.period_end}")
    ws.set_cell_value(row, 10, maybe_decimal(indicator.baseline_value))
    col = get_dynamic_column_start(aggregate_targets)
    if aggregate_targets:
        ws.set_cell_value(
            row, AGGREGATED_TARGET_VALUE_COLUMN,
            indicator.aggregated_target_value
            if use_indicator_target else period.aggregated_target_value)
    else:
        ws.set_cell_value(
            row, col, indicator.target_value
            if use_indicator_target else ensure_decimal(period.target_value))
    col += 1
    ws.set_cell_value(row, col, period.aggregated_value)
    if period.is_quantitative:
        col += 3
        for category, types in disaggregations.items():
            for type in [t for t in types.keys()]:
                ws.set_cell_value(
                    row, col,
                    period.get_aggregated_disaggregation_value(category, type)
                    or '')
                col += 1
                ws.set_cell_value(
                    row, col,
                    period.get_aggregated_disaggregation_target_value(
                        category, type) or '')
                col += 1
    return row + 1
 def get_denominator(self, obj):
     return maybe_decimal(obj.denominator)
 def get_numerator(self, obj):
     return maybe_decimal(obj.numerator)