Beispiel #1
0
def delArticle(id):
    """删除文章"""
    if id is None:
        return output_json(code=ErrCode.ERR_PARAMS)
    handler = ArticleService()
    res = handler.destory(id)

    return output_json(code=0) if res else output_json(code=ErrCode.NO_DATA)
Beispiel #2
0
def getCategory(aid):
    if aid is None:
        return output_json(code=ErrCode.ERR_PARAMS)

    handler = CategoryRelation()
    data = handler.getCategory(aid)

    return output_json(data=data)
Beispiel #3
0
def show(id):
    """文章详情"""
    if id is None:
        return output_json(code=ErrCode.ERR_PARAMS)
    handler = ArticleService()

    flag, data = handler.show(id)

    return output_json(data=data) if flag else output_json(
        code=ErrCode.NO_DATA)
Beispiel #4
0
def create():
    body = request.json

    validate(body, relation_schema)
    handler = CategoryRelation()
    if request.method == "POST":
        res = handler.bindCategory(body.get("name"), body.get("aid"))
    else:
        res = handler.unbindCategory(body.get("name"), body.get("aid"))

    return output_json(code=0) if res else output_json(code=ErrCode.NO_DATA)
Beispiel #5
0
def changePasswd(user_id):
    """修改密码"""
    if user_id is None:
        return output_json(code=ErrCode.ERR_PARAMS)

    body = request.json
    validate(body, passwd_schema)
    user_handler = UserService()
    code = user_handler.changePasswd(user_id, **body)

    return output_json(code=code)
Beispiel #6
0
    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)
Beispiel #7
0
def update(user_id):
    """编辑用户信息"""
    if user_id is None:
        return output_json(code=ErrCode.ERR_PARAMS)

    body = request.json
    allow_field = ["name", "email", "role_id"]
    for key in body.keys():
        if key not in allow_field:
            return output_json(code=ErrCode.ERR_PARAMS)

    user_handler = UserService()
    code = user_handler.update(user_id, body)

    return output_json(code=code)
Beispiel #8
0
def list():
    """文章列表"""
    query = request.args.get("query")

    try:
        query = json.loads(query)
    except:
        return output_json(code=ErrCode.ERR_PARAMS)

    where = query.get("where") or {}
    offset = query.get("offset") or 0
    limit = query.get("limit") or 15
    handler = ArticleService()
    data, count = handler.list(where, offset, limit)

    return output_json(data=data, total=count)
Beispiel #9
0
    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)
Beispiel #10
0
def update(id):
    """编辑文章"""
    if id is None:
        return output_json(code=ErrCode.ERR_PARAMS)

    body = request.json
    if body is None:
        return output_json(code=ErrCode.ERR_PARAMS)

    allowField = ["title", "status", "content"]
    # 检查更新字段
    for key in body.keys():
        if key not in allowField:
            return output_json(code=ErrCode.ERR_PARAMS)

    handler = ArticleService()
    res = handler.update(id, body)

    return output_json(code=0) if res else output_json(code=ErrCode.NO_DATA)
Beispiel #11
0
def require_token():
    """验证token"""
    pathname = request.path
    # 权限受控接口
    if not app.config["DEBUG"]:
        if -1 != pathname.find("admin"):
            headers = request.headers
            origin = headers.get("Origin")
            token = headers.get("Token")

            if token is None:
                return output_json(code=ErrCode.HEADER_ERR)
            # 验证token
            try:
                payload = jwt.decode(token,
                                     app.config["JWT_SECRET"],
                                     algorithms=['HS256'])
            except jwt.ExpiredSignatureError:
                return output_json(code=ErrCode.NO_AUTH), 401
            except jwt.InvalidTokenError:
                return output_json(code=ErrCode.NO_AUTH), 401
Beispiel #12
0
def create():
    """创建文章"""
    body = request.json

    validate(body, create_schema)
    handler = ArticleService()
    res = handler.create(
        Article(title=body["title"],
                author_id=body["author_id"],
                content=body["content"],
                status=body["status"]))

    return output_json(data=res)
Beispiel #13
0
def create():
    """创建用户"""
    body = request.json

    validate(body, create_user_schema)
    user_handler = UserService()
    code = user_handler.create(
        User(name=body["name"],
             email=body["email"],
             password=body["password"],
             role_id=body["role_id"]))

    return output_json(code=code)
Beispiel #14
0
 def list(self, where: object, offset=0, limit=15):
     """查询分类列表
     Args:
         where: object, 查询对象
         offset: int, 分页
         limit:  int, 页长
     """
     query = Category.query
     
     if where.get("name"):
         query = query.filter(Category.name==where.get("name"))
     
     count = query.count()
     rows = query.offset(offset).limit(limit).all()
     data = []
     for item in rows:
         data.append(item.to_dict())
     
     return output_json(data=data, total=count)
Beispiel #15
0
def handle_inner_error(e):
    if DEBUG:
        print(traceback.format_exc())
    return output_json(code=ErrCode.INNERERR), 200
Beispiel #16
0
def handle_bad_request(e):
    """处理全局异常参数请求"""
    if DEBUG:
        print(traceback.format_exc())
    return output_json(code=ErrCode.ERR_PARAMS), 400