Skip to content
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

Fixed #29869 -- Made UUIDField.to_python() convert int values. #10557

Merged
merged 1 commit into from Oct 25, 2018

Conversation

CuriousLearner
Copy link
Member

@CuriousLearner CuriousLearner commented Oct 24, 2018

@@ -2319,6 +2319,8 @@ def get_db_prep_value(self, value, connection, prepared=False):

def to_python(self, value):
if value is not None and not isinstance(value, uuid.UUID):
if isinstance(value, int):
return uuid.UUID(int=value)
Copy link
Member

Choose a reason for hiding this comment

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

This needs to be done in the ValueError try/except below as this will fail for integer larger than 128 bits. A test should be added for this case as well.

> uuid.UUID(int=(2 ** 128) - 1)
UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
> uuid.UUID(int=(2 ** 128))
ValueError: int is out of range (need a 128-bit value)

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, yes! We should check that.

I've modified the patch to keep a check on these things. Can you have a look now, please?

self.error_messages['invalid'],
code='invalid',
params={'value': value},
)
Copy link
Member

Choose a reason for hiding this comment

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

We should find a way to reduce the duplication between the two blocks. What about

form = 'int' if isinstance(value, int) else 'hex'
try:
    return uuid.UUID(**{form: value})
except (AttributeError, ValueError):
    raise exceptions.ValidationError(
        self.error_messages['invalid'],
        code='invalid',
        params={'value': value},
    )

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, this actually looks good to me!

I've made these change! Thanks a lot for you suggestion :)

@charettes charettes changed the title Fixed #29869 -- UUIDField.to_python() convert int values Fixed #29869 -- Made UUIDField.to_python() convert int values. Oct 24, 2018
@timgraham timgraham merged commit 83c7096 into django:master Oct 25, 2018
@CuriousLearner
Copy link
Member Author

Thanks for reviewing and merging

@charettes @timgraham :)

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