Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
38a1b3ec
Commit
38a1b3ec
authored
Aug 06, 2015
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rationalize decimal logic. Closes #3222.
parent
f7d44dfa
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
18 deletions
+30
-18
rest_framework/fields.py
+26
-16
tests/test_fields.py
+4
-2
No files found.
rest_framework/fields.py
View file @
38a1b3ec
...
...
@@ -797,6 +797,11 @@ class DecimalField(Field):
self
.
max_value
=
max_value
self
.
min_value
=
min_value
if
self
.
max_digits
is
not
None
and
self
.
decimal_places
is
not
None
:
self
.
max_whole_digits
=
self
.
max_digits
-
self
.
decimal_places
else
:
self
.
max_whole_digits
=
None
super
(
DecimalField
,
self
)
.
__init__
(
**
kwargs
)
if
self
.
max_value
is
not
None
:
...
...
@@ -840,24 +845,29 @@ class DecimalField(Field):
values or to enhance it in any way you need to.
"""
sign
,
digittuple
,
exponent
=
value
.
as_tuple
()
decimals
=
exponent
*
decimal
.
Decimal
(
-
1
)
if
exponent
<
0
else
0
# digittuple doesn't include any leading zeros.
digits
=
len
(
digittuple
)
if
decimals
>
digits
:
# We have leading zeros up to or past the decimal point. Count
# everything past the decimal point as a digit. We do not count
# 0 before the decimal point as a digit since that would mean
# we would not allow max_digits = decimal_places.
digits
=
decimals
whole_digits
=
digits
-
decimals
if
self
.
max_digits
is
not
None
and
digits
>
self
.
max_digits
:
if
exponent
>=
0
:
# 1234500.0
total_digits
=
len
(
digittuple
)
+
exponent
whole_digits
=
total_digits
decimal_places
=
0
elif
len
(
digittuple
)
>
abs
(
exponent
):
# 123.45
total_digits
=
len
(
digittuple
)
whole_digits
=
total_digits
-
abs
(
exponent
)
decimal_places
=
abs
(
exponent
)
else
:
# 0.001234
total_digits
=
abs
(
exponent
)
whole_digits
=
0
decimal_places
=
total_digits
if
self
.
max_digits
is
not
None
and
total_digits
>
self
.
max_digits
:
self
.
fail
(
'max_digits'
,
max_digits
=
self
.
max_digits
)
if
self
.
decimal_places
is
not
None
and
decimals
>
self
.
decimal_places
:
if
self
.
decimal_places
is
not
None
and
decimal
_place
s
>
self
.
decimal_places
:
self
.
fail
(
'max_decimal_places'
,
max_decimal_places
=
self
.
decimal_places
)
if
self
.
max_
digits
is
not
None
and
self
.
decimal_places
is
not
None
and
whole_digits
>
(
self
.
max_digits
-
self
.
decimal_places
)
:
self
.
fail
(
'max_whole_digits'
,
max_whole_digits
=
self
.
max_
digits
-
self
.
decimal_place
s
)
if
self
.
max_
whole_digits
is
not
None
and
whole_digits
>
self
.
max_whole_digits
:
self
.
fail
(
'max_whole_digits'
,
max_whole_digits
=
self
.
max_
whole_digit
s
)
return
value
...
...
tests/test_fields.py
View file @
38a1b3ec
...
...
@@ -766,15 +766,17 @@ class TestDecimalField(FieldValues):
0
:
Decimal
(
'0'
),
12.3
:
Decimal
(
'12.3'
),
0.1
:
Decimal
(
'0.1'
),
'2E+
2'
:
Decimal
(
'20
0'
),
'2E+
1'
:
Decimal
(
'2
0'
),
}
invalid_inputs
=
(
(
'abc'
,
[
"A valid number is required."
]),
(
Decimal
(
'Nan'
),
[
"A valid number is required."
]),
(
Decimal
(
'Inf'
),
[
"A valid number is required."
]),
(
'12.345'
,
[
"Ensure that there are no more than 3 digits in total."
]),
(
200000000000.0
,
[
"Ensure that there are no more than 3 digits in total."
]),
(
'0.01'
,
[
"Ensure that there are no more than 1 decimal places."
]),
(
123
,
[
"Ensure that there are no more than 2 digits before the decimal point."
])
(
123
,
[
"Ensure that there are no more than 2 digits before the decimal point."
]),
(
'2E+2'
,
[
"Ensure that there are no more than 2 digits before the decimal point."
])
)
outputs
=
{
'1'
:
'1.0'
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment