Exemple #1
0
def create_app(config_name):
    # 设置日志
    setup_log(config_name)
    # 创建flask实例对象
    app = Flask(__name__)
    # 加载配置
    app.config.from_object(config[config_name])
    # 设置redis_store为全局变量
    global redis_store
    # db关联app,真正初始化
    db.init_app(app)
    # 初始化redis数据库,该数据库为保存其他需要保存在redis中的数据
    redis_store = StrictRedis(host=config[config_name].HOST,
                              port=config[config_name].PORT,
                              decode_responses=True)
    # 开启CSRF保护
    # CSRFProtect(app)
    # 设置session保存位置
    Session(app)
    # 注册蓝图
    from info.modules.index import index_blue
    app.register_blueprint(index_blue)
    from info.modules.passport import passport_blue
    app.register_blueprint(passport_blue)

    return app
Exemple #2
0
def creat_app(ms="ts"):
    global db, rs
    app = Flask(__name__)
    app.config.from_object(zd[ms])
    db = SQLAlchemy(app=app)
    rs = StrictRedis(host=zd[ms].REDIS_HOST, port=zd[ms].REDIS_PORT)
    Session(app=app)
    app.add_template_filter(djpmglq, name='cssxz')
    # print(zd[ms].DEBUG)

    #scrf 开启
    CSRFProtect(app)
    from csh import models
    #开启日志
    setup_log()
    # @app.errorhandler(404)
    # def cwxd(e):
    #     return render_template('news/404.html')
    @app.after_request
    def szscrf(resp):
        token = generate_csrf()
        # session['field_name']=token
        # resp.headers['X-CSRFToken'] = token
        resp.set_cookie('X-CSRFToken', token)
        return resp

    # @app.after_request
    # def after_request(response):
    #     from flask_wtf.csrf import generate_csrf
    #     token = generate_csrf()
    #
    #     response.set_cookie('csrf_token', token)
    #
    #     return response
    return app, db
def create_app(config_name):
    # 配置日志,并且传入配置名字,以便能获取到指定配置所对应的日志等级
    setup_log(config_name)
    # 创建Flask对象
    app = Flask(__name__)
    # 加载配置
    app.config.from_object(config[config_name])
    # 通过app初始化
    db.init_app(app)
    # 初始化 redis 存储对象
    global redis_store
    redis_store = StrictRedis(host=config[config_name].REDIS_HOST,
                              port=config[config_name].REDIS_PORT,
                              decode_responses=True)
    # 开启当前项目 CSRF 保护,只做服务器验证功能
    # CSRFProtect(app)
    # 设置session保存指定位置
    Session(app)

    # 注册蓝图
    from info.modules.index import index_blu
    app.register_blueprint(index_blu)

    from info.modules.passport import passport_blu
    app.register_blueprint(passport_blu)

    return app
Exemple #4
0
def create_app(config):
    app = Flask(__name__)  # type: Flask
    app.config.from_object(config)

    config.init_app(app)
    db.init_app(app)
    csrf.init_app(app)
    mail.init_app(app)
    Session(app)
    socket_io.init_app(app)

    app.url_map.converters["re"] = ReConverter

    global redis_store
    redis_store = redis.StrictRedis(host=config.REDIS_HOST,
                                    port=config.REDIS_PORT,
                                    db=config.REDIS_DB,
                                    password=config.REDIS_PASSWD)

    from apps.main import main
    from apps.api import api
    app.register_blueprint(api, url_prefix="/api")
    app.register_blueprint(main)

    return app
