Example #1
0
def app():
    app = Flask(  # pylint: disable=redefined-outer-name
        __name__,
        template_folder=Path(pcapi.__path__[0]) / "templates",
    )

    app.config["SQLALCHEMY_DATABASE_URI"] = settings.DATABASE_URL_TEST
    app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
    app.config["SECRET_KEY"] = "@##&6cweafhv3426445"
    app.config["REMEMBER_COOKIE_HTTPONLY"] = False
    app.config["SESSION_COOKIE_HTTPONLY"] = False
    app.config["TESTING"] = True
    app.url_map.strict_slashes = False
    app.json_encoder = EnumJSONEncoder

    login_manager = LoginManager()
    login_manager.init_app(app)
    db.init_app(app)

    app.app_context().push()
    install_database_extensions(app)

    run_migrations()

    install_activity()
    install_routes(app)
    install_local_providers()
    admin.init_app(app)
    install_admin_views(admin, db.session)

    app.mailjet_client = Mock()
    app.redis_client = Mock()
    app.register_blueprint(native_v1, url_prefix="/native/v1")

    JWTManager(app)

    return app
rate_limiter.init_app(app)


@app.teardown_request
def remove_db_session(
        exc: typing.Optional[Exception] = None,  # pylint: disable=unused-argument
) -> None:
    try:
        db.session.remove()
    except AttributeError:
        pass


admin.init_app(app)
db.init_app(app)
orm.configure_mappers()
login_manager.init_app(app)
install_commands(app)

app.url_map.strict_slashes = False

with app.app_context():
    app.redis_client = redis.from_url(url=settings.REDIS_URL,
                                      decode_responses=True)


@app.shell_context_processor
def get_shell_extra_context():
    # We abuse `shell_context_processor` to call custom code when
    # `flask shell` is run.