示例#1
0
def Deserializer(object_list, **options):
    """
    Deserialize simple Python objects back into Django ORM instances.

    It's expected that you pass the Python objects themselves (instead of a
    stream or a string) to the constructor
    """
    db = options.pop('using', DEFAULT_DB_ALIAS)
    get_apps()
    for d in object_list:
        # Look up the model and starting build a dict of data for it.
        Model = _get_model(d.pop('__model__'))
        data = {}
        #data = {Model._meta.pk.attname : Model._meta.pk.to_python(d["pk"])}
        #m2m_data = {}

        # Handle each field
        for (field_name, field_value) in d.iteritems():
            if isinstance(field_value, str):
                field_value = smart_unicode(field_value,
                                            options.get(
                                                "encoding",
                                                settings.DEFAULT_CHARSET),
                                            strings_only=True)
            data[field_name] = field_value

        yield Model(**data)
示例#2
0
文件: python.py 项目: devhub/baph
def Deserializer(object_list, **options):
    """
    Deserialize simple Python objects back into Django ORM instances.

    It's expected that you pass the Python objects themselves (instead of a
    stream or a string) to the constructor
    """
    db = options.pop('using', DEFAULT_DB_ALIAS)
    get_apps()
    for d in object_list:
        # Look up the model and starting build a dict of data for it.
        Model = _get_model(d.pop('__model__'))
        data = {}
        #data = {Model._meta.pk.attname : Model._meta.pk.to_python(d["pk"])}
        #m2m_data = {}

        # Handle each field
        for (field_name, field_value) in d.iteritems():
            if isinstance(field_value, str):
                field_value = smart_unicode(field_value, 
                    options.get("encoding", settings.DEFAULT_CHARSET), 
                    strings_only=True)
            data[field_name] = field_value

        yield Model(**data)
示例#3
0
文件: sql.py 项目: dieselmachine/baph
def emit_post_sync_signal(created_models, verbosity, interactive, db):
    # Emit the post_sync signal for every application.
    for app in get_apps():
        app_name = app.__name__.rsplit('.',1)[0]
        if verbosity >= 2:
            print("Running post-sync handlers for application %s" % app_name)
        signals.post_syncdb.send(sender=app, app=app,
            created_models=created_models, verbosity=verbosity,
            interactive=interactive, db=db)
示例#4
0
def emit_post_sync_signal(created_models, verbosity, interactive, db):
    # Emit the post_sync signal for every application.
    for app in get_apps():
        app_name = app.__name__.rsplit('.', 1)[0]
        if verbosity >= 2:
            print("Running post-sync handlers for application %s" % app_name)
        signals.post_syncdb.send(sender=app,
                                 app=app,
                                 created_models=created_models,
                                 verbosity=verbosity,
                                 interactive=interactive,
                                 db=db)
示例#5
0
    def handle_noargs(self, **options):
        verbosity = int(options.get('verbosity', 1))
        interactive = options.get('interactive')
        flush = options.get('flush')
        self.style = no_style()

        if flush:
            # clear existing permissions
            session = orm.sessionmaker()
            session.execute(Permission.__table__.delete())
            session.commit()

        for app in get_apps():
            create_permissions(app, [], verbosity)
示例#6
0
    def handle_noargs(self, **options):
        verbosity = int(options.get('verbosity', 1))
        interactive = options.get('interactive')
        flush = options.get('flush')
        self.style = no_style()

        if flush:
            # clear existing permissions
            session = orm.sessionmaker()
            session.execute(Permission.__table__.delete())
            session.commit()
        
        for app in get_apps():
            create_permissions(app, [], verbosity)
示例#7
0
文件: simple.py 项目: gabrielx52/baph
    def build_suite(self, test_labels, extra_tests=None, **kwargs):
        suite = unittest.TestSuite()

        if test_labels:
            for label in test_labels:
                app_name = extract_app_name(label)
                if app_name == label:
                    app = get_app(label)
                    suite.addTest(build_suite(app))
                else:
                    suite.addTest(build_test(label))
        else:
            for app in get_apps():
                if app.__name__.startswith('django.'):
                    continue
                test = build_suite(app)
                suite.addTest(test)

        if extra_tests:
            for test in extra_tests:
                suite.addTest(test)

        return suite