Exemple #5
0
def create_app(config_name):
    app = Flask(__name__)
    config = Config_dict[config_name]
    #调用日志方法
    log_file(config.LEVEL)
    app.config.from_object(config)
    db.init_app(app)
    global redis_store
    redis_store = redis.StrictRedis(host=Config.REDIS_HOST,
                                    port=Config.REDIS_PORT,
                                    decode_responses=True)
    Session(app)
    #开启CSRF保护之后,会对[POST,'PUT','PATCH','DELETE]类型的请求方法做校验
    #获取cookie中的csrfToken,获取headers请求头里的csrfToken做校验
    #开发者需要手动设置cookie和headers中的csrfToken
    #设置CSRF程序保护
    CSRFProtect(app)
    #首页蓝图注册app
    from info.modules.index import index_blu
    app.register_blueprint(index_blu)
    #验证蓝图注册app
    from info.modules.passport.views import passport_blu
    app.register_blueprint(passport_blu)

    #设置请求钩子afterrequest,每次请求完成之后,都会走该钩子装饰的方法
    @app.after_request
    def after_request(resp):
        #由CSRFProtect提供的一个generate_csrf方法生成csrf_token
        csrf_token = generate_csrf()
        resp.set_cookie("csrf_token", csrf_token)
        return resp

    #将过滤器添加到模板过滤器当中
    app.add_template_filter(do_index_filter, "index_filter")
    return app
    def test_flasksqlalchemy_session_with_signer(self):
        app = flask.Flask(__name__)
        app.debug = True
        app.secret_key = 'test_secret_key'
        app.config['SESSION_TYPE'] = 'sqlalchemy'
        app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'
        app.config['SESSION_USE_SIGNER'] = True
        session = Session(app)

        @app.route('/set', methods=['POST'])
        def set():
            flask.session['value'] = flask.request.form['value']
            return 'value set'

        @app.route('/get')
        def get():
            return flask.session['value']

        @app.route('/delete', methods=['POST'])
        def delete():
            del flask.session['value']
            return 'value deleted'

        c = app.test_client()
        self.assertEqual(
            c.post('/set', data={
                'value': '42'
            }).data, b'value '
            b'set')
        self.assertEqual(c.get('/get').data, b'42')
        c.post('/delete')
    def test_peewee_session(self):
        from peewee import SqliteDatabase
        app = flask.Flask(__name__)
        app.debug = True
        app.config['SESSION_DB'] = None
        app.config['SESSION_TYPE'] = 'peewee'
        app.config['SESSION_DB_CLASS'] = SqliteDatabase
        app.config['SESSION_PEEWEE_CONFIG'] = dict(database="sqlite", )
        Session(app)

        @app.route('/set', methods=['POST'])
        def set():
            flask.session['value'] = flask.request.form['value']
            return 'value set'

        @app.route('/get')
        def get():
            return flask.session['value']

        @app.route('/delete', methods=['POST'])
        def delete():
            del flask.session['value']
            return 'value deleted'

        c = app.test_client()
        self.assertEqual(
            c.post('/set', data={
                'value': '42'
            }).data, b'value '
            b'set')
        self.assertEqual(c.get('/get').data, b'42')
        c.post('/delete')
Exemple #8
0
def create_app(config_name):
    """创建flask应用对象"""
    app = Flask(__name__)

    conf = config_dict[config_name]

    # 设置flask的配置信息
    app.config.from_object(conf)

    # 初始化数据库db
    db.init_app(app)

    # 初始化redis
    global redis_store
    redis_store = redis.StrictRedis(host=conf.REDIS_HOST, port=conf.REDIS_PORT)

    # 初始化
    csrf.init_app(app)

    # 将flask里的session数据保存到redis中
    Session(app)

    # 向app中添加自定义的路由转换器
    app.url_map.converters["re"] = RegexConverter

    # 注册蓝图
    import api_1_0
    app.register_blueprint(api_1_0.api, url_prefix="/api/v1_0")

    # 提供html静态文件的蓝图
    import web_html
    app.register_blueprint(web_html.html)

    return app
    def test_filesystem_session(self):
        app = flask.Flask(__name__)
        app.config['SESSION_TYPE'] = 'filesystem'
        app.config['SESSION_FILE_DIR'] = tempfile.gettempdir()
        Session(app)

        @app.route('/set', methods=['POST'])
        def set():
            flask.session['value'] = flask.request.form['value']
            return 'value set'

        @app.route('/get')
        def get():
            return flask.session['value']

        @app.route('/delete', methods=['POST'])
        def delete():
            del flask.session['value']
            return 'value deleted'

        c = app.test_client()
        self.assertEqual(
            c.post('/set', data={
                'value': '42'
            }).data, b'value set')
        self.assertEqual(c.get('/get').data, b'42')
        c.post('/delete')
