You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
969 B
26 lines
969 B
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
|