示例#8
0
    def handle_noargs(self, **options):

        verbosity = int(options.get('verbosity'))
        interactive = options.get('interactive')
        show_traceback = options.get('traceback')
        load_initial_data = options.get('load_initial_data')

        self.style = no_style()

        # Import the 'management' module within each installed app, to register
        # dispatcher events.
        for app_name in settings.INSTALLED_APPS:
            try:
                import_module('.management', app_name)
            except ImportError as exc:
                # This is slightly hackish. We want to ignore ImportErrors
                # if the "management" module itself is missing -- but we don't
                # want to ignore the exception if the management module exists
                # but raises an ImportError for some reason. The only way we
                # can do this is to check the text of the exception. Note that
                # we're a bit broad in how we check the text, because different
                # Python implementations may not use the same text.
                # CPython uses the text "No module named management"
                # PyPy uses "No module named myproject.myapp.management"
                msg = exc.args[0]
                if not msg.startswith(
                        'No module named') or 'management' not in msg:
                    raise

        db = options.get('database')
        engine = connections[db]
        default_schema = engine.url.database

        # the default db may not exist yet, so we remove it before connecting
        engine.url.database = None
        tmp_url = str(engine.url)
        engine.url.database = default_schema
        tmp_engine = create_engine(tmp_url)
        tmp_conn = tmp_engine.connect()
        existing_schemas = set(
            [s[0] for s in tmp_conn.execute('show databases')])
        if not default_schema in existing_schemas:
            tmp_engine.execute(CreateSchema(default_schema))
            existing_schemas.add(default_schema)

        # now reconnect with the default_db provided
        conn = engine.connect()
        Base.metadata.bind = engine

        if verbosity >= 3:
            self.stdout.write("Getting existing schemas...\n")
            for schema in existing_schemas:
                self.stdout.write("\t%s\n" % schema)
            else:
                self.stdout.write("\tNone\n")

        existing_tables = []
        if verbosity >= 1:
            self.stdout.write("Getting existing tables...\n")
        for schema in existing_schemas:
            for name in engine.engine.table_names(schema, connection=conn):
                existing_tables.append('%s.%s' % (schema, name))
                if verbosity >= 3:
                    self.stdout.write("\t%s.%s\n" % (schema, name))

        existing_models = []
        if verbosity >= 1:
            self.stdout.write("Getting existing models...\n")
        for cls_name, cls in Base._decl_class_registry.items():
            tablename = get_tablename(cls)
            if tablename and tablename in existing_tables:
                existing_models.append(cls)
                if verbosity >= 3:
                    self.stdout.write("\t%s\n" % cls)

        all_tables = []
        if verbosity >= 1:
            self.stdout.write("Getting required tables...\n")
        for table in Base.metadata.sorted_tables:
            tablename = get_tablename(table)
            all_tables.append(tablename)
            if verbosity >= 3:
                self.stdout.write("\t%s\n" % tablename)

        all_models = []
        if verbosity >= 1:
            self.stdout.write("Getting required models...\n")
        for app in get_apps():
            for model in get_models(app, include_auto_created=True):
                app_name = app.__name__.rsplit('.', 1)[0]
                all_models.append((app_name, model))
                if verbosity >= 3:
                    self.stdout.write("\t%s.%s\n" % (app_name, model))

        schema_manifest = set()
        table_manifest = set()
        if verbosity >= 1:
            self.stdout.write('Building manifest...\n')
        for app_name, model in all_models:
            tablename = get_tablename(model)
            if tablename in existing_tables:
                continue
            table_manifest.add((app_name, model))
            schema = tablename.rsplit('.', 1)[0]
            if schema not in existing_schemas:
                schema_manifest.add(schema)

        table_manifest = sorted(
            table_manifest,
            key=lambda x: all_tables.index(get_tablename(x[1])))

        if verbosity >= 3:
            print 'Schema Manifest:\n'
            for schema in schema_manifest:
                print '\t%s\n' % schema
            print 'Model/Table Manifest\n'
            for app_name, model in table_manifest:
                print '\t%s.%s (%s)\n' % (app_name, model._meta.object_name,
                                          get_tablename(model))

        # create any missing schemas
        if verbosity >= 1:
            self.stdout.write("Creating schemas ...\n")
        for schema in schema_manifest:
            if verbosity >= 3:
                self.stdout.write("\t%s\n" % schema)
            engine.execute(CreateSchema(schema))
            existing_schemas.add(schema)

        # create any missing tables
        created_models = set()
        if verbosity >= 1:
            self.stdout.write("Creating tables ...\n")
        for app_name, model in table_manifest:
            if verbosity >= 3:
                self.stdout.write("\tCreating table for model %s.%s\n" %
                                  (app_name, model._meta.object_name))
            tablename = get_tablename(model)
            if tablename not in existing_tables:
                model.__table__.create()
                existing_tables.append(tablename)
            existing_models.append(model)
            created_models.add(model)

        # Send the post_syncdb signal
        emit_post_sync_signal(created_models, verbosity, interactive, db)

        # Load initial_data fixtures (unless that has been disabled)
        if load_initial_data:
            call_command('loaddata',
                         'initial_data',
                         verbosity=verbosity,
                         database=db,
                         skip_validation=True)
