mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-29 09:30:07 +03:00
Make use of distutils.util.strtobool in BooleanField class
This commit is contained in:
parent
96885dd9a7
commit
979f4d58ea
|
@ -1,6 +1,7 @@
|
||||||
import copy
|
import copy
|
||||||
import datetime
|
import datetime
|
||||||
import decimal
|
import decimal
|
||||||
|
import distutils.util
|
||||||
import functools
|
import functools
|
||||||
import inspect
|
import inspect
|
||||||
import re
|
import re
|
||||||
|
@ -702,43 +703,31 @@ class BooleanField(Field):
|
||||||
}
|
}
|
||||||
default_empty_html = False
|
default_empty_html = False
|
||||||
initial = False
|
initial = False
|
||||||
TRUE_VALUES = {
|
|
||||||
't', 'T',
|
|
||||||
'y', 'Y', 'yes', 'Yes', 'YES',
|
|
||||||
'true', 'True', 'TRUE',
|
|
||||||
'on', 'On', 'ON',
|
|
||||||
'1', 1,
|
|
||||||
True
|
|
||||||
}
|
|
||||||
FALSE_VALUES = {
|
|
||||||
'f', 'F',
|
|
||||||
'n', 'N', 'no', 'No', 'NO',
|
|
||||||
'false', 'False', 'FALSE',
|
|
||||||
'off', 'Off', 'OFF',
|
|
||||||
'0', 0, 0.0,
|
|
||||||
False
|
|
||||||
}
|
|
||||||
NULL_VALUES = {'null', 'Null', 'NULL', '', None}
|
NULL_VALUES = {'null', 'Null', 'NULL', '', None}
|
||||||
|
|
||||||
def to_internal_value(self, data):
|
def to_internal_value(self, data):
|
||||||
try:
|
try:
|
||||||
if data in self.TRUE_VALUES:
|
if data in self.NULL_VALUES and self.allow_null:
|
||||||
return True
|
|
||||||
elif data in self.FALSE_VALUES:
|
|
||||||
return False
|
|
||||||
elif data in self.NULL_VALUES and self.allow_null:
|
|
||||||
return None
|
return None
|
||||||
|
elif isinstance(data, str):
|
||||||
|
try:
|
||||||
|
return bool(distutils.util.strtobool(data))
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
return data
|
||||||
except TypeError: # Input is an unhashable type
|
except TypeError: # Input is an unhashable type
|
||||||
pass
|
pass
|
||||||
self.fail('invalid', input=data)
|
self.fail('invalid', input=data)
|
||||||
|
|
||||||
def to_representation(self, value):
|
def to_representation(self, value):
|
||||||
if value in self.TRUE_VALUES:
|
|
||||||
return True
|
|
||||||
elif value in self.FALSE_VALUES:
|
|
||||||
return False
|
|
||||||
if value in self.NULL_VALUES and self.allow_null:
|
if value in self.NULL_VALUES and self.allow_null:
|
||||||
return None
|
return None
|
||||||
|
elif isinstance(value, str):
|
||||||
|
try:
|
||||||
|
return bool(distutils.util.strtobool(value))
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
return bool(value)
|
return bool(value)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user