Code review fixes

This commit is contained in:
Joseph Schorr 2014-09-04 17:54:51 -04:00
parent 11176215e1
commit 1c2de35f28
3 changed files with 20 additions and 26 deletions

View file

@ -39,7 +39,6 @@ def user_view(user):
organizations = model.get_user_organizations(user.username)
def login_view(login):
print login.metadata_json
try:
metadata = json.loads(login.metadata_json)
except:

View file

@ -4,7 +4,7 @@ from flask import request, redirect, url_for, Blueprint
from flask.ext.login import current_user
from endpoints.common import render_page_template, common_login, route_show_if
from app import app, analytics
from app import app, analytics, get_app_url
from data import model
from util.names import parse_repository_name
from util.validation import generate_valid_usernames
@ -22,6 +22,11 @@ client = app.config['HTTPCLIENT']
callback = Blueprint('callback', __name__)
def render_ologin_error(service_name,
error_message='Could not load user data. The token may have expired.'):
return render_page_template('ologinerror.html', service_name=service_name,
error_message=error_message,
service_url=get_app_url())
def exchange_code_for_token(code, service_name='GITHUB', for_login=True, form_encode=False,
redirect_suffix=''):
@ -96,15 +101,12 @@ def conduct_oauth_login(service_name, user_id, username, email, metadata={}):
analytics.alias(to_login.username, state)
except model.DataModelException, ex:
return render_page_template('ologinerror.html', service_name=service_name,
error_message=ex.message)
return render_ologin_error(service_name, ex.message)
if common_login(to_login):
return redirect(url_for('web.index'))
return render_page_template('ologinerror.html', service_name=service_name,
error_message='Unknown error')
return render_ologin_error(service_name)
def get_google_username(user_data):
username = user_data['email']
@ -120,17 +122,16 @@ def get_google_username(user_data):
def google_oauth_callback():
error = request.args.get('error', None)
if error:
return render_page_template('ologinerror.html', service_name='Google', error_message=error)
return render_ologin_error('Google', error)
token = exchange_code_for_token(request.args.get('code'), service_name='GOOGLE', form_encode=True)
user_data = get_google_user(token)
if not user_data or not user_data.get('id', None) or not user_data.get('email', None):
return render_page_template('ologinerror.html', service_name = 'Google',
error_message='Could not load user data')
return render_ologin_error('Google')
username = get_google_username(user_data)
metadata = {
'service_username': username
'service_username': user_data['email']
}
return conduct_oauth_login('Google', user_data['id'], username, user_data['email'],
@ -142,13 +143,12 @@ def google_oauth_callback():
def github_oauth_callback():
error = request.args.get('error', None)
if error:
return render_page_template('ologinerror.html', service_name = 'GitHub', error_message=error)
return render_ologin_error('GitHub', error)
token = exchange_code_for_token(request.args.get('code'), service_name='GITHUB')
user_data = get_github_user(token)
if not user_data:
return render_page_template('ologinerror.html', service_name = 'GitHub',
error_message='Could not load user data')
return render_ologin_error('GitHub')
username = user_data['login']
github_id = user_data['id']
@ -186,15 +186,14 @@ def google_oauth_attach():
user_data = get_google_user(token)
if not user_data or not user_data.get('id', None):
return render_page_template('ologinerror.html', service_name = 'Google',
error_message='Could not load user data')
return render_ologin_error('Google')
google_id = user_data['id']
user_obj = current_user.db_user()
username = get_google_username(user_data)
metadata = {
'service_username': username
'service_username': user_data['email']
}
try:
@ -202,9 +201,7 @@ def google_oauth_attach():
except IntegrityError:
err = 'Google account %s is already attached to a %s account' % (
username, app.config['REGISTRY_TITLE_SHORT'])
return render_page_template('ologinerror.html', service_name = 'Google',
error_message=err)
return render_ologin_error('Google', err)
return redirect(url_for('web.user'))
@ -216,8 +213,7 @@ def github_oauth_attach():
token = exchange_code_for_token(request.args.get('code'), service_name='GITHUB')
user_data = get_github_user(token)
if not user_data:
return render_page_template('ologinerror.html', service_name = 'GitHub',
error_message='Could not load user data')
return render_ologin_error('GitHub')
github_id = user_data['id']
user_obj = current_user.db_user()
@ -233,8 +229,7 @@ def github_oauth_attach():
err = 'Github account %s is already attached to a %s account' % (
username, app.config['REGISTRY_TITLE_SHORT'])
return render_page_template('ologinerror.html', service_name = 'Github',
error_message=err)
return render_ologin_error('GitHub', err)
return redirect(url_for('web.user'))

View file

@ -15,7 +15,7 @@
{% endif %}
<div>
Please register using the <a href="/">registration form</a> to continue.
Please register using the <a ng-href="{{ service_url }}/signin" target="_self">registration form</a> to continue.
You will be able to connect your account to your Quay.io account
in the user settings.
</div>