mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 20:40:14 +03:00
documentation for Base64ImageField and test for blank case
This commit is contained in:
parent
50cf7ca528
commit
92566cce56
|
@ -299,6 +299,7 @@ Requires either the `Pillow` package or `PIL` package. The `Pillow` package is
|
||||||
|
|
||||||
Signature and validation is the same as with `FileField`.
|
Signature and validation is the same as with `FileField`.
|
||||||
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
**Note:** `FileFields` and `ImageFields` are only suitable for use with MultiPartParser, since e.g. json doesn't support file uploads.
|
**Note:** `FileFields` and `ImageFields` are only suitable for use with MultiPartParser, since e.g. json doesn't support file uploads.
|
||||||
|
@ -306,6 +307,32 @@ Django's regular [FILE_UPLOAD_HANDLERS] are used for handling uploaded files.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
||||||
|
## Base64ImageField
|
||||||
|
|
||||||
|
An image representation for Base64ImageField
|
||||||
|
|
||||||
|
Intherited by `ImageField`
|
||||||
|
|
||||||
|
**Signature:** `Base64ImageField()`
|
||||||
|
|
||||||
|
- It takes a base64 image as a string.
|
||||||
|
- a base64 image: `data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7`
|
||||||
|
- Base64ImageField accepts only the part after base64, `R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7`
|
||||||
|
|
||||||
|
**Example:**
|
||||||
|
|
||||||
|
#serializer
|
||||||
|
class UploadedBase64ImageSerializer(serializers.Serializer):
|
||||||
|
file = serializers.Base64ImageField(required=False)
|
||||||
|
created = serializers.DateTimeField()
|
||||||
|
|
||||||
|
#use the serializer
|
||||||
|
file = 'R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
|
||||||
|
serializer = UploadedBase64ImageSerializer(data={'created': now, 'file': file})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Custom fields
|
# Custom fields
|
||||||
|
|
||||||
If you want to create a custom field, you'll probably want to override either one or both of the `.to_native()` and `.from_native()` methods. These two methods are used to convert between the initial datatype, and a primitive, serializable datatype. Primitive datatypes may be any of a number, string, date/time/datetime or None. They may also be any list or dictionary like object that only contains other primitive objects.
|
If you want to create a custom field, you'll probably want to override either one or both of the `.to_native()` and `.from_native()` methods. These two methods are used to convert between the initial datatype, and a primitive, serializable datatype. Primitive datatypes may be any of a number, string, date/time/datetime or None. They may also be any list or dictionary like object that only contains other primitive objects.
|
||||||
|
|
BIN
env/share/man/man1/ipcluster.1.gz
vendored
Normal file
BIN
env/share/man/man1/ipcluster.1.gz
vendored
Normal file
Binary file not shown.
BIN
env/share/man/man1/ipcontroller.1.gz
vendored
Normal file
BIN
env/share/man/man1/ipcontroller.1.gz
vendored
Normal file
Binary file not shown.
BIN
env/share/man/man1/ipengine.1.gz
vendored
Normal file
BIN
env/share/man/man1/ipengine.1.gz
vendored
Normal file
Binary file not shown.
BIN
env/share/man/man1/ipython.1.gz
vendored
Normal file
BIN
env/share/man/man1/ipython.1.gz
vendored
Normal file
Binary file not shown.
|
@ -1062,6 +1062,9 @@ class Base64ImageField(ImageField):
|
||||||
"""
|
"""
|
||||||
def from_native(self, base64_data):
|
def from_native(self, base64_data):
|
||||||
# Check if this is a base64 string
|
# Check if this is a base64 string
|
||||||
|
if base64_data in validators.EMPTY_VALUES:
|
||||||
|
return None
|
||||||
|
|
||||||
if isinstance(base64_data, basestring):
|
if isinstance(base64_data, basestring):
|
||||||
# Try to decode the file. Return validation error if it fails.
|
# Try to decode the file. Return validation error if it fails.
|
||||||
try:
|
try:
|
||||||
|
@ -1081,7 +1084,7 @@ class Base64ImageField(ImageField):
|
||||||
|
|
||||||
def to_native(self, value):
|
def to_native(self, value):
|
||||||
# Return url including domain name.
|
# Return url including domain name.
|
||||||
return ""
|
return value.name
|
||||||
|
|
||||||
def get_file_extension(self, filename, decoded_file):
|
def get_file_extension(self, filename, decoded_file):
|
||||||
extension = imghdr.what(filename, decoded_file)
|
extension = imghdr.what(filename, decoded_file)
|
||||||
|
|
|
@ -1011,7 +1011,7 @@ class UploadedBase64Image(object):
|
||||||
|
|
||||||
|
|
||||||
class UploadedBase64ImageSerializer(serializers.Serializer):
|
class UploadedBase64ImageSerializer(serializers.Serializer):
|
||||||
file = serializers.Base64ImageField()
|
file = serializers.Base64ImageField(required=False)
|
||||||
created = serializers.DateTimeField()
|
created = serializers.DateTimeField()
|
||||||
|
|
||||||
def restore_object(self, attrs, instance=None):
|
def restore_object(self, attrs, instance=None):
|
||||||
|
@ -1025,6 +1025,9 @@ class UploadedBase64ImageSerializer(serializers.Serializer):
|
||||||
class Base64ImageSerializerTests(TestCase):
|
class Base64ImageSerializerTests(TestCase):
|
||||||
|
|
||||||
def test_create(self):
|
def test_create(self):
|
||||||
|
"""
|
||||||
|
Test for creating Base64 image in the server side
|
||||||
|
"""
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
file = 'R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
|
file = 'R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
|
||||||
serializer = UploadedBase64ImageSerializer(data={'created': now, 'file': file})
|
serializer = UploadedBase64ImageSerializer(data={'created': now, 'file': file})
|
||||||
|
@ -1033,24 +1036,25 @@ class Base64ImageSerializerTests(TestCase):
|
||||||
self.assertEqual(serializer.object.created, uploaded_image.created)
|
self.assertEqual(serializer.object.created, uploaded_image.created)
|
||||||
self.assertFalse(serializer.object is uploaded_image)
|
self.assertFalse(serializer.object is uploaded_image)
|
||||||
|
|
||||||
|
|
||||||
def test_creation_failure(self):
|
|
||||||
"""
|
|
||||||
Passing file=None should result in an ValidationError
|
|
||||||
"""
|
|
||||||
errmsg = 'This field is required.'
|
|
||||||
now = datetime.datetime.now()
|
|
||||||
serializer = UploadedBase64ImageSerializer(data={'created': now})
|
|
||||||
self.assertFalse(serializer.is_valid())
|
|
||||||
self.assertEqual(serializer.errors, {'file': [errmsg]})
|
|
||||||
|
|
||||||
def test_validation_error_with_non_file(self):
|
def test_validation_error_with_non_file(self):
|
||||||
"""
|
"""
|
||||||
Passing non-base64 should raise a validation error.
|
Passing non-base64 should raise a validation error.
|
||||||
"""
|
"""
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
errmsg = "Please upload a valid image."
|
errmsg = "Please upload a valid image."
|
||||||
|
|
||||||
serializer = UploadedBase64ImageSerializer(data={'created': now, 'file': 'abc'})
|
serializer = UploadedBase64ImageSerializer(data={'created': now, 'file': 'abc'})
|
||||||
self.assertFalse(serializer.is_valid())
|
self.assertFalse(serializer.is_valid())
|
||||||
self.assertEqual(serializer.errors, {'file': [errmsg]})
|
self.assertEqual(serializer.errors, {'file': [errmsg]})
|
||||||
|
|
||||||
|
|
||||||
|
def test_remove_with_empty_string(self):
|
||||||
|
"""
|
||||||
|
Passing empty string as data should cause image to be removed
|
||||||
|
"""
|
||||||
|
now = datetime.datetime.now()
|
||||||
|
file = 'R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
|
||||||
|
uploaded_image = UploadedBase64Image(file=file, created=now)
|
||||||
|
serializer = UploadedBase64ImageSerializer(instance=uploaded_image, data={'created': now, 'file': ''})
|
||||||
|
self.assertTrue(serializer.is_valid())
|
||||||
|
self.assertEqual(serializer.object.created, uploaded_image.created)
|
||||||
|
self.assertIsNone(serializer.object.file)
|
Loading…
Reference in New Issue
Block a user