Exemple #1
0
def init_app(app):
    """
    CloudAlbum application initializer
    :param app: Flask.app
    :return: initialized application
    """
    csrf = CSRFProtect()
    csrf.init_app(app)

    # Regist error handler
    app.register_error_handler(404, errorHandler.not_found)
    app.register_error_handler(405, errorHandler.server_error)
    app.register_error_handler(500, errorHandler.server_error)
    app.register_error_handler(400, errorHandler.csrf_error)

    # CSRF setup for Flask Blueprint module
    userView.blueprint.before_request(csrf.protect)
    siteView.blueprint.before_request(csrf.protect)

    # Regist Flask Blueprint module
    app.register_blueprint(siteView.blueprint, url_prefix='/')
    app.register_blueprint(userView.blueprint, url_prefix='/users')
    app.register_blueprint(photoView.blueprint, url_prefix='/photos')

    # # Setup application configuration
    app.secret_key = conf['FLASK_SECRET']
    app.jinja_env.globals['url_for_other_page'] = url_for_other_page

    # Logger setup
    app.config['LOGGING_LEVEL'] = get_log_level()
    app.config['LOGGING_FORMAT'] = conf['LOGGING_FORMAT']
    app.config['LOGGING_LOCATION'] = conf['LOG_FILE_PATH']
    app.config['LOGGING_FILENAME'] = os.path.join(conf['LOG_FILE_PATH'],
                                                  conf['LOG_FILE_NAME'])
    app.config['LOGGING_MAX_BYTES'] = conf['LOGGING_MAX_BYTES']
    app.config['LOGGING_BACKUP_COUNT'] = conf['LOGGING_BACKUP_COUNT']

    util.log_path_check(conf['LOG_FILE_PATH'])
    file_handler = RotatingFileHandler(
        app.config['LOGGING_FILENAME'],
        maxBytes=app.config['LOGGING_MAX_BYTES'],
        backupCount=app.config['LOGGING_BACKUP_COUNT'])
    file_handler.setFormatter(Formatter(app.config['LOGGING_FORMAT']))
    file_handler.setLevel(app.config['LOGGING_LEVEL'])

    app.logger.addHandler(file_handler)
    app.logger.setLevel(app.config['LOGGING_LEVEL'])
    app.logger.info("logging start")

    # Setup LoginManager
    login.init_app(app)
    login.login_view = 'userView.signin'

    return app
def init_app(app):
    """
    CloudAlbum application initializer
    :param app: Flask.app
    :return: initialized application
    """
    csrf = CSRFProtect()
    csrf.init_app(app)

    # Regist error handler
    app.register_error_handler(404, errorHandler.not_found)
    app.register_error_handler(405, errorHandler.server_error)
    app.register_error_handler(500, errorHandler.server_error)
    app.register_error_handler(400, errorHandler.csrf_error)

    # CSRF setup for Flask Blueprint module
    userView.blueprint.before_request(csrf.protect)
    siteView.blueprint.before_request(csrf.protect)

    # Regist Flask Blueprint module
    app.register_blueprint(siteView.blueprint, url_prefix='/')
    app.register_blueprint(userView.blueprint, url_prefix='/users')
    app.register_blueprint(photoView.blueprint, url_prefix='/photos')

    # Setup application configuration
    app.secret_key = conf['FLASK_SECRET']
    app.config['SQLALCHEMY_DATABASE_URI'] = conf['DB_URL']
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = conf[
        'SQLALCHEMY_TRACK_MODIFICATIONS']
    app.config['SQLALCHEMY_ECHO'] = conf['DB_ECHO_FLAG']

    # SQLITE doesn't support DB connection pool
    if 'sqlite' not in conf['DB_URL'].lower():
        app.config['SQLALCHEMY_POOL_SIZE'] = conf['DB_POOL_SIZE']
        app.config['SQLALCHEMY_MAX_OVERFLOW'] = conf['DB_MAX_OVERFLOW']
        app.config['SQLALCHEMY_POOL_TIMEOUT'] = conf[
            'DB_SQLALCHEMY_POOL_TIMEOUT']
        app.config['SQLALCHEMY_POOL_RECYCLE'] = conf[
            'DB_SQLALCHEMY_POOL_RECYCLE']

    app.jinja_env.globals['url_for_other_page'] = url_for_other_page

    # Logger setup
    app.config['LOGGING_LEVEL'] = get_log_level()
    app.config['LOGGING_FORMAT'] = conf['LOGGING_FORMAT']
    app.config['LOGGING_LOCATION'] = conf['LOG_FILE_PATH']
    app.config['LOGGING_FILENAME'] = os.path.join(conf['LOG_FILE_PATH'],
                                                  conf['LOG_FILE_NAME'])
    app.config['LOGGING_MAX_BYTES'] = conf['LOGGING_MAX_BYTES']
    app.config['LOGGING_BACKUP_COUNT'] = conf['LOGGING_BACKUP_COUNT']

    util.log_path_check(conf['LOG_FILE_PATH'])
    file_handler = RotatingFileHandler(
        app.config['LOGGING_FILENAME'],
        maxBytes=app.config['LOGGING_MAX_BYTES'],
        backupCount=app.config['LOGGING_BACKUP_COUNT'])
    file_handler.setFormatter(Formatter(app.config['LOGGING_FORMAT']))
    file_handler.setLevel(app.config['LOGGING_LEVEL'])

    app.logger.addHandler(file_handler)
    app.logger.setLevel(app.config['LOGGING_LEVEL'])
    app.logger.info("logging start")

    # Setup LoginManager
    login.init_app(app)
    login.login_view = 'userView.signin'

    # Setup models for DB operations
    with app.app_context():
        models.db.init_app(app)
        try:
            models.db.create_all()
        except Exception as e:
            app.logger.error(e)
            exit(-1)
    return app
application.config['LOGGING_BACKUP_COUNT'] = conf['LOGGING_BACKUP_COUNT']

util.log_path_check(conf['LOG_FILE_PATH'])
file_handler = RotatingFileHandler(
    application.config['LOGGING_FILENAME'],
    maxBytes=application.config['LOGGING_MAX_BYTES'],
    backupCount=application.config['LOGGING_BACKUP_COUNT'])
file_handler.setFormatter(Formatter(application.config['LOGGING_FORMAT']))
file_handler.setLevel(application.config['LOGGING_LEVEL'])

application.logger.addHandler(file_handler)
application.logger.setLevel(application.config['LOGGING_LEVEL'])
application.logger.info("logging start")

# Setup LoginManager
login.init_app(application)
login.login_view = 'userView.signin'

# Setup models for DB operations
with application.app_context():
    models.db.init_app(application)
    try:
        models.db.create_all()
    except Exception as e:
        application.logger.error(e)
        exit(-1)

application.logger.debug('DB_URL: {0}'.format(conf['DB_URL']))
application.logger.debug('GMAPS_KEY: {0}'.format(conf['GMAPS_KEY']))

if __name__ == '__main__':