Exemple #10
0
def create_app(config_name):
    # 配置日志
    setup_log(config_name)

    app = Flask(__name__)
    # 加载配置
    app.config.from_object(config[config_name])
    # 初始化数据库
    db.init_app(app)
    # 初始化redis存储对象
    global redis_store
    redis_store = StrictRedis(host=config[config_name].REDIS_HOST,
                              port=config["development"].REDIS_PORT,
                              decode_responses=True)
    # 开启当前项目 CSRF 保护, 只做服务器验证功能
    # 帮我们做了: 从cookie中取出随机值, 从表单中取出随机值, 然后进行校验, 并且响应校验结果
    # 我们需要做: 1. 再界面加载的时候, 往cookie中添加一个csrf_token, 并且再表单中添加一个隐藏的csrf_token
    CSRFProtect(app)
    # 设置session保存位置
    Session(app)

    from info.utils.common import do_index_class
    # 添加自定义过滤器 什么时候使用过滤器,什么时候导入上面的
    app.add_template_filter(do_index_class, "index_class")

    from info.utils.common import user_login_data

    @app.errorhandler(404)
    @user_login_data
    def page_not_found(e):
        user = g.user
        data = {"user": user.to_dict() if user else None}
        return render_template('news/404.html', data=data)

    @app.after_request
    def after_request(response):
        # 生成随机的csrf_token值
        csrf_token = generate_csrf()
        # 设置一个cookie
        response.set_cookie("csrf_token", csrf_token)

        return response

    # 注册蓝图
    # 首页蓝图
    from info.modules.index import index_blu
    app.register_blueprint(index_blu)
    # 注册图片验证码蓝图
    from info.modules.passport import passport_blu
    app.register_blueprint(passport_blu)
    # 新闻详情页蓝图
    from info.modules.news import news_blu
    app.register_blueprint(news_blu)
    # 个人档案页蓝图
    from info.modules.profile import profile_blu
    app.register_blueprint(profile_blu)
    # 管理员登录页蓝图
    from info.modules.admin import admin_blu
    app.register_blueprint(admin_blu)
    return app
    def test_mongodb_session(self):
        app = flask.Flask(__name__)
        app.testing = True
        app.config['SESSION_TYPE'] = 'mongodb'
        Session(app)

        @app.route('/set', methods=['POST'])
        def set():
            flask.session['value'] = flask.request.form['value']
            return 'value set'

        @app.route('/get')
        def get():
            return flask.session['value']

        @app.route('/delete', methods=['POST'])
        def delete():
            del flask.session['value']
            return 'value deleted'

        c = app.test_client()
        self.assertEqual(
            c.post('/set', data={
                'value': '42'
            }).data, b'value set')
        self.assertEqual(c.get('/get').data, b'42')
        c.post('/delete')
Exemple #12
0
def create_app(config_name):
    # 配置日志
    setup_log('develop')
    app = Flask(__name__)

    # 加载配置
    app.config.from_object(config[config_name])

    db.init_app(app)
    # 初始化redis
    global redis_store
    redis_store = StrictRedis(host=config[config_name].REDIS_HOST,
                              port=config[config_name].REDIS_PORT,
                              decode_responses=True)

    # csrf保护 只做服务器验证功能

    CSRFProtect(app)
    Session(app)

    # 添加自定义过滤器
    app.add_template_filter(do_index_class, "index_class")

    # 注册蓝图
    from info.modules.index import index_blu
    app.register_blueprint(index_blu)

    from info.modules.passport import passport_blu
    app.register_blueprint(passport_blu)

    from info.modules.news import news_blu
    app.register_blueprint(news_blu)

    from info.modules.profile import profile_blu
    app.register_blueprint(profile_blu)

    from info.modules.admin import admin_blu
    app.register_blueprint(admin_blu)

    from info.utils.common import user_login_data

    @app.errorhandler('/404')
    @user_login_data
    def page_not_fount(e):
        user = g.user

        data = {"user": user.to_dict() if user else None}
        return render_template('news/404.html', data=data)

    @app.after_request
    def after_request(response):
        csrf_token = generate_csrf()
        response.set_cookie("csrf_token", csrf_token)
        return response

    return app
