Skip to content
Browse files

Merge branch 'release/0.2.14'

  • Loading branch information...
2 parents 76d328a + de0bec0 commit 2212dfc048ee845792c43140e249ba6ef56ec498 @klen committed Aug 4, 2012
View
4 Changelog
@@ -1,8 +1,8 @@
2012-08-04 klen
- * Version 0.2.12
+ * Version 0.2.14
* Add 'APPS' option to production config
- * Add simple feature for create manage commands
+ * Add simple feature for control apps
2012-07-29 klen
View
2 base/__init__.py
@@ -1 +1 @@
-__version__ = "0.2.12"
+__version__ = "0.2.14"
View
30 base/app.py
@@ -1,28 +1,34 @@
-from flask import Flask, render_template
+import importlib
+from flask import Flask
from .config import production
-from .utils import load_modules
def create_app(config=None, **skip):
app = Flask(__name__)
app.config.from_object(config or production)
app.config.from_envvar("APP_SETTINGS", silent=True)
- app.errorhandler(404)(lambda e: (render_template('404.html'), 404))
-
from .ext import config_extensions
config_extensions(app)
- config_blueprints(app)
- return app
+ for mod in load_modules('register', app):
+ try:
+ mod.register_app(app)
+ except AttributeError:
+ app.logger.error('Invalid mod: %s' % mod)
+ return app
-def config_blueprints(app):
- # Main urlconfig
- from .views import urls
- app.register_blueprint(urls)
+def load_modules(name, app=None):
+ " Load modules by apps. "
- for views in load_modules('views'):
- app.register_blueprint(views.blueprint)
+ apps = app and app.config.get('APPS') or production.APPS
+ mods = []
+ for app in apps:
+ try:
+ mods.append(importlib.import_module('base.%s.%s' % (app, name)))
+ except ImportError:
+ continue
+ return mods
View
5 base/auth/models.py
@@ -4,8 +4,9 @@
from sqlalchemy.ext.hybrid import hybrid_property
from werkzeug import check_password_hash, generate_password_hash
-from ..core import BaseMixin
-from ..ext import db, admin
+from ..core.models import BaseMixin
+from ..core.admin import admin
+from ..ext import db
userroles = db.Table(
View
18 base/oauth.py → base/auth/oauth.py
@@ -1,15 +1,23 @@
from flask import url_for, request, flash, redirect
-from flaskext.babel import lazy_gettext as _
from flask_login import current_user
+from flaskext.babel import lazy_gettext as _
+from flaskext.oauth import OAuth
from random import choice
+from ..ext import db
+from .models import User
+from .views import users
+
ASCII_LOWERCASE = 'abcdefghijklmnopqrstuvwxyz'
PROVIDERS = 'twitter',
CLIENTS = dict()
-def config_oauth(oauth, app):
+oauth = OAuth()
+
+
+def config_oauth(app):
" Configure oauth support. "
for name in PROVIDERS:
@@ -61,10 +69,6 @@ class OAuthTwitter(OAuthBase):
name = 'twitter'
def authorize(self, resp):
- from .ext import db
- from .auth.models import User
- from .auth.views import blueprint
-
next_url = request.args.get('next') or url_for('urls.index')
if resp is None:
flash(_(u'You denied the request to sign in.'))
@@ -84,7 +88,7 @@ def authorize(self, resp):
user.oauth_secret = resp['oauth_token_secret']
db.session.commit()
- blueprint.login(user)
+ users.login(user)
flash(_('Welcome %(user)s', user=user.username))
return redirect(next_url)
View
8 base/auth/register.py
@@ -0,0 +1,8 @@
+def register_app(app):
+ " Configure application. "
+
+ from .views import users
+ app.register_blueprint(users)
+
+ from .oauth import config_oauth
+ config_oauth(app)
View
2 base/auth/templates/users/profile.html
@@ -1,4 +1,4 @@
-{% extends "base.html" %}
+{% extends "core/base.html" %}
{% block page_content %}
<b>{{ _('Welcome') }} {{ current_user.username }}!</b>
View
2 base/auth/templates/users/register.html
@@ -1,4 +1,4 @@
-{% extends "base.html" %}
+{% extends "core/base.html" %}
{% block page_content %}
<div class="row">
View
24 base/auth/views.py
@@ -7,16 +7,16 @@
from .models import User
-blueprint = UserManager('users', __name__, url_prefix='/users', template_folder='templates')
+users = UserManager('users', __name__, url_prefix='/users', template_folder='templates')
-@blueprint.route('/profile/')
-@blueprint.login_required
+@users.route('/profile/')
+@users.login_required
def profile():
return render_template("users/profile.html")
-@blueprint.route('/login/', methods=['POST'])
+@users.route('/login/', methods=['POST'])
def login():
" View function which handles an authentication request. "
form = LoginForm(request.form)
@@ -25,22 +25,22 @@ def login():
user = User.query.filter_by(email=form.email.data).first()
# we use werzeug to validate user's password
if user and user.check_password(form.password.data):
- blueprint.login(user)
+ users.login(user)
flash(_('Welcome %(user)s', user=user.username))
return redirect(url_for('users.profile'))
flash(_('Wrong email or password'), 'error-message')
- return redirect(request.referrer or url_for(blueprint._login_manager.login_view))
+ return redirect(request.referrer or url_for(users._login_manager.login_view))
-@blueprint.route('/logout/', methods=['GET'])
-@blueprint.login_required
+@users.route('/logout/', methods=['GET'])
+@users.login_required
def logout():
" View function which handles a logout request. "
- blueprint.logout()
- return redirect(request.referrer or url_for(blueprint._login_manager.login_view))
+ users.logout()
+ return redirect(request.referrer or url_for(users._login_manager.login_view))
-@blueprint.route('/register/', methods=['GET', 'POST'])
+@users.route('/register/', methods=['GET', 'POST'])
def register():
" Registration Form. "
form = RegisterForm(request.form)
@@ -55,7 +55,7 @@ def register():
db.session.add(user)
db.session.commit()
- blueprint.login(user)
+ users.login(user)
# flash will display a message to the user
flash(_('Thanks for registering'))
View
2 base/config/core.py
@@ -15,4 +15,4 @@
BABEL_DEFAULT_LOCALE = 'en'
# APPS
-APPS = 'auth',
+APPS = 'core', 'auth'
View
1 base/core/__init__.py
@@ -0,0 +1 @@
+" base.core "
View
7 base/admin.py → base/core/admin.py
@@ -2,6 +2,8 @@
from flask_admin.contrib.sqlamodel import ModelView
from flask_login import current_user
+from ..ext import db
+
class StaffAdminView(AdminIndexView):
" Staff admin home page. "
@@ -29,7 +31,8 @@ def init_app(self, app):
super(FlaskAdmin, self).init_app(app)
def add_model(self, model, view=None, role='admin', **kwargs):
- from base.ext import db
-
view = view or AuthModelView
self.add_view(view(model, db.session))
+
+
+admin = FlaskAdmin()
View
0 base/core.py → base/core/models.py
File renamed without changes.
View
15 base/core/register.py
@@ -0,0 +1,15 @@
+def register_app(app):
+ " Configure application. "
+
+ from .views import urls
+ app.register_blueprint(urls)
+
+ from .admin import admin
+ admin.init_app(app)
+
+ from flask_bootstrap import Bootstrap
+ bootstrap = Bootstrap()
+ bootstrap.init_app(app)
+
+ from flask import render_template
+ app.errorhandler(404)(lambda e: (render_template('core/404.html'), 404))
View
16 base/script.py → base/core/script.py
@@ -1,15 +1,13 @@
from flaskext.script import prompt_bool
-from .ext import manager
-from .utils import load_modules
-
-
-load_modules('script')
+from ..ext import manager
@manager.command
def create_db():
- from base.ext import db
+ " Create database. "
+
+ from ..ext import db
db.create_all()
# Evolution support
@@ -23,7 +21,9 @@ def create_db():
@manager.command
def drop_db():
- from base.ext import db
+ " Drop all tables. "
+
+ from ..ext import db
if prompt_bool("Are you sure? You will lose all your data!"):
db.drop_all()
@@ -39,6 +39,8 @@ def drop_db():
@manager.command
def reset_db():
+ " Drop and create all tables. "
+
drop_db()
create_db()
View
2 base/templates/404.html → base/core/templates/core/404.html
@@ -1,4 +1,4 @@
-{% extends "base.html" %}
+{% extends "core/base.html" %}
{% block page_content %}
<summary><h1>{{ _('Page not found.') }}</h1></summary>
View
2 base/templates/500.html → base/core/templates/core/500.html
@@ -1,4 +1,4 @@
-{% extends "base.html" %}
+{% extends "core/base.html" %}
{% block page_content %}
<p>{{ _('The server encountered an unexpected error.') }}</p>
View
4 base/templates/base.html → base/core/templates/core/base.html
@@ -4,10 +4,10 @@
{# Navbar #}
{% block navigation %}
- {% include 'blocks/nav.html' %}
+ {% include 'core/blocks/nav.html' %}
{% endblock %}
- {% include "blocks/messages.html" %}
+ {% include "core/blocks/messages.html" %}
<div class="container">
{% block page_content %}{% endblock %}
View
0 base/templates/blocks/messages.html → .../core/templates/core/blocks/messages.html
File renamed without changes.
View
0 base/templates/blocks/nav.html → base/core/templates/core/blocks/nav.html
File renamed without changes.
View
2 base/templates/index.html → base/core/templates/core/index.html
@@ -1,4 +1,4 @@
-{% extends "base.html" %}
+{% extends "core/base.html" %}
{% block page_content %}
View
12 base/core/views.py
@@ -0,0 +1,12 @@
+from flask import render_template, Blueprint
+
+from ..auth.forms import LoginForm
+
+
+urls = Blueprint('urls', __name__, template_folder='templates')
+
+
+@urls.route('/')
+def index():
+ " Main page. "
+ return render_template('core/index.html', loginform=LoginForm())
View
12 base/ext.py
@@ -1,23 +1,16 @@
from flask import request
-from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from flaskext.babel import Babel
-from flaskext.script import Manager
from flaskext.cache import Cache
from flaskext.mail import Mail
-from flaskext.oauth import OAuth
+from flaskext.script import Manager
-from .admin import FlaskAdmin
-from .oauth import config_oauth
from .app import create_app
-admin = FlaskAdmin()
babel = Babel()
-bootstrap = Bootstrap()
cache = Cache()
db = SQLAlchemy()
-oauth = OAuth()
main = Mail()
manager = Manager(create_app)
@@ -27,14 +20,11 @@
def config_extensions(app):
" Init application with extensions. "
- admin.init_app(app)
- bootstrap.init_app(app)
cache.init_app(app)
db.init_app(app)
main.init_app(app)
config_babel(app)
- config_oauth(oauth, app)
def config_babel(app):
View
16 base/utils.py
@@ -1,16 +0,0 @@
-import importlib
-
-from base.config.production import APPS
-
-
-def load_modules(name, app=None):
- " Load modules by apps. "
-
- apps = app and app.config.get('APPS') or APPS
- mods = []
- for app in apps:
- try:
- mods.append(importlib.import_module('base.%s.%s' % (app, name)))
- except ImportError:
- continue
- return mods
View
12 base/views.py
@@ -1,12 +0,0 @@
-from flask import render_template, Blueprint
-
-from .auth.forms import LoginForm
-
-
-urls = Blueprint('urls', __name__)
-
-
-@urls.route('/')
-def index():
- " Main page. "
- return render_template('index.html', loginform=LoginForm())
View
7 manage.py
@@ -1,7 +1,10 @@
#!/usr/bin/env python
# coding: utf-8
-from base.ext import db
-from base.script import manager
+from base.app import load_modules
+from base.ext import db, manager
+
+
+load_modules('script')
@manager.shell

0 comments on commit 2212dfc

Please sign in to comment.
Something went wrong with that request. Please try again.