示例#9
0
    def handle_noargs(self, **options):

        verbosity = int(options.get('verbosity'))
        interactive = options.get('interactive')
        show_traceback = options.get('traceback')
        load_initial_data = options.get('load_initial_data')

        self.style = no_style()

        # Import the 'management' module within each installed app, to register
        # dispatcher events.
        for app_name in settings.INSTALLED_APPS:
            try:
                import_module('.management', app_name)
            except ImportError as exc:
                # This is slightly hackish. We want to ignore ImportErrors
                # if the "management" module itself is missing -- but we don't
                # want to ignore the exception if the management module exists
                # but raises an ImportError for some reason. The only way we
                # can do this is to check the text of the exception. Note that
                # we're a bit broad in how we check the text, because different
                # Python implementations may not use the same text.
                # CPython uses the text "No module named management"
                # PyPy uses "No module named myproject.myapp.management"
                msg = exc.args[0]
                if not msg.startswith('No module named') or 'management' not in msg:
                    raise

        db = options.get('database')
        engine = connections[db]
        default_schema = engine.url.database

        # the default db may not exist yet, so we remove it before connecting
        engine.url.database = None
        tmp_url = str(engine.url)
        engine.url.database = default_schema
        tmp_engine = create_engine(tmp_url)
        tmp_conn = tmp_engine.connect()
        existing_schemas = set([s[0] for s in tmp_conn.execute('show databases')])
        if not default_schema in existing_schemas:
            tmp_engine.execute(CreateSchema(default_schema))
            existing_schemas.add(default_schema)

        # now reconnect with the default_db provided
        conn = engine.connect()
        Base.metadata.bind = engine
        
        if verbosity >= 3:
            self.stdout.write("Getting existing schemas...\n")
            for schema in existing_schemas:
                self.stdout.write("\t%s\n" % schema)
            else:
                self.stdout.write("\tNone\n")

        existing_tables = []
        if verbosity >= 1:
            self.stdout.write("Getting existing tables...\n")
        for schema in existing_schemas:
            for name in engine.engine.table_names(schema, connection=conn):
                existing_tables.append('%s.%s' % (schema,name))
                if verbosity >= 3:
                    self.stdout.write("\t%s.%s\n" % (schema,name))    

        existing_models = []
        if verbosity >= 1:
            self.stdout.write("Getting existing models...\n")
        for cls_name, cls in Base._decl_class_registry.items():
            tablename = get_tablename(cls)
            if tablename and tablename in existing_tables:
                existing_models.append(cls)
                if verbosity >= 3:
                    self.stdout.write("\t%s\n" % cls)

        all_tables = []
        if verbosity >= 1:
            self.stdout.write("Getting required tables...\n")
        for table in Base.metadata.sorted_tables:
            tablename = get_tablename(table)
            all_tables.append(tablename)
            if verbosity >= 3:
                self.stdout.write("\t%s\n" % tablename)

        all_models = []
        if verbosity >= 1:
            self.stdout.write("Getting required models...\n")
        for app in get_apps():
            for model in get_models(app, include_auto_created=True):
                app_name = app.__name__.rsplit('.',1)[0]
                all_models.append( (app_name, model) )
                if verbosity >= 3:
                    self.stdout.write("\t%s.%s\n" % (app_name,model))

        schema_manifest = set()
        table_manifest = set()
        if verbosity >= 1:
            self.stdout.write('Building manifest...\n')
        for app_name, model in all_models:
            tablename = get_tablename(model)
            if tablename in existing_tables:
                continue
            table_manifest.add( (app_name, model) )
            schema = tablename.rsplit('.',1)[0]
            if schema not in existing_schemas:
                schema_manifest.add(schema)

        table_manifest = sorted(table_manifest, key=lambda x: 
            all_tables.index(get_tablename(x[1])))

        if verbosity >= 3:
            print 'Schema Manifest:\n'
            for schema in schema_manifest:
                print '\t%s\n' % schema
            print 'Model/Table Manifest\n'
            for app_name, model in table_manifest:
                print '\t%s.%s (%s)\n' % (app_name, model._meta.object_name, 
                    get_tablename(model)) 

        # create any missing schemas
        if verbosity >= 1:
            self.stdout.write("Creating schemas ...\n")
        for schema in schema_manifest:
            if verbosity >= 3:
                self.stdout.write("\t%s\n" % schema)
            engine.execute(CreateSchema(schema))
            existing_schemas.add(schema)            

        # create any missing tables
        created_models = set()
        if verbosity >= 1:
            self.stdout.write("Creating tables ...\n")
        for app_name, model in table_manifest:
            if verbosity >= 3:
                self.stdout.write("\tCreating table for model %s.%s\n" 
                    % (app_name, model._meta.object_name))
            tablename = get_tablename(model)
            if tablename not in existing_tables:
                model.__table__.create()
                existing_tables.append(tablename)
            existing_models.append(model)
            created_models.add(model)

        # Send the post_syncdb signal
        emit_post_sync_signal(created_models, verbosity, interactive, db)

        # Load initial_data fixtures (unless that has been disabled)
        if load_initial_data:
            call_command('loaddata', 'initial_data', verbosity=verbosity,
                         database=db, skip_validation=True)