Exemple #13
0
def setup_session(app: Flask):
    """
    Setup session extension manager
    :param app: Flask base application
    """

    app.config["SESSION_TYPE"] = "redis"
    app.config["SESSION_REDIS"] = Redis().get_connection()

    context.set_value("session", Session(app))
Exemple #14
0
def create_app(debug=False):
	app = Flask(__name__)
	app.debug = debug
	app.config.from_object('config')

	from .main import main as main_blueprint
	app.register_blueprint(main_blueprint)
	socketio.init_app(app)
	Session(app)
	return app
Exemple #15
0
def creat_app(config_name):
    # 配置日志,并且传入配置名字,以便能获取到指定配置所对应的日志等级
    setup_log(config_name)
    # 创建Flask对象
    app = Flask(__name__)
    # 1.加载配置
    app.config.from_object(config[config_name])
    # 通过app初始化
    db.init_app(app)
    # 3.初始化 redis 存储对象
    global redis_store
    redis_store = StrictRedis(host=config[config_name].REDIS_HOST, port=config[config_name].REDIS_PORT,decode_responses=True)
    # 4.开启当前项目的 CSRF 保护,只做服务器验证功能(所有的请求都会经过before_request请求勾子)
    CSRFProtect(app)
    # 5.设置session保存指定位置
    Session(app)

    # 添加自定义过滤器
    from info.utils.common import do_index_class
    app.add_template_filter(do_index_class ,"index_class")

    from info.utils.common import user_login_data
    @app.errorhandler(404)
    @user_login_data
    def page_not_found(e):
        user = g.user
        data = {"user" : user.to_dict() if user else None}
        return render_template("news/404.html",data=data)

    @app.after_request
    def after_request(response):
        # 生成随机的csrf_token的值
        csrf_token = generate_csrf()
        # 设置一个cookie
        response.set_cookie("csrf_token",csrf_token)
        return response

    # 注册蓝图
    # 什么时候用什么时候导入:index_blu(避免循环导入)
    from info.modules.index import index_blu
    app.register_blueprint(index_blu)

    from info.modules.passport import passport_blu
    app.register_blueprint(passport_blu)

    from info.modules.news import news_blu
    app.register_blueprint(news_blu)

    from info.modules.profile import profile_blu
    app.register_blueprint(profile_blu)

    from info.modules.admin import admin_blu
    app.register_blueprint(admin_blu,url_prefix="/admin")

    return app
Exemple #16
0
def create_app(config_name):
    # 配置日志
    setup_logging(config_name)
    # 创建Flask对象
    app = Flask(__name__)
    # 加载项目配置
    app.config.from_object(config[config_name])
    # 通过app初始化
    db.init_app(app)
    # 初始化redis
    global redis_sr
    redis_sr = StrictRedis(host=config[config_name].REDIS_HOST,
                           port=config[config_name].REDIS_PORT,
                           decode_responses=True)
    # 开启CSRF保护
    CSRFProtect(app)
    # 初始化session
    Session(app)

    from info.utils.common import do_index_class
    app.add_template_filter(do_index_class, "index_class")

    from info.utils.common import user_login_data

    @app.errorhandler(404)
    @user_login_data
    def page_not_found(err):
        user = g.user
        data = {"user": user.to_dict() if user else None}
        return render_template("news/404.html", data=data)

    @app.after_request
    def after_request(response):
        # 生成csrf_token并存储到cookie中
        csrf_token = generate_csrf()
        response.set_cookie("csrf_token", csrf_token)

        return response

    from info.modules.index import index_blu
    app.register_blueprint(index_blu)

    from info.modules.passport import passport_blu
    app.register_blueprint(passport_blu)

    from info.modules.news import news_blu
    app.register_blueprint(news_blu)

    from info.modules.profile import profile_blu
    app.register_blueprint(profile_blu)

    from info.modules.admin import admin_blu
    app.register_blueprint(admin_blu)
    return app
