示例#1
0
文件: app.py 项目: RQuintin/quarchive
def init_app() -> flask.Flask:
    load_config(env_ini=environ.get("QM_ENV_INI", None))
    app = flask.Flask("quarchive")
    app.config["SECRET_KEY"] = environ["QM_SECRET_KEY"]
    app.config["SQLALCHEMY_DATABASE_URI"] = environ["QM_SQL_URL"]
    app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
    app.config["CRYPT_CONTEXT"] = CryptContext(["bcrypt"])

    # By default Postgres will consult the locale to decide what timezone to
    # return datetimes in.  We want UTC in all cases.
    app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {
        "connect_args": {
            "options": "-c timezone=utc"
        }
    }

    app.config["PAGE_SIZE"] = 30
    db.init_app(app)
    with app.app_context():
        log.info("using engine: %s", db.session.bind)

    cors.init_app(app)
    babel = Babel(app,
                  default_locale="en_GB",
                  default_timezone="Europe/London")
    app.register_blueprint(blueprint)

    @babel.timezoneselector
    def use_user_timezone():
        if "user" in flask.g:
            tzinfo = flask.g.user.timezone
            log.debug("using user timezone: %s", tzinfo)
            return tzinfo.zone

    @app.context_processor
    def urlsplit_cp():
        return {"urlunsplit": urlunsplit}

    @app.template_global(name="tag_colour")
    def tag_colour_template_function(tag: str) -> int:
        return tag_colour(tag)

    @app.template_global(name="modify_query")
    def modify_query(**new_args):
        args = flask.request.args.copy()

        for key, value in new_args.items():
            if value is not None:
                args[key] = value
            else:
                # None is a request to unset
                del args[key]

        return "?%s" % url_encode(args)

    return app
示例#2
0
def bg_worker(log_level):
    load_config()
    proc.set_dlq(
        missive.dlq.sqlite.SQLiteDLQ(
            environ["QM_MISSIVE_SQLITE_DLQ_CONNSTRING"]))
    configure_logging(log_level)
    adapted_proc = RabbitMQAdapter(
        PickleMessage,
        proc,
        [environ["QM_RABBITMQ_BG_WORKER_TOPIC"]],
        url_or_conn=environ["QM_RABBITMQ_URL"],
    )
    adapted_proc.run()
示例#3
0
def test_load_config_with_test_config():
    with patch.dict(
        environ,
        {
            "QM_PASSWORD": "",
            "QM_SECRET_KEY": "",
            "QM_SQL_URL": "",
            "QM_RESPONSE_BODY_BUCKET_NAME": "",
            "QM_AWS_SECRET_ACCESS_KEY": "",
            "QM_AWS_ACCESS_KEY": "",
            "QM_AWS_REGION_NAME": "",
            "QM_AWS_S3_ENDPOINT_URL": "",
        },
        clear=True,
    ):
        sut.load_config()
示例#4
0
def test_load_config_with_test_config():
    with patch.dict(
            environ,
        {
            "QM_AWS_ACCESS_KEY": "",
            "QM_AWS_REGION_NAME": "",
            "QM_AWS_S3_ENDPOINT_URL": "",
            "QM_AWS_SECRET_ACCESS_KEY": "",
            "QM_ICON_BUCKET_NAME": "",
            "QM_MISSIVE_SQLITE_DLQ_CONNSTRING": "",
            "QM_PASSWORD": "",
            "QM_RABBITMQ_BG_WORKER_TOPIC": "",
            "QM_RABBITMQ_URL": "",
            "QM_REDDIT_CLIENT_ID": "",
            "QM_REDDIT_CLIENT_SECRET": "",
            "QM_RESPONSE_BODY_BUCKET_NAME": "",
            "QM_SECRET_KEY": "",
            "QM_SQL_URL": "",
        },
            clear=True,
    ):
        sut.load_config()
示例#5
0
def test_load_config_with_incomplete_env_ini():
    with patch.dict(environ, clear=True):
        with pytest.raises(RuntimeError) as e:
            sut.load_config(env_ini=path.join(test_data_path, "incomplete-env.ini"))
示例#6
0
def test_load_config_with_env_ini():
    with patch.dict(environ, clear=True):
        sut.load_config(env_ini=path.join(test_data_path, "env.ini"))
        for key in sut.REQUIRED_CONFIG_KEYS:
            assert key in environ
示例#7
0
def test_load_config_with_incomplete_config():
    with patch.dict(environ, clear=True):
        with pytest.raises(RuntimeError) as e:
            sut.load_config()
        message = e.value.args[0]
        assert "QM_SQL_URL" in message
示例#8
0
def init_app() -> flask.Flask:
    configure_logging()
    load_config(env_ini=environ.get("QM_ENV_INI", None))
    app = flask.Flask("quarchive")
    app.config["SECRET_KEY"] = environ["QM_SECRET_KEY"]
    app.config["SQLALCHEMY_DATABASE_URI"] = environ["QM_SQL_URL"]
    app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
    app.config["CRYPT_CONTEXT"] = CryptContext(["argon2", "bcrypt"])
    app.config["SESSION_COOKIE_SAMESITE"] = "Lax"

    # This configuration option, enabled by default, causes cookies to be
    # refreshed (echoed) for many endpoints where that's not appropriate - eg
    # those in the icon blueprint
    app.config["SESSION_REFRESH_EACH_REQUEST"] = False

    # By default Postgres will consult the locale to decide what timezone to
    # return datetimes in.  We want UTC in all cases.
    app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {
        "connect_args": {
            "options": "-c timezone=utc"
        }
    }

    app.config["PAGE_SIZE"] = 30
    db.init_app(app)
    with app.app_context():
        log.info("using engine: %s", db.session.bind)

    cors.init_app(app)
    babel = Babel(app,
                  default_locale="en_GB",
                  default_timezone="Europe/London")
    app.register_blueprint(web_blueprint)
    app.register_blueprint(icon_blueprint)
    app.register_blueprint(sync_blueprint)

    @babel.timezoneselector
    def use_user_timezone():
        current_user = get_current_user()
        if current_user is not None:
            tzinfo = current_user.timezone
            log.debug("using user timezone: %s", tzinfo)
            return tzinfo.zone

    @app.context_processor
    def urlsplit_cp():
        return {"urlunsplit": urlunsplit}

    @app.template_global(name="tag_colour")
    def tag_colour_template_function(tag: str) -> int:
        return tag_colour(tag)

    @app.template_global(name="modify_query")
    def modify_query(**new_args):
        args = flask.request.args.copy()

        for key, value in new_args.items():
            if value is not None:
                args[key] = value
            else:
                # None is a request to unset
                del args[key]

        return "?%s" % url_encode(args)

    @app.before_first_request
    def log_db() -> None:
        flask.current_app.logger.info("using engine: %s", db.session.bind)

    return app