from django.db.models import FileField from django.forms import forms from django.template.defaultfilters import filesizeformat from django.utils.translation import ugettext_lazy as _ class RestrictedFileField(FileField): def __init__(self, *args, **kwargs): self.content_types = kwargs.pop('content_types', []) self.max_upload_size = kwargs.pop('max_upload_size', []) super(RestrictedFileField, self).__init__(*args, **kwargs) def clean(self, *args, **kwargs): data = super(RestrictedFileField, self).clean(*args, **kwargs) file = data.file try: content_type = file.content_type if content_type in self.content_types: if file._size > self.max_upload_size: raise forms.ValidationError("File too big") else: raise forms.ValidationError("Wrong file type") except AttributeError: pass return data