Exemple #17
0
def create_app(config_name):
    # 配置日志,并且传入配置名字,以便能获取到指定配置所对应的日志等级
    setup_log(config_name)
    # 创建Flask对象
    app = Flask(__name__)
    # 加载配置
    app.config.from_object(config[config_name])
    # 通过app初始化
    db.init_app(app)
    # 初始化 redis 存储对象
    global redis_store
    redis_store = StrictRedis(host=config[config_name].REDIS_HOST,
                              port=config[config_name].REDIS_PORT,
                              decode_responses=True)
    # 开启当前项目 CSRF 保护,只做服务器验证功能
    # 帮我们做了:从cookie中取出随机值,从表单中取出随机,然后进行校验,并且响应校验结果
    # 我们需要做:1. 在返回响应的时候,往cookie中添加一个csrf_token,2. 并且在表单中添加一个隐藏的csrf_token
    # 而我们现在登录或者注册不是使用的表单,而是使用 ajax 请求,所以我们需要在 ajax 请求的时候带上 csrf_token 这个随机值就可以了
    CSRFProtect(app)
    # 设置session保存指定位置
    Session(app)

    # 初始化数据库
    #  在Flask很多扩展里面都可以先初始化扩展的对象,然后再去调用 init_app 方法去初始化
    from info.utils.common import do_index_class
    # 添加自定义过滤器
    app.add_template_filter(do_index_class, "index_class")

    from info.utils.common import user_login_data

    @app.errorhandler(404)
    @user_login_data
    def page_not_fount(e):
        user = g.user
        data = {"user": user.to_dict() if user else None}
        return render_template('news/404.html', data=data)

    # 向cookie中设置csrf_token
    @app.after_request
    def after_request(response):
        # 生成随机的csrf_token的值
        csrf_token = generate_csrf()
        # 设置一个cookie
        response.set_cookie("csrf_token", csrf_token)
        return response

    # 注册蓝图
    from info.modules.index import index_blu
    app.register_blueprint(index_blu)

    from info.modules.passport import passport_blue
    app.register_blueprint(passport_blue)

    return app
Exemple #18
0
def cread_info(ms='ts'):
    app = Flask(__name__)
    app.config.from_object(zd[ms])
    # 初始化
    db = SQLAlchemy(app=app)
    rs = StrictRedis(host=zd[ms].REDIS_HOST, port=zd[ms].REDIS_PORT)
    Session(app=app)
    #scrf 开启
    CSRFProtect(app)
    setup_log(zd[ms])  #日志
    return app, db
