mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-01 02:50:06 +03:00
Merge 602eec8fa0
into 28040b3bda
This commit is contained in:
commit
8c63c6624d
|
@ -1141,11 +1141,13 @@ class DecimalField(Field):
|
||||||
return value
|
return value
|
||||||
|
|
||||||
context = decimal.getcontext().copy()
|
context = decimal.getcontext().copy()
|
||||||
|
# For Python 2.7 compatibility when using cdecimal
|
||||||
|
rounding = context.rounding if self.rounding is None else self.rounding
|
||||||
if self.max_digits is not None:
|
if self.max_digits is not None:
|
||||||
context.prec = self.max_digits
|
context.prec = self.max_digits
|
||||||
return value.quantize(
|
return value.quantize(
|
||||||
decimal.Decimal('.1') ** self.decimal_places,
|
decimal.Decimal('.1') ** self.decimal_places,
|
||||||
rounding=self.rounding,
|
rounding=rounding,
|
||||||
context=context
|
context=context
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ from decimal import ROUND_DOWN, ROUND_UP, Decimal
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import pytz
|
import pytz
|
||||||
|
from _pytest.monkeypatch import MonkeyPatch
|
||||||
from django.core.exceptions import ValidationError as DjangoValidationError
|
from django.core.exceptions import ValidationError as DjangoValidationError
|
||||||
from django.http import QueryDict
|
from django.http import QueryDict
|
||||||
from django.test import TestCase, override_settings
|
from django.test import TestCase, override_settings
|
||||||
|
@ -18,6 +19,11 @@ from rest_framework import exceptions, serializers
|
||||||
from rest_framework.compat import ProhibitNullCharactersValidator
|
from rest_framework.compat import ProhibitNullCharactersValidator
|
||||||
from rest_framework.fields import DjangoImageField, is_simple_callable
|
from rest_framework.fields import DjangoImageField, is_simple_callable
|
||||||
|
|
||||||
|
try:
|
||||||
|
import cdecimal
|
||||||
|
except ImportError:
|
||||||
|
cdecimal = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import typings
|
import typings
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
@ -1150,6 +1156,16 @@ class TestQuantizedValueForDecimal(TestCase):
|
||||||
expected_digit_tuple = (0, (1, 2, 0, 0), -2)
|
expected_digit_tuple = (0, (1, 2, 0, 0), -2)
|
||||||
assert value == expected_digit_tuple
|
assert value == expected_digit_tuple
|
||||||
|
|
||||||
|
@unittest.skipUnless(cdecimal, 'requires python 2.7')
|
||||||
|
def test_quantize_on_monkey_patched_cdecimal(self):
|
||||||
|
# Monkey-patch cdecimal to replace decimal in for DecimalField
|
||||||
|
monkeypatch = MonkeyPatch()
|
||||||
|
|
||||||
|
with monkeypatch.context() as m:
|
||||||
|
m.setattr('rest_framework.fields.decimal', cdecimal)
|
||||||
|
f = rest_framework.fields.DecimalField(max_digits=4, decimal_places=2)
|
||||||
|
f.quantize(cdecimal.Decimal('1.234'))
|
||||||
|
|
||||||
|
|
||||||
class TestNoDecimalPlaces(FieldValues):
|
class TestNoDecimalPlaces(FieldValues):
|
||||||
valid_inputs = {
|
valid_inputs = {
|
||||||
|
|
1
tox.ini
1
tox.ini
|
@ -24,6 +24,7 @@ deps =
|
||||||
django20: Django>=2.0,<2.1
|
django20: Django>=2.0,<2.1
|
||||||
django21: Django>=2.1,<2.2
|
django21: Django>=2.1,<2.2
|
||||||
djangomaster: https://github.com/django/django/archive/master.tar.gz
|
djangomaster: https://github.com/django/django/archive/master.tar.gz
|
||||||
|
py27: m3-cdecimal
|
||||||
-rrequirements/requirements-testing.txt
|
-rrequirements/requirements-testing.txt
|
||||||
-rrequirements/requirements-optionals.txt
|
-rrequirements/requirements-optionals.txt
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user