python – Flask蓝图正确使用

我有一个关于蓝图的问题。我有一个这样的结构的应用程序

app
    /run.py
    /APP
        /__init__.py
        /VIEWS
            /__init__.py
            /general.py
            /crud.py

这是代码http://pastebin.com/bsHsTGAP

run.py

from overwatch import app
app.run()

__init__.py

from flask import Flask, session, g, render_template, request, redirect, url_for, Response
import websiteconfig as config
from flaskext.principal import Identity, Principal, RoleNeed, UserNeed, \
            Permission, identity_changed, identity_loaded

app = Flask(__name__)
app.debug = config.DEBUG
app.secret_key = config.SECRET_KEY


principals = Principal(app)
principals._init_app(app)
@app.errorhandler(404)
def not_found(error):
    return render_template('404.html'), 404

@app.errorhandler(403)
def page_not_found(e):
    session['redirected_from'] = request.url
    return redirect(url_for('crud.login'))

# handle login failed
@app.errorhandler(401)
def page_not_found(e):
    return Response('<p>Login failed</p>')

from overwatch.views import general
from overwatch.views import crud


app.register_blueprint(general.mod)
app.register_blueprint(crud.mod)

general.py

from flask import Blueprint, render_template, session, redirect, url_for, \
     request, flash, g, Response, jsonify
from flaskext.principal import Identity, Principal, RoleNeed, UserNeed, \
            Permission, identity_changed, identity_loaded
from .. import principals

mod = Blueprint('general', __name__)

normal_role = RoleNeed('normal')
normal_permission = Permission(normal_role)

@mod.route('/')
@normal_permission.require(http_exception=403)
def index():
    return "YOU'RE IN"

crud.py

from flask import Blueprint, render_template, session, redirect, url_for, \
     request, flash, g, Response, jsonify, abort, Response
from mongokit import Connection, Document
from db import user_exists, email_exists, return_attribute, check_credentials
from forms import RegistrationForm, LoginForm
from .. import app
from flaskext.principal import Identity, Principal, RoleNeed, UserNeed, \
            Permission, identity_changed, identity_loaded
from general import normal_role, normal_permission

mod = Blueprint('crud', __name__)

@mod.route('/login/', methods=['GET', 'POST'])
def login():
    form = LoginForm(request.form)
    error = None
    if request.method == 'POST' and form.validate():
        if check_credentials(form.username.data,form.password.data):
            identity = Identity(form.username.data)
            identity_changed.send(app, identity=identity)
            return redirect(session['redirected_from'])
        else:
            return abort(401)
    return render_template('login.html', form=form, error=error)

@app.route("/logout/")
def logout():
    for key in ['identity.name', 'identity.auth_type', 'redirected_from']:
        try:
            del session[key]
        except:
            pass
    return Response('<p>Logged out</p>')

@identity_loaded.connect_via(app)
def on_identity_loaded(sender, identity):
    identity.provides.add(normal_role)

事实上,我似乎在很多东西中都输入了很多东西。现在它的工作。如果我进入索引paged,这是由general.py蓝图处理,并使用normal_permission保护,它将重定向到/由crud.py蓝图处理的登录,并且如果登录重定向到索引。再次,现在它…工作,但它也感觉reallllllllly脏和不洁和.. bleac …所以不像一些好的代码我看到:)

欢迎有任何建议。如果这不是解决问题的方法,我愿意学习。我不想有一些代码..只是工作。

感谢您的阅读时间,也可以回答。

PS。如果我在这里粘贴了太多的代码,让我知道,我会编辑它。

要从蓝图的视图访问当前的应用程序,您应该使用flask.current_app对象,它是当前应用程序的代理(例如,它是在烧瓶扩展中使用的)。

关于你的代码,除了未使用的进口,它是有组织的我的意见,但我不能说主要部分,因为我从来没有使用过。

http://stackoverflow.com/questions/7085438/flask-blueprints-proper-usage

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:python – Flask蓝图正确使用