<s> from django . db . models import FileField <EOL> from django . forms import forms <EOL> from django . template . defaultfilters import filesizeformat <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> class ContentTypeRestrictedFileField ( FileField ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . content_types = kwargs . pop ( "<STR_LIT>" ) <EOL> self . max_upload_size = kwargs . pop ( "<STR_LIT>" ) <EOL> super ( ContentTypeRestrictedFileField , self ) . __init__ ( * args , ** kwargs ) <EOL> def clean ( self , * args , ** kwargs ) : <EOL> data = super ( ContentTypeRestrictedFileField , self ) . clean ( * args , ** kwargs ) <EOL> file = data . file <EOL> try : <EOL> content_type = file . content_type <EOL> if content_type in self . content_types : <EOL> if file . _size > self . max_upload_size : <EOL> raise forms . ValidationError ( _ ( '<STR_LIT>' ) % ( filesizeformat ( self . max_upload_size ) , filesizeformat ( file . _size ) ) ) <EOL> else : <EOL> raise forms . ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> except AttributeError : <EOL> pass <EOL> return data <EOL> try : <EOL> from south . modelsinspector import add_introspection_rules <EOL> except : <EOL> pass <EOL> else : <EOL> rules = [ <EOL> ( <EOL> ( ContentTypeRestrictedFileField , ) , [ ] , <EOL> { <EOL> "<STR_LIT>" : [ "<STR_LIT>" , { "<STR_LIT:default>" : None } ] , <EOL> "<STR_LIT>" : [ "<STR_LIT>" , { "<STR_LIT:default>" : None } ] , <EOL> } <EOL> ) , <EOL> ] <EOL> add_introspection_rules ( rules , [ "<STR_LIT>" ] ) </s>
