Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from django import forms
from django.template.defaultfilters import filesizeformat
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
class RestrictedFileField(forms.FileField):
def __init__(self, *args, **kwargs):
self.content_types = kwargs.pop('content_types', None)
self.max_upload_size = kwargs.pop('max_upload_size', None)
if not self.max_upload_size:
self.max_upload_size = settings.MAX_UPLOAD_SIZE
super(RestrictedFileField, self).__init__(*args, **kwargs)
def clean(self, *args, **kwargs):
data = super(RestrictedFileField, self).clean(*args, **kwargs)
try:
if data.content_type in self.content_types:
if data.size > self.max_upload_size:
raise forms.ValidationError(_('File size must be under %s. Current file size is %s.') % (filesizeformat(self.max_upload_size), filesizeformat(data.size)))
else:
raise forms.ValidationError(_('File type (%s) is not supported.') % data.content_type)
except AttributeError:
pass
return data
class RestrictedImageField(forms.ImageField):
def __init__(self, *args, **kwargs):
self.max_upload_size = kwargs.pop('max_upload_size', None)
if not self.max_upload_size:
self.max_upload_size = settings.MAX_UPLOAD_SIZE
super(RestrictedImageField, self).__init__(*args, **kwargs)
def clean(self, *args, **kwargs):
data = super(RestrictedImageField, self).clean(*args, **kwargs)
try:
if data.size > self.max_upload_size:
raise forms.ValidationError(_('File size must be under %s. Current file size is %s.') % (filesizeformat(self.max_upload_size), filesizeformat(data.size)))
except AttributeError:
pass
return data
@seanmavley

This comment has been minimized.

Copy link

@seanmavley seanmavley commented Apr 21, 2015

Hi. This is for forms, thus doesn't show up in admin when used. You've got link to snippet for Models (to be able to show in admin) instead of Django forms?

Update: Got it working. Instead of subclassing forms.FileField, I subclassed models.FileField
See fork for update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment