@@ -0,0 +1,1599 @@
+from django.db.models import FileField%0Afrom django.forms import forms%0Afrom django.template.defaultfilters import filesizeformat%0Afrom django.utils.translation import ugettext_lazy as _%0A%0Aclass ContentTypeRestrictedFileField(FileField):%0A    %22%22%22%0A    Same as FileField, but you can specify:%0A        * content_types - list containing allowed content_types. Example: %5B'application/pdf', 'image/jpeg'%5D%0A        * max_upload_size - a number indicating the maximum file size allowed for upload.%0A            2.5MB - 2621440%0A            5MB - 5242880%0A            10MB - 10485760%0A            20MB - 20971520%0A            50MB - 5242880%0A            100MB 104857600%0A            250MB - 214958080%0A            500MB - 429916160%0A    %22%22%22%0A    def __init__(self, *args, **kwargs):%0A        self.content_types = kwargs.pop(%22content_types%22)%0A        self.max_upload_size = kwargs.pop(%22max_upload_size%22)%0A%0A        super(ContentTypeRestrictedFileField, self).__init__(*args, **kwargs)%0A%0A    def clean(self, *args, **kwargs):%0A        data = super(ContentTypeRestrictedFileField, self).clean(*args, **kwargs)%0A%0A        file = data.file%0A        try:%0A            content_type = file.content_type%0A            if content_type in self.content_types:%0A                if file._size %3E self.max_upload_size:%0A                    raise forms.ValidationError(_('Please keep filesize under %25s. Current filesize %25s') %25 (filesizeformat(self.max_upload_size), filesizeformat(file._size)))%0A            else:%0A                raise forms.ValidationError(_('Filetype not supported.'))%0A        except AttributeError:%0A            pass%0A%0A        return data

add ContentTypeRestrictedFileField field