Пример #1
0
 def from_db_value(self, value, expression, connection):
     if value is not None:
         if isinstance(value, memoryview):
             return format_number(
                 decimal.Decimal(decrypter(bytes(value)).decode()),
                 self.max_digits, self.decimal_places)
         else:
             return format_number(
                 decimal.Decimal(decrypter(value).decode()),
                 self.max_digits, self.decimal_places)
     else:
         return None
Пример #2
0
def decimal_to_string(value, max_digits=16, decimal_places=0):
    """
    Converts decimal to a unicode string for storage / lookup by nonrel
    databases that don't support decimals natively.

    This is an extension to `django.db.backends.utils.format_number`
    that preserves order -- if one decimal is less than another, their
    string representations should compare the same (as strings).

    TODO: Can't this be done using string.format()?
          Not in Python 2.5, str.format is backported to 2.6 only.
    """

    # Handle sign separately.
    if value.is_signed():
        sign = u'-'
        value = abs(value)
    else:
        sign = u''

    # Let Django quantize and cast to a string.
    value = format_number(value, max_digits, decimal_places)

    # Pad with zeroes to a constant width.
    n = value.find('.')
    if n < 0:
        n = len(value)
    if n < max_digits - decimal_places:
        value = u'0' * (max_digits - decimal_places - n) + value
    return sign + value
Пример #3
0
    def get_payment_process_response(self, service, order, urls):
        invoice_id = None
        try:
            invoice_id = str(order.payment_data["localbitcoins"]["id"])
        except KeyError:
            pass

        if not invoice_id:
            invoice_data = {
                'currency':
                order.currency,
                'amount':
                format_number(order.taxful_total_price_value, None,
                              Decimal(2)),
                'description':
                _("Payment for order {id} on {shop}").format(
                    id=order.identifier, shop=order.shop),
                'return_url':
                urls.return_url,
            }
            response = self.get_api_conn().call(
                'POST',
                "/api/merchant/new_invoice/",
                params=invoice_data,
            ).json()['data']
            order.payment_data["localbitcoins"] = response['invoice']
            order.save()
            return HttpResponseRedirect(
                order.payment_data["localbitcoins"]["url"])
        else:
            # Update the status of invoice.
            # If not paid redirect to make payment.
            pass
Пример #4
0
 def convert_decimalfield_value(self, value, expression, connection,
                                context):
     field = expression.field
     val = utils.format_number(value, field.max_digits,
                               field.decimal_places)
     value = utils.typecast_decimal(val)
     return value
Пример #5
0
def decimal_to_string(value, max_digits=16, decimal_places=0):
    """
    Converts decimal to a unicode string for storage / lookup by nonrel
    databases that don't support decimals natively.

    This is an extension to `django.db.backends.utils.format_number`
    that preserves order -- if one decimal is less than another, their
    string representations should compare the same (as strings).

    TODO: Can't this be done using string.format()?
          Not in Python 2.5, str.format is backported to 2.6 only.
    """

    # Handle sign separately.
    if value.is_signed():
        sign = u'-'
        value = abs(value)
    else:
        sign = u''

    # Let Django quantize and cast to a string.
    value = format_number(value, max_digits, decimal_places)

    # Pad with zeroes to a constant width.
    n = value.find('.')
    if n < 0:
        n = len(value)
    if n < max_digits - decimal_places:
        value = u'0' * (max_digits - decimal_places - n) + value
    return sign + value
Пример #6
0
 def convert_decimalfield_value(self, value, expression, connection,
                                *ignore):
     value = backend_utils.format_number(
         value, expression.output_field.max_digits,
         expression.output_field.decimal_places)
     if value is not None:
         return decimal.Decimal(value)
Пример #7
0
 def value_to_db_decimal(self, value, max_digits, decimal_places):
     """
     Transform a decimal.Decimal value to an object compatible with what is
     expected by the backend driver for decimal (numeric) columns.
     """
     if value is None:
         return None
     return utils.format_number(value, max_digits, decimal_places)
Пример #8
0
 def value_to_db_decimal(self, value, max_digits, decimal_places):
     """
     Transform a decimal.Decimal value to an object compatible with what is
     expected by the backend driver for decimal (numeric) columns.
     """
     if value is None:
         return None
     return utils.format_number(value, max_digits, decimal_places)
Пример #9
0
 def adapt_decimalfield_value(self,
                              value,
                              max_digits=None,
                              decimal_places=None):
     """
     Transforms a decimal.Decimal value to an object compatible with what is
     expected by the backend driver for decimal (numeric) columns.
     """
     return utils.format_number(value, max_digits, decimal_places)
Пример #10
0
    def convert_decimalfield_value(self, value, expression, connection, context):
        field = expression.field

        val = utils.format_number(value, field.max_digits, field.decimal_places)
        
        if val is not None:
            value = decimal.Decimal(val)
            
        return value
 def format_number(self, value):
     """
     Formats a number into a string with the requisite number of digits and
     decimal places.
     """
     # Method moved to django.db.backends.utils.
     #
     # It is preserved because it is used by the oracle backend
     # (django.db.backends.oracle.query), and also for
     # backwards-compatibility with any external code which may have used
     # this method.
     from django.db.backends import utils
     return utils.format_number(value, self.max_digits, self.decimal_places)
Пример #12
0
 def adapt_decimalfield_value(self, value, max_digits=None, decimal_places=None):
     """
     Transform a decimal.Decimal value to an object compatible with what is
     expected by the backend driver for decimal (numeric) columns.
     """
     return utils.format_number(value, max_digits, decimal_places)
Пример #13
0
def test_rounding():
    with pytest.raises(InvalidOperation):
        format_number(Decimal("99.99990"), 6, 5)
    format_number(Decimal("99.9999"), 6, 4)
Пример #14
0
 def time_spent(self):
     """
     Return the time spent for the worklog in hours.
     """
     return Decimal(format_number(self.time_spent_seconds / 3600.0, 12, 8))
Пример #15
0
 def equal(value, max_d, places, result):
     self.assertEqual(format_number(Decimal(value), max_d, places),
                      result)
Пример #16
0
def test_rounding():
    with pytest.raises(InvalidOperation):
        format_number(Decimal('99.99990'), 6, 5)
    format_number(Decimal('99.9999'), 6, 4)
Пример #17
0
 def equal(value, max_d, places, result):
     self.assertEqual(format_number(Decimal(value), max_d, places), result)
Пример #18
0
import datetime