def home(): if session.get('islogin') is None: username = request.cookies.get('username') password = request.cookies.get('password') if username != None and password != None: user = User() 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 article = Article() result = article.find_limit_with_user(0, 10) # print(result, end='---------------\n') total = math.ceil(article.get_total_count() / 10) # 向上取整 # 以下是在首页添加侧边栏的内容,side-jinja2.html中使用到,但是这种方式有一个缺陷: # 需要在每个HTML页面中引用引用一下代码,重复率高,改良版在:side.html中 last, most, recommended = article.find_most_recommended() return render_template('index-base.html', result=result, page=1, total=total, last=last, most=most, recommended=recommended)
def admin_article(page): pagesize = 50 start = (page - 1) * pagesize article = Article() result = article.find_all_except_draft(start, pagesize) total = math.ceil(article.get_count_except_draft() / pagesize) return render_template('system-admin.html', page=page, result=result, total=total)
def paginate(page): start = (page - 1) * 10 article = Article() result = article.find_limit_with_user(start, 10) total = math.ceil(article.get_total_count() / 10) # 向上取整 return render_template('index-base.html', result=result, page=page, total=total)
def recommend(): article = Article() last, most, recommended = article.find_most_recommended() # return jsonify(last, most, recommended) list = [] list.append(last) list.append(most) list.append(recommended) return jsonify(list)
def sys_admin(): pagesize = 50 article = Article() result = article.find_all_except_draft(0, pagesize) total = math.ceil(article.get_count_except_draft() / pagesize) return render_template('system-admin.html', page=1, result=result, total=total)
def classify(type, page): article = Article() start = (page - 1) * 10 result = article.find_by_type(type, start, 10) total = math.ceil(article.get_count_by_type(type) / 10) return render_template('type.html', result=result, type=type, page=page, total=total)
def classify(type, page): start = (page - 1) * 5 # 计算某一页需要跳过的文章数目 article = Article() result = article.find_by_type(type, start, 5) # 某一页的文章详情 total = math.ceil(article.get_count_by_type(type) / 5) # 统计页数 return render_template('type.html', result=result, page=page, total=total, type=type)
def home(): article = Article() result = article.find_limit_with_user(0, 10) total = math.ceil(article.get_total_count() / 10) # 总页数 last, most = article.last_most() return render_template('index.html', result=result, page=1, total=total, last=last, most=most)
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 search(page, keyword): keyword = keyword.strip() if keyword is None or keyword == '' or '%' in keyword or len(keyword) > 10: abort(404) start = (page - 1) * 10 article = Article() result = article.find_by_headline(keyword, start, 10) total = math.ceil(article.get_count_by_headline(keyword) / 10) return render_template('search.html', result=result, page=page, total=total, keyword=keyword)
def add_article(): headline = request.form.get('headline') content = request.form.get('content') type = int(request.form.get('type')) credit = int(request.form.get('credit')) drafted = int(request.form.get('drafted')) checked = int(request.form.get('checked')) articleid = int(request.form.get('articleid', 0)) print(drafted) if drafted == 0: drafted = True else: drafted = False print(drafted) url_list = parse_image_url(content) if len(url_list) > 0: thumbname = generate_thumb(url_list) else: thumbname = '%d.png' % type article = Article() if articleid == 0: try: id = article.insert_article(type=type, headline=headline, content=content, credit=credit, thumbnail=thumbname, drafted=drafted, checked=checked, hidden=False) return str(id) except Exception as e: print(e) return 'post-fail' else: try: id = article.update_article(articleid=articleid, type=type, headline=headline, content=content) return str(id) except Exception as e: print(e) return 'post-fail'
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 user_post(): id = request.args.get("id") if id is not None: article = Article().find_by_id2(id) else: article = None return render_template('user-post.html', article=article)
def admin_search_type(type, page): pagesize = 50 start = (page - 1) * pagesize result, total = Article().find_by_type_except_draft(start, pagesize, type) total = math.ceil(total / pagesize) return render_template('system-admin.html', page=page, result=result, total=total)
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 home(): article = Article() result = article.find_with_all(0, 10) last = article.find_last_9() most = article.find_most_9() total = math.ceil(article.get_total_count() / 10) content = render_template('index.html', result=result, page=1, total=total, last=last, most=most) return content
def paginate(page): page = int(page) article = Article() result = article.find_with_all((page - 1) * 10, 10) total = math.ceil(article.get_total_count() / 10) last = article.find_last_9() most = article.find_most_9() content = render_template('index.html', result=result, page=page, total=total, last=last, most=most) return content
def classify(type, page): start = (page - 1) * 10 article = Article() result = article.find_by_type(type, start, 10) total = math.ceil(article.get_count_by_type(type) / 10) last = article.find_last_9() most = article.find_most_9() return render_template('type.html', result=result, page=page, total=total, type=type, 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'
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 admin_article_hide(articleid): hidden = Article().switch_hidden(articleid) return str(hidden)
def pre_post(): id = request.args.get("id") article = Article().find_by_id(id) return render_template('post-user.html', article=article)
def user_post_list(): result = Article().find_with_users() return render_template('user-post-list.html', result=result)
def admin_search_headline(keyword): result = Article().find_by_headline_except_draft(keyword) return render_template('system-admin.html', page=1, result=result, total=1)
def admin_article_check(articleid): checked = Article().switch_checked(articleid) return str(checked)
def admin_article_drafted(articleid): drafted = Article().switch_publish(articleid) return redirect("/user/article")
def admin_article_hide2(articleid): hidden = Article().switch_hidden(articleid) return redirect("/user/article")