def delete(self):
        try:
            logger.info(
                "########################[delete]########################")
            logger.info("self.args.keys(): %s" % self.args.keys())
            json_data = request.get_json(force=True)
            userid = json_data['userid'] if ('userid'
                                             in json_data.keys()) else ""
            logger.info("userid: %s" % userid)
            userToken = json_data['token'] if ('token'
                                               in json_data.keys()) else ""
            logger.info("userToken: %s" % userToken)
            articleIdList = json_data['articleId'] if (
                'articleId' in json_data.keys()) else ""
            logger.info("articleIdList: %s" % articleIdList)
            neededParams = self.args.keys(
            )  # 记录self.reqparse.add_argument中添加的参数列表
            logger.info("neededParams: %s" % neededParams)
            requestParams = request.json.keys()  # 记录发送请求中携带的参数列表
            logger.info("requestParams: %s" % requestParams)
            requestTimestamp = time.time()

            # 校验是否参数都有传过来,不多不少
            if userid and userToken and articleIdList and util.paramsNumResult(
                    neededParams, requestParams):
                getUserInDB = User.query.filter(User.id == userid).first()
                logger.info("getUserInDB: %s" % getUserInDB)
                # 如果用户存在,继续判断是否有登陆时间和token
                if getUserInDB:
                    userLoginTimeInDB = getUserInDB.loginTime  #取出用户的登录时间
                    logger.info("userLoginTimeInDB: %s" % userLoginTimeInDB)
                    # 获取用户在DB中的token
                    userTokenInDB = getUserInDB.token
                    logger.info("userTokenInDB: %s" % userTokenInDB)
                    # 如果表中有登录时间和token,则继续判断token是否过期
                    if userLoginTimeInDB and userTokenInDB:
                        # 校验登录时间是否超过1小时
                        if util.calculateTimeDiff(userLoginTimeInDB,
                                                  requestTimestamp) >= 1:
                            return {
                                "code": "02",
                                "message": u"参数值不合法,token已过期,请重新登录"
                            }
                        # 登录时间没超过1小时,继续校验token是否和useid是否相匹配
                        else:
                            # 判断token和userid是否相匹配,匹配则处理articleIdList列表
                            if userToken == userTokenInDB:
                                # 判断articleIdList的值是否为列表,是的话,则遍历列表继续处理
                                if type(articleIdList) == list:
                                    logger.info("articleIdList: %s" %
                                                articleIdList)
                                    # 遍历articleId列表
                                    for id in articleIdList:
                                        # 用id去找blog
                                        blog = UserBlog.query.filter(
                                            UserBlog.articleId == id).first()
                                        # 能找到,说明id存在,进行删除动作
                                        if blog:
                                            # 先从db session中删掉,最后一起commit
                                            db.session.delete(blog)

                                        # 没找到,说明id不存在,提示参数值不合法
                                        else:
                                            return {
                                                "code":
                                                "02",
                                                "message":
                                                u"参数值不合法,articleId:%s 不存在" % id
                                            }
                                    # 遍历完articleIdList,id都存在,且都做了删除,则提交commit
                                    db.session.commit()
                                    # 数据库提交删除后,返回结果
                                    return {
                                        "articleId": articleIdList,
                                        "code": "00",
                                        "userid": userid
                                    }

                                # articleId的值不是列表,提示articleId传的不是列表
                                else:
                                    return {
                                        "code": "02",
                                        "message": u"参数值不合法,articleId传的不是列表"
                                    }

                            # token和userid不匹配,说明token不正确,返回参数值不合法,token不正确
                            else:
                                return {
                                    "code": "02",
                                    "message": u"参数值不合法,token不正确"
                                }
                    # 表中没有登录时间和token,提示token不正确,请登录并获取token
                    else:
                        return {
                            "code": "02",
                            "message": u"参数值不合法,token不正确,请登录并获取token"
                        }

                # 如果用户不存在,提示参数值不合法,用户不存在
                else:
                    return {"code": "02", "message": u"参数值不合法,用户不存在"}
            # 参数没传全,或参数写错了,或参数多了
            else:
                return {
                    "code": "03",
                    "message": u"参数错误,可能原因:参数少传了、多传了、写错了、参数值为空"
                }

        except Exception as e:
            logger.error("error of delete: %s" % e)
            return {"code": "999", "message": u"未知错误"}
