Exemplo n.º 1
0
def import_data():

    n = ("Beginning import process.")

    yield "<p>" + n

    DB.clean_database()

    xdb = DataSet(DB.dataset_connection())

    xdb.query(DB.pre_import(), commit=False)

    with xdb.transaction() as txn:

        for table_name in xdb.tables:

            xdb.query('DELETE FROM `{}`;'.format(table_name), commit=True)

    with xdb.transaction() as txn:

        for table_name in xdb.tables:

            n = ("Loading table " + table_name)

            yield "<p>" + n

            try:
                table = xdb[table_name]
            except:
                yield ("<p>Sorry, couldn't create table ", table_name)
            else:

                filename = (APPLICATION_PATH + EXPORT_FILE_PATH + '/dump-' +
                            table_name + '.json')
                if os.path.exists(filename):

                    table.thaw(format='json', filename=filename, strict=True)

                else:
                    n = ("No data for table " + table_name)
                    yield "<p>" + n

    xdb.query(DB.post_import())

    xdb.close()

    DB.recreate_indexes()

    n = "Import process ended. <a href='{}'>Click here to continue.</a>".format(
        BASE_URL)

    yield "<p>" + n

    from core.routes import app
    app.reset()
Exemplo n.º 2
0
def import_data():
    from settings import DB
    n = []
    n.append("Beginning import process.")
    # yield "<p>" + n

    n.append("Cleaning DB.")
    # yield "<p>" + n
    try:
        DB.clean_database()
    except:
        from core.models import init_db
        init_db.recreate_database()
        DB.remove_indexes()

    n.append("Clearing tables.")
    # yield "<p>" + n

    xdb = DataSet(DB.dataset_connection())

    # with xdb.atomic() as txn:
    try:
        with xdb.transaction():
            for table_name in xdb.tables:
                n.append("Loading table " + table_name)
                # yield "<p>" + n
                try:
                    table = xdb[table_name]
                except:
                    n.append("<p>Sorry, couldn't create table ", table_name)
                else:
                    filename = (APPLICATION_PATH + EXPORT_FILE_PATH +
                                '/dump-' + table_name + '.json')
                    if os.path.exists(filename):
                        try:
                            table.thaw(format='json',
                                       filename=filename,
                                       strict=True)
                        except Exception as e:
                            n.append("<p>Sorry, error:{}".format(e))

                    else:
                        n.append("No data for table " + table_name)
                        # yield "<p>" + n
    except Exception as e:
        n.append('Ooops: {}'.e)
    else:
        xdb.query(DB.post_import())
        xdb.close()
        DB.recreate_indexes()
        n.append(
            "Import process ended. <a href='{}'>Click here to continue.</a>".
            format(BASE_URL))
    return '<p>'.join(n)