def callback_heroku(): """ Complete Heroku authentication, start app-setup, redirect to app page. """ code, tar_id = request.args.get("code"), request.args.get("state") client_id, client_secret, redirect_uri = heroku_client_info(request) try: data = dict(grant_type="authorization_code", code=code, client_secret=client_secret, redirect_uri="") response = post(heroku_access_token_url, data=data) logger.debug("GET /callback-heroku {}".format(response.json())) access = response.json() if response.status_code != 200: if "message" in access: raise SetupError('Heroku says "{0}"'.format(access["message"])) else: raise SetupError("Heroku Error") url = "{0}://{1}/tarball/{2}".format(get_scheme(request), request.host, tar_id) setup_id, app_name = builders.create_app(builders.get_http_client(), access["access_token"], url) with psycopg2.connect(app.config["SQLALCHEMY_DATABASE_URI"]) as connection: with connection.cursor() as cursor: builders.set_connection_datum(cursor, tar_id, "app_name", app_name) builders.set_connection_datum(cursor, tar_id, "app_setup_id", setup_id) builders.set_connection_datum(cursor, tar_id, "access_token", access["access_token"]) google_name = builders.get_connection_datum(cursor, tar_id, "google name") google_email = builders.get_connection_datum(cursor, tar_id, "google email") google_url = builders.get_connection_datum(cursor, tar_id, "google url") try: if app.config["SEND_EMAIL"]: fromaddr, toaddr = app.config["EMAIL_SENDER"], app.config["EMAIL_RECIPIENT"] msg = "From: {fromaddr}\r\nTo: {toaddr}\r\nCc: {fromaddr}\r\nSubject: City Analytics Dashboard got used\r\n\r\n{google_name} {google_email} at https://{app_name}.herokuapp.com for {google_url}.".format( **locals() ) builders.send_email(fromaddr, toaddr, msg, app.config) except: logger.error("City Analytics Dashboard - SMTP error", exc_info=True) wait_url = "{0}://{1}/{2}/wait-for-heroku".format(get_scheme(request), request.host, tar_id) return redirect(wait_url) return render_template( "done.html", style_base=get_style_base(request), settings_url=builders.heroku_app_activity_template.format(app_name), application_url=builders.heroku_app_direct_template.format(app_name), app_name=app_name, ) return redirect(builders.heroku_app_activity_template.format(app_name)) except SetupError, e: logger.error("City Analytics Dashboard - Heroku error", exc_info=True) values = dict(style_base=get_style_base(request), message=e.message) return make_response(render_template("error.html", **values), 400)
def callback_heroku(): ''' Complete Heroku authentication, start app-setup, redirect to app page. ''' code, tar_id = request.args.get('code'), request.args.get('state') client_id, client_secret, redirect_uri = heroku_client_info(request) try: data = dict(grant_type='authorization_code', code=code, client_secret=client_secret, redirect_uri='') response = post(heroku_access_token_url, data=data) logger.debug('GET /callback-heroku {}'.format(response.json())) access = response.json() if response.status_code != 200: if 'message' in access: raise SetupError('Heroku says "{0}"'.format(access['message'])) else: raise SetupError('Heroku Error') url = '{0}://{1}/tarball/{2}'.format(get_scheme(request), request.host, tar_id) setup_id, app_name = builders.create_app(builders.get_http_client(), access['access_token'], url) with psycopg2.connect(app.config['SQLALCHEMY_DATABASE_URI']) as connection: with connection.cursor() as cursor: builders.set_connection_datum(cursor, tar_id, 'app_name', app_name) builders.set_connection_datum(cursor, tar_id, 'app_setup_id', setup_id) builders.set_connection_datum(cursor, tar_id, 'access_token', access['access_token']) google_name = builders.get_connection_datum(cursor, tar_id, 'google name') google_email = builders.get_connection_datum(cursor, tar_id, 'google email') google_url = builders.get_connection_datum(cursor, tar_id, 'google url') try: if app.config['SEND_EMAIL']: fromaddr, toaddr = app.config['EMAIL_RECIPIENT'], app.config['EMAIL_SENDER'] msg = 'From: {fromaddr}\r\nTo: {toaddr}\r\nSubject: City Analytics Dashboard got used\r\n\r\n{google_name} {google_email} at https://{app_name}.herokuapp.com for {google_url}.'.format(**locals()) builders.send_email(fromaddr, toaddr, msg, app.config) except: logger.error('Local Gov Analytics Dashboard - SMTP error', exc_info=True) wait_url = '{0}://{1}/{2}/wait-for-heroku'.format(get_scheme(request), request.host, tar_id) return redirect(wait_url) return render_template('done.html', settings_url=builders.heroku_app_activity_template.format(app_name), application_url=builders.heroku_app_direct_template.format(app_name), app_name=app_name) return redirect(builders.heroku_app_activity_template.format(app_name)) except SetupError, e: logger.error('Local Gov Analytics Dashboard - Heroku error', exc_info=True) values = dict(message=e.message) return make_response(render_template('error.html', **values), 400)
def wait_for_heroku(conn_id): """ """ with psycopg2.connect(app.config["SQLALCHEMY_DATABASE_URI"]) as connection: with connection.cursor() as cursor: app_setup_id = builders.get_connection_datum(cursor, conn_id, "app_setup_id") access_token = builders.get_connection_datum(cursor, conn_id, "access_token") finished = builders.check_app(builders.get_http_client(), access_token, app_setup_id) if not finished: return render_template("wait.html", style_base=get_style_base(request), url=request.path) finished_url = "{0}://{1}/{2}/finished".format(get_scheme(request), request.host, conn_id) return redirect(finished_url)
def wait_for_heroku(conn_id): ''' ''' with psycopg2.connect(app.config['SQLALCHEMY_DATABASE_URI']) as connection: with connection.cursor() as cursor: app_setup_id = builders.get_connection_datum(cursor, conn_id, 'app_setup_id') access_token = builders.get_connection_datum(cursor, conn_id, 'access_token') finished = builders.check_app(builders.get_http_client(), access_token, app_setup_id) if not finished: return render_template('wait.html', url=request.path) finished_url = '{0}://{1}/{2}/finished'.format(get_scheme(request), request.host, conn_id) return redirect(finished_url)
def wait_for_heroku(conn_id): ''' ''' with psycopg2.connect(app.config['SQLALCHEMY_DATABASE_URI']) as connection: with connection.cursor() as cursor: app_setup_id = builders.get_connection_datum( cursor, conn_id, 'app_setup_id') access_token = builders.get_connection_datum( cursor, conn_id, 'access_token') finished = builders.check_app(builders.get_http_client(), access_token, app_setup_id) if not finished: return render_template('wait.html', url=request.path) finished_url = '{0}://{1}/{2}/finished'.format(get_scheme(request), request.host, conn_id) return redirect(finished_url)
def callback_heroku(): ''' Complete Heroku authentication, start app-setup, redirect to app page. ''' code, tar_id = request.args.get('code'), request.args.get('state') client_id, client_secret, redirect_uri = heroku_client_info(request) try: data = dict(grant_type='authorization_code', code=code, client_secret=client_secret, redirect_uri='') response = post(heroku_access_token_url, data=data) logger.debug('GET /callback-heroku {}'.format(response.json())) access = response.json() if response.status_code != 200: if 'message' in access: raise SetupError('Heroku says "{0}"'.format(access['message'])) else: raise SetupError('Heroku Error') url = '{0}://{1}/tarball/{2}'.format(get_scheme(request), request.host, tar_id) setup_id, app_name = builders.create_app(builders.get_http_client(), access['access_token'], url) with psycopg2.connect( app.config['SQLALCHEMY_DATABASE_URI']) as connection: with connection.cursor() as cursor: builders.set_connection_datum(cursor, tar_id, 'app_name', app_name) builders.set_connection_datum(cursor, tar_id, 'app_setup_id', setup_id) builders.set_connection_datum(cursor, tar_id, 'access_token', access['access_token']) google_name = builders.get_connection_datum( cursor, tar_id, 'google name') google_email = builders.get_connection_datum( cursor, tar_id, 'google email') google_url = builders.get_connection_datum( cursor, tar_id, 'google url') try: if app.config['SEND_EMAIL']: fromaddr, toaddr = app.config['EMAIL_RECIPIENT'], app.config[ 'EMAIL_SENDER'] msg = 'From: {fromaddr}\r\nTo: {toaddr}\r\nSubject: City Analytics Dashboard got used\r\n\r\n{google_name} {google_email} at https://{app_name}.herokuapp.com for {google_url}.'.format( **locals()) builders.send_email(fromaddr, toaddr, msg, app.config) except: logger.error('Local Gov Analytics Dashboard - SMTP error', exc_info=True) wait_url = '{0}://{1}/{2}/wait-for-heroku'.format( get_scheme(request), request.host, tar_id) return redirect(wait_url) return render_template( 'done.html', settings_url=builders.heroku_app_activity_template.format( app_name), application_url=builders.heroku_app_direct_template.format( app_name), app_name=app_name) return redirect(builders.heroku_app_activity_template.format(app_name)) except SetupError, e: logger.error('Local Gov Analytics Dashboard - Heroku error', exc_info=True) values = dict(message=e.message) return make_response(render_template('error.html', **values), 400)