Browse Source

feat: 显示用户讨论列表

SongZihuan 2 years ago
parent
commit
795884b5f5

+ 28 - 4
app/comment.py

@@ -1,6 +1,6 @@
 from flask import Blueprint, render_template, request, abort
 
-from .db import Comment,Archive
+from .db import Comment, Archive, User
 
 
 comment = Blueprint("comment", __name__)
@@ -35,14 +35,15 @@ def list_all_page():
                                items=pagination.items,
                                pagination=pagination,
                                archive_name="全部讨论",
-                               archive_describe="罗列了本站所有的讨论")
+                               archive_describe="罗列了本站所有的讨论",
+                               title="主页")
     else:
         archive = Archive.query.filter_by(id=archive_id).first()
         if not archive:
             return abort(404)
         pagination = (archive.comment
                       .filter(Comment.title != None).filter(Comment.father_id == None)
-                      .order_by(Comment.create_time.desc(), Comment.title.desc())
+                      .order_by(Comment.create_time.desc(), Comment.title.asc())
                       .paginate(page=page, per_page=8, error_out=False))
         return render_template("comment/list.html",
                                page=page,
@@ -50,4 +51,27 @@ def list_all_page():
                                items=pagination.items,
                                pagination=pagination,
                                archive_name=archive.name,
-                               archive_describe=archive.describe)
+                               archive_describe=archive.describe,
+                               title=archive.name)
+
+
+@comment.route("/user")
+def user_page():
+    page = request.args.get("page", 1, type=int)
+    user_id = request.args.get("user", -1, type=int)
+    if user_id == -1:
+        return abort(404)
+
+    user: User = User.query.filter_by(id=user_id).first()
+    if not user:
+        return abort(404)
+
+    pagination = (user.comment
+                  .order_by(Comment.create_time.desc(), Comment.title.asc())
+                  .paginate(page=page, per_page=8, error_out=False))
+    return render_template("comment/user.html",
+                           page=page,
+                           user=user,
+                           items=pagination.items,
+                           pagination=pagination,
+                           title=user.email)

+ 5 - 1
app/db.py

@@ -34,13 +34,17 @@ class User(db.Model, UserMixin):
     passwd_hash = db.Column(db.String(128), nullable=False)
     role_id = db.Column(db.Integer, db.ForeignKey("role.id"), default=3)
     role = db.relationship("Role", back_populates="user")
-    comment = db.relationship("Comment", back_populates="auth")
+    comment = db.relationship("Comment", back_populates="auth", lazy="dynamic")
 
     followed = db.relationship("Follow", primaryjoin="Follow.follower_id==User.id", back_populates="follower",
                                lazy="dynamic")  # User 关注的人
     follower = db.relationship("Follow", primaryjoin="Follow.followed_id==User.id", back_populates="followed",
                                lazy="dynamic")  # 关注 User 的人
 
+    @property
+    def comment_count(self):
+        return self.comment.count()
+
     @property
     def follower_count(self):
         return self.follower.count()

+ 1 - 1
templates/archive/list.html

@@ -14,7 +14,7 @@
                         <p class="text-end">
                             <a class="btn btn-link" href="{{ url_for("comment.list_all_page", archive=i.id, page=1) }}"> 前往查看 </a>
                             <br>
-                            论个数:{{ i.comment_count }}
+                            论个数:{{ i.comment_count }}
                         </p>
                     </div>
                 </div>

+ 1 - 0
templates/auth/user.html

@@ -11,6 +11,7 @@
             <a class="list-group-item">是否封禁:{{ "否" if user.role.has_permission(Role.USABLE) else "是" }} </a>
             <a class="list-group-item">关注:{{ user.followed_count }}</a>
             <a class="list-group-item">粉丝:{{ user.follower_count }}</a>
+            <a class="list-group-item" href="{{ url_for("comment.user_page", page=1, user=user.id) }}">讨论:{{ user.comment_count }}</a>
         </div>
 
         <div class="text-end">

+ 1 - 0
templates/auth/yours.html

@@ -13,6 +13,7 @@
             <a class="list-group-item">用户权限:{{ current_user.role.permission }}</a>
             <a class="list-group-item" href="{{ url_for("auth.followed_page") }}">关注:{{ current_user.followed_count }}</a>
             <a class="list-group-item" href="{{ url_for("auth.follower_page") }}">粉丝:{{ current_user.follower_count }}</a>