示例#10
0
    def handle(self, *app_labels, **options):
        from baph.db.models import get_app, get_apps, get_model

        format = options.get('format')
        indent = options.get('indent')
        using = options.get('database')
        excludes = options.get('exclude')
        show_traceback = options.get('traceback')
        use_natural_keys = options.get('use_natural_keys')
        use_base_manager = options.get('use_base_manager')

        excluded_apps = set()
        excluded_models = set()
        for exclude in excludes:
            if '.' in exclude:
                app_label, model_name = exclude.split('.', 1)
                model_obj = get_model(app_label, model_name)
                if not model_obj:
                    raise CommandError('Unknown model in excludes: %s' %
                                       exclude)
                excluded_models.add(model_obj)
            else:
                try:
                    app_obj = get_app(exclude)
                    excluded_apps.add(app_obj)
                except ImproperlyConfigured:
                    raise CommandError('Unknown app in excludes: %s' % exclude)

        if len(app_labels) == 0:
            app_list = SortedDict(
                (app, None) for app in get_apps() if app not in excluded_apps)
        else:
            app_list = SortedDict()
            for label in app_labels:
                app_label = extract_app_name(label)
                if not app_label:
                    # no match against installed_apps
                    raise CommandError("Unknown application: %s" % label)
                if app_label == label:
                    # This is just an app - no model qualifier
                    try:
                        app = get_app(app_label)
                    except ImproperlyConfigured:
                        raise CommandError("Unknown application: %s" %
                                           app_label)
                    if app in excluded_apps:
                        continue
                    app_list[app] = None
                else:
                    # this is app_label.model
                    model_label = label.rsplit('.')[-1]
                    try:
                        app = get_app(app_label)
                    except ImproperlyConfigured:
                        raise CommandError("Unknown application: %s" %
                                           app_label)
                    if app in excluded_apps:
                        continue
                    model = get_model(app_label, model_label)
                    if model is None:
                        raise CommandError("Unknown model: %s.%s" %
                                           (app_label, model_label))

                    if app in app_list.keys():
                        if app_list[app] and model not in app_list[app]:
                            app_list[app].append(model)
                    else:
                        app_list[app] = [model]

        # Check that the serialization format exists; this is a shortcut to
        # avoid collating all the objects and _then_ failing.
        if format not in serializers.get_public_serializer_formats():
            raise CommandError("Unknown serialization format: %s" % format)

        try:
            serializers.get_serializer(format)
        except KeyError:
            raise CommandError("Unknown serialization format: %s" % format)

        def get_objects():
            # Collate the objects to be serialized.
            session = orm.sessionmaker()
            for model in sort_dependencies(app_list.items()):
                if model in excluded_models:
                    continue
                for obj in session.query(model):
                    yield obj

        try:
            self.stdout.ending = None
            serializers.serialize(format,
                                  get_objects(),
                                  indent=indent,
                                  use_natural_keys=use_natural_keys,
                                  stream=self.stdout)
        except Exception as e:
            if show_traceback:
                raise
            raise CommandError("Unable to serialize database: %s" % e)
