Embed the discovery information directly into the page
This commit is contained in:
parent
619f3abc16
commit
3d899b9f95
4 changed files with 100 additions and 69 deletions
|
@ -26,7 +26,7 @@ from auth.permissions import (ReadRepositoryPermission,
|
||||||
OrganizationMemberPermission,
|
OrganizationMemberPermission,
|
||||||
ViewTeamPermission)
|
ViewTeamPermission)
|
||||||
from endpoints import registry
|
from endpoints import registry
|
||||||
from endpoints.web import common_login
|
from endpoints.common import common_login
|
||||||
from util.cache import cache_control
|
from util.cache import cache_control
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
|
@ -35,6 +35,38 @@ store = app.config['STORAGE']
|
||||||
user_files = app.config['USERFILES']
|
user_files = app.config['USERFILES']
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
route_data = None
|
||||||
|
|
||||||
|
def get_route_data():
|
||||||
|
global route_data
|
||||||
|
if route_data:
|
||||||
|
return route_data
|
||||||
|
|
||||||
|
routes = []
|
||||||
|
for rule in app.url_map.iter_rules():
|
||||||
|
if rule.rule.startswith('/api/'):
|
||||||
|
endpoint_method = globals()[rule.endpoint]
|
||||||
|
is_internal = '__internal_call' in dir(endpoint_method)
|
||||||
|
is_org_api = '__user_call' in dir(endpoint_method)
|
||||||
|
methods = list(rule.methods.difference(['HEAD', 'OPTIONS']))
|
||||||
|
|
||||||
|
route = {
|
||||||
|
'name': rule.endpoint,
|
||||||
|
'methods': methods,
|
||||||
|
'path': rule.rule,
|
||||||
|
'parameters': list(rule.arguments)
|
||||||
|
}
|
||||||
|
|
||||||
|
if is_org_api:
|
||||||
|
route['user_method'] = endpoint_method.__user_call
|
||||||
|
|
||||||
|
routes.append(route)
|
||||||
|
|
||||||
|
route_data = {
|
||||||
|
'endpoints': routes
|
||||||
|
}
|
||||||
|
return route_data
|
||||||
|
|
||||||
|
|
||||||
def log_action(kind, user_or_orgname, metadata={}, repo=None):
|
def log_action(kind, user_or_orgname, metadata={}, repo=None):
|
||||||
performer = current_user.db_user()
|
performer = current_user.db_user()
|
||||||
|
@ -92,29 +124,8 @@ def handle_dme_key_error(ex):
|
||||||
|
|
||||||
@app.route('/api/discovery')
|
@app.route('/api/discovery')
|
||||||
def discovery():
|
def discovery():
|
||||||
routes = []
|
return jsonify(get_route_data())
|
||||||
for rule in app.url_map.iter_rules():
|
|
||||||
if rule.rule.startswith('/api/'):
|
|
||||||
endpoint_method = globals()[rule.endpoint]
|
|
||||||
is_internal = '__internal_call' in dir(endpoint_method)
|
|
||||||
is_org_api = '__user_call' in dir(endpoint_method)
|
|
||||||
methods = list(rule.methods.difference(['HEAD', 'OPTIONS']))
|
|
||||||
|
|
||||||
route = {
|
|
||||||
'name': rule.endpoint,
|
|
||||||
'methods': methods,
|
|
||||||
'path': rule.rule,
|
|
||||||
'parameters': list(rule.arguments)
|
|
||||||
}
|
|
||||||
|
|
||||||
if is_org_api:
|
|
||||||
route['user_method'] = endpoint_method.__user_call
|
|
||||||
|
|
||||||
routes.append(route)
|
|
||||||
|
|
||||||
return jsonify({
|
|
||||||
'endpoints': routes
|
|
||||||
})
|
|
||||||
|
|
||||||
@app.route('/api/')
|
@app.route('/api/')
|
||||||
@internal_api_call
|
@internal_api_call
|
||||||
|
|
52
endpoints/common.py
Normal file
52
endpoints/common.py
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
import logging
|
||||||
|
import requests
|
||||||
|
import stripe
|
||||||
|
|
||||||
|
from flask import (abort, redirect, request, url_for, render_template,
|
||||||
|
make_response, Response)
|
||||||
|
from flask.ext.login import login_user, UserMixin, current_user
|
||||||
|
from flask.ext.principal import identity_changed
|
||||||
|
from urlparse import urlparse
|
||||||
|
|
||||||
|
from data import model
|
||||||
|
from app import app, login_manager
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@login_manager.user_loader
|
||||||
|
def load_user(username):
|
||||||
|
logger.debug('Loading user: %s' % username)
|
||||||
|
return _LoginWrappedDBUser(username)
|
||||||
|
|
||||||
|
class _LoginWrappedDBUser(UserMixin):
|
||||||
|
def __init__(self, db_username, db_user=None):
|
||||||
|
|
||||||
|
self._db_username = db_username
|
||||||
|
self._db_user = db_user
|
||||||
|
|
||||||
|
def db_user(self):
|
||||||
|
if not self._db_user:
|
||||||
|
self._db_user = model.get_user(self._db_username)
|
||||||
|
return self._db_user
|
||||||
|
|
||||||
|
def is_authenticated(self):
|
||||||
|
return self.db_user() is not None
|
||||||
|
|
||||||
|
def is_active(self):
|
||||||
|
return self.db_user().verified
|
||||||
|
|
||||||
|
def get_id(self):
|
||||||
|
return unicode(self._db_username)
|
||||||
|
|
||||||
|
|
||||||
|
def common_login(db_user):
|
||||||
|
if login_user(_LoginWrappedDBUser(db_user.username, db_user)):
|
||||||
|
logger.debug('Successfully signed in as: %s' % db_user.username)
|
||||||
|
new_identity = QuayDeferredPermissionUser(db_user.username, 'username')
|
||||||
|
identity_changed.send(app, identity=new_identity)
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
logger.debug('User could not be logged in, inactive?.')
|
||||||
|
return False
|
|
@ -14,43 +14,19 @@ from auth.permissions import (QuayDeferredPermissionUser,
|
||||||
AdministerOrganizationPermission)
|
AdministerOrganizationPermission)
|
||||||
from util.invoice import renderInvoiceToPdf
|
from util.invoice import renderInvoiceToPdf
|
||||||
from util.seo import render_snapshot
|
from util.seo import render_snapshot
|
||||||
|
from endpoints.api import get_route_data
|
||||||
|
from endpoints.common import common_login
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
def render_page_template(name):
|
||||||
class _LoginWrappedDBUser(UserMixin):
|
return render_template(name, route_data = get_route_data())
|
||||||
def __init__(self, db_username, db_user=None):
|
|
||||||
|
|
||||||
self._db_username = db_username
|
|
||||||
self._db_user = db_user
|
|
||||||
|
|
||||||
def db_user(self):
|
|
||||||
if not self._db_user:
|
|
||||||
self._db_user = model.get_user(self._db_username)
|
|
||||||
return self._db_user
|
|
||||||
|
|
||||||
def is_authenticated(self):
|
|
||||||
return self.db_user() is not None
|
|
||||||
|
|
||||||
def is_active(self):
|
|
||||||
return self.db_user().verified
|
|
||||||
|
|
||||||
def get_id(self):
|
|
||||||
return unicode(self._db_username)
|
|
||||||
|
|
||||||
|
|
||||||
@login_manager.user_loader
|
|
||||||
def load_user(username):
|
|
||||||
logger.debug('Loading user: %s' % username)
|
|
||||||
return _LoginWrappedDBUser(username)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/', methods=['GET'], defaults={'path': ''})
|
@app.route('/', methods=['GET'], defaults={'path': ''})
|
||||||
@app.route('/repository/<path:path>', methods=['GET'])
|
@app.route('/repository/<path:path>', methods=['GET'])
|
||||||
@app.route('/organization/<path:path>', methods=['GET'])
|
@app.route('/organization/<path:path>', methods=['GET'])
|
||||||
def index(path):
|
def index(path):
|
||||||
return render_template('index.html')
|
return render_page_template('index.html')
|
||||||
|
|
||||||
|
|
||||||
@app.route('/snapshot', methods=['GET'])
|
@app.route('/snapshot', methods=['GET'])
|
||||||
|
@ -119,17 +95,17 @@ def status():
|
||||||
|
|
||||||
@app.route('/tos', methods=['GET'])
|
@app.route('/tos', methods=['GET'])
|
||||||
def tos():
|
def tos():
|
||||||
return render_template('tos.html')
|
return render_page_template('tos.html')
|
||||||
|
|
||||||
|
|
||||||
@app.route('/disclaimer', methods=['GET'])
|
@app.route('/disclaimer', methods=['GET'])
|
||||||
def disclaimer():
|
def disclaimer():
|
||||||
return render_template('disclaimer.html')
|
return render_page_template('disclaimer.html')
|
||||||
|
|
||||||
|
|
||||||
@app.route('/privacy', methods=['GET'])
|
@app.route('/privacy', methods=['GET'])
|
||||||
def privacy():
|
def privacy():
|
||||||
return render_template('privacy.html')
|
return render_page_template('privacy.html')
|
||||||
|
|
||||||
|
|
||||||
@app.route('/receipt', methods=['GET'])
|
@app.route('/receipt', methods=['GET'])
|
||||||
|
@ -162,17 +138,6 @@ def receipt():
|
||||||
abort(404)
|
abort(404)
|
||||||
|
|
||||||
|
|
||||||
def common_login(db_user):
|
|
||||||
if login_user(_LoginWrappedDBUser(db_user.username, db_user)):
|
|
||||||
logger.debug('Successfully signed in as: %s' % db_user.username)
|
|
||||||
new_identity = QuayDeferredPermissionUser(db_user.username, 'username')
|
|
||||||
identity_changed.send(app, identity=new_identity)
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
logger.debug('User could not be logged in, inactive?.')
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/oauth2/github/callback', methods=['GET'])
|
@app.route('/oauth2/github/callback', methods=['GET'])
|
||||||
def github_oauth_callback():
|
def github_oauth_callback():
|
||||||
code = request.args.get('code')
|
code = request.args.get('code')
|
||||||
|
@ -228,12 +193,12 @@ def github_oauth_callback():
|
||||||
mixpanel.alias(to_login.username, state)
|
mixpanel.alias(to_login.username, state)
|
||||||
|
|
||||||
except model.DataModelException, ex:
|
except model.DataModelException, ex:
|
||||||
return render_template('githuberror.html', error_message=ex.message)
|
return render_page_template('githuberror.html', error_message=ex.message)
|
||||||
|
|
||||||
if common_login(to_login):
|
if common_login(to_login):
|
||||||
return redirect(url_for('index'))
|
return redirect(url_for('index'))
|
||||||
|
|
||||||
return render_template('githuberror.html')
|
return render_page_template('githuberror.html')
|
||||||
|
|
||||||
|
|
||||||
@app.route('/confirm', methods=['GET'])
|
@app.route('/confirm', methods=['GET'])
|
||||||
|
|
|
@ -65,7 +65,10 @@
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
<script src="static/js/bootstrap.js"></script>
|
<script type="text/javascript">
|
||||||
|
window.__endpoints = {{ route_data|safe }}.endpoints;
|
||||||
|
</script>
|
||||||
|
|
||||||
<script src="static/js/app.js"></script>
|
<script src="static/js/app.js"></script>
|
||||||
<script src="static/js/controllers.js"></script>
|
<script src="static/js/controllers.js"></script>
|
||||||
<script src="static/js/graphing.js"></script>
|
<script src="static/js/graphing.js"></script>
|
||||||
|
|
Reference in a new issue