mirror of
https://github.com/wassname/flask-security.git
synced 2026-06-27 16:10:11 +08:00
Refactor example
This commit is contained in:
+26
-30
@@ -16,7 +16,28 @@ from flask.ext.security import (Security, LoginForm, user_datastore,
|
||||
from flask.ext.security.datastore.sqlalchemy import SQLAlchemyUserDatastore
|
||||
from flask.ext.security.datastore.mongoengine import MongoEngineUserDatastore
|
||||
|
||||
def add_endpoints(app):
|
||||
def create_users():
|
||||
user_datastore.create_user(username='matt', email='matt@lp.com',
|
||||
password='password',
|
||||
roles=['admin'])
|
||||
|
||||
user_datastore.create_user(username='joe', email='joe@lp.com',
|
||||
password='password',
|
||||
roles=['editor'])
|
||||
|
||||
user_datastore.create_user(username='jill', email='jill@lp.com',
|
||||
password='password',
|
||||
roles=['author'])
|
||||
|
||||
user_datastore.create_user(username='tiya', email='tiya@lp.com',
|
||||
password='password', active=False)
|
||||
|
||||
def create_app(auth_config):
|
||||
app = Flask(__name__)
|
||||
app.debug = True
|
||||
app.config['SECRET_KEY'] = 'secret'
|
||||
app.config['AUTH'] = auth_config or {}
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return render_template('index.html', content='Home Page')
|
||||
@@ -53,35 +74,15 @@ def add_endpoints(app):
|
||||
def admin_or_editor():
|
||||
return render_template('index.html', content='Admin or Editor Page')
|
||||
|
||||
def create_users():
|
||||
user_datastore.create_user(username='matt', email='matt@lp.com',
|
||||
password='password',
|
||||
roles=['admin'])
|
||||
|
||||
user_datastore.create_user(username='joe', email='joe@lp.com',
|
||||
password='password',
|
||||
roles=['editor'])
|
||||
|
||||
user_datastore.create_user(username='jill', email='jill@lp.com',
|
||||
password='password',
|
||||
roles=['author'])
|
||||
|
||||
user_datastore.create_user(username='tiya', email='tiya@lp.com',
|
||||
password='password', active=False)
|
||||
return app
|
||||
|
||||
def create_sqlalchemy_app(auth_config=None):
|
||||
app = Flask(__name__)
|
||||
app.debug = True
|
||||
app.config['SECRET_KEY'] = 'secret'
|
||||
app = create_app(auth_config)
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
|
||||
app.config['AUTH'] = auth_config or {}
|
||||
|
||||
db = SQLAlchemy(app)
|
||||
|
||||
Security(app, SQLAlchemyUserDatastore(db))
|
||||
|
||||
add_endpoints(app)
|
||||
|
||||
@app.before_first_request
|
||||
def before_first_request():
|
||||
db.create_all()
|
||||
@@ -90,20 +91,14 @@ def create_sqlalchemy_app(auth_config=None):
|
||||
return app
|
||||
|
||||
def create_mongoengine_app(auth_config=None):
|
||||
app = Flask(__name__)
|
||||
app.debug = True
|
||||
app.config['SECRET_KEY'] = 'secret'
|
||||
app.config['AUTH'] = auth_config or {}
|
||||
app = create_app(auth_config)
|
||||
app.config['MONGODB_DB'] = 'flask_security_example'
|
||||
app.config['MONGODB_HOST'] = 'localhost'
|
||||
app.config['MONGODB_PORT'] = 27017
|
||||
|
||||
db = MongoEngine(app)
|
||||
|
||||
Security(app, MongoEngineUserDatastore(db))
|
||||
|
||||
add_endpoints(app)
|
||||
|
||||
@app.before_first_request
|
||||
def before_first_request():
|
||||
from flask.ext.security import User, Role
|
||||
@@ -115,4 +110,5 @@ def create_mongoengine_app(auth_config=None):
|
||||
|
||||
if __name__ == '__main__':
|
||||
app = create_sqlalchemy_app()
|
||||
#app = create_mongoengine_app()
|
||||
app.run()
|
||||
Reference in New Issue
Block a user