示例#11
0
文件: dumpdata.py 项目: devhub/baph
    def handle(self, *app_labels, **options):
        from baph.db.models import get_app, get_apps, get_model

        format = options.get('format')
        indent = options.get('indent')
        using = options.get('database')
        excludes = options.get('exclude')
        show_traceback = options.get('traceback')
        use_natural_keys = options.get('use_natural_keys')
        use_base_manager = options.get('use_base_manager')

        excluded_apps = set()
        excluded_models = set()
        for exclude in excludes:
            if '.' in exclude:
                app_label, model_name = exclude.split('.', 1)
                model_obj = get_model(app_label, model_name)
                if not model_obj:
                    raise CommandError('Unknown model in excludes: %s' % exclude)
                excluded_models.add(model_obj)
            else:
                try:
                    app_obj = get_app(exclude)
                    excluded_apps.add(app_obj)
                except ImproperlyConfigured:
                    raise CommandError('Unknown app in excludes: %s' % exclude)

        if len(app_labels) == 0:
            app_list = SortedDict((app, None) for app in get_apps() 
                                    if app not in excluded_apps)
        else:
            app_list = SortedDict()
            for label in app_labels:
                app_label = extract_app_name(label)
                if not app_label:
                    # no match against installed_apps
                    raise CommandError("Unknown application: %s" % label)
                if app_label == label:
                    # This is just an app - no model qualifier
                    try:
                        app = get_app(app_label)
                    except ImproperlyConfigured:
                        raise CommandError("Unknown application: %s" % app_label)
                    if app in excluded_apps:
                        continue
                    app_list[app] = None
                else:
                    # this is app_label.model
                    model_label = label.rsplit('.')[-1]
                    try:
                        app = get_app(app_label)
                    except ImproperlyConfigured:
                        raise CommandError("Unknown application: %s" % app_label)
                    if app in excluded_apps:
                        continue
                    model = get_model(app_label, model_label)
                    if model is None:
                        raise CommandError("Unknown model: %s.%s" % (app_label, model_label))

                    if app in app_list.keys():
                        if app_list[app] and model not in app_list[app]:
                            app_list[app].append(model)
                    else:
                        app_list[app] = [model]

        # Check that the serialization format exists; this is a shortcut to
        # avoid collating all the objects and _then_ failing.
        if format not in serializers.get_public_serializer_formats():
            raise CommandError("Unknown serialization format: %s" % format)

        try:
            serializers.get_serializer(format)
        except KeyError:
            raise CommandError("Unknown serialization format: %s" % format)

        def get_objects():
            # Collate the objects to be serialized.
            session = orm.sessionmaker()
            for model in sort_dependencies(app_list.items()):
                if model in excluded_models:
                    continue
                for obj in session.query(model):
                    yield obj

        try:
            self.stdout.ending = None
            serializers.serialize(format, get_objects(), indent=indent,
                    use_natural_keys=use_natural_keys, stream=self.stdout)
        except Exception as e:
            if show_traceback:
                raise
            raise CommandError("Unable to serialize database: %s" % e)
示例#12
0
文件: flush.py 项目: devhub/baph
    def handle_noargs(self, **options):
        #db = options.get('database', DEFAULT_DB_ALIAS)
        #connection = connections[db]
        verbosity = int(options.get('verbosity', 1))
        interactive = options.get('interactive')

        self.style = no_style()

        # Import the 'management' module within each installed app, to register
        # dispatcher events.
        for app_name in settings.INSTALLED_APPS:
            try:
                import_module('.management', app_name)
            except ImportError:
                pass

        #sql_list = sql_flush(self.style, connection, only_django=True)

        if interactive:
            confirm = raw_input("""You have requested a flush of the database.
This will IRREVERSIBLY DESTROY all data currently in the database,
and return each table to the state it was in after syncdb.
Are you sure you want to do this?

    Type 'yes' to continue, or 'no' to cancel: """)
        else:
            confirm = 'yes'

        if confirm == 'yes':
            session = orm.sessionmaker()
            session.expunge_all()
            try:
                session.execute('set foreign_key_checks=0')
                for table in reversed(Base.metadata.sorted_tables):
                    if table.info.get('preserve_during_flush', False):
                        continue
                    try:
                        session.execute(table.delete())
                    except Exception as e:
                        # table not present
                        pass
                session.flush()
            except Exception as e:
                session.rollback()
                raise CommandError('Could not flush the database')
            finally:
                session.execute('set foreign_key_checks=1')
                session.commit()

            # Emit the post sync signal. This allows individual
            # applications to respond as if the database had been
            # sync'd from scratch.
            all_models = []
            for app in get_apps():
                all_models.extend([
                    m for m in get_models(app, include_auto_created=True)
                ])
            emit_post_sync_signal(set(all_models), verbosity, interactive, None) 

            # Reinstall the initial_data fixture.
            if options.get('load_initial_data'):
                # Reinstall the initial_data fixture.
                call_command('loaddata', 'initial_data', **options)

        else:
            self.stdout.write("Flush cancelled.\n")
