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

Add support for allowed file extensions #9224

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
from django.core.exceptions import ObjectDoesNotExist
from django.core.exceptions import ValidationError as DjangoValidationError
from django.core.validators import (
EmailValidator, MaxLengthValidator, MaxValueValidator, MinLengthValidator,
MinValueValidator, ProhibitNullCharactersValidator, RegexValidator,
URLValidator
EmailValidator, FileExtensionValidator, MaxLengthValidator,
MaxValueValidator, MinLengthValidator, MinValueValidator,
ProhibitNullCharactersValidator, RegexValidator, URLValidator
)
from django.forms import FilePathField as DjangoFilePathField
from django.forms import ImageField as DjangoImageField
Expand Down Expand Up @@ -1516,9 +1516,18 @@ class FileField(Field):
def __init__(self, **kwargs):
self.max_length = kwargs.pop('max_length', None)
self.allow_empty_file = kwargs.pop('allow_empty_file', False)
allowed_extensions = kwargs.pop('allowed_extensions', None)
assert allowed_extensions is None or isinstance(allowed_extensions, (list, tuple)), (
'Invalid value for allowed_extensions. It must be either None or a list/tuple, but %s provided.'
% (allowed_extensions.__name__ if inspect.isclass(
allowed_extensions) else allowed_extensions.__class__.__name__)
)
if 'use_url' in kwargs:
self.use_url = kwargs.pop('use_url')
super().__init__(**kwargs)
if allowed_extensions is not None:
self.validators.append(
FileExtensionValidator(allowed_extensions))

def to_internal_value(self, data):
try:
Expand Down
Loading