<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from django . db import models <EOL> from django . contrib . contenttypes . generic import GenericRelation <EOL> from django . utils . datastructures import SortedDict <EOL> from south . creator . freezer import remove_useless_attributes , freeze_apps , model_key <EOL> from south . utils import auto_through <EOL> from south . utils . py3 import string_types <EOL> class BaseChanges ( object ) : <EOL> """<STR_LIT>""" <EOL> def suggest_name ( self ) : <EOL> return '<STR_LIT>' <EOL> def split_model_def ( self , model , model_def ) : <EOL> """<STR_LIT>""" <EOL> real_fields = SortedDict ( ) <EOL> meta = SortedDict ( ) <EOL> m2m_fields = SortedDict ( ) <EOL> for name , triple in model_def . items ( ) : <EOL> if name == "<STR_LIT:Meta>" : <EOL> meta = triple <EOL> elif isinstance ( model . _meta . get_field_by_name ( name ) [ <NUM_LIT:0> ] , models . ManyToManyField ) : <EOL> m2m_fields [ name ] = triple <EOL> else : <EOL> real_fields [ name ] = triple <EOL> return real_fields , meta , m2m_fields <EOL> def current_model_from_key ( self , key ) : <EOL> app_label , model_name = key . split ( "<STR_LIT:.>" ) <EOL> return models . get_model ( app_label , model_name ) <EOL> def current_field_from_key ( self , key , fieldname ) : <EOL> app_label , model_name = key . split ( "<STR_LIT:.>" ) <EOL> if fieldname == "<STR_LIT>" : <EOL> field = models . IntegerField ( ) <EOL> field . name = "<STR_LIT>" <EOL> field . attname = "<STR_LIT>" <EOL> field . column = "<STR_LIT>" <EOL> field . default = <NUM_LIT:0> <EOL> return field <EOL> return models . get_model ( app_label , model_name ) . _meta . get_field_by_name ( fieldname ) [ <NUM_LIT:0> ] <EOL> class AutoChanges ( BaseChanges ) : <EOL> """<STR_LIT>""" <EOL> IGNORED_FIELD_TYPES = [ <EOL> GenericRelation , <EOL> ] <EOL> def __init__ ( self , migrations , old_defs , old_orm , new_defs ) : <EOL> self . migrations = migrations <EOL> self . old_defs = old_defs <EOL> self . old_orm = old_orm <EOL> self . new_defs = new_defs <EOL> def suggest_name ( self ) : <EOL> parts = [ "<STR_LIT>" ] <EOL> for change_name , params in self . get_changes ( ) : <EOL> if change_name == "<STR_LIT>" : <EOL> parts . append ( "<STR_LIT>" % params [ '<STR_LIT>' ] . _meta . object_name . lower ( ) ) <EOL> elif change_name == "<STR_LIT>" : <EOL> parts . append ( "<STR_LIT>" % params [ '<STR_LIT>' ] . _meta . object_name . lower ( ) ) <EOL> elif change_name == "<STR_LIT>" : <EOL> parts . append ( "<STR_LIT>" % ( <EOL> params [ '<STR_LIT>' ] . _meta . object_name . lower ( ) , <EOL> params [ '<STR_LIT>' ] . name , <EOL> ) ) <EOL> elif change_name == "<STR_LIT>" : <EOL> parts . append ( "<STR_LIT>" % ( <EOL> params [ '<STR_LIT>' ] . _meta . object_name . lower ( ) , <EOL> params [ '<STR_LIT>' ] . name , <EOL> ) ) <EOL> elif change_name == "<STR_LIT>" : <EOL> parts . append ( "<STR_LIT>" % ( <EOL> params [ '<STR_LIT>' ] . _meta . object_name . lower ( ) , <EOL> params [ '<STR_LIT>' ] . name , <EOL> ) ) <EOL> elif change_name == "<STR_LIT>" : <EOL> parts . append ( "<STR_LIT>" % ( <EOL> params [ '<STR_LIT>' ] . _meta . object_name . lower ( ) , <EOL> "<STR_LIT:_>" . join ( [ x . name for x in params [ '<STR_LIT>' ] ] ) , <EOL> ) ) <EOL> elif change_name == "<STR_LIT>" : <EOL> parts . append ( "<STR_LIT>" % ( <EOL> params [ '<STR_LIT>' ] . _meta . object_name . lower ( ) , <EOL> "<STR_LIT:_>" . join ( [ x . name for x in params [ '<STR_LIT>' ] ] ) , <EOL> ) ) <EOL> elif change_name == "<STR_LIT>" : <EOL> parts . append ( "<STR_LIT>" % ( <EOL> params [ '<STR_LIT>' ] . _meta . object_name . lower ( ) , <EOL> "<STR_LIT:_>" . join ( [ x . name for x in params [ '<STR_LIT>' ] ] ) , <EOL> ) ) <EOL> elif change_name == "<STR_LIT>" : <EOL> parts . append ( "<STR_LIT>" % ( <EOL> params [ '<STR_LIT>' ] . _meta . object_name . lower ( ) , <EOL> "<STR_LIT:_>" . join ( [ x . name for x in params [ '<STR_LIT>' ] ] ) , <EOL> ) ) <EOL> return ( "<STR_LIT>" . join ( parts ) ) [ : <NUM_LIT> ] <EOL> def get_changes ( self ) : <EOL> """<STR_LIT>""" <EOL> deleted_models = set ( ) <EOL> for key in self . old_defs : <EOL> if key not in self . new_defs : <EOL> old_fields , old_meta , old_m2ms = self . split_model_def ( self . old_orm [ key ] , self . old_defs [ key ] ) <EOL> if old_meta . get ( "<STR_LIT>" , "<STR_LIT:True>" ) != "<STR_LIT:False>" : <EOL> yield ( "<STR_LIT>" , { <EOL> "<STR_LIT>" : self . old_orm [ key ] , <EOL> "<STR_LIT>" : old_fields , <EOL> } ) <EOL> for fieldname in old_m2ms : <EOL> field = self . old_orm [ key + "<STR_LIT::>" + fieldname ] <EOL> if auto_through ( field ) : <EOL> yield ( "<STR_LIT>" , { "<STR_LIT>" : self . old_orm [ key ] , "<STR_LIT>" : field } ) <EOL> for attr , operation in ( ( "<STR_LIT>" , "<STR_LIT>" ) , ( "<STR_LIT>" , "<STR_LIT>" ) ) : <EOL> together = eval ( old_meta . get ( attr , "<STR_LIT>" ) ) <EOL> if together : <EOL> if isinstance ( together [ <NUM_LIT:0> ] , string_types ) : <EOL> together = [ together ] <EOL> for fields in together : <EOL> yield ( operation , { <EOL> "<STR_LIT>" : self . old_orm [ key ] , <EOL> "<STR_LIT>" : [ self . old_orm [ key ] . _meta . get_field_by_name ( x ) [ <NUM_LIT:0> ] for x in fields ] , <EOL> } ) <EOL> deleted_models . add ( key ) <EOL> for key in self . new_defs : <EOL> if key not in self . old_defs : <EOL> new_fields , new_meta , new_m2ms = self . split_model_def ( self . current_model_from_key ( key ) , self . new_defs [ key ] ) <EOL> if new_meta . get ( "<STR_LIT>" , "<STR_LIT:True>" ) != "<STR_LIT:False>" : <EOL> yield ( "<STR_LIT>" , { <EOL> "<STR_LIT>" : self . current_model_from_key ( key ) , <EOL> "<STR_LIT>" : new_fields , <EOL> } ) <EOL> for fieldname in new_m2ms : <EOL> field = self . current_field_from_key ( key , fieldname ) <EOL> if auto_through ( field ) : <EOL> yield ( "<STR_LIT>" , { "<STR_LIT>" : self . current_model_from_key ( key ) , "<STR_LIT>" : field } ) <EOL> for attr , operation in ( ( "<STR_LIT>" , "<STR_LIT>" ) , ( "<STR_LIT>" , "<STR_LIT>" ) ) : <EOL> together = eval ( new_meta . get ( attr , "<STR_LIT>" ) ) <EOL> if together : <EOL> if isinstance ( together [ <NUM_LIT:0> ] , string_types ) : <EOL> together = [ together ] <EOL> for fields in together : <EOL> yield ( operation , { <EOL> "<STR_LIT>" : self . current_model_from_key ( key ) , <EOL> "<STR_LIT>" : [ self . current_model_from_key ( key ) . _meta . get_field_by_name ( x ) [ <NUM_LIT:0> ] for x in fields ] , <EOL> } ) <EOL> for key in self . old_defs : <EOL> if key not in deleted_models : <EOL> old_fields , old_meta , old_m2ms = self . split_model_def ( self . old_orm [ key ] , self . old_defs [ key ] ) <EOL> new_fields , new_meta , new_m2ms = self . split_model_def ( self . current_model_from_key ( key ) , self . new_defs [ key ] ) <EOL> if new_meta . get ( "<STR_LIT>" , "<STR_LIT:True>" ) == "<STR_LIT:False>" : <EOL> continue <EOL> for fieldname in old_fields : <EOL> if fieldname not in new_fields : <EOL> field = self . old_orm [ key + "<STR_LIT::>" + fieldname ] <EOL> field_allowed = True <EOL> for field_type in self . IGNORED_FIELD_TYPES : <EOL> if isinstance ( field , field_type ) : <EOL> field_allowed = False <EOL> if field_allowed : <EOL> yield ( "<STR_LIT>" , { <EOL> "<STR_LIT>" : self . old_orm [ key ] , <EOL> "<STR_LIT>" : field , <EOL> "<STR_LIT>" : old_fields [ fieldname ] , <EOL> } ) <EOL> for fieldname in new_fields : <EOL> if fieldname not in old_fields : <EOL> field = self . current_field_from_key ( key , fieldname ) <EOL> field_allowed = True <EOL> for field_type in self . IGNORED_FIELD_TYPES : <EOL> if isinstance ( field , field_type ) : <EOL> field_allowed = False <EOL> if field_allowed : <EOL> yield ( "<STR_LIT>" , { <EOL> "<STR_LIT>" : self . current_model_from_key ( key ) , <EOL> "<STR_LIT>" : field , <EOL> "<STR_LIT>" : new_fields [ fieldname ] , <EOL> } ) <EOL> for fieldname in old_m2ms : <EOL> if fieldname not in new_m2ms : <EOL> field = self . old_orm [ key + "<STR_LIT::>" + fieldname ] <EOL> if auto_through ( field ) : <EOL> yield ( "<STR_LIT>" , { "<STR_LIT>" : self . old_orm [ key ] , "<STR_LIT>" : field } ) <EOL> for fieldname in new_m2ms : <EOL> if fieldname not in old_m2ms : <EOL> field = self . current_field_from_key ( key , fieldname ) <EOL> if auto_through ( field ) : <EOL> yield ( "<STR_LIT>" , { "<STR_LIT>" : self . current_model_from_key ( key ) , "<STR_LIT>" : field } ) <EOL> for fieldname in set ( old_fields ) . intersection ( set ( new_fields ) ) : <EOL> if self . different_attributes ( <EOL> remove_useless_attributes ( old_fields [ fieldname ] , True , True ) , <EOL> remove_useless_attributes ( new_fields [ fieldname ] , True , True ) ) : <EOL> yield ( "<STR_LIT>" , { <EOL> "<STR_LIT>" : self . current_model_from_key ( key ) , <EOL> "<STR_LIT>" : self . old_orm [ key + "<STR_LIT::>" + fieldname ] , <EOL> "<STR_LIT>" : self . current_field_from_key ( key , fieldname ) , <EOL> "<STR_LIT>" : old_fields [ fieldname ] , <EOL> "<STR_LIT>" : new_fields [ fieldname ] , <EOL> } ) <EOL> old_field = self . old_orm [ key + "<STR_LIT::>" + fieldname ] <EOL> new_field = self . current_field_from_key ( key , fieldname ) <EOL> if not old_field . db_index and new_field . db_index : <EOL> yield ( "<STR_LIT>" , { <EOL> "<STR_LIT>" : self . current_model_from_key ( key ) , <EOL> "<STR_LIT>" : [ new_field ] , <EOL> } ) <EOL> if old_field . db_index and not new_field . db_index : <EOL> yield ( "<STR_LIT>" , { <EOL> "<STR_LIT>" : self . old_orm [ key ] , <EOL> "<STR_LIT>" : [ old_field ] , <EOL> } ) <EOL> if old_field . unique != new_field . unique : <EOL> if new_field . unique : <EOL> yield ( "<STR_LIT>" , { <EOL> "<STR_LIT>" : self . current_model_from_key ( key ) , <EOL> "<STR_LIT>" : [ new_field ] , <EOL> } ) <EOL> else : <EOL> yield ( "<STR_LIT>" , { <EOL> "<STR_LIT>" : self . old_orm [ key ] , <EOL> "<STR_LIT>" : [ old_field ] , <EOL> } ) <EOL> for fieldname in set ( old_m2ms ) . intersection ( set ( new_m2ms ) ) : <EOL> old_field = self . old_orm [ key + "<STR_LIT::>" + fieldname ] <EOL> new_field = self . current_field_from_key ( key , fieldname ) <EOL> if auto_through ( old_field ) and not auto_through ( new_field ) : <EOL> yield ( "<STR_LIT>" , { "<STR_LIT>" : self . old_orm [ key ] , "<STR_LIT>" : old_field } ) <EOL> if not auto_through ( old_field ) and auto_through ( new_field ) : <EOL> yield ( "<STR_LIT>" , { "<STR_LIT>" : self . current_model_from_key ( key ) , "<STR_LIT>" : new_field } ) <EOL> for attr , add_operation , del_operation in ( ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) ) : <EOL> old_together = eval ( old_meta . get ( attr , "<STR_LIT>" ) ) <EOL> new_together = eval ( new_meta . get ( attr , "<STR_LIT>" ) ) <EOL> if old_together and isinstance ( old_together [ <NUM_LIT:0> ] , string_types ) : <EOL> old_together = [ old_together ] <EOL> if new_together and isinstance ( new_together [ <NUM_LIT:0> ] , string_types ) : <EOL> new_together = [ new_together ] <EOL> old_together = frozenset ( tuple ( o ) for o in old_together ) <EOL> new_together = frozenset ( tuple ( n ) for n in new_together ) <EOL> disappeared = old_together . difference ( new_together ) <EOL> appeared = new_together . difference ( old_together ) <EOL> for item in disappeared : <EOL> yield ( del_operation , { <EOL> "<STR_LIT>" : self . old_orm [ key ] , <EOL> "<STR_LIT>" : [ self . old_orm [ key + "<STR_LIT::>" + x ] for x in item ] , <EOL> } ) <EOL> for item in appeared : <EOL> yield ( add_operation , { <EOL> "<STR_LIT>" : self . current_model_from_key ( key ) , <EOL> "<STR_LIT>" : [ self . current_field_from_key ( key , x ) for x in item ] , <EOL> } ) <EOL> @ classmethod <EOL> def is_triple ( cls , triple ) : <EOL> "<STR_LIT>" <EOL> return isinstance ( triple , ( list , tuple ) ) and len ( triple ) == <NUM_LIT:3> and isinstance ( triple [ <NUM_LIT:0> ] , string_types ) and isinstance ( triple [ <NUM_LIT:1> ] , ( list , tuple ) ) and isinstance ( triple [ <NUM_LIT:2> ] , dict ) <EOL> @ classmethod <EOL> def different_attributes ( cls , old , new ) : <EOL> """<STR_LIT>""" <EOL> if not cls . is_triple ( old ) or not cls . is_triple ( new ) : <EOL> return old != new <EOL> old_field , old_pos , old_kwd = old <EOL> new_field , new_pos , new_kwd = new <EOL> old_pos , new_pos = old_pos [ : ] , new_pos [ : ] <EOL> old_kwd = dict ( old_kwd . items ( ) ) <EOL> new_kwd = dict ( new_kwd . items ( ) ) <EOL> if "<STR_LIT>" in old_kwd : <EOL> del old_kwd [ '<STR_LIT>' ] <EOL> if "<STR_LIT>" in new_kwd : <EOL> del new_kwd [ '<STR_LIT>' ] <EOL> if old_field != new_field : <EOL> if old_field . startswith ( "<STR_LIT>" ) and ( new_field . startswith ( "<STR_LIT>" ) or new_field . startswith ( "<STR_LIT>" ) ) : <EOL> if old_field . split ( "<STR_LIT:.>" ) [ - <NUM_LIT:1> ] != new_field . split ( "<STR_LIT:.>" ) [ - <NUM_LIT:1> ] : <EOL> return True <EOL> else : <EOL> old_field = new_field = "<STR_LIT>" <EOL> if ( old_pos and "<STR_LIT:to>" in new_kwd ) and ( "<STR_LIT>" in new_kwd [ '<STR_LIT:to>' ] and "<STR_LIT>" not in old_pos [ <NUM_LIT:0> ] ) : <EOL> try : <EOL> if old_pos [ <NUM_LIT:0> ] != new_kwd [ '<STR_LIT:to>' ] . split ( "<STR_LIT:'>" ) [ <NUM_LIT:1> ] . split ( "<STR_LIT:.>" ) [ <NUM_LIT:1> ] : <EOL> return True <EOL> except IndexError : <EOL> pass <EOL> old_pos = old_pos [ <NUM_LIT:1> : ] <EOL> del new_kwd [ '<STR_LIT:to>' ] <EOL> return old_field != new_field or old_pos != new_pos or old_kwd != new_kwd <EOL> class ManualChanges ( BaseChanges ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , migrations , added_models , added_fields , added_indexes ) : <EOL> self . migrations = migrations <EOL> self . added_models = added_models <EOL> self . added_fields = added_fields <EOL> self . added_indexes = added_indexes <EOL> def suggest_name ( self ) : <EOL> bits = [ ] <EOL> for model_name in self . added_models : <EOL> bits . append ( '<STR_LIT>' % model_name ) <EOL> for field_name in self . added_fields : <EOL> bits . append ( '<STR_LIT>' % field_name ) <EOL> for index_name in self . added_indexes : <EOL> bits . append ( '<STR_LIT>' % index_name ) <EOL> return '<STR_LIT:_>' . join ( bits ) . replace ( '<STR_LIT:.>' , '<STR_LIT:_>' ) <EOL> def get_changes ( self ) : <EOL> model_defs = freeze_apps ( [ self . migrations . app_label ( ) ] ) <EOL> for model_name in self . added_models : <EOL> model = models . get_model ( self . migrations . app_label ( ) , model_name ) <EOL> real_fields , meta , m2m_fields = self . split_model_def ( model , model_defs [ model_key ( model ) ] ) <EOL> yield ( "<STR_LIT>" , { <EOL> "<STR_LIT>" : model , <EOL> "<STR_LIT>" : real_fields , <EOL> } ) <EOL> for field_desc in self . added_fields : <EOL> try : <EOL> model_name , field_name = field_desc . split ( "<STR_LIT:.>" ) <EOL> except ( TypeError , ValueError ) : <EOL> raise ValueError ( "<STR_LIT>" % field_desc ) <EOL> model = models . get_model ( self . migrations . app_label ( ) , model_name ) <EOL> real_fields , meta , m2m_fields = self . split_model_def ( model , model_defs [ model_key ( model ) ] ) <EOL> yield ( "<STR_LIT>" , { <EOL> "<STR_LIT>" : model , <EOL> "<STR_LIT>" : model . _meta . get_field_by_name ( field_name ) [ <NUM_LIT:0> ] , <EOL> "<STR_LIT>" : real_fields [ field_name ] , <EOL> } ) <EOL> for field_desc in self . added_indexes : <EOL> try : <EOL> model_name , field_name = field_desc . split ( "<STR_LIT:.>" ) <EOL> except ( TypeError , ValueError ) : <EOL> print ( "<STR_LIT>" % field_desc ) <EOL> model = models . get_model ( self . migrations . app_label ( ) , model_name ) <EOL> yield ( "<STR_LIT>" , { <EOL> "<STR_LIT>" : model , <EOL> "<STR_LIT>" : [ model . _meta . get_field_by_name ( field_name ) [ <NUM_LIT:0> ] ] , <EOL> } ) <EOL> class InitialChanges ( BaseChanges ) : <EOL> """<STR_LIT>""" <EOL> def suggest_name ( self ) : <EOL> return '<STR_LIT>' <EOL> def __init__ ( self , migrations ) : <EOL> self . migrations = migrations <EOL> def get_changes ( self ) : <EOL> model_defs = freeze_apps ( [ self . migrations . app_label ( ) ] ) <EOL> for model in models . get_models ( models . get_app ( self . migrations . app_label ( ) ) ) : <EOL> if model . _meta . abstract or getattr ( model . _meta , "<STR_LIT>" , False ) or not getattr ( model . _meta , "<STR_LIT>" , True ) : <EOL> continue <EOL> real_fields , meta , m2m_fields = self . split_model_def ( model , model_defs [ model_key ( model ) ] ) <EOL> yield ( "<STR_LIT>" , { <EOL> "<STR_LIT>" : model , <EOL> "<STR_LIT>" : real_fields , <EOL> } ) <EOL> if meta : <EOL> for attr , operation in ( ( "<STR_LIT>" , "<STR_LIT>" ) , ( "<STR_LIT>" , "<STR_LIT>" ) ) : <EOL> together = eval ( meta . get ( attr , "<STR_LIT>" ) ) <EOL> if together : <EOL> if isinstance ( together [ <NUM_LIT:0> ] , string_types ) : <EOL> together = [ together ] <EOL> for fields in together : <EOL> yield ( operation , { <EOL> "<STR_LIT>" : model , <EOL> "<STR_LIT>" : [ model . _meta . get_field_by_name ( x ) [ <NUM_LIT:0> ] for x in fields ] , <EOL> } ) <EOL> for name , triple in m2m_fields . items ( ) : <EOL> field = model . _meta . get_field_by_name ( name ) [ <NUM_LIT:0> ] <EOL> if field . rel . through : <EOL> try : <EOL> through_model = field . rel . through_model <EOL> except AttributeError : <EOL> through_model = field . rel . through <EOL> if ( not field . rel . through ) or getattr ( through_model . _meta , "<STR_LIT>" , False ) : <EOL> yield ( "<STR_LIT>" , { <EOL> "<STR_LIT>" : model , <EOL> "<STR_LIT>" : field , <EOL> } ) </s>
