From ee5ea5153204d5eea16c9f2bb9158c1bc14ebc26 Mon Sep 17 00:00:00 2001 From: yackob03 Date: Wed, 25 Sep 2013 12:45:12 -0400 Subject: [PATCH] Refactor the code into modules, it was getting unweildy. --- app.py | 60 +-------------------------- auth/__init__.py | 0 auth.py => auth/auth.py | 25 ++++++++++- permissions.py => auth/permissions.py | 3 +- data/__init__.py | 0 database.py => data/database.py | 0 model.py => data/model.py | 0 endpoints/__init__.py | 0 api.py => endpoints/api.py | 3 +- index.py => endpoints/index.py | 17 ++++++-- endpoints/web.py | 30 ++++++++++++++ wsgi.py | 5 ++- 12 files changed, 73 insertions(+), 70 deletions(-) create mode 100644 auth/__init__.py rename auth.py => auth/auth.py (85%) rename permissions.py => auth/permissions.py (99%) create mode 100644 data/__init__.py rename database.py => data/database.py (100%) rename model.py => data/model.py (100%) create mode 100644 endpoints/__init__.py rename api.py => endpoints/api.py (98%) rename index.py => endpoints/index.py (92%) create mode 100644 endpoints/web.py diff --git a/app.py b/app.py index 96243e9bc..e6b009e1a 100644 --- a/app.py +++ b/app.py @@ -1,11 +1,9 @@ import logging -from flask import (Flask, make_response, request, abort, send_file, - redirect, url_for) +from flask import Flask 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__) logger = logging.getLogger(__name__) @@ -17,57 +15,3 @@ app.secret_key = '1cb18882-6d12-440d-a4cc-b7430fb5f884' login_manager = LoginManager() login_manager.init_app(app) 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 diff --git a/auth/__init__.py b/auth/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/auth.py b/auth/auth.py similarity index 85% rename from auth.py rename to auth/auth.py index b3cb6d59f..9c2ecf7dd 100644 --- a/auth.py +++ b/auth/auth.py @@ -3,10 +3,11 @@ import logging from functools import wraps from flask import request, make_response, _request_ctx_stack, abort from flask.ext.principal import identity_changed, Identity +from flask.ext.login import UserMixin from base64 import b64decode -import model -from app import app +from data import model +from app import app, login_manager from util import parse_namespace_repository @@ -14,6 +15,26 @@ from util import parse_namespace_repository 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(): return getattr(_request_ctx_stack.top, 'authenticated_user', None) diff --git a/permissions.py b/auth/permissions.py similarity index 99% rename from permissions.py rename to auth/permissions.py index 24733d7b2..4408387eb 100644 --- a/permissions.py +++ b/auth/permissions.py @@ -3,8 +3,7 @@ import logging from flask.ext.principal import identity_loaded, UserNeed, Permission from collections import namedtuple -import model - +from data import model from app import app from auth import get_authenticated_user, get_validated_token diff --git a/data/__init__.py b/data/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/database.py b/data/database.py similarity index 100% rename from database.py rename to data/database.py diff --git a/model.py b/data/model.py similarity index 100% rename from model.py rename to data/model.py diff --git a/endpoints/__init__.py b/endpoints/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/api.py b/endpoints/api.py similarity index 98% rename from api.py rename to endpoints/api.py index b064438f8..232c77d84 100644 --- a/api.py +++ b/endpoints/api.py @@ -4,8 +4,7 @@ from flask import request, make_response, jsonify, abort from flask.ext.login import login_required, current_user from functools import wraps -import model - +from data import model from app import app from util import parse_repository_name diff --git a/index.py b/endpoints/index.py similarity index 92% rename from index.py rename to endpoints/index.py index adbdf6544..f94080508 100644 --- a/index.py +++ b/endpoints/index.py @@ -6,13 +6,14 @@ import logging from flask import request, make_response, jsonify, abort from functools import wraps +from data import model 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 permissions import (ModifyRepositoryPermission, ReadRepositoryPermission, - UserPermission) +from auth.permissions import (ModifyRepositoryPermission, + ReadRepositoryPermission, UserPermission) -import model logger = logging.getLogger(__name__) @@ -197,3 +198,11 @@ def put_repository_auth(namespace, repository): @app.route('/v1/search', methods=['GET']) def get_search(): 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 diff --git a/endpoints/web.py b/endpoints/web.py new file mode 100644 index 000000000..cd76e31e2 --- /dev/null +++ b/endpoints/web.py @@ -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') \ No newline at end of file diff --git a/wsgi.py b/wsgi.py index 411075378..f46fe3444 100644 --- a/wsgi.py +++ b/wsgi.py @@ -2,8 +2,9 @@ import logging from app import app -import index -import api +import endpoints.index +import endpoints.api +import endpoints.web if __name__ == '__main__': FORMAT = '%(asctime)-15s - %(levelname)s - %(pathname)s - ' + \