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)
Пример #2
0
def format_playback_value(value: Union[float, Decimal],
                          currency: Optional[str] = None) -> str:
    if currency:
        return get_formatted_currency(value, currency)

    formatted_number: str = format_number(value)
    return formatted_number
    def test_too_big_when_max_set_is_invalid(self):
        validator = NumberRange(maximum=9999999999)

        mock_form = Mock()
        mock_field = Mock()
        mock_field.data = 10000000000

        with self.assertRaises(ValidationError) as ite:
            validator(mock_form, mock_field)

        self.assertEqual(
            error_messages['NUMBER_TOO_LARGE'] %
            dict(max=format_number(9999999999)), str(ite.exception))
Пример #4
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
Пример #5
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)
Пример #6
0
def format_playback_value(value, currency=None):
    if currency:
        return get_formatted_currency(value, currency)
    return format_number(value)
Пример #7
0
 def test_format_number(self):
     self.assertEqual(format_number(123), "123")
     self.assertEqual(format_number("123.4"), "123.4")
     self.assertEqual(format_number("123.40"), "123.4")
     self.assertEqual(format_number("1000"), "1,000")
     self.assertEqual(format_number("10000"), "10,000")
     self.assertEqual(format_number("100000000"), "100,000,000")
     self.assertEqual(format_number(0), "0")
     self.assertEqual(format_number(0.00), "0")
     self.assertEqual(format_number(""), "")
     self.assertEqual(format_number(None), "")
     self.assertEqual(format_number(Undefined()), "")
Пример #8
0
 def test_format_number(self):
     self.assertEqual(format_number(123), '123')
     self.assertEqual(format_number('123.4'), '123.4')
     self.assertEqual(format_number('123.40'), '123.4')
     self.assertEqual(format_number('1000'), '1,000')
     self.assertEqual(format_number('10000'), '10,000')
     self.assertEqual(format_number('100000000'), '100,000,000')
     self.assertEqual(format_number(0), '0')
     self.assertEqual(format_number(0.00), '0')
     self.assertEqual(format_number(''), '')
     self.assertEqual(format_number(None), '')
     self.assertEqual(format_number(Undefined()), '')