115 lines
4.4 KiB
Python
115 lines
4.4 KiB
Python
import os
|
|
|
|
from flask import render_template, request, url_for, flash, jsonify, session
|
|
from werkzeug.utils import redirect
|
|
|
|
from flask_app.decorators import permission_required
|
|
from . import faqs
|
|
from .. import db
|
|
from .forms import PostForm, CommentForm
|
|
from flask_login import login_required,current_user
|
|
from ..models import User, Post, Comment, Like, Collect, Permission
|
|
|
|
root_path=faqs.root_path
|
|
root_dir=os.path.dirname(root_path)
|
|
UPLOAD_FOLDER = os.path.join(root_dir,"static/images")
|
|
|
|
@faqs.route('/article/<int:id>',methods=['GET','POST'])
|
|
@permission_required(Permission.COMMENT)
|
|
def article(id):
|
|
session['next'] = request.url # 将当前URL保存到session中
|
|
post=Post.query.get_or_404(id)
|
|
form=CommentForm()
|
|
has_liked = Like.query.filter_by(user_id=current_user.id, post_id=id).first() is not None
|
|
has_collected = Collect.query.filter_by(user_id=current_user.id, post_id=id).first() is not None
|
|
if form.validate_on_submit():
|
|
comment=Comment(body=form.body.data,post=post,author=current_user._get_current_object())
|
|
post.comment_num+=1
|
|
db.session.add(comment)
|
|
db.session.add(post)
|
|
db.session.commit()
|
|
flash('评论发布成功!','success')
|
|
form.body.data = ''
|
|
return redirect(url_for('faqs.article',id=post.id))
|
|
else:
|
|
for field, errors in form.errors.items():
|
|
for error in errors:
|
|
flash(f'Error in {getattr(form, field).label.text}: {error}')
|
|
comments = Comment.query.filter_by(post_id=id).all()
|
|
authors = [User.query.get(comment.author_id) for comment in comments] #authors指发布评论的人
|
|
comment_pairs = zip(comments, authors)
|
|
empty_comments = (len(comments) == 0)
|
|
return render_template('faqs/article.html',post=post,form=form,comment_pairs=comment_pairs,
|
|
empty_comments=empty_comments,has_liked=has_liked,has_collected=has_collected)
|
|
|
|
|
|
@faqs.route('/post/<username>',methods=['GET','POST'])
|
|
@permission_required(Permission.COMMENT)
|
|
def post(username):
|
|
session['next'] = request.url # 将当前URL保存到session中
|
|
form = PostForm()
|
|
UPLOAD_FOLDER = os.path.join(root_dir, "static/images")
|
|
if form.validate_on_submit():
|
|
post = Post(title=form.title.data, content=form.content.data,
|
|
author=current_user._get_current_object())
|
|
img_count = 0
|
|
filefolder = os.path.join(UPLOAD_FOLDER, "post", "none")
|
|
if not os.path.exists(filefolder):
|
|
os.makedirs(filefolder)
|
|
for i, file in enumerate(request.files.getlist('images')):
|
|
filepath = os.path.join(filefolder, str(i+1)+".jpg")
|
|
file.save(filepath)
|
|
img_count += 1
|
|
post.img_count = img_count
|
|
db.session.commit()
|
|
new_dir_name=os.path.join(os.path.dirname(filefolder),str(post.id))
|
|
os.rename(filefolder,new_dir_name)
|
|
flash('您已成功发布一篇文章')
|
|
return redirect(url_for('main.faqs'))
|
|
return render_template('faqs/post.html', form=form)
|
|
|
|
@faqs.route('/like/<int:post_id>', methods=['POST', 'DELETE'])
|
|
@login_required
|
|
def like(post_id):
|
|
post = Post.query.get_or_404(post_id)
|
|
user = current_user
|
|
|
|
if request.method == 'POST':
|
|
# 处理点赞请求
|
|
post.like_num += 1
|
|
like = Like(post_id=post_id, user_id=user.id)
|
|
db.session.add(like)
|
|
db.session.add(post)
|
|
db.session.commit()
|
|
elif request.method == 'DELETE':
|
|
# 处理取消点赞请求
|
|
post.like_num -= 1
|
|
like = Like.query.filter_by(post_id=post_id, user_id=user.id).first()
|
|
db.session.delete(like)
|
|
db.session.add(post)
|
|
db.session.commit()
|
|
# 返回点赞状态
|
|
return jsonify()
|
|
|
|
@faqs.route('/collect/<int:post_id>', methods=['POST', 'DELETE'])
|
|
@login_required
|
|
def collect(post_id):
|
|
post = Post.query.get_or_404(post_id)
|
|
user = current_user
|
|
if request.method == 'POST':
|
|
# 处理点赞请求
|
|
post.collect_num += 1
|
|
collect = Collect(post_id=post_id, user_id=user.id)
|
|
db.session.add(collect)
|
|
db.session.add(post)
|
|
db.session.commit()
|
|
elif request.method == 'DELETE':
|
|
# 处理取消点赞请求
|
|
post.collect_num -= 1
|
|
collect = Like.query.filter_by(post_id=post_id, user_id=user.id).first()
|
|
db.session.delete(collect)
|
|
db.session.add(post)
|
|
db.session.commit()
|
|
# 返回点赞状态
|
|
return jsonify()
|