<s> from django . db import models , connections , connection <EOL> from . . helpers import remote_field_model <EOL> class RandomBigInt ( object ) : <EOL> def sql ( self ) : <EOL> raise NotImplementedError <EOL> class TriggerNestedSelect : <EOL> def __init__ ( self , table , columns , ** kwargs ) : <EOL> self . table = table <EOL> self . columns = "<STR_LIT:U+002CU+0020>" . join ( columns ) <EOL> self . kwargs = kwargs <EOL> def sql ( self ) : <EOL> raise NotImplementedError <EOL> class TriggerAction ( object ) : <EOL> def __init__ ( self ) : <EOL> pass <EOL> def sql ( self ) : <EOL> pass <EOL> class TriggerActionInsert ( TriggerAction ) : <EOL> def __init__ ( self , model , columns , values ) : <EOL> self . model = model <EOL> self . columns = columns <EOL> self . values = values <EOL> def sql ( self ) : <EOL> raise NotImplementedError <EOL> class TriggerActionUpdate ( TriggerAction ) : <EOL> def __init__ ( self , model , columns , values , where ) : <EOL> self . model = model <EOL> self . columns = columns <EOL> self . where = where <EOL> self . values = [ ] <EOL> for value in values : <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> self . values . append ( value . sql ( ) ) <EOL> else : <EOL> self . values . append ( value ) <EOL> def sql ( self ) : <EOL> raise NotImplementedError <EOL> def get_fields_with_model ( model , meta ) : <EOL> try : <EOL> return [ <EOL> ( f , f . model if f . model != model else None ) <EOL> for f in meta . get_fields ( ) <EOL> if not f . is_relation <EOL> or f . one_to_one <EOL> or ( f . many_to_one and f . related_model ) <EOL> ] <EOL> except AttributeError : <EOL> return meta . get_fields_with_model ( ) <EOL> class Trigger ( object ) : <EOL> def __init__ ( self , subject , time , event , actions , content_type , using = None , skip = None ) : <EOL> self . subject = subject <EOL> self . time = time <EOL> self . event = event <EOL> self . content_type = content_type <EOL> self . content_type_field = None <EOL> self . actions = [ ] <EOL> self . append ( actions ) <EOL> self . using = using <EOL> if self . using : <EOL> self . connection = connections [ self . using ] <EOL> else : <EOL> self . connection = connection <EOL> try : <EOL> from django . contrib . contenttypes . fields import GenericRelation <EOL> except ImportError : <EOL> from django . contrib . contenttypes . generic import GenericRelation <EOL> if isinstance ( subject , models . ManyToManyField ) : <EOL> self . model = None <EOL> self . db_table = subject . m2m_db_table ( ) <EOL> self . fields = [ ( subject . m2m_column_name ( ) , '<STR_LIT>' ) , ( subject . m2m_reverse_name ( ) , '<STR_LIT>' ) ] <EOL> elif isinstance ( subject , GenericRelation ) : <EOL> self . model = None <EOL> self . db_table = remote_field_model ( subject ) . _meta . db_table <EOL> self . fields = [ ( k . attname , k . db_type ( connection = self . connection ) ) for k , v in get_fields_with_model ( remote_field_model ( subject ) , remote_field_model ( subject ) . _meta ) if not v ] <EOL> self . content_type_field = subject . content_type_field_name + '<STR_LIT>' <EOL> elif isinstance ( subject , models . ForeignKey ) : <EOL> self . model = subject . model <EOL> self . db_table = self . model . _meta . db_table <EOL> skip = skip or ( ) + getattr ( self . model , '<STR_LIT>' , ( ) ) <EOL> self . fields = [ ( k . attname , k . db_type ( connection = self . connection ) ) for k , v in get_fields_with_model ( subject . model , self . model . _meta ) if not v and k . attname not in skip ] <EOL> elif hasattr ( subject , "<STR_LIT>" ) : <EOL> self . model = subject <EOL> self . db_table = self . model . _meta . db_table <EOL> skip = skip or ( ) + getattr ( self . model , '<STR_LIT>' , ( ) ) <EOL> self . fields = [ ] <EOL> try : <EOL> from django . db . models . fields . related import ForeignObjectRel <EOL> except : <EOL> from django . db . models . related import RelatedObject as ForeignObjectRel <EOL> for k , v in get_fields_with_model ( subject , self . model . _meta ) : <EOL> if isinstance ( k , ForeignObjectRel ) : <EOL> pass <EOL> else : <EOL> if not v and k . attname not in skip : <EOL> self . fields . append ( ( k . attname , k . db_type ( connection = self . connection ) ) ) <EOL> else : <EOL> raise NotImplementedError <EOL> def append ( self , actions ) : <EOL> if not isinstance ( actions , list ) : <EOL> actions = [ actions ] <EOL> for action in actions : <EOL> self . actions . append ( action ) <EOL> def name ( self ) : <EOL> return "<STR_LIT:_>" . join ( [ <EOL> "<STR_LIT>" , <EOL> self . time , <EOL> "<STR_LIT>" , <EOL> self . event , <EOL> "<STR_LIT>" , <EOL> self . db_table <EOL> ] ) <EOL> def sql ( self ) : <EOL> raise NotImplementedError <EOL> class TriggerSet ( object ) : <EOL> def __init__ ( self , using = None ) : <EOL> self . using = using <EOL> self . triggers = { } <EOL> if self . using : <EOL> self . connection = connections [ self . using ] <EOL> else : <EOL> self . connection = connection <EOL> def cursor ( self ) : <EOL> return self . connection . cursor ( ) <EOL> def append ( self , triggers ) : <EOL> if not isinstance ( triggers , list ) : <EOL> triggers = [ triggers ] <EOL> for trigger in triggers : <EOL> name = trigger . name ( ) <EOL> if name in self . triggers : <EOL> self . triggers [ name ] . append ( trigger . actions ) <EOL> else : <EOL> self . triggers [ name ] = trigger <EOL> def install ( self ) : <EOL> raise NotImplementedError <EOL> def drop ( self ) : <EOL> raise NotImplementedError </s>