Exemple #2
0
    def put(self):
        try:
            logger.info(
                "########################[Update]########################")
            logger.info("self.args.keys(): %s" % self.args.keys())
            json_data = request.get_json(force=True)
            logger.info("json_data: %s" % json_data)
            userid = json_data['userid'] if ('userid'
                                             in json_data.keys()) else ""
            logger.info("userid: %s" % userid)
            userToken = json_data['token'] if ('token'
                                               in json_data.keys()) else ""
            logger.info("userToken: %s" % userToken)
            articleId = json_data['articleId'] if ('articleId'
                                                   in json_data.keys()) else ""
            logger.info("articleId: %s" % articleId)
            blogTitle = json_data['title'] if ('title'
                                               in json_data.keys()) else ""
            logger.info("blogTitle: %s" % blogTitle)
            blogContent = json_data['content'] if ('content'
                                                   in json_data.keys()) else ""
            logger.info("blogContent: %s" % blogContent)
            neededParams = self.args.keys(
            )  # 记录self.reqparse.add_argument中添加的参数列表
            logger.info("neededParams: %s" % neededParams)
            requestParams = request.json.keys()  # 记录发送请求中携带的参数列表
            logger.info("requestParams: %s" % requestParams)
            requestTimestamp = time.time()

            # 校验是否参数都有传过来,不多不少
            if userid and userToken and blogTitle and blogContent and articleId and util.paramsNumResult(
                    neededParams, requestParams):
                getUserInDB = User.query.filter(User.id == userid).first()
                logger.info("getUserInDB: %s" % getUserInDB)
                # 如果用户存在,判断表中是否有登录时间和token
                if getUserInDB:
                    userLoginTimeInDB = getUserInDB.loginTime  #取出用户的登录时间
                    logger.info("userLoginTimeInDB: %s" % userLoginTimeInDB)
                    # 获取用户在DB中的token
                    userTokenInDB = getUserInDB.token
                    logger.info("userTokenInDB: %s" % userTokenInDB)
                    # 如果有登录时间和token,则继续校验token是否过期
                    if userLoginTimeInDB and userTokenInDB:
                        # 校验登录时间是否超过1小时
                        if util.calculateTimeDiff(userLoginTimeInDB,
                                                  requestTimestamp) >= 1:
                            return {
                                "code": "02",
                                "message": u"参数值不合法,token已过期,请重新登录"
                            }
                        # 登录时间没超过1小时,继续校验token是否和useid是否相匹配
                        else:
                            # 判断token和userid是否相匹配,匹配则继续检验articleId是否存在
                            if userToken == userTokenInDB:
                                # 判断artileId是否在DB中存在
                                getBlogInDB = UserBlog.query.filter(
                                    UserBlog.articleId == articleId).first()
                                logger.info("getBlogInDB: %s" % getBlogInDB)
                                # 如果用articleId能查到博文,存表(update_time,title, content),并返回
                                if getBlogInDB:
                                    # 先获取到更新博文的时间
                                    updateBlogTimeString = time.strftime(
                                        "%Y-%m-%d %H:%M:%S",
                                        time.localtime(requestTimestamp))
                                    logger.info("updateBlogTimeString: %s" %
                                                updateBlogTimeString)

                                    # 存表: blogTitle、blogContent、updateTime
                                    updateBlog = UserBlog.query.filter(
                                        UserBlog.articleId ==
                                        articleId).first()
                                    updateBlog.updateTime = updateBlogTimeString
                                    updateBlog.blogTitle = blogTitle
                                    updateBlog.blogContent = blogContent
                                    db.session.commit()
                                    # 返回成功
                                    return {
                                        "articleId": articleId,
                                        "update_time": updateBlogTimeString,
                                        "code": "00",
                                        "userid": userid
                                    }
                                # 如果用articleId查不到博文,则提示参数值不合法,articleId不存在
                                else:
                                    return {
                                        "code": "02",
                                        "message": u"参数值不合法,articleId不存在"
                                    }

                            # token和userid不匹配,说明token不正确,返回参数值不合法,token不正确
                            else:
                                return {
                                    "code": "02",
                                    "message": u"参数值不合法,token不正确"
                                }

                    # 如果没有登录时间和token,则提示token不正确,请登录并获取token
                    else:
                        return {
                            "code": "02",
                            "message": u"参数值不合法,token不正确,请登录并获取token"
                        }

                        # 如果用户不存在,提示参数值不合法,用户不存在
                else:
                    return {"code": "02", "message": u"参数值不合法,用户不存在"}
            # 参数没传全,或参数写错了,或参数多了
            else:
                return {
                    "code": "03",
                    "message": u"参数错误,可能原因:参数少传了、多传了、写错了、值为空"
                }

        except Exception as e:
            logger.error("error of update: %s" % e)
            return {"code": "999", "message": u"未知错误"}
