Skip to content

Add localization support to the FloatField #4925

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

Merged
merged 25 commits into from
Oct 4, 2017

Conversation

kgeorgy
Copy link

@kgeorgy kgeorgy commented Feb 27, 2017

Description

Full description of the issue can be found here: Issue #4584.

The goals of this pull request is add the localize and coerce_to_string parameters, which are available with the DecimalField, to the FloatField.

This pull request is a simplification of the pull request #4586 as recommended by the maintainer.

refs #4584

kgeorgy and others added 3 commits February 21, 2017 15:27
Merge master from official django-rest-framework
…FloatField like it is done for the DecimalField, in order to use the user current local to serialise and deserialise the float value if required.
@@ -939,7 +945,12 @@ def __init__(self, **kwargs):

def to_internal_value(self, data):

if isinstance(data, six.text_type) and len(data) > self.MAX_STRING_LENGTH:
data = smart_text(data).strip()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not keen on this. For the default behavior we'd now be unnecessarily transforming the incoming value into a string, and then back to a float again. Perhaps we could push this inside the if self.localize: block?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No because we could have a string as input without having the localize attribute to True.

I'll propose a change doing all string related sanitization only if the input is a string.

@tomchristie
Copy link
Member

Looking good. One comment to address.
We also need documentation. That could go into this pull request (add to the serializer fields section). Alternatively I'd be happy with the documentation just added against this thread, so that I can then easily go ahead and update the documentation accordingly.

@tomchristie
Copy link
Member

And apologies for the delay! This one seems to have slipped through the cracks for a bit.

@kgeorgy
Copy link
Author

kgeorgy commented Aug 16, 2017

I had a bit of time to rework on this :) Then now the string related stuff are done only if the input of to_internal_value is a string (using six to ensure python 2 and 3 compatibility). This will ensure to not convert a float value to string and then back to float if the input is already a float.

coerce_to_string and localize parameters ares now documented.

Thank you for the review, this stuff starts to look good.

@carltongibson carltongibson added this to the 3.6.5 Release milestone Aug 21, 2017
@carltongibson carltongibson removed this from the 3.6.4 Release milestone Aug 21, 2017
@carltongibson
Copy link
Collaborator

@kgeorgy I've milestoned this for 3.7.0. I'll review it, and we'll either have it or not for then.

Any chance you could rebase?

Thanks for the input!

@kgeorgy kgeorgy changed the base branch from master to version-3.7 September 28, 2017 09:50
@kgeorgy
Copy link
Author

kgeorgy commented Sep 28, 2017

@carltongibson Done!

- `max_value` Validate that the number provided is no greater than this value.
- `min_value` Validate that the number provided is no less than this value.
- `localize` Set to `True` to enable localization of input and output based on the current locale. This will also force `coerce_to_string` to `True`. Defaults to `False`. Note that data formatting is enabled if you have set `USE_L10N=True` in your settings file.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defaults to False

Do you mean None? It's a boolean field right? Should the default be False, rather than None?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it is the way it is in done in the DecimalField, and this pull request is just to apply the exact same principle to FloatField, but it is OK for me to use False as default.

@@ -954,6 +956,10 @@ class FloatField(Field):
def __init__(self, **kwargs):
self.max_value = kwargs.pop('max_value', None)
self.min_value = kwargs.pop('min_value', None)
self.localize = kwargs.pop('localize', False)
self.coerce_to_string = kwargs.pop('coerce_to_string', None)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not False here?

Copy link
Author

@kgeorgy kgeorgy Oct 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same reason as in the previous point. But we can change both.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you just make this False (and in the docs signature too) — I think it's cleaner. Thanks!

self.localize = kwargs.pop('localize', False)
self.coerce_to_string = kwargs.pop('coerce_to_string', None)
if self.localize:
self.coerce_to_string = True
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this here?

In to_representation:

        if self.localize:
             return number_format(value)
         if self.coerce_to_string:
             ...

So if self.localize, self.coerce_to_string is never tested.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because if localize is True, we always coerce to string (number_format returns a string). We only needs to check coerce_to_string if localize is False. (And we override coerce_to_string to True in the init, so when localize is True, coerce_to_string must be True).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we can drop this line...? (We don't need to override coerce_to_string since it will never be used.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be used if we don't set localize to True, we could have localize = False and coerce_to_string = True.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure... Line 960 is fine. This comes into play in to_representation when localize is `False.

But line 961 and 962 are no-ops really (right?) — So (I think) we can delete them. Yes/no?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok got it... Yes it seems to... just come from a copy & past from line 989 and 990 in DecimalField.init. I'll remove it.

Copy link
Collaborator

@carltongibson carltongibson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kgeorgy I just have a couple of questions. (See comments) but looks good!

@kgeorgy
Copy link
Author

kgeorgy commented Oct 4, 2017

@carltongibson coerce_to_string is now defaulted to False (as it was documented), and the unneeded overriding lines are now removed.

Copy link
Collaborator

@carltongibson carltongibson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Awesome work. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants