def _get_formatted_total(groups):
    calculated_total = 0
    answer_format = {'type': None}
    for group in groups:
        for block in group['blocks']:
            for question in block['questions']:
                for answer in question['answers']:
                    if not answer_format['type']:
                        answer_format = {
                            'type': answer['type'],
                            'unit': answer.get('unit'),
                            'unit_length': answer.get('unit_length'),
                            'currency': answer.get('currency'),
                        }
                    answer_value = answer.get('value') or 0
                    calculated_total += answer_value

    if answer_format['type'] == 'currency':
        return get_formatted_currency(calculated_total,
                                      answer_format['currency'])

    if answer_format['type'] == 'unit':
        return format_unit(answer_format['unit'], calculated_total,
                           answer_format['unit_length'])

    if answer_format['type'] == 'percentage':
        return format_percentage(calculated_total)

    return format_number(calculated_total)
Beispiel #2
0
def _get_formatted_total(block, answer_store, group_instance, schema):
    calculated_total = 0
    for answer in block['calculation']['answers_to_calculate']:
        if group_instance > 0 and not schema.answer_is_in_repeating_group(
                answer):
            group_instance = 0
        calculated_total += get_answer_store_value(answer, answer_store,
                                                   group_instance) or 0

    answer_json = schema.get_answer(
        block['calculation']['answers_to_calculate'][0])
    number_type = answer_json['type']
    if number_type == 'Currency':
        formatted_total = format_currency(calculated_total,
                                          answer_json['currency'])
    elif number_type == 'Unit':
        formatted_total = format_unit(answer_json['unit'], calculated_total)
    elif number_type == 'Percentage':
        formatted_total = format_percentage(calculated_total)
    else:
        formatted_total = format_number(calculated_total)

    return formatted_total
Beispiel #3
0
    def _get_formatted_total(self, groups, current_location):
        calculated_total = 0
        answer_format = {"type": None}
        for group in groups:
            for block in group["blocks"]:
                question = choose_question_to_display(
                    block,
                    self._schema,
                    self._metadata,
                    self._response_metadata,
                    self._answer_store,
                    self._list_store,
                    current_location=current_location,
                )
                for answer in question["answers"]:
                    if not answer_format["type"]:
                        answer_format = {
                            "type": answer["type"],
                            "unit": answer.get("unit"),
                            "unit_length": answer.get("unit_length"),
                            "currency": answer.get("currency"),
                        }
                    answer_value = answer.get("value") or 0
                    calculated_total += answer_value

        if answer_format["type"] == "currency":
            return get_formatted_currency(calculated_total,
                                          answer_format["currency"])

        if answer_format["type"] == "unit":
            return format_unit(answer_format["unit"], calculated_total,
                               answer_format["unit_length"])

        if answer_format["type"] == "percentage":
            return format_percentage(calculated_total)

        return format_number(calculated_total)
Beispiel #4
0
 def test_format_percentage(self):
     self.assertEqual(format_percentage("100"), "100%")
     self.assertEqual(format_percentage(100), "100%")
     self.assertEqual(format_percentage(4.5), "4.5%")
Beispiel #5
0
 def test_format_percentage(self):
     self.assertEqual(format_percentage('100'), '100%')
     self.assertEqual(format_percentage(100), '100%')
     self.assertEqual(format_percentage(4.5), '4.5%')