def postArticle(userid, title, brief, keys, coverurl, bodyurl): artid = blogDB.addArticle( userid, title, brief, keys, coverurl, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), bodyurl) if artid is not None: return Common.trueReturn({"articleid": artid}, 'add article ok') return Common.falseReturn(None, 'add article false')
def get_Comments(articleid): pageNumber = request.args.get('pageNumber') pageSize = request.args.get('pageSize') if pageNumber is None or pageSize is None: return jsonify(Common.falseReturn(None, 'please make pagenation')) comments = CommentApi.getCommentByArticleId(articleid) if not comments['status']: return jsonify(comments) #将线性的评论转换为树状结构 entities = {} [ entities.update({content['commentid']: content}) for content in comments['data'] ] l = [] for e_id in entities: entitiy = entities[e_id] fid = entitiy['refid'] if fid == '': l.append(entitiy) else: entities[fid].setdefault('soncomment', []).append(entitiy) #取前N个 left = min(len(l), max(0, (int(pageNumber) - 1) * int(pageSize))) right = min(len(l), max(0, int(pageNumber) * int(pageSize))) return jsonify(Common.trueReturn(l[left:right], 'query ok'))
def post_Article(): userid = IdentifyUtil.get_user_id() if not userid: return jsonify(Common.falseReturn(None, 'user not find')) params = request.get_json() if 'title' in params and 'brief' in params and 'body' in params: #先将内容保存成文件,然后将地址等信息上传到数据库 filePath = FileApi.generateFilePath( ''.join([ random.choice(string.digits + string.ascii_letters) for i in range(5) ]) + '.md', "articles/bodys/" + userid) absFilePath = os.path.join(config.STATIC_FILE_PATH, filePath) if FileApi.saveFile(absFilePath, params['body'])['status']: return jsonify( ArticleApi.postArticle(userid, params['title'], params['brief'], params["keywords"], params["coverurl"], filePath)) else: return jsonify(Common.falseReturn(None, 'file save failure!')) else: return jsonify( Common.falseReturn( None, 'Please make sure {"title":a,"breif":a,"keywords":a,"body":a}') )
def postArticleBrowser(articleid, userid, ip): if BrowserApi.checkAlreadyBrowserArticle(articleid, userid, ip)['data']: return Common.falseReturn(None, 'already in') if blogDB.addBrowserArticle(articleid, userid, ip): return Common.trueReturn(True, 'browser ok') return Common.falseReturn(None, 'browser false')
def getUserInfoByUserid(userid): if not userid: return Common.falseReturn(None,'userid is required') user = blogDB.getUserInfoById(userid) if user: return Common.trueReturn(dict(zip(("userid","realname","idcard","cellphone","email","avatarurl"),user)), 'query ok') else: return Common.falseReturn(None, 'cannot find {}'.format(userid))
def getUserAvatarById(userid): if not userid: return Common.falseReturn(None,'userid is required') user = blogDB.getUserInfoById(userid) if user: return Common.trueReturn(user[-1],'query ok') else: return Common.falseReturn(None,'not found')
def getUserBaseByName(username): if not username: return Common.falseReturn(None,'username is none') user = blogDB.getUserByName(username) if user: return Common.trueReturn(dict(zip(("id", "name", "password"), user)),'query ok') else: return Common.falseReturn(None,'cannot find {}'.format(username))
def add_commentLikes(): userid = IdentifyUtil.get_user_id() if not userid: return jsonify(Common.falseReturn(None, 'login required')) params = request.get_json() if 'commentid' in params: return jsonify(LikesApi.postLikesComment(params['commentid'], userid)) return jsonify(Common.falseReturn(None, 'commentid in json is needed'))
def authenticate(username, password): '''登陆验证,成功则返回token''' userid = blogDB.checkPassword(username, password) if not userid is None: payload = Authority.encode_jwt(userid) return Common.trueReturn(payload.decode(), "Get Token OK") else: return Common.falseReturn(None, 'Get Token Flase')
def getArticleCountByUserId(userid): res = blogDB.getArticleCountByUserid(userid) if res is not None: if len(res) == 0: return Common.trueReturn(0, 'query ok') else: return Common.trueReturn(res[0], 'query ok') return Common.falseReturn(None, 'query false')
def getCommentByCommentId(commentid): comment = blogDB.getCommentByCommentId(commentid) if comment is not None: return Common.trueReturn( dict( zip(("commentid", "articleid", "userid", "uptime", "comments", "refid"), comment)), 'query ok') return Common.falseReturn(None, 'not found')
def getIsSelfComment(userid, commentid): comment = blogDB.getCommentByCommentId(commentid) if comment is not None: if comment[2] == userid: return Common.trueReturn(True, 'yes') else: return Common.falseReturn(False, 'not mine') return Common.falseReturn(False, 'no')
def getChildCommentCountByCommentId(commentid): res = blogDB.getChildNumberByCommentId(commentid) if res is not None: if len(res) == 0: return Common.trueReturn(0, 'query ok') else: return Common.trueReturn(res[0], 'query ok') return Common.falseReturn(None, 'query false')
def update_UserIntroduceTag(): userid = IdentifyUtil.get_user_id() if not userid: return jsonify(Common.falseReturn(None, 'login required')) params = request.get_json() if 'tags' in params: return jsonify(IntroduceApi.changeTags(userid, params['tags'])) return jsonify(Common.falseReturn(None, 'tags is needed'))
def getIsSelfArticlesComment(userid, commentid): comment = blogDB.getCommentByCommentId(commentid) if comment is not None: article = blogDB.getArticleById(comment[1]) if article is not None: if article[1] == userid: return Common.trueReturn(True, 'yes') return Common.falseReturn(False, 'no')
def getArticleBaseByID(artid): blogbase = blogDB.getArticleById(artid) if blogbase is not None: result = dict( zip(("articleid", "userid", "title", "breif", "keywords", "coverurl", "uptime", "bodyurl"), blogbase)) return Common.trueReturn(result, 'query ok') return Common.falseReturn(None, 'not found')
def getLikesCommentNumber(commentid): res = blogDB.getLikesNumberByCommentId(commentid) if not res is None: if len(res) == 0: return Common.trueReturn(0, 'query ok') else: return Common.trueReturn(res[0], 'query ok') return Common.falseReturn(None, 'query false')
def getIsSelfArticle(userid, articleid): article = blogDB.getArticleById(articleid) if article is not None: if article[1] == userid: return Common.trueReturn(True, 'yes') else: return Common.falseReturn(False, 'not mine') return Common.falseReturn(False, 'no')
def getArticleCountByUsername(username): res = blogDB.getArticleCountByUsername(username) if not res is None: if len(res) == 0: return Common.trueReturn(0, 'query ok') else: return Common.trueReturn(res[0], 'query ok') return Common.falseReturn(None, 'query false')
def change_username(): userid = IdentifyUtil.get_user_id() if not userid: return jsonify(Common.falseReturn(None, 'user not find')) params = request.get_json() if not params.get('username'): return jsonify(Common.falseReturn(None, 'username cannot be empty')) return jsonify(UserApi.updateUserName(userid, params.get('username')))
def getArticleBrowser(articleid): res = blogDB.getBrowserNumberByArticleId(articleid) if res is not None: if len(res) == 0: return Common.trueReturn(0, 'query ok') else: return Common.trueReturn(res[0], 'query ok') return Common.falseReturn(None, 'query false')
def saveFile(filePath, content): FileApi.makeSureFilePath(filePath) with open(filePath, 'w', encoding='utf-8') as f: try: f.write(content) return Common.trueReturn(True, 'save ok') except Exception as e: return Common.falseReturn(e, 'unknown error') return Common.falseReturn(None, 'open error' + filePath)
def updateUserBase(userid,username,password): if not blogDB.getUserById(userid): return Common.falseReturn(None, "{} doesn't exist in user_base".format(userid)) if not username or not password: return Common.falseReturn(None, 'username or password cannot be empty') if blogDB.updateUser(userid,username,IdentifyUtil.hash_secret(password)): return Common.trueReturn({'userid':userid},'change ok') else: return Common.falseReturn(None,'change false')
def postComment(articleid, userid, comments, refid): commentid = blogDB.addComment( articleid, userid, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), comments, refid) if commentid is not None: return Common.trueReturn({"commentid": commentid}, 'add comment ok') return Common.falseReturn(None, 'add comment false')
def updateUserInfo(userid,realname,phone,idcard,email,avatarurl): if not realname or not phone or not idcard or not email: return Common.falseReturn(None,'params cannot be none') if not blogDB.getUserInfoById(userid) : return Common.falseReturn(None,"{} doesn't exist in user_info".format(userid)) if blogDB.updateUserInfo(userid,realname,idcard,phone,email,avatarurl): return Common.trueReturn(userid,'modify ok') else: return Common.falseReturn(None,'unknown reason')
def registerUserBase(username,password): if not username or not password: return Common.falseReturn(None, 'username or password cannot be empty') if blogDB.getUserByName(username) is not None: return Common.falseReturn(None,'{} is already exists'.format(username)) userid = blogDB.addUser(username, IdentifyUtil.hash_secret(password)) if userid is not None: return Common.trueReturn({'userid':userid},'register ok') else : return Common.falseReturn('unknown reason','register wrong')
def delete_Comments(commentid): #获取自己id,只能删除自己文章下或者自己的评论 userid = IdentifyUtil.get_user_id() if not userid: return jsonify(Common.falseReturn(None, 'login required')) if CommentApi.getIsSelfComment( userid, commentid)['data'] or CommentApi.getIsSelfArticlesComment( userid, commentid)['data']: return jsonify(CommentApi.deleteComment(commentid)) return jsonify(Common.falseReturn(None, 'permission denied'))
def update_UserIntroduce(): userid = IdentifyUtil.get_user_id() if not userid: return jsonify(Common.falseReturn(None, 'login required')) params = request.get_json() if 'resume' in params and 'tags' in params: return jsonify( IntroduceApi.changeIntroduce(userid, params['resume'], params['tags'])) return jsonify(Common.falseReturn(None, 'add error'))
def post_Comments(): '''需要提供articleid,comment,refid''' userid = IdentifyUtil.get_user_id() if not userid: return jsonify(Common.falseReturn(None, 'login required')) params = request.get_json() if 'articleid' not in params or 'comment' not in params: return jsonify(Common.falseReturn('articleid and comment is required')) res = CommentApi.postComment(params.get('articleid'), userid, params.get('comment'), params.get('refid')) return jsonify(res)
def checkAlreadyBrowserArticle(articleid, userid, ip): res = blogDB.getBrowserArticleByIp(articleid, ip) if res is not None: if len(res) == 0: return Common.trueReturn(False, 'not in') for k, v in enumerate(res): if v[2] == userid: return Common.trueReturn(True, 'already in') return Common.trueReturn(False, 'not in') else: return Common.falseReturn(None, 'not in')