def upload(): image = request.files.get("image", None) if not image: raise ParameterError("没有找到图片") if image and allowed_file(image.filename): path = os.path.join(lin_config.get_config("oss.upload_folder"), image.filename) image.save(path) else: raise ParameterError("图片类型不允许或图片key不合法") return Success()
def update_user(uid: int, json: UpdateUserInfoSchema): """ 更新用户信息 """ user = manager.user_model.get(id=uid) if user is None: raise NotFound("用户不存在") if user.email != g.email: exists = manager.user_model.get(email=g.email) if exists: raise ParameterError("邮箱已被注册,请重新输入邮箱") with db.auto_commit(): user.email = g.email group_ids = g.group_ids # 清空原来的所有关联关系 manager.user_group_model.query.filter_by(user_id=user.id).delete( synchronize_session=False) # 根据传入分组ids 新增关联记录 user_group_list = list() # 如果没传分组数据,则将其设定为 guest 分组 if not group_ids: group_ids = [ manager.group_model.get(level=GroupLevelEnum.GUEST.value).id ] for group_id in group_ids: user_group = manager.user_group_model() user_group.user_id = user.id user_group.group_id = group_id user_group_list.append(user_group) db.session.add_all(user_group_list) return Success("操作成功")
def validate_email(self, value): if value.data: if not re.match( r"^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$", value.data, ): raise ParameterError("电子邮箱不符合规范,请输入正确的邮箱")
def update(json: UserBaseInfoSchema): """ 更新用户信息 """ user = get_current_user() if g.email and user.email != g.email: exists = manager.user_model.get(email=g.email) if exists: raise ParameterError("邮箱已被注册,请重新输入邮箱") with db.auto_commit(): if g.email: user.email = g.email if g.nickname: user.nickname = g.nickname if g.avatar: user._avatar = g.avatar return Success("用户信息更新成功")
def upload_to_ali(): image = request.files.get("image", None) if not image: raise ParameterError("没有找到图片") if image and allowed_file(image.filename): url = upload_image_bytes(image.filename, image) if url: res = {"url": url} with db.auto_commit(): exist = OSS.get(url=url) if not exist: data = {"url": url} one = OSS.create(**data) db.session.flush() res["id"] = one.id else: res["id"] = exist.id return jsonify(res) return Failed("上传图片失败,请检查图片路径")
def paginate(): from lin import ParameterError count = int( request.args.get( "count", current_app.config.get("COUNT_DEFAULT") if current_app.config.get("COUNT_DEFAULT") else 5, )) start = int( request.args.get( "page", current_app.config.get("PAGE_DEFAULT") if current_app.config.get("PAGE_DEFAULT") else 0, )) count = 15 if count >= 15 else count start = start * count if start < 0 or count < 0: raise ParameterError() return start, count
def upload_multiple_to_ali(): imgs = [] for item in request.files: img = request.files.get(item, None) if not img: raise ParameterError("没接收到图片,请检查图片路径") if img and allowed_file(img.filename): url = upload_image_bytes(img.filename, img) if url: # 每上传成功一次图片需记录到数据库 with db.auto_commit(): exist = OSS.get(url=url) if not exist: data = {"url": url} res = OSS.create(**data) db.session.flush() imgs.append({"key": item, "url": url, "id": res.id}) else: imgs.append({"key": item, "url": url, "id": exist.id}) return jsonify(imgs)
def check_username(cls, v, values, **kwargs): if not re.match(r"^[a-zA-Z0-9_]{2,10}$", v): raise ParameterError("用户名只能由字母、数字、下划线组成,且长度为2-10位") return v
def passwords_match(cls, v, values, **kwargs): if v != values["password"]: raise ParameterError("两次输入的密码不一致,请输入相同的密码") return v
path="/cms/user/login", permission="", commit=True, ) access_token, refresh_token = get_tokens(user) return LoginTokenSchema(access_token=access_token, refresh_token=refresh_token) @user_api.route("", methods=["PUT"]) @permission_meta(name="用户更新信息", module="用户", mount=False) @login_required @api.validate( tags=["用户"], security=[AuthorizationBearerSecurity], resp=DocResponse(Success("用户信息更新成功"), ParameterError("邮箱已被注册,请重新输入邮箱")), ) def update(json: UserBaseInfoSchema): """ 更新用户信息 """ user = get_current_user() if g.email and user.email != g.email: exists = manager.user_model.get(email=g.email) if exists: raise ParameterError("邮箱已被注册,请重新输入邮箱") with db.auto_commit(): if g.email: user.email = g.email if g.nickname:
manager.user_group_model.query.filter_by(user_id=uid).delete( synchronize_session=False) user.hard_delete() return Success("操作成功") @admin_api.route("/user/<int:uid>", methods=["PUT"]) @permission_meta(name="管理员更新用户信息", module="管理员", mount=False) @admin_required @api.validate( tags=["管理员"], security=[AuthorizationBearerSecurity], resp=DocResponse( NotFound("用户不存在"), Success("更新成功"), ParameterError("邮箱已被注册,请重新输入邮箱"), ), ) def update_user(uid: int, json: UpdateUserInfoSchema): """ 更新用户信息 """ user = manager.user_model.get(id=uid) if user is None: raise NotFound("用户不存在") if user.email != g.email: exists = manager.user_model.get(email=g.email) if exists: raise ParameterError("邮箱已被注册,请重新输入邮箱") with db.auto_commit(): user.email = g.email
def check_permission_id(cls, v, values, **kwargs): if v <= 0: raise ParameterError("权限ID必须大于0") return v
def validate_group_ids(self, value): for group_id in value.data: if not manager.group_model.count_by_id(group_id): raise ParameterError("分组不存在")
def validate_nickname(self, value): if value.data: length = len(value.data) if length < 2 or length > 10: raise ParameterError("昵称长度必须在2~10之间")
def validate_permission_id(self, value): exists = manager.permission_model.get(id=value.data) if not exists: raise ParameterError("权限不存在")
def check_group_id(cls, v, values, **kwargs): if v <= 0: raise ParameterError("用户组ID必须大于0") return v