Source

django-model-validators / model_validators / validators.py

from django.core.exceptions import ValidationError
from os.path import splitext


class FileTypeValidator(object):
    def __init__(self, file_extensions):
        if isinstance(file_extensions, basestring):
            file_extensions = [file_extensions]
        self.file_extensions = [file_extension.lower() for file_extension in file_extensions]

    def __call__(self, value):
        file_extension = splitext(value.name)[-1][1:].lower()
        if file_extension not in self.file_extensions:
            raise ValidationError(u'File type %s not allowed.' % file_extension, code='invalid')