def edit_user(user_id): reg_info = g.json_data u1 = User.get_by_status_or_404(user_id) # 密码不通过这种方式修改,删除提交的密码信息 user_dict = del_keys(reg_info, ['password', 'roles', 'avatar_img']) u1.update(**user_dict) return HttpHelper.json_response(ResultHelper.success(u1))
def register(): """ 用户注册 :return: json """ reg_info = g.json_data username = reg_info['account'] password = reg_info['password'] password = generate_password_hash(password) # 获取用户level level = reg_info.get('level') level = UserLevelEnum.check_value_or_default(level) # 获取用户类型 client_type = reg_info.get('client_type') client_type = ClientTypeEnum.check_value_or_default(client_type) u1 = User.get_by_account(username) if u1: usr = {'id': u1.id, 'name': u1.account} return HttpHelper.error_handler(ResultEnum.USER_ALREADY_EXIST_ERROR, data=usr) # 已经算出了加密密码,用户属性字典中去掉password属性,否则保存将覆盖 usr = User(account=username, password=password, level=level, client_type=client_type) user_dict = del_keys(reg_info, ['password', 'client_type']) result = usr.save(**user_dict) if not usr.id: return HttpHelper.error_handler(ResultEnum.USER_REGISTER_ERROR, data=result) # 添加默认的角色 default_role = AuthRole.get_default().first() auth_user_role = AuthUserRoles(role_id=default_role.id, user_id=usr.id, status=0) auth_user_role.save() ret_user = {'id': usr.id, 'name': usr.account, 'role': default_role} return HttpHelper.json_response(ResultHelper.success(ret_user))