-
-
Save mobula/da99e4db843b9ceb3a3f to your computer and use it in GitHub Desktop.
Performs file upload and image upload validation for django.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from os.path import splitext | |
from django.core.exceptions import ValidationError | |
from django.utils.translation import ugettext_lazy as _ | |
from django.forms.fields import FileField | |
from django.template.defaultfilters import filesizeformat | |
class ValidatingFileField(FileField): | |
""" | |
Subclass of the FileField form field which optionally checks the | |
extension, mimetype and size of the uploaded files. | |
Initialization parameters: | |
allowed_extensions: iterable with allowed file extensions | |
ie. ('txt', 'doc') | |
allowd_mimetypes: iterable with allowed mimetypes | |
ie. ('image/png', ) | |
min_size: minimum number of bytes allowed | |
ie. 100 | |
max_size: maximum number of bytes allowed | |
ie. 24*1024*1024 for 24 MB | |
""" | |
extension_message = _("Extension '%(extension)s' not allowed. Allowed extensions: '%(allowed_extensions)s'") | |
mime_message = _("MIME type '%(mimetype)s' is not valid. Allowed types: %(allowed_mimetypes)s") | |
min_size_message = _('The current file %(size)s, which is too small. The minumum file size is %(allowed_size)s') | |
max_size_message = _('The current file %(size)s, which is too large. The maximum file size is %(allowed_size)s') | |
def __init__(self, *args, **kwargs): | |
self.allowed_extensions = kwargs.pop('allowed_extensions', None) | |
self.allowed_mimetypes = kwargs.pop('allowed_mimetypes', None) | |
self.min_size = kwargs.pop('min_size', 0) | |
self.max_size = kwargs.pop('max_size', None) | |
super(ValidatingFileField, self).__init__(*args, **kwargs) | |
def clean(self, value): | |
""" | |
Check the extension, content type and file size. | |
""" | |
value = super(ValidatingFileField, self).clean(value) | |
# Check the extension | |
ext = splitext(value)[1][1:].lower() | |
if self.allowed_extensions and not ext in self.allowed_extensions: | |
message = self.extension_message % { | |
'extension' : ext, | |
'allowed_extensions': ', '.join(self.allowed_extensions) | |
} | |
raise ValidationError(message) | |
# Check the content type | |
mimetype = value.file.content_type | |
if self.allowed_mimetypes and not mimetype in self.allowed_mimetypes: | |
message = self.mime_message % { | |
'mimetype': mimetype, | |
'allowed_mimetypes': ', '.join(self.allowed_mimetypes) | |
} | |
raise ValidationError(message) | |
# Check the file size | |
filesize = value.file._size | |
if filesize > self.max_size: | |
message = self.max_size_message % { | |
'size': filesizeformat(filesize), | |
'allowed_size': filesizeformat(self.max_size) | |
} | |
raise ValidationError(message) | |
elif filesize < self.min_size: | |
message = self.min_size_message % { | |
'size': filesizeformat(filesize), | |
'allowed_size': filesizeformat(self.min_size) | |
} | |
raise ValidationError(message) | |
return value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment