Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
partial finish, took a lot of code from wtxlog, need refactoring
- Loading branch information
1 parent
bd18f2c
commit 2d1c99c
Showing
17 changed files
with
1,487 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,100 @@ | ||
#!usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
import os | ||
import sys | ||
|
||
from flask import Flask, render_template, g, request | ||
from flask.ext.bootstrap import Bootstrap | ||
from flask.ext.mail import Mail, Message | ||
from flask.ext.moment import Moment | ||
from flask.ext.login import LoginManager, current_user, logout_user | ||
|
||
# import os | ||
# import sys | ||
|
||
from flask import Flask, send_from_directory, flash, render_template | ||
from config import config | ||
from flask_wtf.csrf import CsrfProtect | ||
from flask.login import logout_user, current_user | ||
from ext import (babel, bootstrap, db, moment, cache, mail, | ||
login_manager, bcrypt) | ||
|
||
from models import User, AnonymousUser | ||
|
||
# 将project目录加入sys.path | ||
project_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) | ||
if project_path not in sys.path: | ||
sys.path.insert(0, project_path) | ||
# project_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) | ||
# if project_path not in sys.path: | ||
# sys.path.insert(0, project_path) | ||
|
||
csrf = CsrfProtect() | ||
|
||
bootstrap = Bootstrap() | ||
moment = Moment() | ||
mail = Mail() | ||
|
||
@login_manager.user_loader | ||
def load_user(user_id): | ||
return User.query.get(int(user_id)) | ||
|
||
|
||
def create_app(config_name): | ||
app = Flask(__name__) | ||
app.config.from_object(config[config_name]) | ||
config[config_name].init_app(app) | ||
# config[config_name].init_app(app) | ||
|
||
db.init_app(app) | ||
bootstrap.init_app(app) | ||
mail.init_app(app) | ||
moment.init_app(app) | ||
csrf.init_app(app) | ||
babel.init_app(app) | ||
cache.init_app(app) | ||
bcrypt.init_app(app) | ||
|
||
register_db(app) | ||
register_login_manager(app) | ||
register_managers(app) | ||
register_routes(app) | ||
register_uploadsets(app) | ||
register_error_handle(app) | ||
# register_uploadsets(app) | ||
# register_error_handle(app) | ||
|
||
# before every request | ||
@app.before_request | ||
def before_request(): | ||
if not current_user.is_anonymous: | ||
if not current_user.confirmed: | ||
flash('请登录邮箱激活账户。') | ||
logout_user() | ||
if current_user.is_banned: | ||
flash('账户已被禁用,请联系管理员。') | ||
logout_user() | ||
|
||
@app.route('/favicon.ico') | ||
def favicon(): | ||
return send_from_directory(app.static_folder, 'favicon.ico', | ||
mimetype='image/vnd.microsoft.icon') | ||
|
||
@app.route('/robots.txt') | ||
def robotstxt(): | ||
return send_from_directory(app.static_folder, 'robots.txt') | ||
|
||
return app | ||
|
||
|
||
def register_routes(app): | ||
from .controllers import admin, site, account | ||
|
||
app.register_blueprint(site.bp, url_prefix='') | ||
app.register_blueprint(account.bp, url_prefix='/account') | ||
app.register_blueprint(admin.bp, url_prefix='/admin') | ||
|
||
|
||
def register_managers(app): | ||
login_manager.session_protection = 'strong' | ||
# flask-login will keep track of ip and broswer agent, | ||
# will log user out if it detects a change | ||
login_manager.login_view = 'account.login' | ||
login_manager.login_message = '请先登陆' | ||
login_manager.anonymous_user = AnonymousUser | ||
login_manager.init_app(app) | ||
|
||
|
||
def register_error_handle(app): | ||
@app.errorhandler(403) | ||
def page_403(error): | ||
return render_template('errors/403.html'), 403 | ||
|
||
@app.errorhandler(404) | ||
def page_404(error): | ||
return render_template('errors/404.html'), 404 | ||
|
||
@app.errorhandler(500) | ||
def page_500(error): | ||
return render_template('errors/500.html'), 500 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
from threading import Thread | ||
from functools import wraps | ||
from flask import abort | ||
from flask.ext.login import current_user | ||
|
||
from models import Permission | ||
|
||
|
||
def async(f): | ||
def wrapper(*args, **kwargs): | ||
thr = Thread(target=f, args=args, kwargs=kwargs) | ||
thr.start() | ||
return wrapper | ||
|
||
|
||
def permission_required(permission): | ||
def decorator(f): | ||
@wraps(f) | ||
def decorated_function(*args, **kwargs): | ||
if not current_user.can(permission): | ||
abort(403) | ||
return f(*args, **kwargs) | ||
return decorated_function | ||
return decorator | ||
|
||
|
||
def admin_required(f): | ||
return permission_required(Permission.ADMINISTER)(f) |
Oops, something went wrong.