def validate_category_id(self, category_id): try: category_id_int = int(category_id.data) category_id.data = category_id_int except: raise ValidationError("category_id", "category_id不是整数") else: if category_id.data <= 0: raise ValidationError("category_id", "category_id必须大于0")
def validate_page(self, page): try: page_int = int(page.data) page.data = page_int except: raise ValidationError("page", "page不是整数") else: if page.data <= 0: raise ValidationError("page", "page必须大于0")
def validate_link_id(self, link_id): try: link_id_int = int(link_id.data) link_id.data = link_id_int except: raise ValidationError("id", "链接ID不正确") else: if link_id.data <= 0: raise ValidationError("id", "链接ID不正确")
def validate_user_id(self, user_id): try: user_id_int = int(user_id.data) user_id.data = user_id_int except: raise ValidationError("id", "用户ID不正确") else: if user_id.data <= 0: raise ValidationError("id", "用户ID不正确")
def validate_category_info(self, category_info): category_info_dict = category_info.data # 如果传入了id, 删除掉,因为添加时id字段设置为自增 if "id" in category_info_dict: category_info_dict.pop("id") # 检查是否缺少必要的信息 if "name" not in category_info_dict or category_info_dict["name"] == "": raise ValidationError("name", "类别名称不能为空") if check_category_name_duplicate(category_info_dict["name"]): raise ValidationError("name", "类别名称重复")
def validate_worthy_info(self, worthy_info): worthy_info_dict = worthy_info.data # worthy_id 是否合法 worthy_id = worthy_info_dict["id"] try: worthy_id = int(worthy_id) except ValueError: raise ValidationError("id", "推荐ID不正确") else: if worthy_id <= 0: raise ValidationError("id", "推荐ID不正确") # 检查要修改的值是否符合要求 if "img_url" in worthy_info_dict and worthy_info_dict["img_url"] == "": raise ValidationError("img_url", "图片URL不能为空")
def validate_user_info(self, user_info): user_info_dict = user_info.data # 如果传入了id, 删除掉,因为添加时id字段设置为自增 if "id" in user_info_dict: user_info_dict.pop("id") # 检查是否缺少必要的信息 # 用户名不能为空 if "name" not in user_info_dict or user_info_dict["name"] == "": raise ValidationError("name", "用户名不能为空") # 用户名合法 if not check_user_name_valid(user_info_dict["name"]): raise ValidationError("name", "用户名非法") # 用户名不能重复 if check_user_name_duplicate(user_info_dict["name"]): raise ValidationError("name", "用户名重复") # 密码不能为空 if "password" not in user_info_dict or user_info_dict["password"] == "": raise ValidationError("password", "密码不能为空") # 邮箱不能为空 if "email" not in user_info_dict or user_info_dict["email"] == "": raise ValidationError("email", "邮箱不能为空") # 邮箱合法 if not check_user_email_valid(user_info_dict["email"]): raise ValidationError("email", "邮箱不合法") # 邮箱已注册 if check_user_email_duplicate(user_info_dict["email"]): raise ValidationError("email", "邮箱已注册")
def validate_role_info(self, role_info): role_info_dict = role_info.data # 检查是否缺少必要的信息 if "name" in role_info_dict: if role_info_dict["name"] == "": raise ValidationError("name", "角色名称不能为空") if check_role_name_duplicate(role_info_dict["name"], except_id=role_info_dict["id"]): raise ValidationError("name", "角色名称重复") if "permissions" in role_info_dict: if role_info_dict["permissions"] == "": raise ValidationError("name", "角色权限不能为空") if not isinstance(role_info_dict["permissions"], list): raise ValidationError("name", "角色权限要求数组格式") # 将单个权限列表组合成一个权限值(二进制位置1,表示拥有某权限,具体权限原理,详见Permission.py文件) role_info.data["permissions"] = Permission.combine_permissions( role_info_dict["permissions"])
def validate_category_info(self, category_info): category_info_dict = category_info.data # category_id 是否合法 category_id = category_info_dict["id"] try: category_id = int(category_id) except: raise ValidationError("id", "类别ID不正确") else: if category_id <= 0: raise ValidationError("id", "类别ID不正确") # 检查要修改的值是否符合要求 if "name" in category_info_dict: if category_info_dict["name"] == "": raise ValidationError("name", "类别名称不能为空") if check_category_name_duplicate(category_info_dict['name'], except_id=category_id): raise ValidationError("name", "类别名称重复")
def validate_role_info(self, role_info): role_info_dict = role_info.data # 如果传入了id, 删除掉,因为添加时id字段设置为自增 if "id" in role_info_dict: role_info_dict.pop("id") # 检查是否缺少必要的信息 if "name" not in role_info_dict or role_info_dict["name"] == "": raise ValidationError("name", "角色名称不能为空") if check_role_name_duplicate(role_info_dict["name"]): raise ValidationError("name", "角色名称重复") if "permissions" not in role_info_dict or role_info_dict[ "permissions"] == "": raise ValidationError("name", "角色权限不能为空") if not isinstance(role_info_dict["permissions"], list): raise ValidationError("name", "角色权限要求数组格式") # 将单个权限列表组合成一个权限值(二进制位置1,表示拥有某权限,具体权限原理,详见Permission.py文件) role_info.data["permissions"] = Permission.combine_permissions( role_info_dict["permissions"])
def validate_link_info(self, link_info): link_info_dict = link_info.data # link_id 是否合法 link_id = link_info_dict["id"] try: link_id = int(link_id) except ValueError: raise ValidationError("id", "链接ID不正确") else: if link_id <= 0: raise ValidationError("id", "链接ID不正确") # 检查要修改的值是否符合要求 if "name" in link_info_dict and link_info_dict["name"] == "": raise ValidationError("name", "链接名称不能为空") if "link" in link_info_dict: if link_info_dict["link"] == "": raise ValidationError("link", "链接地址不能为空") if not check_link_valid(link_info_dict["link"]): raise ValidationError("link", "链接地址非法") if "category_id" in link_info_dict and link_info_dict[ "category_id"] == "": raise ValidationError("category_id", "类别ID不能为空") if "user_id" in link_info_dict: link_info_dict.pop("user_id")
def validate_link_info(self, link_info): link_info_dict = link_info.data # 如果传入了id, 删除掉,因为添加时id字段设置为自增 if "id" in link_info_dict: link_info_dict.pop("id") # 检查是否缺少必要的信息 if "name" not in link_info_dict or link_info_dict["name"] == "": raise ValidationError("name", "链接名称不能为空") if "link" not in link_info_dict or link_info_dict["link"] == "": raise ValidationError("link", "链接地址不能为空") if not check_link_valid(link_info_dict["link"]): raise ValidationError("link", "链接地址非法") if "category_id" not in link_info_dict or link_info_dict[ "category_id"] == "": raise ValidationError("category_id", "类别ID不能为空") # 添加操作者ID:当前登录的用户ID try: link_info_dict["user_id"] = current_user.id except: raise ValidationError("user_id", "用户ID获取失败,请检查登录状态")
def validate_user_info(self, user_info): user_info_dict = user_info.data # user_id 是否合法 user_id = user_info_dict["id"] try: user_id = int(user_id) except ValueError: raise ValidationError("id", "用户名ID不正确") else: if user_id <= 0: raise ValidationError("id", "用户名ID不正确") # 判断user_id 对应的用户是否存在数据库中 # db_user = UserManager.get_user_model_by_id(user_id) # if db_user is None: # raise ValidationError("id", "用户不存在") # 检查要修改的值是否符合要求 if "name" in user_info_dict: if user_info_dict["name"] == "": raise ValidationError("name", "用户名不能为空") if not check_user_name_valid(name=user_info_dict["name"]): raise ValidationError("name", "用户名非法") if check_user_name_duplicate(user_info_dict["name"], except_id=user_id): raise ValidationError("name", "用户名重复") if "password" in user_info_dict and user_info_dict["password"] == "": raise ValidationError("password", "密码不能为空") if "email" in user_info_dict: if user_info_dict["email"] == "": raise ValidationError("email", "邮箱不能为空") if not check_user_email_valid(user_info_dict["email"]): raise ValidationError("email", "邮箱不合法") if check_user_email_duplicate(user_info_dict["email"], except_id=user_id): raise ValidationError("email", "邮箱已注册")