mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-06 21:40:13 +03:00
Add 'format' argument to UUIDField
To allow serializations to control the representation format of a UUID value
This commit is contained in:
parent
aa131ae258
commit
079da5e7f0
|
@ -639,20 +639,37 @@ class URLField(CharField):
|
||||||
|
|
||||||
|
|
||||||
class UUIDField(Field):
|
class UUIDField(Field):
|
||||||
|
valid_formats = ('hex_verbose', 'hex', 'int', 'urn')
|
||||||
|
|
||||||
default_error_messages = {
|
default_error_messages = {
|
||||||
'invalid': _('"{value}" is not a valid UUID.'),
|
'invalid': _('"{value}" is not a valid UUID.'),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
self.uuid_format = kwargs.pop('format', None) or 'hex_verbose'
|
||||||
|
if self.uuid_format not in self.valid_formats:
|
||||||
|
raise ValueError(
|
||||||
|
'Invalid format for uuid representation. '
|
||||||
|
'Must be one of "{0}"'.format('", "'.join(self.valid_formats))
|
||||||
|
)
|
||||||
|
super(UUIDField, self).__init__(**kwargs)
|
||||||
|
|
||||||
def to_internal_value(self, data):
|
def to_internal_value(self, data):
|
||||||
if not isinstance(data, uuid.UUID):
|
if not isinstance(data, uuid.UUID):
|
||||||
try:
|
try:
|
||||||
return uuid.UUID(data)
|
if self.uuid_format == 'int':
|
||||||
|
return uuid.UUID(int=data)
|
||||||
|
else:
|
||||||
|
return uuid.UUID(hex=data)
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
self.fail('invalid', value=data)
|
self.fail('invalid', value=data)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def to_representation(self, value):
|
def to_representation(self, value):
|
||||||
return str(value)
|
if self.uuid_format == 'hex_verbose':
|
||||||
|
return str(value)
|
||||||
|
else:
|
||||||
|
return getattr(value, self.uuid_format)
|
||||||
|
|
||||||
|
|
||||||
# Number types...
|
# Number types...
|
||||||
|
|
|
@ -526,7 +526,8 @@ class TestUUIDField(FieldValues):
|
||||||
"""
|
"""
|
||||||
valid_inputs = {
|
valid_inputs = {
|
||||||
'825d7aeb-05a9-45b5-a5b7-05df87923cda': uuid.UUID('825d7aeb-05a9-45b5-a5b7-05df87923cda'),
|
'825d7aeb-05a9-45b5-a5b7-05df87923cda': uuid.UUID('825d7aeb-05a9-45b5-a5b7-05df87923cda'),
|
||||||
'825d7aeb05a945b5a5b705df87923cda': uuid.UUID('825d7aeb-05a9-45b5-a5b7-05df87923cda')
|
'825d7aeb05a945b5a5b705df87923cda': uuid.UUID('825d7aeb-05a9-45b5-a5b7-05df87923cda'),
|
||||||
|
'urn:uuid:213b7d9b-244f-410d-828c-dabce7a2615d': uuid.UUID('213b7d9b-244f-410d-828c-dabce7a2615d'),
|
||||||
}
|
}
|
||||||
invalid_inputs = {
|
invalid_inputs = {
|
||||||
'825d7aeb-05a9-45b5-a5b7': ['"825d7aeb-05a9-45b5-a5b7" is not a valid UUID.']
|
'825d7aeb-05a9-45b5-a5b7': ['"825d7aeb-05a9-45b5-a5b7" is not a valid UUID.']
|
||||||
|
@ -536,6 +537,18 @@ class TestUUIDField(FieldValues):
|
||||||
}
|
}
|
||||||
field = serializers.UUIDField()
|
field = serializers.UUIDField()
|
||||||
|
|
||||||
|
def _test_format(self, uuid_format, formatted_uuid_0):
|
||||||
|
field = serializers.UUIDField(format=uuid_format)
|
||||||
|
assert field.to_representation(uuid.UUID(int=0)) == formatted_uuid_0
|
||||||
|
assert field.to_internal_value(formatted_uuid_0) == uuid.UUID(int=0)
|
||||||
|
|
||||||
|
def test_formats(self):
|
||||||
|
self._test_format('int', 0)
|
||||||
|
self._test_format(None, '00000000-0000-0000-0000-000000000000')
|
||||||
|
self._test_format('hex_verbose', '00000000-0000-0000-0000-000000000000')
|
||||||
|
self._test_format('urn', 'urn:uuid:00000000-0000-0000-0000-000000000000')
|
||||||
|
self._test_format('hex', '0' * 32)
|
||||||
|
|
||||||
|
|
||||||
# Number types...
|
# Number types...
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user