Exemple #19
0
def create_app(config_name):
    # 配置日志,并且传入配置名字,一边获取到指定配置所对应的日志等级
    setup_log(config_name)
    # 逻辑业务(需要哪些辅助工具)
    app = Flask(__name__)

    # 加载配置(从配置对象中加载)
    app.config.from_object(config[config_name])
    # 通过app初始化将app中的数据库参数传递到db里面
    db.init_app(app)
    # 初始化redis存储对象
    global redis_store
    redis_store = StrictRedis(host=config[config_name].REDIS_HOST,
                              port=config[config_name].REDIS_PORT,
                              decode_responses=True)
    # 开启当前项目CSRF保护,只做服务器验证功能
    CSRFProtect(app)
    # 设置session保存指定位置
    Session(app)
    # 初始化数据库 在Flask很多扩展里面都可以先初始化扩展的对象,然后再去调用init_app方法去初始化
    from info.utils.common import do_index_class
    app.add_template_filter(do_index_class, "index_class")

    from info.utils.common import user_login_data

    @app.errorhandler(404)
    @user_login_data
    def page_not_fount(e):
        user = g.user
        data = {"user": user.to_dict() if user else None}
        return render_template('news/404.html', data=data)

    @app.after_request
    def after_request(response):
        csrf_token = generate_csrf()
        response.set_cookie("csrf_token", csrf_token)
        return response

    # 注册蓝图
    from info.modules.index import index_blu
    app.register_blueprint(index_blu)
    from info.modules.passport import passport_blu
    app.register_blueprint(passport_blu)
    from info.modules.news import news_blu
    app.register_blueprint(news_blu)
    from info.modules.profile import profile_blu
    app.register_blueprint(profile_blu)
    from info.modules.admin import admin_blu
    app.register_blueprint(admin_blu, url_prefix="/admin")

    return app
Exemple #20
0
def create_app(**config):
    """
    Create the application and return it to the user
    :return: flask.Flask application
    """

    app = AbovlADSFlask('arxiv_biboverlay', local_config=config)
    app.url_map.strict_slashes = False
    app.register_blueprint(bp)

    sess = Session()
    sess.init_app(app)

    return app
Exemple #21
0
def create_app(config_name):
    setup_log(config_name)
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    db.init_app(app)
    global redis_store
    redis_store = StrictRedis(host=config[config_name].REDIS_HOST,
                              port=config[config_name].REDIS_PORT,
                              decode_responses=True)
    CSRFProtect(app)
    Session(app)

    from info.utils.common import do_index_class
    #添加自定义过滤器
    app.add_template_filter(do_index_class, "index_class")

    from info.utils.common import user_login_data

    @app.errorhandler(404)
    @user_login_data
    def page_not_found(e):
        user = g.user
        data = {"user": user.to_dict() if user else None}
        return render_template('news/404.html', data=data)

    @app.after_request
    def after_request(response):
        # 调用函数生成 csrf_token
        csrf_token = generate_csrf()
        # 通过 cookie 将值传给前端
        response.set_cookie("csrf_token", csrf_token)
        return response

    #注册蓝图
    from info.modules.index import index_blu
    app.register_blueprint(index_blu)

    from info.modules.passport import passport_blu
    app.register_blueprint(passport_blu)

    from info.modules.news import news_blu
    app.register_blueprint(news_blu)

    from info.modules.profile import profile_blu
    app.register_blueprint(profile_blu)

    from info.modules.admin import admin_blu
    app.register_blueprint(admin_blu, url_prefix='/admin')

    return app
Exemple #22
0
def creat_app(config_name):
    app = Flask(__name__)
    # 加载配置
    app.config.from_object(config[config_name])
    # 通过app初始化
    db.init_app(app)
    # 初始化redis存储对象
    redise_store = StrictRedis(host=config[config_name].REDIS_HOST,
                               port=config[config_name].REDIS_PORT)
    # 开启当前项目CSRF保护
    CSRFProtect(app)
    # 设置session保存指定位置
    Session(app)

    return app
Exemple #23
0
    def test_null_session(self):
        app = flask.Flask(__name__)
        Session(app)

        def expect_exception(f, *args, **kwargs):
            try:
                f(*args, **kwargs)
            except RuntimeError as e:
                self.assertTrue(e.args
                                and 'session is unavailable' in e.args[0])
            else:
                self.assertTrue(False, 'expected exception')

        with app.test_request_context():
            self.assertTrue(flask.session.get('missing_key') is None)
            expect_exception(flask.session.__setitem__, 'foo', 42)
            expect_exception(flask.session.pop, 'foo')
