|
5 | 5 | from datetime import datetime |
6 | 6 |
|
7 | 7 | from app import engine |
8 | | -from app.models import Post, User |
| 8 | +from app.models import Post, User, Comment |
| 9 | +from app.forms import CommentForm, EditCommentForm |
9 | 10 |
|
10 | 11 | # 创建蓝图 |
11 | 12 | main = Blueprint('main', __name__) |
@@ -66,12 +67,29 @@ def view_post(post_id): |
66 | 67 | with Session(engine) as session: |
67 | 68 | post = session.get(Post, post_id) |
68 | 69 | if post: |
69 | | - return render_template('view_post.html', post=post) |
| 70 | + return render_template('view_post.html', post=post, form=CommentForm()) |
70 | 71 | else: |
71 | 72 | flash('博文未找到!') |
72 | 73 | return redirect(url_for('main.index')) |
73 | 74 |
|
74 | 75 |
|
| 76 | +@main.route('/post/<int:post_id>/comment', methods=['POST']) |
| 77 | +@login_required |
| 78 | +def add_comment(post_id): |
| 79 | + form = CommentForm() |
| 80 | + if form.validate_on_submit(): |
| 81 | + with Session(engine) as session: |
| 82 | + new_comment = Comment( |
| 83 | + content=form.content.data, |
| 84 | + author_id=current_user.id, |
| 85 | + post_id=post_id |
| 86 | + ) |
| 87 | + session.add(new_comment) |
| 88 | + session.commit() |
| 89 | + flash('评论已发布!', 'success') |
| 90 | + return redirect(url_for('main.view_post', post_id=post_id)) |
| 91 | + |
| 92 | + |
75 | 93 | @main.route('/edit_post/<int:post_id>', methods=['GET', 'POST']) |
76 | 94 | @login_required |
77 | 95 | def edit_post(post_id): |
@@ -116,3 +134,47 @@ def delete_post(post_id): |
116 | 134 | session.commit() |
117 | 135 | flash('你的博文已被删除!') |
118 | 136 | return redirect(url_for('main.dashboard')) |
| 137 | + |
| 138 | + |
| 139 | +@main.route('/comment/<int:comment_id>/edit', methods=['GET', 'POST']) |
| 140 | +@login_required |
| 141 | +def edit_comment(comment_id): |
| 142 | + with Session(engine) as session: |
| 143 | + comment = session.get(Comment, comment_id) |
| 144 | + if not comment: |
| 145 | + flash('评论未找到!', 'error') |
| 146 | + return redirect(url_for('main.index')) |
| 147 | + if comment.author_id != current_user.id: |
| 148 | + flash('你没有权限编辑这条评论!', 'error') |
| 149 | + return redirect(url_for('main.view_post', post_id=comment.post_id)) |
| 150 | + |
| 151 | + form = EditCommentForm() |
| 152 | + if form.validate_on_submit(): |
| 153 | + comment.content = form.content.data |
| 154 | + session.commit() |
| 155 | + flash('评论已更新!', 'success') |
| 156 | + return redirect(url_for('main.view_post', post_id=comment.post_id)) |
| 157 | + elif request.method == 'GET': |
| 158 | + form.content.data = comment.content |
| 159 | + |
| 160 | + return render_template('edit_comment.html', form=form, comment=comment) |
| 161 | + |
| 162 | + |
| 163 | +# 删除评论 |
| 164 | +@main.route('/comment/<int:comment_id>/delete', methods=['POST']) |
| 165 | +@login_required |
| 166 | +def delete_comment(comment_id): |
| 167 | + with Session(engine) as session: |
| 168 | + comment = session.get(Comment, comment_id) |
| 169 | + if not comment: |
| 170 | + flash('评论未找到!', 'error') |
| 171 | + return redirect(url_for('main.index')) |
| 172 | + if comment.author_id != current_user.id: |
| 173 | + flash('你没有权限删除这条评论!', 'error') |
| 174 | + return redirect(url_for('main.view_post', post_id=comment.post_id)) |
| 175 | + |
| 176 | + post_id = comment.post_id |
| 177 | + session.delete(comment) |
| 178 | + session.commit() |
| 179 | + flash('评论已删除!', 'success') |
| 180 | + return redirect(url_for('main.view_post', post_id=post_id)) |
0 commit comments