def create(self, article: Article): """新增文章 Args: article: class, 文章对象 :return 返回dict """ article.id = md5_id() with db.auto_commit(): db.session.add(article) return {"id": article.id, "title": article.title}
def destory(self, user_id: str): """删除用户 Args: user_id: string, """ user = User.query.filter(User.id == user_id, User.deleted_at == DELETED_AT).first() if user is None: return False with db.auto_commit(): user.update({"deleted_at": datetime.now()}) return True
def destory(self, id: str) -> bool: """删除文章 Args: id: string, 文章ID """ row = Article.query.filter_by(id=id) if row is None: return False with db.auto_commit(): row.update({"deleted_at": datetime.now()}) return True
def create(self, name: str): """新增分类 Args: name: string, 类名,值唯一 """ exist = Category.query.filter(Category.name==name).first() if exist: return output_json(code=ErrCode.EXIST_DATA) row = Category(name=name) row.id = md5_id() with db.auto_commit(): db.session.add(row) return output_json(code=0)
def bindCategory(self, name: str, aid: str) -> bool: """文章添加到某个分类 Args: name: string, 分类名 aid: string, 文章ID :return False: 文章ID不存在 True: 添加成功 """ # 文章是否存在 article = Article.query.filter(Article.id == aid).first() if article is None: return False category = Category.query.filter(Category.name == name).first() # 分类不存在时,添加 if category is None: row = Category(name=name) row.id = md5_id() cid = row.id with db.auto_commit(): db.session.add(row) else: cid = category.id # 文章id与分类id绑定 with db.auto_commit(): # duplicate key update # 若已存在则更新, 否则插入 date = datetime.now() sql = "insert into tomato_category_relationship(cid, aid) values('%s', '%s') \ on duplicate key update updated_at = '%s';" % ( cid, aid, date.strftime(DATETIME_FORMAT)) db.engine.execute(sql) return True
def update(self, id: str, kwargs: object): """编辑文章 Args: id: string, 文章ID kwargs: object, 更新的属性 :title: string :status: int Return bool """ row = Article.query.filter_by(id=id) if row is None: return False with db.auto_commit(): row.update(kwargs) return True
def update(self, id: str, kwargs: object): """更新分类 Args: id: string, kwargs: object, 包含category属性的对象 """ exist = Category.query.filter_by(id=id) if exist is None: return output_json(code=ErrCode.NO_DATA) allow_field = ["name"] for key in kwargs.keys(): if key not in allow_field: return output_json(code=ErrCode.ERR_PARAMS) if hasattr(kwargs, "name"): with db.auto_commit(): exist.update(kwargs) return output_json(code=0)
def update(self, user_id: str, user: object): """更新用户信息 Args: user_id: string user: object, User对象 - name: string, 用户名 - email: string, 用户邮箱 - role_id: string, 用户所属角色ID """ query = User.query exist = query.filter_by(id=user_id) if not exist: return ErrCode.NO_DATA # update with db.auto_commit(): exist.update(user) return ErrCode.SUCCESS
def unbindCategory(self, name: str, aid: str) -> bool: """解绑文章与分类关系 Args: name: string, 分类名 aid: string, 文章ID :return False: 分类不存在 True: 解绑成功 """ category = Category.query.filter(Category.name == name).first() # 更新绑定deleted_at字段值设为已删除 if category: with db.auto_commit(): Category_Relationship.query\ .filter(Category_Relationship.cid==category.id, Category_Relationship.aid==aid)\ .update({"deleted_at": datetime.now()}) else: return False return True
def changePasswd(self, user_id: str, **kwagrs): """修改密码 Args: user_id: string kwagrs: - new_passwd: string, 新密码 - old_passwd: string, 原密码 """ row = User.query.filter(User.id == user_id, User.deleted_at == DELETED_AT).first() if row is None: return ErrCode.NO_DATA row = row.to_dict() if not decrypt(kwagrs.get("old_passwd"), bytes(row["password"], encoding="utf8")): return ErrCode.ERR_PASSWD with db.auto_commit(): User.query.filter_by(id=user_id).update( {"password": encrypt(kwagrs["new_passwd"])}) return ErrCode.SUCCESS
def create(self, user: User): """创建用户 Args: user: class, User对象 :return bool, 创建成功返回True, 否则False """ # 检查用户名, 邮箱是否已存在 exist = User.query\ .filter(db.or_(User.name==user.name, User.email==user.email))\ .filter(User.deleted_at == DELETED_AT)\ .first() if exist is None: # 密码加密 user.password = encrypt(user.password) user.id = md5_id() with db.auto_commit(): db.session.add(user) else: return ErrCode.EXIST_DATA return ErrCode.SUCCESS