def update_local_database():
    app = flask.current_app
    if not app.config.get("DEBUG", False):
        flask.abort(403)

    session = database.get_session()
    cursor = session.conn.cursor()

    # get the database
    url = "http://cites.eaudeweb.ro%s" % flask.url_for("update.dump_database")
    resp = requests.get(url, headers={"X-CitesAuth": app.config["API_KEY"]})
    assert resp.status_code == 200

    # drop everything
    database.drop_all()
    database.create_all()

    for table, items in resp.json.items():
        for i in items:
            row = getattr(database, table)()
            row.update(i)
            session.save(row, row.get("id", None))
            session.commit()

    for table in resp.json.keys():
        table = getattr(database, table)
        cursor.execute("SELECT setval('%s_id_seq', (SELECT MAX(id) FROM %s)+1)" % (table._table, table._table))

    return flask.jsonify({"status": "success"})
예제 #2
0
def resetdb():
    database.drop_all()