Refactor the code into modules, it was getting unweildy.
This commit is contained in:
parent
2611d70185
commit
ee5ea51532
12 changed files with 73 additions and 70 deletions
60
app.py
60
app.py
|
@ -1,11 +1,9 @@
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from flask import (Flask, make_response, request, abort, send_file,
|
from flask import Flask
|
||||||
redirect, url_for)
|
|
||||||
from flask.ext.principal import Principal
|
from flask.ext.principal import Principal
|
||||||
from flask.ext.login import login_user, LoginManager, UserMixin
|
from flask.ext.login import LoginManager
|
||||||
|
|
||||||
import model
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -17,57 +15,3 @@ app.secret_key = '1cb18882-6d12-440d-a4cc-b7430fb5f884'
|
||||||
login_manager = LoginManager()
|
login_manager = LoginManager()
|
||||||
login_manager.init_app(app)
|
login_manager.init_app(app)
|
||||||
login_manager.login_view = 'signin'
|
login_manager.login_view = 'signin'
|
||||||
|
|
||||||
|
|
||||||
class _LoginWrappedDBUser(UserMixin):
|
|
||||||
def __init__(self, db_user):
|
|
||||||
self.db_user = db_user
|
|
||||||
|
|
||||||
def is_active(self):
|
|
||||||
return self.db_user.verified
|
|
||||||
|
|
||||||
def get_id(self):
|
|
||||||
return unicode(self.db_user.username)
|
|
||||||
|
|
||||||
|
|
||||||
@login_manager.user_loader
|
|
||||||
def load_user(username):
|
|
||||||
db_user = model.get_user(username)
|
|
||||||
if db_user:
|
|
||||||
return _LoginWrappedDBUser(db_user)
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/', methods=['GET'])
|
|
||||||
def index():
|
|
||||||
return send_file('templates/index.html')
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/signin', methods=['POST'])
|
|
||||||
def signin():
|
|
||||||
username = request.form['username']
|
|
||||||
password = request.form['password']
|
|
||||||
|
|
||||||
#TODO Allow email login
|
|
||||||
verified = model.verify_user(username, password)
|
|
||||||
if verified:
|
|
||||||
logger.debug('Successfully signed in as: %s' % username)
|
|
||||||
|
|
||||||
login_user(_LoginWrappedDBUser(verified))
|
|
||||||
return redirect(request.args.get('next') or url_for('index'))
|
|
||||||
|
|
||||||
abort(403)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/signin', methods=['GET'])
|
|
||||||
def render_signin_page():
|
|
||||||
return send_file('templates/signin.html')
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/_ping')
|
|
||||||
@app.route('/v1/_ping')
|
|
||||||
def ping():
|
|
||||||
response = make_response('true', 200)
|
|
||||||
response.headers['X-Docker-Registry-Version'] = '0.6.0'
|
|
||||||
return response
|
|
||||||
|
|
0
auth/__init__.py
Normal file
0
auth/__init__.py
Normal file
|
@ -3,10 +3,11 @@ import logging
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from flask import request, make_response, _request_ctx_stack, abort
|
from flask import request, make_response, _request_ctx_stack, abort
|
||||||
from flask.ext.principal import identity_changed, Identity
|
from flask.ext.principal import identity_changed, Identity
|
||||||
|
from flask.ext.login import UserMixin
|
||||||
from base64 import b64decode
|
from base64 import b64decode
|
||||||
|
|
||||||
import model
|
from data import model
|
||||||
from app import app
|
from app import app, login_manager
|
||||||
|
|
||||||
from util import parse_namespace_repository
|
from util import parse_namespace_repository
|
||||||
|
|
||||||
|
@ -14,6 +15,26 @@ from util import parse_namespace_repository
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class _LoginWrappedDBUser(UserMixin):
|
||||||
|
def __init__(self, db_user):
|
||||||
|
self.db_user = db_user
|
||||||
|
|
||||||
|
def is_active(self):
|
||||||
|
return self.db_user.verified
|
||||||
|
|
||||||
|
def get_id(self):
|
||||||
|
return unicode(self.db_user.username)
|
||||||
|
|
||||||
|
|
||||||
|
@login_manager.user_loader
|
||||||
|
def load_user(username):
|
||||||
|
db_user = model.get_user(username)
|
||||||
|
if db_user:
|
||||||
|
return _LoginWrappedDBUser(db_user)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_authenticated_user():
|
def get_authenticated_user():
|
||||||
return getattr(_request_ctx_stack.top, 'authenticated_user', None)
|
return getattr(_request_ctx_stack.top, 'authenticated_user', None)
|
||||||
|
|
|
@ -3,8 +3,7 @@ import logging
|
||||||
from flask.ext.principal import identity_loaded, UserNeed, Permission
|
from flask.ext.principal import identity_loaded, UserNeed, Permission
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
import model
|
from data import model
|
||||||
|
|
||||||
from app import app
|
from app import app
|
||||||
from auth import get_authenticated_user, get_validated_token
|
from auth import get_authenticated_user, get_validated_token
|
||||||
|
|
0
data/__init__.py
Normal file
0
data/__init__.py
Normal file
0
endpoints/__init__.py
Normal file
0
endpoints/__init__.py
Normal file
|
@ -4,8 +4,7 @@ from flask import request, make_response, jsonify, abort
|
||||||
from flask.ext.login import login_required, current_user
|
from flask.ext.login import login_required, current_user
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
import model
|
from data import model
|
||||||
|
|
||||||
from app import app
|
from app import app
|
||||||
from util import parse_repository_name
|
from util import parse_repository_name
|
||||||
|
|
|
@ -6,13 +6,14 @@ import logging
|
||||||
from flask import request, make_response, jsonify, abort
|
from flask import request, make_response, jsonify, abort
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
|
from data import model
|
||||||
from app import app
|
from app import app
|
||||||
from auth import process_auth, get_authenticated_user, get_validated_token
|
from auth.auth import (process_auth, get_authenticated_user,
|
||||||
|
get_validated_token)
|
||||||
from util import parse_namespace_repository, parse_repository_name
|
from util import parse_namespace_repository, parse_repository_name
|
||||||
from permissions import (ModifyRepositoryPermission, ReadRepositoryPermission,
|
from auth.permissions import (ModifyRepositoryPermission,
|
||||||
UserPermission)
|
ReadRepositoryPermission, UserPermission)
|
||||||
|
|
||||||
import model
|
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -197,3 +198,11 @@ def put_repository_auth(namespace, repository):
|
||||||
@app.route('/v1/search', methods=['GET'])
|
@app.route('/v1/search', methods=['GET'])
|
||||||
def get_search():
|
def get_search():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/_ping')
|
||||||
|
@app.route('/v1/_ping')
|
||||||
|
def ping():
|
||||||
|
response = make_response('true', 200)
|
||||||
|
response.headers['X-Docker-Registry-Version'] = '0.6.0'
|
||||||
|
return response
|
30
endpoints/web.py
Normal file
30
endpoints/web.py
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
from flask import abort, send_file, redirect, request
|
||||||
|
|
||||||
|
from data import model
|
||||||
|
from app import app
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/', methods=['GET'])
|
||||||
|
def index():
|
||||||
|
return send_file('templates/index.html')
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/signin', methods=['POST'])
|
||||||
|
def signin():
|
||||||
|
username = request.form['username']
|
||||||
|
password = request.form['password']
|
||||||
|
|
||||||
|
#TODO Allow email login
|
||||||
|
verified = model.verify_user(username, password)
|
||||||
|
if verified:
|
||||||
|
logger.debug('Successfully signed in as: %s' % username)
|
||||||
|
|
||||||
|
login_user(_LoginWrappedDBUser(verified))
|
||||||
|
return redirect(request.args.get('next') or url_for('index'))
|
||||||
|
|
||||||
|
abort(403)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/signin', methods=['GET'])
|
||||||
|
def render_signin_page():
|
||||||
|
return send_file('templates/signin.html')
|
5
wsgi.py
5
wsgi.py
|
@ -2,8 +2,9 @@ import logging
|
||||||
|
|
||||||
from app import app
|
from app import app
|
||||||
|
|
||||||
import index
|
import endpoints.index
|
||||||
import api
|
import endpoints.api
|
||||||
|
import endpoints.web
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
FORMAT = '%(asctime)-15s - %(levelname)s - %(pathname)s - ' + \
|
FORMAT = '%(asctime)-15s - %(levelname)s - %(pathname)s - ' + \
|
||||||
|
|
Reference in a new issue