Example #1
0
def create(config=None):
    """
    Create and configure a Flask application object.

    Args:
        config (dict): The configuration to use when creating the application.
            If no configuration is provided, :data:`anitya.config.config` is
            used.

    Returns:
        flask.Flask: The configured Flask application.
    """
    app = flask.Flask(__name__)

    if config is None:
        config = anitya_config
    app.config.update(config)
    initialize_db(config)

    app.register_blueprint(social_auth)
    if len(social_models.UserSocialAuth.__table_args__) == 0:
        # This is a bit of a hack - this initialization call sets up the SQLAlchemy
        # models with our own models and multiple calls to this function will cause
        # SQLAlchemy to fail with sqlalchemy.exc.InvalidRequestError. Only calling it
        # when there are no table arguments should ensure we only call it one time.
        #
        # Be aware that altering the configuration values this function uses, namely
        # the SOCIAL_AUTH_USER_MODEL, after the first time ``create`` has been called
        # will *not* cause the new configuration to be used for subsequent calls to
        # ``create``.
        social_models.init_social(app, Session)

    login_manager = LoginManager()
    login_manager.user_loader(authentication.load_user_from_session)
    login_manager.request_loader(authentication.load_user_from_request)
    login_manager.login_view = "/login/"
    login_manager.init_app(app)

    # Register the v2 API resources
    packages_view = api_v2.PackagesResource.as_view("apiv2.packages")
    app.add_url_rule("/api/v2/packages/",
                     view_func=packages_view,
                     methods=["GET", "POST"])
    projects_view = api_v2.ProjectsResource.as_view("apiv2.projects")
    app.add_url_rule("/api/v2/projects/",
                     view_func=projects_view,
                     methods=["GET", "POST"])
    versions_view = api_v2.VersionsResource.as_view("apiv2.versions")
    app.add_url_rule("/api/v2/versions/",
                     view_func=versions_view,
                     methods=["GET", "POST"])

    # Register all the view blueprints
    app.register_blueprint(ui.ui_blueprint)
    app.register_blueprint(api.api_blueprint)

    app.before_request(global_user)
    app.teardown_request(shutdown_session)
    app.register_error_handler(IntegrityError, integrity_error_handler)
    app.register_error_handler(AuthException, auth_error_handler)

    app.context_processor(inject_variable)

    # subscribe to signals
    user_logged_in.connect(when_user_log_in, app)

    if app.config.get("EMAIL_ERRORS"):
        # If email logging is configured, set up the anitya logger with an email
        # handler for any ERROR-level logs.
        _anitya_log = logging.getLogger("anitya")
        _anitya_log.addHandler(
            anitya.mail_logging.get_mail_handler(
                smtp_server=app.config.get("SMTP_SERVER"),
                mail_admin=app.config.get("ADMIN_EMAIL"),
            ))

    return app
Example #2
0
def create(config=None):
    """
    Create and configure a Flask application object.

    Args:
        config (dict): The configuration to use when creating the application.
            If no configuration is provided, :data:`anitya.config.config` is
            used.

    Returns:
        flask.Flask: The configured Flask application.
    """
    app = flask.Flask(__name__)

    if config is None:
        config = anitya_config
    app.config.update(config)
    initialize_db(config)

    app.register_blueprint(social_auth)
    if len(social_models.UserSocialAuth.__table_args__) == 0:
        # This is a bit of a hack - this initialization call sets up the SQLAlchemy
        # models with our own models and multiple calls to this function will cause
        # SQLAlchemy to fail with sqlalchemy.exc.InvalidRequestError. Only calling it
        # when there are no table arguments should ensure we only call it one time.
        #
        # Be aware that altering the configuration values this function uses, namely
        # the SOCIAL_AUTH_USER_MODEL, after the first time ``create`` has been called
        # will *not* cause the new configuration to be used for subsequent calls to
        # ``create``.
        social_models.init_social(app, Session)

    login_manager = LoginManager()
    login_manager.user_loader(authentication.load_user_from_session)
    login_manager.request_loader(authentication.load_user_from_request)
    login_manager.login_view = '/login/'
    login_manager.init_app(app)

    # Register the v2 API resources
    app.api = Api(app)
    app.api.add_resource(api_v2.ProjectsResource, '/api/v2/projects/')
    app.api.add_resource(api_v2.PackagesResource, '/api/v2/packages/')

    # Register all the view blueprints
    app.register_blueprint(ui.ui_blueprint)
    app.register_blueprint(api.api_blueprint)

    app.before_request(global_user)
    app.teardown_request(shutdown_session)

    app.context_processor(inject_variable)

    if app.config.get('EMAIL_ERRORS'):
        # If email logging is configured, set up the anitya logger with an email
        # handler for any ERROR-level logs.
        _anitya_log = logging.getLogger('anitya')
        _anitya_log.addHandler(
            anitya.mail_logging.get_mail_handler(
                smtp_server=app.config.get('SMTP_SERVER'),
                mail_admin=app.config.get('ADMIN_EMAIL')))

    return app
Example #3
0
def create(config=None):
    """
    Create and configure a Flask application object.

    Args:
        config (dict): The configuration to use when creating the application.
            If no configuration is provided, :data:`anitya.config.config` is
            used.

    Returns:
        flask.Flask: The configured Flask application.
    """
    app = flask.Flask(__name__)

    if config is None:
        config = anitya_config
    app.config.update(config)
    initialize_db(config)

    app.register_blueprint(social_auth)
    if len(social_models.UserSocialAuth.__table_args__) == 0:
        # This is a bit of a hack - this initialization call sets up the SQLAlchemy
        # models with our own models and multiple calls to this function will cause
        # SQLAlchemy to fail with sqlalchemy.exc.InvalidRequestError. Only calling it
        # when there are no table arguments should ensure we only call it one time.
        #
        # Be aware that altering the configuration values this function uses, namely
        # the SOCIAL_AUTH_USER_MODEL, after the first time ``create`` has been called
        # will *not* cause the new configuration to be used for subsequent calls to
        # ``create``.
        social_models.init_social(app, Session)

    login_manager = LoginManager()
    login_manager.user_loader(authentication.load_user_from_session)
    login_manager.request_loader(authentication.load_user_from_request)
    login_manager.login_view = "/login/"
    login_manager.init_app(app)

    # Register the v2 API resources
    app.api = Api(app)
    app.api.add_resource(api_v2.ProjectsResource, "/api/v2/projects/")
    app.api.add_resource(api_v2.PackagesResource, "/api/v2/packages/")

    # Register all the view blueprints
    app.register_blueprint(ui.ui_blueprint)
    app.register_blueprint(api.api_blueprint)

    app.before_request(global_user)
    app.teardown_request(shutdown_session)
    app.register_error_handler(IntegrityError, integrity_error_handler)
    app.register_error_handler(AuthException, auth_error_handler)

    app.context_processor(inject_variable)

    # subscribe to signals
    user_logged_in.connect(when_user_log_in, app)

    if app.config.get("EMAIL_ERRORS"):
        # If email logging is configured, set up the anitya logger with an email
        # handler for any ERROR-level logs.
        _anitya_log = logging.getLogger("anitya")
        _anitya_log.addHandler(
            anitya.mail_logging.get_mail_handler(
                smtp_server=app.config.get("SMTP_SERVER"),
                mail_admin=app.config.get("ADMIN_EMAIL"),
            )
        )

    return app