mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-25 07:50:41 +03:00
Change ImageField validation pattern, use validators from DjangoImageField (#5539)
This commit is contained in:
parent
0552810410
commit
d49d796c85
|
@ -1553,8 +1553,7 @@ class ImageField(FileField):
|
|||
file_object = super(ImageField, self).to_internal_value(data)
|
||||
django_field = self._DjangoImageField()
|
||||
django_field.error_messages = self.error_messages
|
||||
django_field.to_python(file_object)
|
||||
return file_object
|
||||
return django_field.clean(file_object)
|
||||
|
||||
|
||||
# Composite field types...
|
||||
|
|
|
@ -14,7 +14,7 @@ from django.utils.timezone import activate, deactivate, utc
|
|||
|
||||
import rest_framework
|
||||
from rest_framework import compat, serializers
|
||||
from rest_framework.fields import is_simple_callable
|
||||
from rest_framework.fields import DjangoImageField, is_simple_callable
|
||||
|
||||
try:
|
||||
import pytz
|
||||
|
@ -1714,15 +1714,24 @@ class TestFieldFieldWithName(FieldValues):
|
|||
field = serializers.FileField(use_url=False)
|
||||
|
||||
|
||||
def ext_validator(value):
|
||||
if not value.name.endswith('.png'):
|
||||
raise serializers.ValidationError('File extension is not allowed. Allowed extensions is png.')
|
||||
|
||||
|
||||
# Stub out mock Django `forms.ImageField` class so we don't *actually*
|
||||
# call into it's regular validation, or require PIL for testing.
|
||||
class FailImageValidation(object):
|
||||
class PassImageValidation(DjangoImageField):
|
||||
default_validators = [ext_validator]
|
||||
|
||||
def to_python(self, value):
|
||||
return value
|
||||
|
||||
|
||||
class FailImageValidation(PassImageValidation):
|
||||
def to_python(self, value):
|
||||
if value.name == 'badimage.png':
|
||||
raise serializers.ValidationError(self.error_messages['invalid_image'])
|
||||
|
||||
|
||||
class PassImageValidation(object):
|
||||
def to_python(self, value):
|
||||
return value
|
||||
|
||||
|
||||
|
@ -1732,7 +1741,8 @@ class TestInvalidImageField(FieldValues):
|
|||
"""
|
||||
valid_inputs = {}
|
||||
invalid_inputs = [
|
||||
(MockFile(name='example.txt', size=10), ['Upload a valid image. The file you uploaded was either not an image or a corrupted image.'])
|
||||
(MockFile(name='badimage.png', size=10), ['Upload a valid image. The file you uploaded was either not an image or a corrupted image.']),
|
||||
(MockFile(name='goodimage.html', size=10), ['File extension is not allowed. Allowed extensions is png.'])
|
||||
]
|
||||
outputs = {}
|
||||
field = serializers.ImageField(_DjangoImageField=FailImageValidation)
|
||||
|
@ -1743,7 +1753,7 @@ class TestValidImageField(FieldValues):
|
|||
Values for an valid `ImageField`.
|
||||
"""
|
||||
valid_inputs = [
|
||||
(MockFile(name='example.txt', size=10), MockFile(name='example.txt', size=10))
|
||||
(MockFile(name='example.png', size=10), MockFile(name='example.png', size=10))
|
||||
]
|
||||
invalid_inputs = {}
|
||||
outputs = {}
|
||||
|
|
Loading…
Reference in New Issue
Block a user