-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add localize keyword argument to DecimalField
#4233
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
) | ||
from django.utils.duration import duration_string | ||
from django.utils.encoding import is_protected_type, smart_text | ||
from django.utils.formats import localize_input, sanitize_separators | ||
from django.utils.functional import cached_property | ||
from django.utils.ipv6 import clean_ipv6_address | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
@@ -871,6 +872,7 @@ def __init__(self, **kwargs): | |
self.validators.append(MinValueValidator(self.min_value, message=message)) | ||
|
||
def to_internal_value(self, data): | ||
|
||
if isinstance(data, six.text_type) and len(data) > self.MAX_STRING_LENGTH: | ||
self.fail('max_string_length') | ||
|
||
|
@@ -895,9 +897,11 @@ class DecimalField(Field): | |
} | ||
MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs. | ||
|
||
def __init__(self, max_digits, decimal_places, coerce_to_string=None, max_value=None, min_value=None, **kwargs): | ||
def __init__(self, max_digits, decimal_places, coerce_to_string=None, max_value=None, min_value=None, | ||
localize=False, **kwargs): | ||
self.max_digits = max_digits | ||
self.decimal_places = decimal_places | ||
self.localize = localize | ||
if coerce_to_string is not None: | ||
self.coerce_to_string = coerce_to_string | ||
|
||
|
@@ -923,7 +927,12 @@ def to_internal_value(self, data): | |
Validate that the input is a decimal number and return a Decimal | ||
instance. | ||
""" | ||
|
||
if self.localize: | ||
data = sanitize_separators(data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would make slightly more sense after the |
||
|
||
data = smart_text(data).strip() | ||
|
||
if len(data) > self.MAX_STRING_LENGTH: | ||
self.fail('max_string_length') | ||
|
||
|
@@ -988,6 +997,9 @@ def to_representation(self, value): | |
|
||
if not coerce_to_string: | ||
return quantized | ||
if self.localize: | ||
return localize_input(quantized) | ||
|
||
return '{0:f}'.format(quantized) | ||
|
||
def quantize(self, value): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Presumable we should also ensure that setting
localize
forces coerce_to_string to beTrue
, right?