Przeglądaj źródła

feat: 新增删除评论

SongZihuan 3 lat temu
rodzic
commit
5695d1914b
3 zmienionych plików z 48 dodań i 2 usunięć
  1. 6 1
      core/comment.py
  2. 27 0
      templates/docx/article.html
  3. 15 1
      view/docx.py

+ 6 - 1
core/comment.py

@@ -1,6 +1,8 @@
 from sql.comment import read_comment, create_comment, get_user_comment_count, delete_comment
 import core.user
 
+from typing import Optional
+
 
 def load_comment_list(blog_id: int):
     comment_list = read_comment(blog_id)
@@ -11,7 +13,10 @@ def load_comment_list(blog_id: int):
 
 
 class Comment:
-    def __init__(self, comment_id, blog_id: int, auth: "core.user.User", context: str, update_time=None):
+    def __init__(self, comment_id,
+                 blog_id: Optional[int],
+                 auth: "Optional[core.user.User]",
+                 context: Optional[str], update_time=None):
         self.comment_id = comment_id
         self.blog_id = blog_id
         self.auth = auth

+ 27 - 0
templates/docx/article.html

@@ -59,6 +59,27 @@
                     <hr>
 
                     {% for comment in article.comment %}
+
+                        {% if show_delete %}
+                            <div id="DeleteModal{{comment.comment_id}}" class="modal fade" role="dialog" aria-hidden="true">
+                                <div class="modal-dialog">
+                                    <div class="modal-content text-left">
+                                        <div class="modal-header">
+                                            <h4 class="modal-title"> 确认删除评论? </h4>
+                                        </div>
+                                        <div class="modal-body">
+                                            <p> 是否确认删除评论? </p>
+                                        </div>
+                                        <div class="modal-footer">
+                                            <a class="btn btn-danger"
+                                               href="{{ url_for("docx.delete_comment_page", comment_id=comment.comment_id) }}"> 删除 </a>
+                                            <button type="button" class="btn btn-secondary" data-dismiss="modal"> 取消 </button>
+                                        </div>
+                                    </div>
+                                </div>
+                            </div>
+                        {% endif %}
+
                         <section class="col-12">
                             <div class="comment">
                                 <p class="comment-title h5">
@@ -67,6 +88,12 @@
                                     {% else %}
                                         {{ comment.auth.s_email }}
                                     {% endif %}
+
+                                    {% if show_delete %}
+                                        <a class="mb-2"
+                                            data-toggle="modal" data-target="#DeleteModal{{comment.comment_id}}"> &times; </a>
+                                    {% endif %}
+
                                     <br>
                                     <small> {{ comment.update_time }} </small>
                                 </p>

+ 15 - 1
view/docx.py

@@ -77,7 +77,8 @@ def article_page(blog_id: int):
     return render_template("docx/article.html",
                            article=article,
                            archive_list=article.archive,
-                           form=WriteCommentForm())
+                           form=WriteCommentForm(),
+                           show_delete=current_user.check_role("DeleteComment"))
 
 
 @docx.route('/comment/<int:blog>', methods=["POST"])
@@ -146,6 +147,19 @@ def delete_blog_page(blog_id: int):
     return redirect(url_for("docx.docx_page", page=1))
 
 
+@docx.route("delete_comment/<int:comment_id>")
+@login_required
+def delete_comment_page(comment_id: int):
+    if not current_user.check_role("DeleteComment"):
+        abort(403)
+        return
+    if Comment(comment_id, None, None, None).delete():
+        flash("博文评论成功")
+    else:
+        flash("博文评论失败")
+    return redirect(url_for("docx.docx_page", page=1))
+
+
 @docx.context_processor
 def inject_base():
     return {"top_nav": ["", "", "active", "", "", ""]}