def _get_quantity(self, shop_product): quantity_text = self.request.GET.get("quantity", '') quantity = parse_simple_decimal(quantity_text, None) if quantity is None or quantity < 0: return None unit_type = self.request.GET.get('unitType', 'internal') if unit_type == 'internal': return quantity else: return shop_product.unit.from_display(decimal.Decimal(quantity))
def _get_quantity(self, shop_product): quantity_text = self.request.GET.get("quantity", '') quantity = parse_simple_decimal(quantity_text, None) if quantity is None or quantity < 0: return None unit_type = self.request.GET.get('unitType', 'internal') if unit_type == 'internal': return quantity else: return shop_product.unit.from_display(decimal.Decimal(quantity))
def _get_quantity(self): quantity_text = self.request.GET.get("quantity", '') quantity = parse_simple_decimal(quantity_text, None) if quantity is None or quantity < 0: return None unit_type = self.request.GET.get('unitType', 'internal') if unit_type == 'internal': return quantity else: try: shop_product = self.object.get_shop_instance(self.request.shop) except ShopProduct.DoesNotExist: return None return shop_product.unit.from_display(decimal.Decimal(quantity))
def test_parse_simple_decimal(input_value, expected_result): if expected_result is not None: result = parse_simple_decimal(input_value) assert result == expected_result assert isinstance(result, Decimal) if six.PY2 and isinstance(input_value, six.text_type): bytes_input = input_value.encode('utf-8') assert parse_simple_decimal(bytes_input) == expected_result else: assert parse_simple_decimal(input_value, None) is None assert parse_simple_decimal(input_value, 0) == 0 with pytest.raises(ValueError) as exc_info: parse_simple_decimal(input_value) assert '{}'.format(exc_info.value) == ( "Error! Value `%r` can't be parsed as a simple decimal." % (input_value,))
def test_parse_simple_decimal(input_value, expected_result): if expected_result is not None: result = parse_simple_decimal(input_value) assert result == expected_result assert isinstance(result, Decimal) if six.PY2 and isinstance(input_value, six.text_type): bytes_input = input_value.encode('utf-8') assert parse_simple_decimal(bytes_input) == expected_result else: assert parse_simple_decimal(input_value, None) is None assert parse_simple_decimal(input_value, 0) == 0 with pytest.raises(ValueError) as exc_info: parse_simple_decimal(input_value) assert '{}'.format(exc_info.value) == ( "Cannot parse as simple decimal: %r" % (input_value,))