def get_reportable_header_table(self, reportable): is_percentage = reportable.blueprint.unit == IndicatorBlueprint.PERCENTAGE calculated_target = format_total_value_to_string( reportable.target, is_percentage=is_percentage, percentage_display_type="ratio" if reportable.blueprint.display_type == 'ratio' else None) if reportable.blueprint.display_type == 'percentage': calculated_baseline = "%.2f" % reportable.calculated_baseline * 100 if reportable.calculated_baseline * 100 != 0.0 else 0.0 else: v = int(reportable.baseline.get(ValueType.VALUE, 0)) if reportable.baseline.get( ValueType.VALUE, 0) else 0 d = int(reportable.baseline.get( ValueType.DENOMINATOR, 1)) if reportable.baseline.get( ValueType.DENOMINATOR, 1) else 1 calculated_baseline = format_total_value_to_string( { ValueType.VALUE: v, ValueType.DENOMINATOR: d }, is_percentage=is_percentage, percentage_display_type="ratio" if reportable.blueprint.display_type == 'ratio' else None) return [ [ HTMLTableHeader(reportable.blueprint.title, colspan=3, klass='section'), ], [ HTMLTableHeader('Calculation method'), HTMLTableCell(reportable.blueprint.display_type, colspan=2), ], [ HTMLTableHeader('Baseline'), HTMLTableCell(calculated_baseline, colspan=2), ], [ HTMLTableHeader('Target'), HTMLTableCell(calculated_target, colspan=2), ], [ HTMLTableHeader('Current Progress'), HTMLTableCell(format_percent( reportable.progress_percentage / 100, '#%'), colspan=2), ], ]
def get_current_previous_location_data_table(self, current_data): previous_data = current_data.previous_location_data current_location_progress = format_total_value_to_string( current_data.disaggregation.get('()'), is_percentage=current_data.indicator_report.reportable.blueprint. unit == IndicatorBlueprint.PERCENTAGE, percentage_display_type="ratio" if current_data.indicator_report.reportable.blueprint.display_type == 'ratio' else None) previous_location_progress = format_total_value_to_string( previous_data.disaggregation.get('()'), is_percentage=previous_data.indicator_report.reportable.blueprint. unit == IndicatorBlueprint.PERCENTAGE, percentage_display_type="ratio" if current_data.indicator_report.reportable.blueprint.display_type == 'ratio' else None) if previous_data else '' previous_time_period = previous_data.indicator_report.display_time_period if previous_data else '' previous_submission_date = previous_data.indicator_report.submission_date if previous_data else '' return [ [ HTMLTableHeader(current_data.location.name, colspan=3, klass='subsection'), ], [ HTMLTableHeader('Reporting period'), HTMLTableHeader('Current {}'.format( current_data.indicator_report.display_time_period)), HTMLTableHeader('Previous {}'.format(previous_time_period)), ], [ HTMLTableHeader('Total Progress'), HTMLTableCell(current_location_progress), HTMLTableCell(previous_location_progress), ], [ HTMLTableHeader('Report submitted'), HTMLTableCell(current_data.indicator_report.submission_date), HTMLTableCell(previous_submission_date), ], ]
def create_tables_for_indicator_reports(self, indicator_reports): grouped_indicators = group_indicator_reports_by_lower_level_output(indicator_reports) tables = list() for indicators in grouped_indicators: tables.append([ [ HTMLTableHeader( 'PD Output: {}'.format(indicators[0].reportable.content_object.title), colspan=2, klass='section' ), ], [ HTMLTableHeader('Overall Status'), HTMLTableCell(indicators[0].get_overall_status_display(), klass=indicators[0].overall_status), ], [ HTMLTableHeader('Narrative assessment'), HTMLTableCell(indicators[0].narrative_assessment), ], ]) for indicator in indicators: is_percentage = indicator.reportable.blueprint.unit == IndicatorBlueprint.PERCENTAGE calculated_target = format_total_value_to_string( indicator.reportable.target, is_percentage=is_percentage, percentage_display_type="ratio" if indicator.reportable.blueprint.display_type == 'ratio' else None ) total_cumulative_progress = format_total_value_to_string( indicator.reportable.total, is_percentage=is_percentage, percentage_display_type="ratio" if indicator.reportable.blueprint.display_type == 'ratio' else None ) achievement_in_reporting_period = format_total_value_to_string( indicator.total, is_percentage=is_percentage, percentage_display_type="ratio" if indicator.reportable.blueprint.display_type == 'ratio' else None ) indicator_table = [ [ HTMLTableCell(indicator.reportable.blueprint.title, rowspan=2, colspan=2), HTMLTableHeader('Target'), HTMLTableCell(calculated_target), ], [ HTMLTableHeader('Total cumulative progress'), HTMLTableCell(total_cumulative_progress), ], [ HTMLTableHeader('Calculation method across locations'), HTMLTableCell(indicator.reportable.blueprint.calculation_formula_across_locations), HTMLTableHeader('Achievement in reporting period'), HTMLTableCell(achievement_in_reporting_period), ], ] tables.append(indicator_table) for location_data in indicator.indicator_location_data.all(): location_progress = format_total_value_to_string( location_data.disaggregation.get('()'), is_percentage=is_percentage, percentage_display_type="ratio" if indicator.reportable.blueprint.display_type == 'ratio' else None ) if location_data.previous_location_data: prev_value = location_data.previous_location_data.disaggregation.get('()', {}) else: prev_value = {} previous_location_progress = format_total_value_to_string( prev_value, is_percentage=is_percentage, percentage_display_type="ratio" if indicator.reportable.blueprint.display_type == 'ratio' else None ) location_table = [ [ HTMLTableCell(location_data.location.name, rowspan=2, colspan=2), HTMLTableHeader('Previous Location Progress'), HTMLTableCell(previous_location_progress), ], [ HTMLTableHeader('Location Progress'), HTMLTableCell(location_progress), ], ] tables.append(location_table) return tables
def test_sum_empty(self): total = {} self.assertEqual(format_total_value_to_string(total, False, None), "0")
def test_ratio(self): total = {"c": 10000, "d": 20000, "v": 30000} self.assertEqual( format_total_value_to_string(total, True, "ratio"), "30000/20000", )
def test_ratio_missing_denominator(self): total = {"c": 100, "v": 300} self.assertEqual( format_total_value_to_string(total, True, "ratio"), "300/1", )
def test_ratio_empty(self): total = {} self.assertEqual( format_total_value_to_string(total, True, "ratio"), "0/1", )
def test_percentage_zero_denominator(self): total = {"c": 100, "d": 0, "v": 300} self.assertEqual( format_total_value_to_string(total, True, None), "0%", )
def test_percentage(self): total = {"c": 10000, "d": 20000, "v": 30000} self.assertEqual( format_total_value_to_string(total, True, None), "150%", )
def test_percentage_empty(self): total = {} self.assertEqual(format_total_value_to_string(total, True, None), "0%")
def test_sum(self): total = {"c": 10000, "d": 20000, "v": 30000} self.assertEqual( format_total_value_to_string(total, False, None), "30,000", )