Example #1
0
def configure_extensions(app):
    from models import Anonymous
    import admin
    from flask_app import socket
    import commands

    db.init_app(app)
    migrate.init_app(
        app,
        db,
        render_as_batch=app.config["SQLALCHEMY_DATABASE_URI"].startswith(
            "sqlite:"))
    login.init_app(app)
    login.login_view = "main.index"
    login.login_message = None
    login.anonymous_user = Anonymous
    admin.init_app(app, db)
    mail.init_app(app)
    cors.init_app(app,
                  resources={r"/*": {
                      "origins": app.config["ALLOWED_URLS"]
                  }},
                  supports_credentials=True)
    socket.init_app(app)
    commands.init_app(app)
Example #2
0
def create_app(config_class=Config):
    app = Flask(__name__)
    # app.config['SECRET_KEY']='dai3hoahudhiuahduiah'
    # app.config['BABEL_DEFAULT_LOCALE'] = 'zh_Hans_CN'
    app.config.from_object(config_class)

    mongo.init_app(app)
    login.init_app(app)
    mail.init_app(app)
    bootstrap.init_app(app)
    moment.init_app(app)
    babel.init_app(app)
    from app.errors import bp as errors_bp
    app.register_blueprint(errors_bp)

    from app.auth import bp as auth_bp
    app.register_blueprint(auth_bp, url_prefix='/auth')

    from app.main import bp as main_bp
    app.register_blueprint(main_bp)

    from app.admin import bp as admin_bp
    app.register_blueprint(admin_bp, url_prefix='/admin')

    if not app.debug and not app.testing:
        if app.config['MAIL_SERVER']:
            auth = None
            if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
                auth = (app.config['MAIL_USERNAME'],
                        app.config['MAIL_PASSWORD'])
            secure = None
            if app.config['MAIL_USE_TLS']:
                secure = ()
            mail_handler = SMTPHandler(
                mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
                fromaddr='no-reply@' + app.config['MAIL_SERVER'],
                toaddrs=app.config['ADMINS'],
                subject='Microblog Failure',
                credentials=auth,
                secure=secure)
            mail_handler.setLevel(logging.ERROR)
            app.logger.addHandler(mail_handler)
        if not os.path.exists('logs'):
            os.mkdir('logs')
        file_handler = RotatingFileHandler('logs/microblog.log',
                                           maxBytes=10240,
                                           backupCount=10)
        file_handler.setFormatter(
            logging.Formatter(
                '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'
            ))
        file_handler.setLevel(logging.INFO)
        app.logger.addHandler(file_handler)

        app.logger.setLevel(logging.INFO)
        app.logger.info('Project System Startup')
    return app
Example #3
0
def create_app(config_class=Config):
    app = Flask(__name__)
    # app.config['SECRET_KEY']='dai3hoahudhiuahduiah'
    # app.config['BABEL_DEFAULT_LOCALE'] = 'zh_Hans_CN'
    app.config.from_object(config_class)

    db.init_app(app)  # SQLite初始化
    migrate.init_app(app, db)  # 数据库迁移初始化
    bootstrap.init_app(app)  # Bootstrap初始化
    babel.init_app(app)  # I18nL10n 初始化
    images.init_app(app)  # Imagine Normalise 初始化
    login.init_app(app)  # 用户系统 初始化
    login.login_view = 'admin.login'
    mail.init_app(app)  # 邮件系统 初始化

    configure_uploads(app, (photos, sphotos, videos))  # 文件系统初始化
    patch_request_class(app)  # 文件大小限制,默认为16MB

    cache.init_app(app, config={'CACHE_TYPE': 'simple'})  # 页面缓存 初始化

    from app.main import bp as main_bp  # 首页注册
    app.register_blueprint(main_bp)

    from app.module import bp as module_bp  # 课程模块注册
    app.register_blueprint(module_bp)

    from app.experiment import bp as experiment_bp  # 通识实验注册
    app.register_blueprint(experiment_bp, url_prefix='/experiment')

    from app.guide import bp as guide_bp  # 课程指南注册
    app.register_blueprint(guide_bp, url_prefix='/guide')

    from app.admin import bp as admin_bp  # 管理页面注册
    app.register_blueprint(admin_bp, url_prefix='/admin')

    from app.errors import bp as errors_bp  #错误处理页面注册
    app.register_blueprint(errors_bp)

    return app
Example #4
0
def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    db.init_app(app)
    migrate.init_app(app, db)
    bootstrap.init_app(app)
    babel.init_app(app)

    login.init_app(app)
    login.login_view = 'admin.login'

    configure_uploads(app, (photos, sphotos))
    patch_request_class(app)

    from app.admin import bp as admin_bp
    app.register_blueprint(admin_bp)

    from app.auth import bp as auth_bp
    from app.auth.routes import StuAPI, StuListAPI, We_Api

    api_stu = Api(auth_bp)

    api_stu.add_resource(StuListAPI, '/stu')
    api_stu.add_resource(StuAPI, '/stu/<id>')
    api_stu.add_resource(We_Api, '/weapi')
    app.register_blueprint(auth_bp)

    from app.main import bp as main_bp
    from app.main.routes import TaskAPI, TaskListAPI, OpenRes, MediaRes

    api_main = Api(main_bp)

    api_main.add_resource(TaskListAPI, '/tasks')
    api_main.add_resource(TaskAPI, '/tasks/<int:id>')
    api_main.add_resource(OpenRes, '/res')
    api_main.add_resource(MediaRes, '/media')

    app.register_blueprint(main_bp)

    from app.errors import bp as errors_bp
    app.register_blueprint(errors_bp)

    if not app.debug and not app.testing:
        # ...

        if app.config['LOG_TO_STDOUT']:
            stream_handler = logging.StreamHandler()
            stream_handler.setLevel(logging.INFO)
            app.logger.addHandler(stream_handler)
        else:
            if not os.path.exists('logs'):
                os.mkdir('logs')
            file_handler = RotatingFileHandler('logs/weapi.log',
                                               maxBytes=10240,
                                               backupCount=10)
            file_handler.setFormatter(
                logging.Formatter('%(asctime)s %(levelname)s: %(message)s '
                                  '[in %(pathname)s:%(lineno)d]'))
            file_handler.setLevel(logging.INFO)
            app.logger.addHandler(file_handler)

        app.logger.setLevel(logging.INFO)
        app.logger.info('*' * 45)
        app.logger.info('*' * 3 + '    ' * 3 + 'Weapp_API Startup' +
                        '   ' * 3 + '*' * 3)
        app.logger.info('*' * 45)

        try:
            app.logger.info('*' * 45)
            app.logger.info('*' * 3 + ' ' * 3 + app.config['SECRET_KEY'] +
                            '  ' * 3)
            app.logger.info('*' * 45)

        except KeyError:
            app.logger.info('*' * 45)
            app.logger.info('*' * 3 + '    ' * 3 + "GET ADMINS Failed" +
                            '   ' * 3)
            app.logger.info('*' * 45)

    return app