def addc(): blogid = request.form.get('blogid') content = request.form.get('content') content = content.replace('\n', '<br/>') print(content) if len(content) > 530: return 'len-error' blog = Blog() comment = Comment() credit = Credit() if session.get('ban') == '1': return 'comment-banned' if not comment.check_limit_comment(): try: comment.insert_comment(blogid, content) blog.update_blog(blogid) credit.insert_exp(type='评论', exp=2) Users().update_exp(2) return 'add-success' except: return 'add-fail' else: comment.insert_comment(blogid, content) blog.update_blog(blogid) return 'add-success'
def reply(): commentid = request.form.get('commentid') blogid = request.form.get('blogid') content = request.form.get('content') if len(content) > 530: return 'len-error' comment = Comment() blog = Blog() credit = Credit() if session.get('ban') == 1: return 'comment-banned' if not comment.check_limit_comment(): try: comment.insert_reply(blogid=blogid, commentid=commentid, content=content) blog.update_blog(blogid) credit.insert_exp(type='评论', exp=2) Users().update_exp(2) return 'add-success' except: return 'add-fail' else: comment.insert_reply(blogid=blogid, commentid=commentid, content=content) blog.update_blog(blogid) return 'add-success'
def addlike(): commentid = request.form.get('commentid') like = Likes() credit = Credit() if like.find_like(commentid=commentid): return 'have-liked' if not like.check_limit_like(): comment = Comment().find_by_commentid(commentid) like.insert_like(commentid=commentid) credit.insert_exp_user(type='点赞', exp=1, userid=comment.userid) Users().update_like_exp(exp=1, userid=comment.userid) return 'like-success' else: like.insert_like(commentid=commentid) return 'like-success'
def write(): if request.method == 'GET': return render_template('write-blog.html', type='no') blog = Blog() credit = Credit() headline = request.form.get('headline') content = request.form.get('content') content = content.replace('\n', '<br/>') type = request.form.get('type') level = request.form.get('level') if level == '1级': level = 1 elif level == '2级': level = 2 elif level == '3级': level = 3 elif level == '4级': level = 4 elif level == '5级': level = 5 if session.get('ban') == 1: return 'banned' if len(content) > 1000: return 'content-len-error' if len(headline) > 15: return 'headline-len-error' if session.get('islogin') != 'true': return 'not-login' if int(session.get('exp')) < int(level) * 100: return 'level-error' try: if not blog.check_limit_blog(): blog.write_blog(headline=headline, content=content, type=type, level=level) credit.insert_exp('发布帖子', 5) Users().update_exp(5) return 'write-success' else: blog.write_blog(headline=headline, content=content, type=type, level=level) return 'write-success' except: return 'write-fail'
def login(): user = Users() username = request.form.get('username').strip() password = request.form.get('password').strip() vcode = request.form.get('vcode').lower().strip() if vcode != session.get('vcode'): #check verification code return 'vcode-error' else: password = hashlib.md5(password.encode()).hexdigest() result = user.find_by_username(username) if len(result) == 1 and result[0].password == password: session['islogin'] = '******' session['userid'] = result[0].userid session['username'] = username session['nickname'] = result[0].nickname session['role'] = result[0].role Credit().insert_detail(type='Normal login', target='0', credit=1) user.update_credit(1) response = make_response('login-pass') response.set_cookie('username', username, max_age=30 * 24 * 3600) response.set_cookie('password', password, max_age=30 * 24 * 3600) return response else: return 'login-fail'
def register(): user = User() username = request.form.get('username').strip() password = request.form.get('password').strip() code = request.form.get('code').strip() if code != session.get('ecode'): print(code, session.get('ecode')) return 'ecode-error' elif not re.match('.+@.+\..+', username) or len(password) < 5: return 'up-invalid' elif len(user.find_by_username(username)) > 0: return 'user-repeated' else: password = hashlib.md5(password.encode()).hexdigest() result = user.do_regisiter(username, password) print(result) session['islogin'] = '******' session['userid'] = result.userid session['username'] = username session['nickname'] = result.nickname session['role'] = result.role Credit().insert_detail(type='用户注册', target='0', credit=50) return 'reg-pass'
def read_all(): position = int(request.form.get('position')) articleid = request.form.get('articleid') article = Article() result = article.find_by_id(articleid) content = result[0].content[position:] payed = Credit().check_payed_article(articleid) if not payed: # 添加积分明细 Credit().insert_detail(type='阅读文章', target=articleid, credit=-1 * result[0].credit) # 减少用户表的剩余积分 User().update_credit(credit=-1 * result[0].credit) return content
def reply(): articleid = request.form.get('articleid') commentid = request.form.get('commentid') content = request.form.get('content').strip() ipaddr = request.remote_addr # 如果评论的字数低于5个或多于1000,视为不合法 if len(content) < 5 or len(content) > 1000: return 'content-invalid' comment = Comment() # 没有超出限制才能发表评论 if not comment.check_limit_per_5(): try: comment.insert_reply(articleid=articleid, commentid=commentid, content=content, ipaddr=ipaddr) # 评论成功后,同步更新credit表明细,user表积分和article表回复数 Credit().insert_detail(type='回复评论', target=articleid, credit=2) User().update_credit(2) Article().update_replycount(articleid) return 'reply-pass' except: return 'reply-fail' else: return 'reply-limit'
def login(): user = User() username = request.form.get('username').strip() password = request.form.get('password').strip() vcode = request.form.get('vcode').lower().strip() # 校验图像验证码是否正确,0000用来做测试 if vcode != session.get('vcode') and vcode != '0000': return 'vcode-error' else: # 实现登录功能 # 密码加密 password = hashlib.md5(password.encode()).hexdigest() result = user.find_by_username(username) if len(result) == 1 and result[0].password == password: session['islogin'] = '******' session['userid'] = result[0].userid session['username'] = username session['nickname'] = result[0].nickname session['role'] = result[0].role # 更新积分详情表 Credit().insert_detail(type='正常登录', target='0', credit=1) user.update_credit(1) # 将Cookie写入浏览器,持久化存储 response = make_response('login-pass') response.set_cookie('username', username, max_age=30 * 24 * 3600) response.set_cookie('password', password, max_age=30 * 24 * 3600) return response else: return 'login-fail'
def register(): user = User() username = request.form.get('username').strip() password = request.form.get('password').strip() ecode = request.form.get('ecode').strip() # 校验邮箱验证码是否正确 if ecode != session.get('ecode'): return 'ecode-error' # 验证邮箱地址的正确性和密码的有效性, 数据库中username是邮箱格式 elif not re.match('.+@.+\..+', username) or len(password) < 5: return 'up-invalid' # 验证用户是否已经注册 elif len(user.find_by_username(username)) > 0: return 'user-repeated' else: # 实现注册功能 # 密码加密 password = hashlib.md5(password.encode()).hexdigest() result = user.do_register(username, password) session['islogin'] = '******' session['userid'] = result.userid session['username'] = username session['nickname'] = result.nickname session['role'] = result.role # 更新积分详情表 Credit().insert_detail(type='用户注册', target='0', credit=50) return 'reg-pass'
def read_all(): position = int(request.form.get('position')) articleid = request.form.get('articleid') article = Article() result = article.find_by_id(articleid) content = result[0].content[position:] user = Users().find_by_userid(session.get("userid")) if user.credit < result[0].credit: return "not credit" payed = Credit().check_payed_article(articleid) if not payed: Credit().insert_detail(type='阅读文章', target=articleid, credit=-1 * result[0].credit) Users().update_credit(credit=-1 * result[0].credit) return content
def read(articleid): try: # 数据格式:(Article, 'nickname') result = Article().find_by_id(articleid) if result is None: abort(404) except: abort(500) dict = {} for k, v in result[0].__dict__.items(): if not k.startswith('_sa_instance_state'): dict[k] = v dict['nickname'] = result.nickname # 如果已经消耗过积分,不再截取文章内容 payed = Credit().check_payed_article(articleid) position = 0 if dict['credit'] != 0: if not payed: content = dict['content'] temp = content[0:int(len(content) / 2)] # 找到一个闭合标签 position = temp.rindex('</p>') + 4 dict['content'] = temp[0:position] # print(dict) Article().update_read_count(articleid) # 阅读次数+1 # 是否收藏 is_favorited = Favorite().check_favorite(articleid) # 获取当前文章的上一篇和下一篇 prev_next = Article().find_prev_next_by_id(articleid) # 显示当前文章评论信息 comment_user = Comment().find_limit_with_user(articleid, 0, 50) # 对应新的模板文件——article-base.html comment_list = Comment().get_comment_user_list(articleid, 0, 10) # 原始评论总页数 count = Comment().get_count_by_article(articleid) total = math.ceil(count / 10) # return render_template('article-base.html', result=result) # 评论有分页,回复已做好,最终模样,点赞、反对、隐藏 功能俱全 return render_template('article-base.html', article=dict, position=position, payed=payed, is_favorited=is_favorited, prev_next=prev_next, comment_list=comment_list, total=total)
def read(articleid): try: result, user = Article().find_by_id(articleid) if result is None: abort(404) except Exception as e: abort(500) article = Article() last = article.find_last_9() most = article.find_most_9() payed = Credit().check_payed_article(articleid) position = 0 if not payed: content = result.content temp = content[0:int(len(content) / 2)] try: position = temp.rindex('</p>') + 4 content = temp[0:position] except: content = temp position = int(len(content) / 2) else: content = result.content Article().update_read_count(articleid) is_favorited = Favorite().check_favorite(articleid) total = 0 return render_template('article-user.html', article=result, position=position, payed=payed, is_favorited=is_favorited, content=content, total=total, user=user, last=last, most=most)
def add(): articleid = request.form.get('articleid') content = request.form.get('content').strip() ipaddr = request.remote_addr # 对评论内容进行简单检验 if len(content) < 5 or len(content) > 1000: return 'content-invalid' comment = Comment() if not comment.check_limit_per_5(): # try: comment.insert_comment(articleid, content, ipaddr) # 评论成功后,更新积分明细和剩余积分,以及文章回复数量 Credit().insert_detail(type='添加评论', target=articleid, credit=2) User().update_credit(2) Article().update_replycount(articleid) return 'add-pass' # except: # return 'add-fail' else: return 'add-limit'