+            <a class="list-group-item" href="{{ url_for("comment.user_page", page=1, user=current_user.id) }}">讨论:{{ current_user.comment_count }}</a>
         </div>
 
         <div class="text-end">

+ 2 - 2
templates/comment/comment.html

@@ -21,7 +21,7 @@
                         <a class="btn btn-link" href="{{ url_for("comment.comment_page", comment_id=comment.father_id) }}"> 查看父讨论 </a>
                         <br>
                     {% endif %}
-                    论个数:{{ comment.son_count }}
+                    子讨论个数:{{ comment.son_count }}
                     <br>
                     {{ show_time(comment.update_time) }}/{{ show_time(comment.create_time) }}
                 </p>
@@ -40,7 +40,7 @@
                     <p class="text-end">
                         <a class="btn btn-link" href="{{ url_for("comment.comment_page", comment_id=i.id) }}"> 前往查看 </a>
                         <br>
-                        论个数:{{ i.son_count }}
+                        子讨论个数:{{ i.son_count }}
                         <br>
                         {{ show_time(i.update_time) }}/{{ show_time(i.create_time) }}
                     </p>

+ 2 - 2
templates/comment/list.html

@@ -1,6 +1,6 @@
 {% extends "base.html" %}
 
-{% block title %} 主页 {% endblock %}
+{% block title %} {{ title }} {% endblock %}
 
 {% block content %}
     <div class="container mt-3">
@@ -27,7 +27,7 @@
                         <p class="text-end">
                             <a class="btn btn-link" href="{{ url_for("comment.comment_page", comment_id=i.id) }}"> 前往查看 </a>
                             <br>
-                            论个数:{{ i.son_count }}
+                            子讨论个数:{{ i.son_count }}
                             <br>
                             {{ show_time(i.update_time) }}/{{ show_time(i.create_time) }}
                         </p>

+ 76 - 0
templates/comment/user.html

@@ -0,0 +1,76 @@
+{% extends "base.html" %}
+
+{% block title %} {{ title }} {% endblock %}
+
+{% block content %}
+    <div class="container mt-3">
+        <div class="card">
+            <div class="card-body">
+                <h4 class="card-title">  用户: {{ user.email }} </h4>
+                <p class="card-text"> 
+                    用户ID:{{ user.id }}
+                    <br>
+                    粉丝:{{ user.follower_count }}
+                    <br>
+                    关注:{{ user.followed_count }}
+                </p>
+            </div>
+        </div>
+    </div>
+
+
+    <div class="container text-center">
+        <div class="mt-2 text-start">
+            {% for i in items %}
+                <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>
+
+                        <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>
+
+        <ul class="pagination justify-content-center mt-2">
+            {% if pagination.has_prev %}
+                <li class="page-item">
+                    <a class="page-link" href="{{ url_for("comment.list_all_page", page=pagination.prev_num, user=user.id) }}"> 上一页 </a>
+                </li>
+            {% endif %}
+
+            {% for p in pagination.iter_pages(left_edge=2, left_current=2, right_current=5, right_edge=2) %}
+                {% if p %}
+                    {% if p == pagination.page %}
+                        <li class="page-item active">
+                            <a class="page-link" href="{{ url_for("comment.list_all_page", page=p, user=user.id) }}"> {{ p }} </a>
+                        </li>
+                    {% else %}
+                        <li class="page-item">
+                            <a class="page-link" href="{{ url_for("comment.list_all_page", page=p, user=user.id) }}"> {{ p }} </a>
+                        </li>
+                    {% endif %}
+                {% else %}
+                    <li class="page-item disabled">
+                        <a class="page-link" href="#">&hellip;</a>
+                    </li>
+                {% endif %}
+            {% endfor %}
+
+            {% if pagination.has_next %}
+                <li class="page-item">
+                    <a class="page-link" href="{{ url_for("comment.list_all_page", page=pagination.next_num, user=user.id) }}"> 下一页 </a>
+                </li>
+            {% endif %}
+        </ul>
+    </div>
+{% endblock %}