Browse Source

feat: 显示单个讨论

SongZihuan 2 years ago
parent
commit
101a1d97c5
4 changed files with 104 additions and 10 deletions
  1. 18 4
      app/comment.py
  2. 5 0
      app/db.py
  3. 53 0
      templates/comment/comment.html
  4. 28 6
      templates/comment/list.html

+ 18 - 4
app/comment.py

@@ -1,20 +1,34 @@
-from flask import Blueprint, render_template, request
+from flask import Blueprint, render_template, request, abort
 
 from .db import Comment
-from datetime import datetime
 
 
 comment = Blueprint("comment", __name__)
 
 
+@comment.route("/")
+def comment_page():
+    comment_id = request.args.get("comment_id", -1, type=int)
+    if comment_id == -1:
+        return abort(404)
+    cm: Comment = Comment.query.filter_by(id=comment_id).first()
+    if cm:
+        return render_template("comment/comment.html",
+                               comment=cm,
+                               comment_son=cm.son)
+    return abort(404)
+
+
 @comment.route("/all")
 def list_all_page():
     page = request.args.get("page", 1, type=int)
     pagination = (Comment.query
                   .filter(Comment.title != None).filter(Comment.father_id == None)
                   .order_by(Comment.create_time.desc(), Comment.title.desc())
-                  .paginate(page=page, per_page=20, error_out=False))
+                  .paginate(page=page, per_page=8, error_out=False))
     return render_template("comment/list.html",
                            page=page,
                            items=pagination.items,
-                           pagination=pagination)
+                           pagination=pagination,
+                           archive_name="全部讨论",
+                           archive_describe="罗列了本站所有的讨论")

+ 5 - 0
app/db.py

@@ -142,6 +142,11 @@ class Comment(db.Model):
     archive = db.relationship("Archive", back_populates="comment", secondary="archive_comment")
 
 
+    @property
+    def son_count(self):
+        return len(self.son)
+
+
 class Archive(db.Model):
     __tablename__ = "archive"
 

+ 53 - 0
templates/comment/comment.html

@@ -0,0 +1,53 @@
+{% extends "base.html" %}
+
+{% block title %} 主页 {% endblock %}
+
+{% block content %}
+    <div class="container mt-3">
+        <div>
+            <span class="h5"> 讨论ID:{{ comment.id }} </span>
+            <a class="btn btn-warning float-end"> 添加子讨论 </a>
+        </div>
+
+        <div class="card mt-4">
+            <div class="card-body">
+                {% if comment.title %}
+                    <h4 class="card-title"> {{ comment.title }} </h4>
+                {% endif %}
+                <p class="card-text"> {{ comment.content }} </p>
+                <span class="badge bg-info"> {{ comment.auth.email }} </span>
+                <p class="text-end">
+                    {% if comment.father_id %}
+                        <a class="btn btn-link" href="{{ url_for("comment.comment_page", comment_id=comment.father_id) }}"> 查看父讨论 </a>
+                        <br>
+                    {% endif %}
+                    评论个数:{{ comment.son_count }}
+                    <br>
+                    {{ show_time(comment.update_time) }}/{{ show_time(comment.create_time) }}
+                </p>
+            </div>
+        </div>
+
+        {% for i in comment_son %}
+            <div class="card mt-2">
+                <div class="card-body">
+                    {% if i.title %}
+                        <h4 class="card-title"> {{ i.title }} </h4>
+                    {% endif %}
+                    <p class="card-text"> {{ i.content }} </p>
+                    <span class="badge bg-info"> {{ i.auth.email }} </span>
+
+                    <p class="text-end">
+                        <a class="btn btn-link" href="{{ url_for("comment.comment_page", comment_id=i.id) }}"> 前往查看 </a>
+                        <br>
+                        评论个数:{{ i.son_count }}
+                        <br>
+                        {{ show_time(i.update_time) }}/{{ show_time(i.create_time) }}
+                    </p>
+                </div>
+            </div>
+        {% endfor %}
+
+    </div>
+
+{% endblock %}

+ 28 - 6
templates/comment/list.html

@@ -3,14 +3,36 @@
 {% block title %} 主页 {% endblock %}
 
 {% block content %}
+    <div class="container mt-3">
+        <div class="card">
+            <div class="card-body">
+                <h4 class="card-title"> {{ archive_name }} </h4>
+                <p class="card-text"> {{ archive_describe }} </p>
+            </div>
+        </div>
+    </div>
+
+
     <div class="container text-center">
-        <div class="list-group mt-2 text-start">
+        <div class="mt-2 text-start">
             {% for i in items %}
-                <a href="#" class="list-group-item list-group-item-action">
-                    <span class="h5"> {{ i.title }} </span>
-                    ({{ show_time(i.update_time) }}/{{ show_time(i.create_time) }})
-                    <span class="badge bg-info"> {{ i.auth.email }} </span>
-                </a>
+                <div class="card mt-2">
+                    <div class="card-body">
+                        {% if i.title %}
+                            <h4 class="card-title"> {{ i.title }} </h4>
+                        {% endif %}
+                        <p class="card-text"> {{ i.content }} </p>
+                        <span class="badge bg-info"> {{ i.auth.email }} </span>
+
+                        <p class="text-end">
+                            <a class="btn btn-link" href="{{ url_for("comment.comment_page", comment_id=i.id) }}"> 前往查看 </a>
+                            <br>
+                            评论个数:{{ i.son_count }}
+                            <br>
+                            {{ show_time(i.update_time) }}/{{ show_time(i.create_time) }}
+                        </p>
+                    </div>
+                </div>
             {% endfor %}
         </div>