()
添加、编辑用户
添加、编辑用户
添加、编辑用户
def register():
    """ 添加、编辑用户 """
    data = request.json
    name = data.get('name')
    account = data.get('account')
    password = data.get('password')
    status_password = data.get('statusPassword')
    role_id = data.get('role_id')
    user_id = data.get('id')
    if user_id:
        old_data = User.query.filter_by(id=user_id).first()
        if User.query.filter_by(name=name).first() and name != old_data.name:
            return jsonify({'msg': '名字已存在', 'status': 0})
        elif User.query.filter_by(account=account).first() and account != old_data.account:
            return jsonify({'msg': '账号已存在', 'status': 0})

        if status_password:
            if not password:
                return jsonify({'msg': '密码不能为空', 'status': 0})
            else:
                old_data.password = password
        old_data.name = name
        old_data.account = account
        old_data.role_id = role_id
        db.session.commit()
        return jsonify({'msg': '修改成功', 'status': 1})
    else:
        if User.query.filter_by(name=name).first():
            return jsonify({'msg': '名字已存在', 'status': 0})
        elif User.query.filter_by(account=account).first():
            return jsonify({'msg': '账号已存在', 'status': 0})
        else:
            user = User(name=name, account=account, password=password, status=1, role_id=role_id)
            db.session.add(user)
            db.session.commit()
            return jsonify({'msg': '注册成功', 'status': 1})
def register ( ) : data = request . json name = data . get ( 'name' ) account = data . get ( 'account' ) password = data . get ( 'password' ) status_password = data . get ( 'statusPassword' ) role_id = data . get ( 'role_id' ) user_id = data . get ( 'id' ) if user_id : old_data = User . query . filter_by ( id = user_id ) . first ( ) if User . query . filter_by ( name = name ) . first ( ) and name != old_data . name : return jsonify ( { 'msg' : '名字已存在', 'status' : 0}) elif User . query . filter_by ( account = account ) . first ( ) and account != old_data . account : return jsonify ( { 'msg' : '账号已存在', 'status' : 0}) if status_password : if not password : return jsonify ( { 'msg' : '密码不能为空', 'status':  0 ) else : old_data . password = password old_data . name = name old_data . account = account old_data . role_id = role_id db . session . commit ( ) return jsonify ( { 'msg' : '修改成功', 'statu s : 1}) else : if User . query . filter_by ( name = name ) . first ( ) : return jsonify ( { 'msg' : '名字已存在', 'status' : 0}) elif User . query . filter_by ( account = account ) . first ( ) : return jsonify ( { 'msg' : '账号已存在', 'status' : 0}) else : user = User ( name = name , account = account , password = password , status = 1 , role_id = role_id ) db . session . add ( user ) db . session . commit ( ) return jsonify ( { 'msg' : '注册成功', 'statu s : 1})