Exemple #3
0
    def post(self):
        try:
            userid = self.args['userid']
            userToken = self.args['token']
            blogTitle = self.args['title']
            blogContent = self.args['content']
            neededParams = self.args.keys(
            )  # 记录self.reqparse.add_argument中添加的参数列表
            logger.info("neededParams: %s" % neededParams)
            requestParams = request.json.keys()  # 记录发送请求中携带的参数列表
            logger.info("requestParams: %s" % requestParams)
            requestTimestamp = time.time()

            #校验是否参数都有传过来,不多不少
            if userid and userToken and blogTitle and blogContent and util.paramsNumResult(
                    neededParams, requestParams):
                getUserInDB = User.query.filter(User.id == userid).first()
                logger.info("getUserInDB: %s" % getUserInDB)
                # 如果用户存在,判断登录时间是否超过一小时
                if getUserInDB:
                    userLoginTime = getUserInDB.loginTime  #取出用户的登录时间
                    logger.info("userLoginTime: %s" % userLoginTime)
                    # 校验登录时间是否超过1小时
                    if util.calculateTimeDiff(userLoginTime,
                                              requestTimestamp) >= 1:
                        return {
                            "code": "02",
                            "message": u"参数值不合法,token已过期,请重新登录"
                        }
                    # 登录时间没超过1小时,继续校验token是否和useid是否相匹配
                    else:
                        # 获取用户在DB中的token
                        userTokenInDB = getUserInDB.token
                        logger.info("userTokenInDB: %s" % userTokenInDB)
                        # 判断token和userid是否相匹配,如果匹配,则存表并返回
                        if userToken == userTokenInDB:
                            # 先获取到创建博文的时间
                            createBlogTimeString = time.strftime(
                                "%Y-%m-%d %H:%M:%S",
                                time.localtime(requestTimestamp))
                            logger.info("createBlogTimeString: %s" %
                                        createBlogTimeString)
                            #存表: blogTitle、blogContent、user_id(User表中的id),createTime
                            blogNew = UserBlog(blogTitle=blogTitle,
                                               blogContent=blogContent,
                                               user_id=userid,
                                               createTime=createBlogTimeString)
                            db.session.add(blogNew)
                            db.session.commit()
                            # 返回成功
                            return {
                                "data": [{
                                    "content": blogContent,
                                    "title": blogTitle
                                }],
                                "code":
                                "00",
                                "userid":
                                userid,
                                "articleId":
                                blogNew.articleId
                            }
                        # token和userid不匹配,说明token不正确
                        else:
                            return {
                                "code": "02",
                                "message": u"参数值不合法,token不正确"
                            }
                # 如果用户不存在
                else:
                    return {"code": "02", "message": u"参数值不合法,用户不存在"}
            else:
                #参数没传全,或参数写错了,或参数多了
                return {
                    "code": "03",
                    "message": u"参数错误,可能原因:参数少传了、多传了、写错了、值为空"
                }

        except Exception as e:
            logger.info("error of create: %s" % e)
            return {"code": "999", "message": u"未知错误"}
    def post(self):
        try:
            userid = self.args['userid']
            userToken = self.args['token']
            offset = self.args['offset']
            lines = self.args['lines']
            neededParams = self.args.keys(
            )  # 记录self.reqparse.add_argument中添加的参数列表
            logger.info("neededParams: %s" % neededParams)
            requestParams = request.json.keys()  # 记录发送请求中携带的参数列表
            logger.info("requestParams: %s" % requestParams)
            requestTimestamp = time.time()
            logger.info("type(offset) -> %s \n type(lines) -> %s" %
                        (type(offset), type(lines)))

            # 校验是否参数都有传过来(除了offset和lines),不多不少
            if userid and userToken and util.paramsNumResult(
                    neededParams, requestParams):
                getUserInDB = User.query.filter(User.id == userid).first()
                logger.info("getUserInDB: %s" % getUserInDB)
                # 如果用户存在,判断登录时间是否超过一小时
                if getUserInDB:
                    userLoginTime = getUserInDB.loginTime  #取出用户的登录时间
                    logger.info("userLoginTime: %s" % userLoginTime)
                    # 校验登录时间是否超过1小时
                    if util.calculateTimeDiff(userLoginTime,
                                              requestTimestamp) >= 1:
                        return {
                            "code": "02",
                            "message": u"参数值不合法,token已过期,请重新登录"
                        }
                    # 登录时间没超过1小时,继续校验token是否和useid是否相匹配
                    else:
                        # 获取用户在DB中的token
                        userTokenInDB = getUserInDB.token
                        logger.info("userTokenInDB: %s" % userTokenInDB)
                        # 判断token和userid是否相匹配,匹配则处理offset和lines逻辑
                        if userToken == userTokenInDB:
                            # 定义一个临时字典
                            responseDict = {
                                "data": [],
                                "code": "00",
                                "userid": userid
                            }
                            userOfBlog = User.query.filter(
                                User.id == userid).first()
                            blogs = userOfBlog.blogs
                            logger.info("blogs: %s" % blogs)
                            # 如果用户有博文,则继续,否则直接返回responseDict
                            if blogs:
                                # 如果offset和lines都传了则继续校验offset和lines的值
                                if offset and lines:
                                    # 判断博文数是否够跳offset条,够跳,继续判断是否够返回lines条数据
                                    if len(blogs) > int(offset):
                                        # 跳过offset够取lines条数据,返回跳过offset条后,lines条数据:blogs[offset : (offset + lines + 1)],包含第lines条
                                        if (len(blogs) -
                                                int(offset)) > int(lines):
                                            blogsAfterJumpOffsetGetLines = blogs[
                                                int(offset):(int(offset) +
                                                             int(lines))]
                                            logger.info(
                                                "blogsAfterJumpOffsetGetLines : %s"
                                                % blogsAfterJumpOffsetGetLines)
                                            responseDictFilled = util.fillInResponseDict(
                                                responseDict,
                                                blogsAfterJumpOffsetGetLines)
                                            return responseDictFilled
                                        # 跳过offset不够取lines条数据,返回跳过offset条后所有的blog:blogs[offset : ]
                                        else:
                                            blogsAfterJumpOffset = blogs[
                                                int(offset):]
                                            logger.info(
                                                "blogsAfterJumpOffset: %s" %
                                                blogsAfterJumpOffset)
                                            responseDictFilled = util.fillInResponseDict(
                                                responseDict,
                                                blogsAfterJumpOffset)
                                            return responseDictFilled

                                    # 博文数不够跳offset条,则返回博文列表为空
                                    else:
                                        return responseDict
                                # offset和lines没有都传过来,则返回用户全部博文
                                else:
                                    responseDictFilled = util.fillInResponseDict(
                                        responseDict, blogs)
                                    return responseDictFilled
                            # 没有博文,直接返回,"data"列表为空
                            else:
                                return responseDict

                        # token和userid不匹配,说明token不正确,返回参数值不合法,token不正确
                        else:
                            return {
                                "code": "02",
                                "message": u"参数值不合法,token不正确"
                            }
                # 如果用户不存在,提示参数值不合法,用户不存在
                else:
                    return {"code": "02", "message": u"参数值不合法,用户不存在"}
            # 参数没传全,或参数写错了,或参数多了
            else:
                return {
                    "code": "03",
                    "message": u"参数错误,可能原因:参数少传了、多传了、写错了、值为空"
                }

        except Exception as e:
            logger.error("error of update: %s" % e)
            return {"code": "999", "message": u"未知错误"}
    def post(self):
        try:
            userid = self.args['userid']
            userToken = self.args['token']
            articleId = self.args['articleId']
            blogTitle = self.args['title']
            blogContent = self.args['content']
            neededParams = self.args.keys(
            )  # 记录self.reqparse.add_argument中添加的参数列表
            logger.info("neededParams: %s" % neededParams)
            requestParams = request.json.keys()  # 记录发送请求中携带的参数列表
            logger.info("requestParams: %s" % requestParams)
            requestTimestamp = time.time()

            # 校验是否参数都有传过来,不多不少
            if userid and userToken and blogTitle and blogContent and articleId and util.paramsNumResult(
                    neededParams, requestParams):
                getUserInDB = User.query.filter(User.id == userid).first()
                logger.info("getUserInDB: %s" % getUserInDB)
                # 如果用户存在,判断登录时间是否超过一小时
                if getUserInDB:
                    userLoginTime = getUserInDB.loginTime  #取出用户的登录时间
                    logger.info("userLoginTime: %s" % userLoginTime)
                    # 校验登录时间是否超过1小时
                    if util.calculateTimeDiff(userLoginTime,
                                              requestTimestamp) >= 1:
                        return {
                            "code": "02",
                            "message": u"参数值不合法,token已过期,请重新登录"
                        }
                    # 登录时间没超过1小时,继续校验token是否和useid是否相匹配
                    else:
                        # 获取用户在DB中的token
                        userTokenInDB = getUserInDB.token
                        logger.info("userTokenInDB: %s" % userTokenInDB)
                        # 判断token和userid是否相匹配
                        if userToken == userTokenInDB:
                            # 判断artileId是否在DB中存在
                            getBlogInDB = UserBlog.query.filter(
                                UserBlog.articleId == articleId).first()
                            logger.info("getBlogInDB: %s" % getBlogInDB)
                            # 如果用articleId能查到博文,存表(update_time,title, content),并返回
                            if getBlogInDB:
                                # 先获取到更新博文的时间
                                updateBlogTimeString = time.strftime(
                                    "%Y-%m-%d %H:%M:%S",
                                    time.localtime(requestTimestamp))
                                logger.info("updateBlogTimeString: %s" %
                                            updateBlogTimeString)

                                #存表: blogTitle、blogContent、updateTime
                                updateBlog = UserBlog.query.filter(
                                    UserBlog.articleId == articleId).first()
                                updateBlog.updateTime = updateBlogTimeString
                                updateBlog.blogTitle = blogTitle
                                updateBlog.blogContent = blogContent
                                db.session.commit()
                                # 返回成功
                                return {
                                    "articleId": articleId,
                                    "update_time": updateBlogTimeString,
                                    "code": "00",
                                    "userid": userid
                                }
                            # 如果用articleId查不到博文,则提示参数值不合法,articleId不存在
                            else:
                                return {
                                    "code": "02",
                                    "message": u"参数值不合法,articleId不存在"
                                }

                        # token和userid不匹配,说明token不正确,返回参数值不合法,token不正确
                        else:
                            return {
                                "code": "02",
                                "message": u"参数值不合法,token不正确"
                            }
                # 如果用户不存在,提示参数值不合法,用户不存在
                else:
                    return {"code": "02", "message": u"参数值不合法,用户不存在"}
            # 参数没传全,或参数写错了,或参数多了
            else:
                return {
                    "code": "03",
                    "message": u"参数错误,可能原因:参数少传了、多传了、写错了、值为空"
                }

        except Exception as e:
            logger.error("error of update: %s" % e)
            return {"code": "999", "message": u"未知错误"}