<s> from django . core . exceptions import ImproperlyConfigured <EOL> from django . core . management . base import BaseCommand , CommandError <EOL> from django . core import serializers <EOL> from django . db import connections , router , DEFAULT_DB_ALIAS <EOL> from django . utils . datastructures import SortedDict <EOL> from optparse import make_option <EOL> class Command ( BaseCommand ) : <EOL> option_list = BaseCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , default = '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , default = None , dest = '<STR_LIT>' , type = '<STR_LIT:int>' , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> default = DEFAULT_DB_ALIAS , help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT>' , default = [ ] , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> ) <EOL> help = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> args = '<STR_LIT>' <EOL> def handle ( self , * app_labels , ** options ) : <EOL> from django . db . models import get_app , get_apps , get_models , get_model <EOL> format = options . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> indent = options . get ( '<STR_LIT>' , None ) <EOL> using = options . get ( '<STR_LIT>' , DEFAULT_DB_ALIAS ) <EOL> connection = connections [ using ] <EOL> exclude = options . get ( '<STR_LIT>' , [ ] ) <EOL> show_traceback = options . get ( '<STR_LIT>' , False ) <EOL> use_natural_keys = options . get ( '<STR_LIT>' , False ) <EOL> excluded_apps = set ( get_app ( app_label ) for app_label in exclude ) <EOL> if len ( app_labels ) == <NUM_LIT:0> : <EOL> app_list = SortedDict ( ( app , None ) for app in get_apps ( ) if app not in excluded_apps ) <EOL> else : <EOL> app_list = SortedDict ( ) <EOL> for label in app_labels : <EOL> try : <EOL> app_label , model_label = label . split ( '<STR_LIT:.>' ) <EOL> try : <EOL> app = get_app ( app_label ) <EOL> except ImproperlyConfigured : <EOL> raise CommandError ( "<STR_LIT>" % app_label ) <EOL> model = get_model ( app_label , model_label ) <EOL> if model is None : <EOL> raise CommandError ( "<STR_LIT>" % ( app_label , model_label ) ) <EOL> if app in app_list . keys ( ) : <EOL> if app_list [ app ] and model not in app_list [ app ] : <EOL> app_list [ app ] . append ( model ) <EOL> else : <EOL> app_list [ app ] = [ model ] <EOL> except ValueError : <EOL> app_label = label <EOL> try : <EOL> app = get_app ( app_label ) <EOL> except ImproperlyConfigured : <EOL> raise CommandError ( "<STR_LIT>" % app_label ) <EOL> app_list [ app ] = None <EOL> if format not in serializers . get_public_serializer_formats ( ) : <EOL> raise CommandError ( "<STR_LIT>" % format ) <EOL> try : <EOL> serializers . get_serializer ( format ) <EOL> except KeyError : <EOL> raise CommandError ( "<STR_LIT>" % format ) <EOL> objects = [ ] <EOL> for model in sort_dependencies ( app_list . items ( ) ) : <EOL> if not model . _meta . proxy and router . allow_syncdb ( using , model ) : <EOL> objects . extend ( model . _default_manager . using ( using ) . all ( ) ) <EOL> try : <EOL> return serializers . serialize ( format , objects , indent = indent , <EOL> use_natural_keys = use_natural_keys ) <EOL> except Exception , e : <EOL> if show_traceback : <EOL> raise <EOL> raise CommandError ( "<STR_LIT>" % e ) <EOL> def sort_dependencies ( app_list ) : <EOL> """<STR_LIT>""" <EOL> from django . db . models import get_model , get_models <EOL> model_dependencies = [ ] <EOL> models = set ( ) <EOL> for app , model_list in app_list : <EOL> if model_list is None : <EOL> model_list = get_models ( app ) <EOL> for model in model_list : <EOL> models . add ( model ) <EOL> if hasattr ( model , '<STR_LIT>' ) : <EOL> deps = getattr ( model . natural_key , '<STR_LIT>' , [ ] ) <EOL> if deps : <EOL> deps = [ get_model ( * d . split ( '<STR_LIT:.>' ) ) for d in deps ] <EOL> else : <EOL> deps = [ ] <EOL> for field in model . _meta . fields : <EOL> if hasattr ( field . rel , '<STR_LIT:to>' ) : <EOL> rel_model = field . rel . to <EOL> if hasattr ( rel_model , '<STR_LIT>' ) : <EOL> deps . append ( rel_model ) <EOL> for field in model . _meta . many_to_many : <EOL> rel_model = field . rel . to <EOL> if hasattr ( rel_model , '<STR_LIT>' ) : <EOL> deps . append ( rel_model ) <EOL> model_dependencies . append ( ( model , deps ) ) <EOL> model_dependencies . reverse ( ) <EOL> model_list = [ ] <EOL> while model_dependencies : <EOL> skipped = [ ] <EOL> changed = False <EOL> while model_dependencies : <EOL> model , deps = model_dependencies . pop ( ) <EOL> found = True <EOL> for candidate in ( ( d not in models or d in model_list ) for d in deps ) : <EOL> if not candidate : <EOL> found = False <EOL> if found : <EOL> model_list . append ( model ) <EOL> changed = True <EOL> else : <EOL> skipped . append ( ( model , deps ) ) <EOL> if not changed : <EOL> raise CommandError ( "<STR_LIT>" % <EOL> '<STR_LIT:U+002CU+0020>' . join ( '<STR_LIT>' % ( model . _meta . app_label , model . _meta . object_name ) <EOL> for model , deps in sorted ( skipped , key = lambda obj : obj [ <NUM_LIT:0> ] . __name__ ) ) <EOL> ) <EOL> model_dependencies = skipped <EOL> return model_list </s>