示例#13
0
文件: flush.py 项目: gabrielx52/baph
    def handle_noargs(self, **options):
        #db = options.get('database', DEFAULT_DB_ALIAS)
        #connection = connections[db]
        verbosity = int(options.get('verbosity', 1))
        interactive = options.get('interactive')

        self.style = no_style()

        # Import the 'management' module within each installed app, to register
        # dispatcher events.
        for app_name in settings.INSTALLED_APPS:
            try:
                import_module('.management', app_name)
            except ImportError:
                pass

        #sql_list = sql_flush(self.style, connection, only_django=True)

        if interactive:
            confirm = raw_input("""You have requested a flush of the database.
This will IRREVERSIBLY DESTROY all data currently in the database,
and return each table to the state it was in after syncdb.
Are you sure you want to do this?

    Type 'yes' to continue, or 'no' to cancel: """)
        else:
            confirm = 'yes'

        if confirm == 'yes':
            session = orm.sessionmaker()
            session.expunge_all()
            try:
                session.execute('set foreign_key_checks=0')
                for table in reversed(Base.metadata.sorted_tables):
                    if table.info.get('preserve_during_flush', False):
                        continue
                    try:
                        session.execute(table.delete())
                    except Exception as e:
                        # table not present
                        pass
                session.flush()
            except Exception as e:
                session.rollback()
                raise CommandError('Could not flush the database')
            finally:
                session.execute('set foreign_key_checks=1')
                session.commit()

            # Emit the post sync signal. This allows individual
            # applications to respond as if the database had been
            # sync'd from scratch.
            all_models = []
            for app in get_apps():
                all_models.extend(
                    [m for m in get_models(app, include_auto_created=True)])
            emit_post_sync_signal(set(all_models), verbosity, interactive,
                                  None)

            # Reinstall the initial_data fixture.
            if options.get('load_initial_data'):
                # Reinstall the initial_data fixture.
                call_command('loaddata', 'initial_data', **options)

        else:
            self.stdout.write("Flush cancelled.\n")
示例#14
0
    def handle_noargs(self, **options):
        #db = options.get('database', DEFAULT_DB_ALIAS)
        #connection = connections[db]
        verbosity = int(options.get('verbosity', 1))
        interactive = options.get('interactive')

        self.style = no_style()

        # Import the 'management' module within each installed app, to register
        # dispatcher events.
        for app_name in settings.INSTALLED_APPS:
            try:
                import_module('.management', app_name)
            except ImportError:
                pass

        #sql_list = sql_flush(self.style, connection, only_django=True)

        if interactive:
            confirm = raw_input("""You have requested a flush of the database.
This will IRREVERSIBLY DESTROY all data currently in the database,
and return each table to the state it was in after syncdb.
Are you sure you want to do this?

    Type 'yes' to continue, or 'no' to cancel: """)
        else:
            confirm = 'yes'

        if confirm == 'yes':
            try:
                session = Session()
                for table in reversed(Base.metadata.sorted_tables):
                    if table.info.get('preserve_during_flush', False):
                        continue
                    try:
                        session.execute(table.delete())
                    except:
                        pass
                session.commit()
            except Exception, e:
                session.rollback()
                raise
                raise CommandError("""Database couldn't be flushed. Possible reasons:
  * The database isn't running or isn't configured correctly.
  * At least one of the expected database tables doesn't exist.
  * The SQL was invalid.
Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run.
The full error: %s""")

            # Emit the post sync signal. This allows individual
            # applications to respond as if the database had been
            # sync'd from scratch.
            all_models = []
            for app in get_apps():
                all_models.extend([
                    m for m in get_models(app, include_auto_created=True)
                ])
            emit_post_sync_signal(set(all_models), verbosity, interactive, None) 

            # Reinstall the initial_data fixture.
            if options.get('load_initial_data'):
                # Reinstall the initial_data fixture.
                call_command('loaddata', 'initial_data', **options)