<s> """<STR_LIT>""" <EOL> import re <EOL> import six <EOL> import warnings <EOL> try : <EOL> import uuid <EOL> HAS_UUID = True <EOL> except ImportError : <EOL> HAS_UUID = False <EOL> try : <EOL> import shortuuid <EOL> HAS_SHORT_UUID = True <EOL> except ImportError : <EOL> HAS_SHORT_UUID = False <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . template . defaultfilters import slugify <EOL> from django . db . models import DateTimeField , CharField , SlugField <EOL> try : <EOL> from django . utils . timezone import now as datetime_now <EOL> assert datetime_now <EOL> except ImportError : <EOL> import datetime <EOL> datetime_now = datetime . datetime . now <EOL> try : <EOL> from django . utils . encoding import force_unicode <EOL> except ImportError : <EOL> from django . utils . encoding import force_text as force_unicode <EOL> class AutoSlugField ( SlugField ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> kwargs . setdefault ( '<STR_LIT:blank>' , True ) <EOL> kwargs . setdefault ( '<STR_LIT>' , False ) <EOL> populate_from = kwargs . pop ( '<STR_LIT>' , None ) <EOL> if populate_from is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> self . _populate_from = populate_from <EOL> self . slugify_function = kwargs . pop ( '<STR_LIT>' , slugify ) <EOL> self . separator = kwargs . pop ( '<STR_LIT>' , six . u ( '<STR_LIT:->' ) ) <EOL> self . overwrite = kwargs . pop ( '<STR_LIT>' , False ) <EOL> if not isinstance ( self . overwrite , bool ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . allow_duplicates = kwargs . pop ( '<STR_LIT>' , False ) <EOL> if not isinstance ( self . allow_duplicates , bool ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> super ( AutoSlugField , self ) . __init__ ( * args , ** kwargs ) <EOL> def _slug_strip ( self , value ) : <EOL> """<STR_LIT>""" <EOL> re_sep = '<STR_LIT>' % re . escape ( self . separator ) <EOL> value = re . sub ( '<STR_LIT>' % re_sep , self . separator , value ) <EOL> return re . sub ( r'<STR_LIT>' % ( re_sep , re_sep ) , '<STR_LIT>' , value ) <EOL> def get_queryset ( self , model_cls , slug_field ) : <EOL> for field , model in model_cls . _meta . get_fields_with_model ( ) : <EOL> if model and field == slug_field : <EOL> return model . _default_manager . all ( ) <EOL> return model_cls . _default_manager . all ( ) <EOL> def slugify_func ( self , content ) : <EOL> if content : <EOL> return self . slugify_function ( content ) <EOL> return '<STR_LIT>' <EOL> def create_slug ( self , model_instance , add ) : <EOL> if not isinstance ( self . _populate_from , ( list , tuple ) ) : <EOL> self . _populate_from = ( self . _populate_from , ) <EOL> slug_field = model_instance . _meta . get_field ( self . attname ) <EOL> if add or self . overwrite : <EOL> slug_for_field = lambda field : self . slugify_func ( getattr ( model_instance , field ) ) <EOL> slug = self . separator . join ( map ( slug_for_field , self . _populate_from ) ) <EOL> next = <NUM_LIT:2> <EOL> else : <EOL> slug = getattr ( model_instance , self . attname ) <EOL> return slug <EOL> slug_len = slug_field . max_length <EOL> if slug_len : <EOL> slug = slug [ : slug_len ] <EOL> slug = self . _slug_strip ( slug ) <EOL> original_slug = slug <EOL> if self . allow_duplicates : <EOL> return slug <EOL> queryset = self . get_queryset ( model_instance . __class__ , slug_field ) <EOL> if model_instance . pk : <EOL> queryset = queryset . exclude ( pk = model_instance . pk ) <EOL> kwargs = { } <EOL> for params in model_instance . _meta . unique_together : <EOL> if self . attname in params : <EOL> for param in params : <EOL> kwargs [ param ] = getattr ( model_instance , param , None ) <EOL> kwargs [ self . attname ] = slug <EOL> while not slug or queryset . filter ( ** kwargs ) : <EOL> slug = original_slug <EOL> end = '<STR_LIT>' % ( self . separator , next ) <EOL> end_len = len ( end ) <EOL> if slug_len and len ( slug ) + end_len > slug_len : <EOL> slug = slug [ : slug_len - end_len ] <EOL> slug = self . _slug_strip ( slug ) <EOL> slug = '<STR_LIT>' % ( slug , end ) <EOL> kwargs [ self . attname ] = slug <EOL> next += <NUM_LIT:1> <EOL> return slug <EOL> def pre_save ( self , model_instance , add ) : <EOL> value = force_unicode ( self . create_slug ( model_instance , add ) ) <EOL> setattr ( model_instance , self . attname , value ) <EOL> return value <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def south_field_triple ( self ) : <EOL> "<STR_LIT>" <EOL> from south . modelsinspector import introspector <EOL> field_class = '<STR_LIT>' % self . __module__ <EOL> args , kwargs = introspector ( self ) <EOL> kwargs . update ( { <EOL> '<STR_LIT>' : repr ( self . _populate_from ) , <EOL> '<STR_LIT>' : repr ( self . separator ) , <EOL> '<STR_LIT>' : repr ( self . overwrite ) , <EOL> '<STR_LIT>' : repr ( self . allow_duplicates ) , <EOL> } ) <EOL> return ( field_class , args , kwargs ) <EOL> def deconstruct ( self ) : <EOL> name , path , args , kwargs = super ( AutoSlugField , self ) . deconstruct ( ) <EOL> kwargs [ '<STR_LIT>' ] = self . _populate_from <EOL> if not self . separator == six . u ( '<STR_LIT:->' ) : <EOL> kwargs [ '<STR_LIT>' ] = self . separator <EOL> if self . overwrite is not False : <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> if self . allow_duplicates is not False : <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> return name , path , args , kwargs <EOL> class CreationDateTimeField ( DateTimeField ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> kwargs . setdefault ( '<STR_LIT>' , False ) <EOL> kwargs . setdefault ( '<STR_LIT:blank>' , True ) <EOL> kwargs . setdefault ( '<STR_LIT:default>' , datetime_now ) <EOL> DateTimeField . __init__ ( self , * args , ** kwargs ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def south_field_triple ( self ) : <EOL> "<STR_LIT>" <EOL> from south . modelsinspector import introspector <EOL> field_class = "<STR_LIT>" <EOL> args , kwargs = introspector ( self ) <EOL> return ( field_class , args , kwargs ) <EOL> def deconstruct ( self ) : <EOL> name , path , args , kwargs = super ( CreationDateTimeField , self ) . deconstruct ( ) <EOL> if self . editable is not False : <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> if self . blank is not True : <EOL> kwargs [ '<STR_LIT:blank>' ] = False <EOL> if self . default is not datetime_now : <EOL> kwargs [ '<STR_LIT:default>' ] = self . default <EOL> return name , path , args , kwargs <EOL> class ModificationDateTimeField ( CreationDateTimeField ) : <EOL> """<STR_LIT>""" <EOL> def pre_save ( self , model , add ) : <EOL> value = datetime_now ( ) <EOL> setattr ( model , self . attname , value ) <EOL> return value <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def south_field_triple ( self ) : <EOL> "<STR_LIT>" <EOL> from south . modelsinspector import introspector <EOL> field_class = "<STR_LIT>" <EOL> args , kwargs = introspector ( self ) <EOL> return ( field_class , args , kwargs ) <EOL> class UUIDVersionError ( Exception ) : <EOL> pass <EOL> class UUIDField ( CharField ) : <EOL> """<STR_LIT>""" <EOL> DEFAULT_MAX_LENGTH = <NUM_LIT> <EOL> def __init__ ( self , verbose_name = None , name = None , auto = True , version = <NUM_LIT:4> , node = None , clock_seq = None , namespace = None , uuid_name = None , * args , ** kwargs ) : <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning ) <EOL> if not HAS_UUID : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" ) <EOL> kwargs . setdefault ( '<STR_LIT:max_length>' , self . DEFAULT_MAX_LENGTH ) <EOL> if auto : <EOL> self . empty_strings_allowed = False <EOL> kwargs [ '<STR_LIT:blank>' ] = True <EOL> kwargs . setdefault ( '<STR_LIT>' , False ) <EOL> self . auto = auto <EOL> self . version = version <EOL> self . node = node <EOL> self . clock_seq = clock_seq <EOL> self . namespace = namespace <EOL> self . uuid_name = uuid_name or name <EOL> super ( UUIDField , self ) . __init__ ( verbose_name = verbose_name , * args , ** kwargs ) <EOL> def create_uuid ( self ) : <EOL> if not self . version or self . version == <NUM_LIT:4> : <EOL> return uuid . uuid4 ( ) <EOL> elif self . version == <NUM_LIT:1> : <EOL> return uuid . uuid1 ( self . node , self . clock_seq ) <EOL> elif self . version == <NUM_LIT:2> : <EOL> raise UUIDVersionError ( "<STR_LIT>" ) <EOL> elif self . version == <NUM_LIT:3> : <EOL> return uuid . uuid3 ( self . namespace , self . uuid_name ) <EOL> elif self . version == <NUM_LIT:5> : <EOL> return uuid . uuid5 ( self . namespace , self . uuid_name ) <EOL> else : <EOL> raise UUIDVersionError ( "<STR_LIT>" % self . version ) <EOL> def pre_save ( self , model_instance , add ) : <EOL> value = super ( UUIDField , self ) . pre_save ( model_instance , add ) <EOL> if self . auto and add and value is None : <EOL> value = force_unicode ( self . create_uuid ( ) ) <EOL> setattr ( model_instance , self . attname , value ) <EOL> return value <EOL> else : <EOL> if self . auto and not value : <EOL> value = force_unicode ( self . create_uuid ( ) ) <EOL> setattr ( model_instance , self . attname , value ) <EOL> return value <EOL> def formfield ( self , ** kwargs ) : <EOL> if self . auto : <EOL> return None <EOL> return super ( UUIDField , self ) . formfield ( ** kwargs ) <EOL> def south_field_triple ( self ) : <EOL> "<STR_LIT>" <EOL> from south . modelsinspector import introspector <EOL> field_class = "<STR_LIT>" <EOL> args , kwargs = introspector ( self ) <EOL> return ( field_class , args , kwargs ) <EOL> def deconstruct ( self ) : <EOL> name , path , args , kwargs = super ( UUIDField , self ) . deconstruct ( ) <EOL> if kwargs . get ( '<STR_LIT:max_length>' , None ) == self . DEFAULT_MAX_LENGTH : <EOL> del kwargs [ '<STR_LIT:max_length>' ] <EOL> if self . auto is not True : <EOL> kwargs [ '<STR_LIT>' ] = self . auto <EOL> if self . version != <NUM_LIT:4> : <EOL> kwargs [ '<STR_LIT:version>' ] = self . version <EOL> if self . node is not None : <EOL> kwargs [ '<STR_LIT>' ] = self . node <EOL> if self . clock_seq is not None : <EOL> kwargs [ '<STR_LIT>' ] = self . clock_seq <EOL> if self . namespace is not None : <EOL> kwargs [ '<STR_LIT>' ] = self . namespace <EOL> if self . uuid_name is not None : <EOL> kwargs [ '<STR_LIT>' ] = self . name <EOL> return name , path , args , kwargs <EOL> class PostgreSQLUUIDField ( UUIDField ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning ) <EOL> super ( PostgreSQLUUIDField , self ) . __init__ ( * args , ** kwargs ) <EOL> def db_type ( self , connection = None ) : <EOL> return "<STR_LIT>" <EOL> def get_db_prep_value ( self , value , connection , prepared = False ) : <EOL> if isinstance ( value , six . integer_types ) : <EOL> value = uuid . UUID ( int = value ) <EOL> elif isinstance ( value , ( six . string_types , six . binary_type ) ) : <EOL> if len ( value ) == <NUM_LIT:16> : <EOL> value = uuid . UUID ( bytes = value ) <EOL> else : <EOL> value = uuid . UUID ( value ) <EOL> return super ( PostgreSQLUUIDField , self ) . get_db_prep_value ( <EOL> value , connection , prepared = False ) <EOL> class ShortUUIDField ( UUIDField ) : <EOL> """<STR_LIT>""" <EOL> DEFAULT_MAX_LENGTH = <NUM_LIT> <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( ShortUUIDField , self ) . __init__ ( * args , ** kwargs ) <EOL> if not HAS_SHORT_UUID : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" ) <EOL> kwargs . setdefault ( '<STR_LIT:max_length>' , self . DEFAULT_MAX_LENGTH ) <EOL> def create_uuid ( self ) : <EOL> if not self . version or self . version == <NUM_LIT:4> : <EOL> return shortuuid . uuid ( ) <EOL> elif self . version == <NUM_LIT:1> : <EOL> return shortuuid . uuid ( ) <EOL> elif self . version == <NUM_LIT:2> : <EOL> raise UUIDVersionError ( "<STR_LIT>" ) <EOL> elif self . version == <NUM_LIT:3> : <EOL> raise UUIDVersionError ( "<STR_LIT>" ) <EOL> elif self . version == <NUM_LIT:5> : <EOL> return shortuuid . uuid ( name = self . namespace ) <EOL> else : <EOL> raise UUIDVersionError ( "<STR_LIT>" % self . version ) </s>
