def init(with_testdb, with_data): """ Initialize the database. :param with_data: :param with_testdb: Create a test database :return: None """ uri = app.config['SQLALCHEMY_DATABASE_URI'] print(f"Database uri is {uri}") if not database_exists(uri): create_database(uri) db.create_all() else: db.drop_all() db.create_all() if with_testdb: db_uri = '{0}_test'.format(app.config['SQLALCHEMY_DATABASE_URI']) if not database_exists(db_uri): create_database(db_uri) user = User() user.username = "******" user.save() if with_data: _seed_catalog() return None
def google_logged_in(blueprint, token): if not token: flash("Failed to log in with {name}".format(name=blueprint.name)) return # figure out who the user is resp = blueprint.session.get("/oauth2/v2/userinfo") if resp.ok: username = resp.json()["email"] query = User.query.filter_by(username=username) try: user = query.one() except NoResultFound: # create a user user = User() user.username = username user.save() login_user(user) flash("Successfully signed in with Google", "success") else: msg = "Failed to fetch user info from {name}".format( name=blueprint.name) flash(msg, category="error")
def github_logged_in(blueprint, token): if not token: flash("Failed to log in with {name}".format(name=blueprint.name)) return # figure out who the user is resp = blueprint.session.get("/user") if resp.ok: print(f"Response from github: {resp.json()}") username = resp.json()["login"] query = User.query.filter_by(username=username) try: user = query.one() except NoResultFound: # create a user user = User() user.username = username user.save() login_user(user) flash("Successfully signed in with Github", "success") else: print(resp.text) msg = "Failed to fetch user info from {name}".format( name=blueprint.name) flash(msg, category="error")