<s> from __future__ import unicode_literals <EOL> from functools import wraps <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . db import connections , models , transaction <EOL> from django . db . models . fields import FieldDoesNotExist <EOL> from . . compat import get_remote_field <EOL> from . . state import handler as state_handler <EOL> from . . utils import allow_migrate , popattr , remove_from_app_cache <EOL> def perform_ddl ( action , model , * args , ** kwargs ) : <EOL> if model . _meta . managed : <EOL> return <EOL> for alias in allow_migrate ( model ) : <EOL> connection = connections [ alias ] <EOL> with transaction . atomic ( alias ) , connection . schema_editor ( ) as editor : <EOL> getattr ( editor , action ) ( model , * args , ** kwargs ) <EOL> def nonraw_instance ( receiver ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( receiver ) <EOL> def wrapper ( sender , instance , raw , using , ** kwargs ) : <EOL> if raw : <EOL> instance = sender . _default_manager . using ( using ) . get ( pk = instance . pk ) <EOL> return receiver ( sender = sender , raw = raw , instance = instance , using = using , <EOL> ** kwargs ) <EOL> return wrapper <EOL> @ nonraw_instance <EOL> def model_definition_post_save ( sender , instance , created , ** kwargs ) : <EOL> model_class = instance . model_class ( force_create = True ) <EOL> opts = model_class . _meta <EOL> db_table = opts . db_table <EOL> if created : <EOL> primary_key = opts . pk <EOL> fields = [ ( field . get_attname_column ( ) [ <NUM_LIT:1> ] , field ) for field in opts . fields <EOL> if field is not primary_key ] <EOL> try : <EOL> extra_fields = getattr ( instance . _state , '<STR_LIT>' ) <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> for column , field in extra_fields : <EOL> remote_field = get_remote_field ( field ) <EOL> if field . primary_key : <EOL> assert isinstance ( primary_key , models . AutoField ) <EOL> primary_key = field <EOL> elif ( remote_field and remote_field . parent_link and <EOL> isinstance ( primary_key , models . AutoField ) ) : <EOL> field . primary_key = True <EOL> primary_key = field <EOL> else : <EOL> fields . append ( ( column , field ) ) <EOL> delattr ( instance . _state , '<STR_LIT>' ) <EOL> fields . insert ( <NUM_LIT:0> , ( primary_key . get_attname_column ( ) [ <NUM_LIT:1> ] , primary_key ) ) <EOL> try : <EOL> delayed_save = getattr ( instance . _state , '<STR_LIT>' ) <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> for obj in delayed_save : <EOL> obj . model_def = instance <EOL> obj . save ( force_insert = True , force_create_model_class = False ) <EOL> delattr ( instance . _state , '<STR_LIT>' ) <EOL> model_class = instance . model_class ( force_create = True ) <EOL> perform_ddl ( '<STR_LIT>' , model_class ) <EOL> else : <EOL> old_model_class = instance . _model_class <EOL> if old_model_class : <EOL> old_db_table = old_model_class . _meta . db_table <EOL> if db_table != old_db_table : <EOL> perform_ddl ( '<STR_LIT>' , model_class , old_db_table , db_table ) <EOL> ContentType . objects . clear_cache ( ) <EOL> instance . _model_class = model_class . model <EOL> def model_definition_pre_delete ( sender , instance , ** kwargs ) : <EOL> model_class = instance . model_class ( ) <EOL> instance . _state . _deletion = ( <EOL> model_class , <EOL> instance . pk , <EOL> ) <EOL> def model_definition_post_delete ( sender , instance , ** kwargs ) : <EOL> model_class , pk = popattr ( instance . _state , '<STR_LIT>' ) <EOL> perform_ddl ( '<STR_LIT>' , model_class ) <EOL> remove_from_app_cache ( model_class ) <EOL> model_class . mark_as_obsolete ( ) <EOL> state_handler . clear_checksum ( pk ) <EOL> ContentType . objects . clear_cache ( ) <EOL> del instance . _model_class <EOL> def base_definition_post_save ( sender , instance , created , raw , ** kwargs ) : <EOL> declared_fields = instance . get_declared_fields ( ) <EOL> if declared_fields : <EOL> model_class = instance . model_def . model_class ( ) . render_state ( ) <EOL> opts = model_class . _meta <EOL> if created : <EOL> add_columns = popattr ( instance . _state , '<STR_LIT>' , True ) <EOL> if add_columns : <EOL> auto_pk = isinstance ( opts . pk , models . AutoField ) <EOL> for field in declared_fields : <EOL> field . model = model_class <EOL> remote_field = get_remote_field ( field ) <EOL> if auto_pk and remote_field and remote_field . parent_link : <EOL> auto_pk = False <EOL> field . primary_key = True <EOL> perform_ddl ( '<STR_LIT>' , model_class , opts . pk , field , strict = True ) <EOL> else : <EOL> perform_ddl ( '<STR_LIT>' , model_class , field ) <EOL> else : <EOL> for field in declared_fields : <EOL> try : <EOL> old_field = opts . get_field ( field . name ) <EOL> except FieldDoesNotExist : <EOL> perform_ddl ( '<STR_LIT>' , model_class , field ) <EOL> else : <EOL> perform_ddl ( '<STR_LIT>' , model_class , old_field , field , strict = True ) <EOL> def base_definition_pre_delete ( sender , instance , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> cascade_deletion_origin = popattr ( <EOL> instance . _state , '<STR_LIT>' , None <EOL> ) <EOL> if cascade_deletion_origin == '<STR_LIT>' : <EOL> return <EOL> if ( instance . base and issubclass ( instance . base , models . Model ) and <EOL> instance . base . _meta . abstract ) : <EOL> instance . _state . _deletion = instance . model_def . model_class ( ) . render_state ( ) <EOL> def base_definition_post_delete ( sender , instance , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( instance . _state , '<STR_LIT>' ) : <EOL> model = popattr ( instance . _state , '<STR_LIT>' ) <EOL> for field in instance . base . _meta . fields : <EOL> perform_ddl ( '<STR_LIT>' , model , field ) <EOL> def unique_together_field_defs_changed ( instance , action , model , ** kwargs ) : <EOL> model_class = instance . model_def . model_class ( ) <EOL> if action . startswith ( '<STR_LIT>' ) : <EOL> perform_ddl ( <EOL> '<STR_LIT>' , <EOL> model_class , <EOL> model_class . _meta . unique_together , <EOL> instance . model_def . get_state ( ) . options . get ( '<STR_LIT>' , [ ] ) <EOL> ) <EOL> model_class . mark_as_obsolete ( ) <EOL> def raw_field_definition_proxy_post_save ( sender , instance , raw , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if raw : <EOL> model_class = instance . content_type . model_class ( ) <EOL> opts = model_class . _meta <EOL> if opts . proxy and opts . concrete_model is sender : <EOL> field_definition_post_save ( <EOL> sender = model_class , instance = instance . type_cast ( ) , raw = raw , <EOL> ** kwargs <EOL> ) <EOL> @ nonraw_instance <EOL> def field_definition_post_save ( sender , instance , created , raw , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> model_class = instance . model_def . model_class ( ) . render_state ( ) <EOL> field = instance . construct_for_migrate ( ) <EOL> field . model = model_class <EOL> if created : <EOL> if hasattr ( instance . _state , '<STR_LIT>' ) : <EOL> field . default = instance . _state . _creation_default_value <EOL> delattr ( instance . _state , '<STR_LIT>' ) <EOL> add_column = popattr ( instance . _state , '<STR_LIT>' , True ) <EOL> if add_column : <EOL> perform_ddl ( '<STR_LIT>' , model_class , field ) <EOL> if raw : <EOL> instance . model_def . model_class ( ) . mark_as_obsolete ( ) <EOL> else : <EOL> old_field = instance . _state . _pre_save_field <EOL> delattr ( instance . _state , '<STR_LIT>' ) <EOL> perform_ddl ( '<STR_LIT>' , model_class , old_field , field , strict = True ) <EOL> FIELD_DEFINITION_POST_SAVE_UID = "<STR_LIT>" <EOL> def field_definition_pre_delete ( sender , instance , ** kwargs ) : <EOL> cascade_deletion_origin = popattr ( <EOL> instance . _state , '<STR_LIT>' , None <EOL> ) <EOL> if cascade_deletion_origin == '<STR_LIT>' : <EOL> return <EOL> model_class = instance . model_def . model_class ( ) <EOL> opts = model_class . _meta <EOL> field = opts . get_field ( instance . name ) <EOL> instance . _state . _deletion = ( model_class , field ) <EOL> def field_definition_post_delete ( sender , instance , ** kwargs ) : <EOL> if hasattr ( instance . _state , '<STR_LIT>' ) : <EOL> model , field = popattr ( instance . _state , '<STR_LIT>' ) <EOL> if field . primary_key : <EOL> primary_key = models . AutoField ( name = '<STR_LIT:id>' , primary_key = True ) <EOL> perform_ddl ( '<STR_LIT>' , model , field , primary_key , strict = True ) <EOL> else : <EOL> perform_ddl ( '<STR_LIT>' , model , field ) </s>
