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 prepare_app(): ''' Prepare app, ask Heroku to authenticate, return to /callback-heroku. ''' view_id, website_url = request.form.get('property').split(' ', 1) name, email = request.form.get('name'), request.form.get('email') client_id = request.form.get('client_id') client_secret = request.form.get('client_secret') refresh_token = request.form.get('refresh_token') organisation_name = request.form.get('organisation_name', 'LG Analytics Dashboard') shortcut_icon = request.form.get('shortcut_icon', '/favicon.ico') theme_colour = request.form.get('theme_colour', '#212121') title_filter = request.form.get('title_filter', ' | ') # Make sure these vars have values if not (organisation_name and organisation_name.strip()): organisation_name = 'LG Analytics Dashboard' if not (shortcut_icon and shortcut_icon.strip()): shortcut_icon = '/favicon.ico' if not (theme_colour and theme_colour.strip()): theme_colour = '#212121' if not (title_filter and title_filter.strip()): title_filter = ' | ' env = dict(LANG='en_US.UTF-8', RACK_ENV='production', GA_VIEW_ID=view_id, GA_WEBSITE_URL=website_url, CLIENT_ID=client_id, CLIENT_SECRET=client_secret, REFRESH_TOKEN=refresh_token, ORGANISATION_NAME=organisation_name, SHORTCUT_ICON=shortcut_icon, THEME_COLOUR=theme_colour, TITLE_FILTER=title_filter) tarpath = prepare_tarball(display_screen_tarball_url, dict(name='Display Screen', env=env)) logger.debug('GET /prepare-app {} {}'.format(app.config, env)) with psycopg2.connect(app.config['SQLALCHEMY_DATABASE_URI']) as connection: with connection.cursor() as cursor: tarball_id = builders.add_connection(cursor, email, name, website_url, tarpath) builders.set_connection_datum(cursor, tarball_id, 'google name', name) builders.set_connection_datum(cursor, tarball_id, 'google email', email) builders.set_connection_datum(cursor, tarball_id, 'google url', website_url) client_id, _, redirect_uri = heroku_client_info(request) query_string = urlencode(dict(client_id=client_id, redirect_uri=redirect_uri, response_type='code', scope='global', state=str(tarball_id))) logger.debug('POST /prepare-app redirect {}?{}'.format(heroku_authorize_url, query_string)) return redirect(heroku_authorize_url + '?' + query_string)
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 prepare_app(): ''' Prepare app, ask Heroku to authenticate, return to /callback-heroku. ''' view_id, website_url = request.form.get('property').split(' ', 1) name, email = request.form.get('name'), request.form.get('email') client_id = request.form.get('client_id') client_secret = request.form.get('client_secret') refresh_token = request.form.get('refresh_token') env = dict(LANG='en_US.UTF-8', RACK_ENV='production', GA_VIEW_ID=view_id, GA_WEBSITE_URL=website_url, CLIENT_ID=client_id, CLIENT_SECRET=client_secret, REFRESH_TOKEN=refresh_token) tarpath = prepare_tarball(display_screen_tarball_url, dict(name='Display Screen', env=env)) with psycopg2.connect(app.config['SQLALCHEMY_DATABASE_URI']) as connection: with connection.cursor() as cursor: tarball_id = builders.add_connection(cursor, email, name, website_url, tarpath) builders.set_connection_datum(cursor, tarball_id, 'google name', name) builders.set_connection_datum(cursor, tarball_id, 'google email', email) builders.set_connection_datum(cursor, tarball_id, 'google url', website_url) client_id, _, redirect_uri = heroku_client_info(request) query_string = urlencode( dict(client_id=client_id, redirect_uri=redirect_uri, response_type='code', scope='global', state=str(tarball_id))) logger.debug('POST /prepare-app redirect {}?{}'.format( heroku_authorize_url, query_string)) return redirect(heroku_authorize_url + '?' + query_string)
def prepare_app(): """ Prepare app, ask Heroku to authenticate, return to /callback-heroku. """ view_id, website_url = request.form.get("property").split(" ", 1) name, email = request.form.get("name"), request.form.get("email") client_id = request.form.get("client_id") client_secret = request.form.get("client_secret") refresh_token = request.form.get("refresh_token") env = dict( LANG="en_US.UTF-8", RACK_ENV="production", GA_VIEW_ID=view_id, GA_WEBSITE_URL=website_url, CLIENT_ID=client_id, CLIENT_SECRET=client_secret, REFRESH_TOKEN=refresh_token, ) tarpath = prepare_tarball(display_screen_tarball_url, dict(name="Display Screen", env=env)) with psycopg2.connect(app.config["SQLALCHEMY_DATABASE_URI"]) as connection: with connection.cursor() as cursor: tarball_id = builders.add_connection(cursor, email, name, website_url, tarpath) builders.set_connection_datum(cursor, tarball_id, "google name", name) builders.set_connection_datum(cursor, tarball_id, "google email", email) builders.set_connection_datum(cursor, tarball_id, "google url", website_url) client_id, _, redirect_uri = heroku_client_info(request) query_string = urlencode( dict( client_id=client_id, redirect_uri=redirect_uri, response_type="code", scope="global", state=str(tarball_id) ) ) logger.debug("POST /prepare-app redirect {}?{}".format(heroku_authorize_url, query_string)) return redirect(heroku_authorize_url + "?" + query_string)
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 prepare_app(): ''' Prepare app, ask Heroku to authenticate, return to /callback-heroku. ''' view_id, website_url = request.form.get('property').split(' ', 1) name, email = request.form.get('name'), request.form.get('email') client_id = request.form.get('client_id') client_secret = request.form.get('client_secret') refresh_token = request.form.get('refresh_token') organisation_name = request.form.get('organisation_name', 'LG Analytics Dashboard') shortcut_icon = request.form.get('shortcut_icon', '/favicon.ico') theme_colour = request.form.get('theme_colour', '#212121') title_filter = request.form.get('title_filter', ' | ') # Make sure these vars have values if not (organisation_name and organisation_name.strip()): organisation_name = 'LG Analytics Dashboard' if not (shortcut_icon and shortcut_icon.strip()): shortcut_icon = '/favicon.ico' if not (theme_colour and theme_colour.strip()): theme_colour = '#212121' if not (title_filter and title_filter.strip()): title_filter = ' | ' env = dict(LANG='en_US.UTF-8', RACK_ENV='production', GA_VIEW_ID=view_id, GA_WEBSITE_URL=website_url, CLIENT_ID=client_id, CLIENT_SECRET=client_secret, REFRESH_TOKEN=refresh_token, ORGANISATION_NAME=organisation_name, SHORTCUT_ICON=shortcut_icon, THEME_COLOUR=theme_colour, TITLE_FILTER=title_filter) tarpath = prepare_tarball(display_screen_tarball_url, dict(name='Display Screen', env=env)) logger.debug('GET /prepare-app {} {}'.format(app.config, env)) with psycopg2.connect(app.config['SQLALCHEMY_DATABASE_URI']) as connection: with connection.cursor() as cursor: tarball_id = builders.add_connection(cursor, email, name, website_url, tarpath) builders.set_connection_datum(cursor, tarball_id, 'google name', name) builders.set_connection_datum(cursor, tarball_id, 'google email', email) builders.set_connection_datum(cursor, tarball_id, 'google url', website_url) client_id, _, redirect_uri = heroku_client_info(request) query_string = urlencode( dict(client_id=client_id, redirect_uri=redirect_uri, response_type='code', scope='global', state=str(tarball_id))) logger.debug('POST /prepare-app redirect {}?{}'.format( heroku_authorize_url, query_string)) return redirect(heroku_authorize_url + '?' + query_string)