Exemple #24
0
def create_app(config_name):
    # setup_log(config_name)
    app = Flask(__name__, )
    app.config.from_object(config[config_name])
    db.init_app(app)
    global redis_store
    redis_store = StrictRedis(host=config[config_name].REDIS_HOST,
                              port=config[config_name].REDIS_PORT,
                              decode_responses=True)
    CSRFProtect(app)
    # 帮我们做了, 从cookie中取出随机值,从表单中取出随机值,然后进行校验
    # 但是没做表单和cookie中设置值 在响应中设置afert_response
    # 所以我们需要在cookie中csrf_token, 在表单中加入隐藏标签
    Session(app)
    from info.utils.common import do_index_class
    app.add_template_filter(do_index_class, "index_class")

    @app.after_request
    def after_request(response):
        csrf_token = generate_csrf()
        response.set_cookie("csrf_token", csrf_token)
        return response

    # from info.utils.common import do_index_class
    # 注册蓝图
    from info.modules.index import index_blu
    app.register_blueprint(index_blu)
    from info.modules.passport import passport_blu
    app.register_blueprint(passport_blu)
    from info.modules.news import detail_blu
    app.register_blueprint(detail_blu)
    from info.modules.profile import profile_blu
    app.register_blueprint(profile_blu)
    from info.modules.admin import admin_blu
    app.register_blueprint(admin_blu)

    from info.utils.common import user_login_data

    @app.errorhandler(404)
    @user_login_data
    def page_not_found(_):
        user = g.user
        data = {"user_info": user.to_dict() if user else None}
        return render_template('news/404.html', data=data)

    return app
Exemple #25
0
def create_app(class_name):
    app = Flask(__name__)
    config_class = config_maps.get(class_name)
    app.config.from_object(config_class)  # 数据库最先加载
    db.init_app(app)
    global redis_store
    redis_store = redis.StrictRedis(host='127.0.0.1',
                                    port=6379,
                                    decode_responses=True)
    Session(app)
    CSRFProtect(app)

    @app.after_request
    def after_request(response):
        csrf_token = generate_csrf()
        response.set_cookie('csrf_token', csrf_token)
        return response

    @app.errorhandler(404)
    @user_login_data
    def page_not_found(_):
        user = g.user
        data = {'user_info': user.to_dict() if user else None}
        return render_template('news/404.html', data=data)

    from info.utils.common import do_index
    # 添加一个模板过滤器
    app.add_template_filter(do_index, 'indexClass')

    from info.admin import admin_blue
    app.register_blueprint(admin_blue)

    from info.user import profile_blue
    app.register_blueprint(profile_blue)

    from info.index import index_blue
    app.register_blueprint(index_blue)  # 要注册方法.

    from info.passport import passport_blue
    app.register_blueprint(passport_blue)  # 要注册方法.

    from info.news import news_blue
    app.register_blueprint(news_blue)

    return app
Exemple #26
0
def create_app(subdomain="", username="", password=""):
    app = Flask("CTFd",
                static_folder="../static",
                template_folder="../templates")
    with app.app_context():
        app.config.from_object('CTFd.config')

        if subdomain:
            app.config.update(
                SQLALCHEMY_DATABASE_URI='mysql://' + username + ':' +
                password + '@localhost:3306/' + subdomain + '_ctfd',
                HOST=subdomain + app.config["HOST"],
                SESSION_FILE_DIR=app.config['SESSION_FILE_DIR'] + "/" +
                subdomain,
                DEBUG=True)

        from CTFd.models import db, Teams, Solves, Challenges, WrongKeys, Keys, Tags, Files, Tracking

        db.init_app(app)
        db.create_all()

        app.db = db
        # app.setup = True

        global mail
        mail = Mail(app)

        Session(app)

        from CTFd.views import init_views
        init_views(app)
        from CTFd.errors import init_errors
        init_errors(app)
        from CTFd.challenges import init_challenges
        init_challenges(app)
        from CTFd.scoreboard import init_scoreboard
        init_scoreboard(app)
        from CTFd.auth import init_auth
        init_auth(app)
        from CTFd.admin import init_admin
        init_admin(app)
        from CTFd.utils import init_utils
        init_utils(app)

        return app
