Beispiel #1
0
def create_app():
    app = Flask(__name__)

    app.config.from_object('app.config')

    register_blueprint(app)

    db.init_app(app)

    mail.init_app(app)

    login_manager.init_app(app)
    login_manager.login_view = 'web.login'
    login_manager.login_message = '请先登录或注册'

    # 第一次运行成功之后可以把34-45行注释掉
    with app.app_context():

        db.create_all(app=app)
        if not Admin.query.filter_by(nickname='admin').first():
            ad = Admin()
            ad.nickname = 'admin'
            ad.role = 'super'
            ad.password = generate_password_hash('123456')

            with db.auto_commit():
                db.session.add(ad)
    return app
Beispiel #2
0
def create_admin(name, password, email):
    """
    设置管理员
    :param name:
    :param password:
    :param email:
    :return:
    """
    with db.auto_commit():
        admin = Admin()
        admin.name = name
        admin.password = password
        admin.email = email
        admin.avatar = '/static/admin/images/guest.png'
        admin.role = Role.query.get(1)
        db.session.add(admin)
    print("成功添加管理员,用户名:%s 密码:%s 邮箱:%s" % (name, password, email))