<s> from django . db import models <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . contrib . contenttypes . fields import ( <EOL> GenericForeignKey , GenericRelation <EOL> ) <EOL> from django import forms <EOL> from django . conf import settings <EOL> INSTALLED = '<STR_LIT>' in settings . INSTALLED_APPS <EOL> class FileField ( models . ImageField ) : <EOL> def formfield ( self , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = forms . FileField <EOL> return super ( FileField , self ) . formfield ( ** kwargs ) <EOL> def update_dimension_fields ( self , instance , force = False , * args , ** kwargs ) : <EOL> if ( getattr ( instance , '<STR_LIT>' , None ) is not None and <EOL> '<STR_LIT:image>' in instance . mimetype ) : <EOL> super ( FileField , self ) . update_dimension_fields ( instance , force , <EOL> * args , ** kwargs ) <EOL> else : <EOL> pass <EOL> def generate_filename ( self , instance , filename ) : <EOL> if hasattr ( instance , '<STR_LIT>' ) : <EOL> self . upload_to = instance . get_directory ( ) <EOL> return super ( FileField , self ) . generate_filename ( instance , filename ) <EOL> class FileType ( models . Model ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> mimetype = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . name , self . mimetype ) <EOL> class Meta : <EOL> db_table = '<STR_LIT>' <EOL> abstract = not INSTALLED <EOL> class FileManager ( models . Manager ) : <EOL> def get_queryset ( self ) : <EOL> qs = super ( FileManager , self ) . get_query_set ( ) <EOL> if self . model . type_name is not None : <EOL> return qs . filter ( type__name = self . model . type_name ) <EOL> else : <EOL> return qs <EOL> class File ( models . Model ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:255> , null = True , blank = True ) <EOL> type = models . ForeignKey ( FileType , null = True , blank = True ) <EOL> file = FileField ( upload_to = '<STR_LIT:.>' , width_field = '<STR_LIT:width>' , height_field = '<STR_LIT>' ) <EOL> size = models . IntegerField ( null = True , blank = True ) <EOL> width = models . IntegerField ( null = True , blank = True ) <EOL> height = models . IntegerField ( null = True , blank = True ) <EOL> content_type = models . ForeignKey ( ContentType ) <EOL> object_id = models . PositiveIntegerField ( db_index = True ) <EOL> content_object = GenericForeignKey ( ) <EOL> type_name = "<STR_LIT>" <EOL> def get_directory ( self ) : <EOL> if self . is_image : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> @ property <EOL> def mimetype ( self ) : <EOL> if self . type is not None : <EOL> return self . type . mimetype <EOL> if self . file . name not in ( None , "<STR_LIT>" ) : <EOL> from wq . io . util import guess_type <EOL> mimetype = guess_type ( self . file . name , self . file . read ( <NUM_LIT> ) ) <EOL> return mimetype <EOL> else : <EOL> return None <EOL> @ property <EOL> def is_image ( self ) : <EOL> return self . mimetype is not None and self . mimetype . startswith ( '<STR_LIT>' ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> if self . type is None : <EOL> self . type , isnew = FileType . objects . get_or_create ( <EOL> mimetype = self . mimetype , <EOL> name = self . type_name <EOL> ) <EOL> if self . size is None : <EOL> self . size = self . file . size <EOL> if self . name is None or self . name == '<STR_LIT>' : <EOL> self . name = self . file . name <EOL> super ( File , self ) . save ( * args , ** kwargs ) <EOL> def __str__ ( self ) : <EOL> if self . name is not None : <EOL> return self . name <EOL> else : <EOL> return '<STR_LIT>' % self . id <EOL> class Meta : <EOL> db_table = '<STR_LIT>' <EOL> ordering = ( "<STR_LIT:name>" , ) <EOL> abstract = not INSTALLED <EOL> class FiledModel ( models . Model ) : <EOL> files = GenericRelation ( File ) <EOL> class Meta : <EOL> abstract = True </s>