Exemple #27
0
def create_app(u_selected_config):
    app = Flask(__name__)
    set_up_log(u_selected_config)

    app.config.from_object(seleceted_config[u_selected_config])
    app.add_template_filter(do_index_class, "indexClass")

    @app.after_request
    def after_request(response):
        # 调用函数生成 csrf_token
        csrf_token = generate_csrf()
        # 通过 cookie 将值传给前端
        response.set_cookie("csrf_token", csrf_token)
        return response

    CSRFProtect(app)
    Session(app)

    db.init_app(app)
    # db = SQLAlchemy(app)
    # @app.errorhandler(404)
    # @check_user
    # def page_notfound(e):
    #     user=g.user
    #     data={
    #         'user':user.to_dict()
    #     }
    #     return render_template('others/404.html',data=data)

    global redis_store

    redis_store = redis.StrictRedis(host=My_config.REDIS_HOST,
                                    port=My_config.REDIS_PORT)
    from info.moduls.index import index_
    from info.moduls.passport import passport_
    from info.moduls.news import news_blu
    from info.moduls.profile import profile_blu
    from info.moduls.admin import admin_blu

    app.register_blueprint(profile_blu)
    app.register_blueprint(index_)
    app.register_blueprint(passport_)
    app.register_blueprint(news_blu)
    app.register_blueprint(admin_blu)
    return app
Exemple #28
0
def create_app(config_name):
    app = Flask(__name__)
    config = config_dict.get(config_name)
    app.config.from_object(config)
    db = SQLAlchemy(app, "db")
    db.init_app(app)
    global  redis_store
    redis_store = redis.StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT)

    Session(app)
    CSRFProtect(app)
    app.url_map.converters["re"] = RegexConverter

    from ihome.api_1_0 import api
    app.register_blueprint(api)
    from ihome.web_html import web_html
    app.register_blueprint(web_html)
    return app
Exemple #29
0
    def __init__(self):
        super(Main, self).__init__(
            __name__,
            static_url_path='/res',
            static_folder='../webapp/resources',
            template_folder='templates')

        # Runtime environment (e.g., export FLASK_ENV=PRODUCTION)
        # https://pythonhosted.org/Flask-Environments
        env = Environments(self)
        env.from_yaml(os.path.join(os.getcwd(), 'config/config.yml'))

        # Config logging.
        # https://docs.python.org/2/howto/logging.html
        logging.config.dictConfig(yaml.load(open(self.config['LOG_CONFIG'])))

        # Jinja2 templates.
        # http://jinja.pocoo.org/docs/dev/api
        # http://flask.pocoo.org/docs/0.10/api/#flask.Flask.jinja_options
        self.jinja_options = flask.Flask.jinja_options.copy()
        self.jinja_options.update(dict(
            trim_blocks=True,
            lstrip_blocks=True
        ))

        # http://pythonhosted.org/Flask-Session
        # http://flask.pocoo.org/docs/0.10/quickstart/#sessions
        self.session = Session(self)

        # Enable injection within this class.
        # https://github.com/alecthomas/injector
        injector = Injector()
        injector.install_into(self)

        # Flask injection.
        # NOTE: Order of modules is important.
        LOG.info('### %s ###' % self.config['ENVIORNMENT'])
        FlaskInjector(app=self, injector=injector, modules=[
            ConfigModule,
            OAuthConfigModule,
            TwitterModule,
            ViewModule,
            OAuthRouterModule
        ])
Exemple #30
0
    def test_memcached_session(self):
        app = flask.Flask(__name__)
        app.config['SESSION_TYPE'] = 'memcached'
        Session(app)

        @app.route('/set', methods=['POST'])
        def set():
            flask.session['value'] = flask.request.form['value']
            return 'value set'

        @app.route('/get')
        def get():
            return flask.session['value']

        c = app.test_client()
        self.assertEqual(
            c.post('/set', data={
                'value': '42'
            }).data, b'value set')
        self.assertEqual(c.get('/get').data, b'42')