def create_app(object_name): """ An flask application factory, as explained here: http://flask.pocoo.org/docs/patterns/appfactories/ Arguments: object_name: the python path of the config object, e.g. project.config.ProdConfig """ app = Flask(__name__) app.config.from_object(object_name) rest_api.add_resource( AuthApi, '/api/auth', ) rest_api.add_resource( PostApi, '/api/post', '/api/post/<int:post_id>', ) rest_api.init_app(app) db.init_app(app) mongo.init_app(app) bcrypt.init_app(app) login_manager.init_app(app) principals.init_app(app) @identity_loaded.connect_via(app) def on_identity_loaded(sender, identity): # Set the identity user object identity.user = current_user # Add the UserNeed to the identity if hasattr(current_user, 'id'): identity.provides.add(UserNeed(current_user.id)) # Add each role to the identity if hasattr(current_user, 'roles'): for role in current_user.roles: identity.provides.add(RoleNeed(role.name)) app.register_blueprint(main_blueprint) app.register_blueprint(blog_blueprint) return app
def create_app(object_name): app = Flask(__name__) app.config.from_object(object_name) db.init_app(app) bcrypt.init_app(app) login_manager.init_app(app) principals.init_app(app) celery.init_app(app) rest_api.add_resource(PostApi, '/api/post', '/api/post/<int:post_id>', endpoint='api') rest_api.add_resource(AuthApi, '/api/auth') rest_api.init_app(app) @identity_loaded.connect_via(app) def on_identity_loaded(sender, identity): #set the identity user object identity.user = current_user #add the user need to the identity if hasattr(current_user, 'id'): identity.provides.add(UserNeed(current_user.id)) #add each role to the identity if hasattr(current_user, 'roles'): for role in current_user.roles: identity.provides.add(RoleNeed(role.name)) # @app.route('/') # def index(): # return redirect(url_for('blog.home')) app.register_blueprint(blog_blueprint) app.register_blueprint(main_blueprint) return app
def create_app(object_name): app = Flask(__name__) app.config.from_object(object_name) #db.init_app(app) #event.listen(Reminder, 'after_insert', on_reminder_save) db.init_app(app) bcrypt.init_app(app) login_manager.init_app(app) principals.init_app(app) celery.init_app(app) debug_toolbar.init_app(app) cache.init_app(app) admin.init_app(app) admin.add_view(CustomView(name='Custom')) models = [User,Role,Comment,Tag] for model in models: admin.add_view(CustomModelView(model,db.session,category='Models')) admin.add_view( PostView( Post, db.session, category='PostManager' ) ) admin.add_view( CustomFileAdmin( os.path.join(os.path.dirname(__file__), 'static'), '/static/', name='Static Files' ) ) rest_api.add_resource( AuthApi, '/api/auth', ) rest_api.add_resource( PostApi, '/api/post', '/api/post/<int:post_id>', ) rest_api.add_resource( CommentApi, '/api/comment', '/api/comment/<int:comment_id>', '/api/post/<int:post_id>/comment', '/api/post/<int:post_id>/comment/<int:comment_id>', ) rest_api.init_app(app) @identity_loaded.connect_via(app) def on_identity_loaded(sender, identity): # Set the identity user object identity.user = current_user print "In __init_.py......:%s" %current_user # Add the UserNeed to the identity if hasattr(current_user, 'id'): identity.provides.add(UserNeed(current_user.id)) print UserNeed(current_user.id) # Add each role to the identity if hasattr(current_user, 'roles'): for role in current_user.roles: identity.provides.add(RoleNeed(role.name)) print RoleNeed(role.name) app.register_blueprint(blog_blueprint) app.register_blueprint(main_blueprint) return app
app = Flask(__name__) app.config.from_object(DevConfig) app.config['SECRET_KEY'] = 'super-secret' db.init_app(app) bcrypt.init_app(app) jwt = JWT(app, authenticate, identity) app.register_blueprint(user_blueprint) app.register_blueprint(login_blueprint) app.register_blueprint(blog_blueprint) rest_api.add_resource(UserService, '/user') rest_api.add_resource(BlogService, '/blog') rest_api.init_app(app) # @auth.verify_password # def verify_password(username_or_token, password): # # first try to authenticate by token # user = User.verify_auth_token(username_or_token) # if not user: # # try to authenticate with username/password # user = User.query.filter_by(username=username_or_token).first() # if not user or not user.check_password(password): # return False # # g.user = user # return True # @app.before_request # def before_request():
def create_app(object_name): app = Flask(__name__) app.config.from_object(object_name) db.init_app(app) event.listen(Reminder, 'after_insert', on_reminder_save) bcrypt.init_app(app) oid.init_app(app) login_manager.init_app(app) principals.init_app(app) rest_api.add_resource(PostApi, '/api/post', '/api/post/<int:post_id>', endpoint='api') rest_api.add_resource(AuthApi, '/api/auth', endpoint='auth') rest_api.init_app(app) celery.init_app(app) debug_toolbar.init_app(app) cache.init_app(app) assets_env.init_app(app) assets_env.register("main_js", main_js) assets_env.register("main_css", main_css) admin.init_app(app) # admin.add_view(CustomView(name="Custom")) # models = [User, Role, Post, Comment, Tag, Reminder] # for model in models: # if model is not Post: # admin.add_view( # CustomModelView(model, db.session, category="models") # ) # else: # admin.add_view( # PostView(Post, db.session, category="models") # ) admin.add_view(CustomView(name='Custom')) admin.add_view(CustomModelView(User, db.session, category="Models")) admin.add_view(CustomModelView(Role, db.session, category="Models")) # # Need to use a special view for Posts to get the CKEditor functionality # admin.add_view(PostView(Post, db.session, category="Models")) admin.add_view(CustomModelView(Comment, db.session, category="Models")) admin.add_view(CustomModelView(Tag, db.session, category="Models")) admin.add_view(CustomModelView(Reminder, db.session, category="Models")) admin.add_view( CustomFileAdmin(os.path.join(os.path.dirname(__file__), 'static'), '/static/', name='Static Files')) mail.init_app(app) youtube_ext.init_app(app) # # The gzip extension and the debug toolbar can't both be running... # # flask_gzip.init_app(app) @identity_loaded.connect_via(app) def on_identity_loaded(sender, identity): identity.user = current_user if hasattr(current_user, 'id'): identity.provides.add(UserNeed(current_user.id)) if hasattr(current_user, 'roles'): for role in current_user.roles: identity.provides.add(RoleNeed(role.name)) # # Routes # @app.route('/') def index(): return redirect(url_for('blog.home')) app.register_blueprint(blog_blueprint) app.register_blueprint(main_blueprint) return app
def create_app(DevConfig): app = Flask(__name__) # 跨域加允许 cookies cors = CORS( app, resources={r'/api/*': { 'origins': '*', 'supports_credentials': True }}) app.config.from_object(DevConfig) mail.init_app(app) mail.app = app db.init_app(app) with app.app_context(): # db.drop_all() db.create_all() # create_user() # from webapp.app_blueprint import create_fake_data # create_fake_data() # whoosh_index(app, Blog) # whoosh_index(app, Comment) # whoosh_index(app, User) # index_all(app) flask_whooshalchemyplus.init_app(app) login_manager.init_app(app) rest_api.add_resource(BlogApi, '/api/blog', '/api/blog/<int:id>') rest_api.add_resource(CommentApi, '/api/blog/<int:blog_id>/comment') rest_api.add_resource(BlogTitleApi, '/api/blog/all') rest_api.add_resource(BlogTagApi, '/api/blog/tag', '/api/blog/tag/<int:tag_id>') rest_api.add_resource(LoginApi, '/api/login') rest_api.add_resource(UserApi, '/api/user') rest_api.add_resource(LogoutApi, '/api/logout') rest_api.add_resource(RegisterApi, '/api/register') rest_api.add_resource(CheckMessageApi, '/api/checkMessages') rest_api.add_resource(MessageApi, '/api/message', '/api/message/<int:message_id>') rest_api.add_resource(MentionApi, '/api/usernameMention') rest_api.add_resource(ZanApi, '/api/comment/<int:comment_id>/zan') rest_api.add_resource(GetImgApi, '/api/imgs') rest_api.add_resource(SearchApi, '/api/search') rest_api.add_resource(userAndRoomApi, '/api/wuziqi/userandroom') rest_api.init_app(app) celery.conf.update(app.config) # celery.init_app(app) from webapp.app_blueprint import app_blueprint app.register_blueprint(app_blueprint) from webapp.tasks import tasks_blueprint app.register_blueprint(tasks_blueprint) from webapp.send_email import emails_blueprint app.register_blueprint(emails_blueprint, url_prefix='/emails') socketio.init_app(app, message_queue=app.config['CELERY_BROKER_URL']) return app