<s> from __future__ import unicode_literals <EOL> import django <EOL> from django . db import models <EOL> from django . conf import settings <EOL> from django . utils . encoding import python_2_unicode_compatible <EOL> from django . utils . timezone import now <EOL> DEFAULT_CHOICES_NAME = '<STR_LIT>' <EOL> class AutoCreatedField ( models . DateTimeField ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> kwargs . setdefault ( '<STR_LIT>' , False ) <EOL> kwargs . setdefault ( '<STR_LIT:default>' , now ) <EOL> super ( AutoCreatedField , self ) . __init__ ( * args , ** kwargs ) <EOL> class AutoLastModifiedField ( AutoCreatedField ) : <EOL> """<STR_LIT>""" <EOL> def pre_save ( self , model_instance , add ) : <EOL> value = now ( ) <EOL> setattr ( model_instance , self . attname , value ) <EOL> return value <EOL> class StatusField ( models . CharField ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> kwargs . setdefault ( '<STR_LIT:max_length>' , <NUM_LIT:100> ) <EOL> self . check_for_status = not kwargs . pop ( '<STR_LIT>' , False ) <EOL> self . choices_name = kwargs . pop ( '<STR_LIT>' , DEFAULT_CHOICES_NAME ) <EOL> super ( StatusField , self ) . __init__ ( * args , ** kwargs ) <EOL> def prepare_class ( self , sender , ** kwargs ) : <EOL> if not sender . _meta . abstract and self . check_for_status : <EOL> assert hasattr ( sender , self . choices_name ) , "<STR_LIT>" % ( sender . __name__ , self . choices_name ) <EOL> self . _choices = getattr ( sender , self . choices_name ) <EOL> if django . VERSION >= ( <NUM_LIT:1> , <NUM_LIT:9> , <NUM_LIT:0> ) : <EOL> self . choices = self . _choices <EOL> if not self . has_default ( ) : <EOL> self . default = tuple ( getattr ( sender , self . choices_name ) ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> def contribute_to_class ( self , cls , name ) : <EOL> models . signals . class_prepared . connect ( self . prepare_class , sender = cls ) <EOL> self . _choices = [ ( <NUM_LIT:0> , '<STR_LIT>' ) ] <EOL> if django . VERSION >= ( <NUM_LIT:1> , <NUM_LIT:9> , <NUM_LIT:0> ) : <EOL> self . choices = self . _choices <EOL> super ( StatusField , self ) . contribute_to_class ( cls , name ) <EOL> def deconstruct ( self ) : <EOL> name , path , args , kwargs = super ( StatusField , self ) . deconstruct ( ) <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> return name , path , args , kwargs <EOL> class MonitorField ( models . DateTimeField ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> kwargs . setdefault ( '<STR_LIT:default>' , now ) <EOL> monitor = kwargs . pop ( '<STR_LIT>' , None ) <EOL> if not monitor : <EOL> raise TypeError ( <EOL> '<STR_LIT>' % self . __class__ . __name__ ) <EOL> self . monitor = monitor <EOL> when = kwargs . pop ( '<STR_LIT>' , None ) <EOL> if when is not None : <EOL> when = set ( when ) <EOL> self . when = when <EOL> super ( MonitorField , self ) . __init__ ( * args , ** kwargs ) <EOL> def contribute_to_class ( self , cls , name ) : <EOL> self . monitor_attname = '<STR_LIT>' % name <EOL> models . signals . post_init . connect ( self . _save_initial , sender = cls ) <EOL> super ( MonitorField , self ) . contribute_to_class ( cls , name ) <EOL> def get_monitored_value ( self , instance ) : <EOL> return getattr ( instance , self . monitor ) <EOL> def _save_initial ( self , sender , instance , ** kwargs ) : <EOL> setattr ( instance , self . monitor_attname , <EOL> self . get_monitored_value ( instance ) ) <EOL> def pre_save ( self , model_instance , add ) : <EOL> value = now ( ) <EOL> previous = getattr ( model_instance , self . monitor_attname , None ) <EOL> current = self . get_monitored_value ( model_instance ) <EOL> if previous != current : <EOL> if self . when is None or current in self . when : <EOL> setattr ( model_instance , self . attname , value ) <EOL> self . _save_initial ( model_instance . __class__ , model_instance ) <EOL> return super ( MonitorField , self ) . pre_save ( model_instance , add ) <EOL> def deconstruct ( self ) : <EOL> name , path , args , kwargs = super ( MonitorField , self ) . deconstruct ( ) <EOL> kwargs [ '<STR_LIT>' ] = self . monitor <EOL> if self . when is not None : <EOL> kwargs [ '<STR_LIT>' ] = self . when <EOL> return name , path , args , kwargs <EOL> SPLIT_MARKER = getattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) <EOL> SPLIT_DEFAULT_PARAGRAPHS = getattr ( settings , '<STR_LIT>' , <NUM_LIT:2> ) <EOL> _excerpt_field_name = lambda name : '<STR_LIT>' % name <EOL> def get_excerpt ( content ) : <EOL> excerpt = [ ] <EOL> default_excerpt = [ ] <EOL> paras_seen = <NUM_LIT:0> <EOL> for line in content . splitlines ( ) : <EOL> if not line . strip ( ) : <EOL> paras_seen += <NUM_LIT:1> <EOL> if paras_seen < SPLIT_DEFAULT_PARAGRAPHS : <EOL> default_excerpt . append ( line ) <EOL> if line . strip ( ) == SPLIT_MARKER : <EOL> return '<STR_LIT:\n>' . join ( excerpt ) <EOL> excerpt . append ( line ) <EOL> return '<STR_LIT:\n>' . join ( default_excerpt ) <EOL> @ python_2_unicode_compatible <EOL> class SplitText ( object ) : <EOL> def __init__ ( self , instance , field_name , excerpt_field_name ) : <EOL> self . instance = instance <EOL> self . field_name = field_name <EOL> self . excerpt_field_name = excerpt_field_name <EOL> def _get_content ( self ) : <EOL> return self . instance . __dict__ [ self . field_name ] <EOL> def _set_content ( self , val ) : <EOL> setattr ( self . instance , self . field_name , val ) <EOL> content = property ( _get_content , _set_content ) <EOL> def _get_excerpt ( self ) : <EOL> return getattr ( self . instance , self . excerpt_field_name ) <EOL> excerpt = property ( _get_excerpt ) <EOL> def _get_has_more ( self ) : <EOL> return self . excerpt . strip ( ) != self . content . strip ( ) <EOL> has_more = property ( _get_has_more ) <EOL> def __str__ ( self ) : <EOL> return self . content <EOL> class SplitDescriptor ( object ) : <EOL> def __init__ ( self , field ) : <EOL> self . field = field <EOL> self . excerpt_field_name = _excerpt_field_name ( self . field . name ) <EOL> def __get__ ( self , instance , owner ) : <EOL> if instance is None : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> content = instance . __dict__ [ self . field . name ] <EOL> if content is None : <EOL> return None <EOL> return SplitText ( instance , self . field . name , self . excerpt_field_name ) <EOL> def __set__ ( self , obj , value ) : <EOL> if isinstance ( value , SplitText ) : <EOL> obj . __dict__ [ self . field . name ] = value . content <EOL> setattr ( obj , self . excerpt_field_name , value . excerpt ) <EOL> else : <EOL> obj . __dict__ [ self . field . name ] = value <EOL> class SplitField ( models . TextField ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . add_excerpt_field = not kwargs . pop ( '<STR_LIT>' , False ) <EOL> super ( SplitField , self ) . __init__ ( * args , ** kwargs ) <EOL> def contribute_to_class ( self , cls , name ) : <EOL> if self . add_excerpt_field and not cls . _meta . abstract : <EOL> excerpt_field = models . TextField ( editable = False ) <EOL> cls . add_to_class ( _excerpt_field_name ( name ) , excerpt_field ) <EOL> super ( SplitField , self ) . contribute_to_class ( cls , name ) <EOL> setattr ( cls , self . name , SplitDescriptor ( self ) ) <EOL> def pre_save ( self , model_instance , add ) : <EOL> value = super ( SplitField , self ) . pre_save ( model_instance , add ) <EOL> excerpt = get_excerpt ( value . content ) <EOL> setattr ( model_instance , _excerpt_field_name ( self . attname ) , excerpt ) <EOL> return value . content <EOL> def value_to_string ( self , obj ) : <EOL> value = self . _get_val_from_obj ( obj ) <EOL> return value . content <EOL> def get_prep_value ( self , value ) : <EOL> try : <EOL> return value . content <EOL> except AttributeError : <EOL> return value <EOL> def deconstruct ( self ) : <EOL> name , path , args , kwargs = super ( SplitField , self ) . deconstruct ( ) <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> return name , path , args , kwargs <EOL> try : <EOL> from south . modelsinspector import add_introspection_rules <EOL> add_introspection_rules ( rules = [ <EOL> ( <EOL> ( SplitField , ) , <EOL> [ ] , <EOL> { '<STR_LIT>' : ( '<STR_LIT>' , { } ) } <EOL> ) , <EOL> ( <EOL> ( MonitorField , ) , <EOL> [ ] , <EOL> { '<STR_LIT>' : ( '<STR_LIT>' , { } ) } <EOL> ) , <EOL> ( <EOL> ( StatusField , ) , <EOL> [ ] , <EOL> { '<STR_LIT>' : ( '<STR_LIT>' , { } ) } <EOL> ) , <EOL> ] , patterns = [ '<STR_LIT>' ] ) <EOL> except ImportError : <EOL> pass </s>
