فهرست منبع

feat: 显示归档列表

SongZihuan 2 سال پیش
والد
کامیت
119a5cdeba
6فایلهای تغییر یافته به همراه135 افزوده شده و 7 حذف شده
  1. 3 0
      app/__init__.py
  2. 18 0
      app/archive.py
  3. 49 5
      app/db.py
  4. 7 1
      main.py
  5. 57 0
      templates/archive/list.html
  6. 1 1
      templates/base.html

+ 3 - 0
app/__init__.py

@@ -51,6 +51,9 @@ class HTalkFlask(Flask):
         from .comment import comment
         self.register_blueprint(comment, url_prefix="/cm")
 
+        from .archive import archive
+        self.register_blueprint(archive, url_prefix="/ac")
+
     def profile_setting(self):
         if conf["DEBUG_PROFILE"]:
             self.wsgi_app = ProfilerMiddleware(self.wsgi_app, sort_by=("cumtime",))

+ 18 - 0
app/archive.py

@@ -0,0 +1,18 @@
+from flask import Blueprint, render_template, request
+
+from .db import Archive
+
+
+archive = Blueprint("archive", __name__)
+
+
+@archive.route("/all")
+def list_all_page():
+    page = request.args.get("page", 1, type=int)
+    pagination = (Archive.query
+                  .order_by(Archive.name.asc())
+                  .paginate(page=page, per_page=8, error_out=False))
+    return render_template("archive/list.html",
+                           page=page,
+                           items=pagination.items,
+                           pagination=pagination)

+ 49 - 5
app/db.py

@@ -117,11 +117,11 @@ class Role(db.Model):
             self.permission -= permission
 
 
-StudentClass = db.Table("archive_comment",
-                        db.Column("archive_id", db.Integer, db.ForeignKey("archive.id"),
-                                  nullable=False, primary_key=True),
-                        db.Column("comment_id", db.Integer, db.ForeignKey("comment.id"),
-                                  nullable=False, primary_key=True))
+ArchiveComment = db.Table("archive_comment",
+                          db.Column("archive_id", db.Integer, db.ForeignKey("archive.id"),
+                                    nullable=False, primary_key=True),
+                          db.Column("comment_id", db.Integer, db.ForeignKey("comment.id"),
+                                    nullable=False, primary_key=True))
 
 
 class Comment(db.Model):
@@ -155,6 +155,10 @@ class Archive(db.Model):
     describe = db.Column(db.String(100), nullable=False)
     comment = db.relationship("Comment", back_populates="archive", secondary="archive_comment")
 
+    @property
+    def comment_count(self):
+        return len(self.comment)
+
 
 def create_all():
     try:
@@ -216,3 +220,43 @@ def create_faker_comment(auth_max=100):
             db.session.rollback()
         else:
             count_comment += 1
+
+
+def create_faker_archive():
+    from faker import Faker
+    from sqlalchemy.exc import IntegrityError
+    fake = Faker("zh_CN")
+
+    count_archive = 0
+    while count_archive < 20:
+        company = fake.company()
+        archive = Archive(name=company, describe=f"加人{company}")
+        db.session.add(archive)
+
+        try:
+            db.session.commit()
+        except IntegrityError:
+            db.session.rollback()
+        else:
+            count_archive += 1
+
+
+def create_fake_archive_comment():
+    from random import randint
+    from sqlalchemy.exc import IntegrityError
+
+    comment_count = Comment.query.count()
+    archive_count = Archive.query.count()
+
+    count_archive_comment = 0
+    while count_archive_comment < 20:
+        comment = Comment.query.offset(randint(0, comment_count)).limit(1).first()
+        archive = Archive.query.offset(randint(0, archive_count)).limit(1).first()
+        archive.archive.append(comment)
+
+        try:
+            db.session.commit()
+        except IntegrityError:
+            db.session.rollback()
+        else:
+            count_archive_comment += 1

+ 7 - 1
main.py

@@ -19,11 +19,17 @@ app = HTalkFlask(__name__)
 
 @app.shell_context_processor
 def make_shell_context():
-    from app.db import db, create_all, create_faker_user, create_faker_comment
+    from app.db import (db, create_all,
+                        create_faker_user,
+                        create_faker_comment,
+                        create_faker_archive,
+                        create_fake_archive_comment)
     return {
         "app": app,
         "db": db,
         "create_all": create_all,
         "create_faker_user": create_faker_user,
         "create_faker_comment": create_faker_comment,
+        "create_faker_archive": create_faker_archive,
+        "create_fake_archive_comment": create_fake_archive_comment,
     }

+ 57 - 0
templates/archive/list.html

@@ -0,0 +1,57 @@
+{% extends "base.html" %}
+
+{% block title %} 主页 {% endblock %}
+
+{% block content %}
+    <div class="container text-center">
+        <div class="mt-2 text-start">
+            {% for i in items %}
+                <div class="card mt-2">
+                    <div class="card-body">
+                        <h4 class="card-title"> {{ i.name }} </h4>
+                        <p class="card-text"> {{ i.describe }} </p>
+
+                        <p class="text-end">
+                            <a class="btn btn-link" href="#"> 前往查看 </a>
+                            <br>
+                            评论个数:{{ i.comment_count }}
+                        </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("archive.list_all_page", page=pagination.prev_num) }}"> 上一页 </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("archive.list_all_page", page=p) }}"> {{ p }} </a>
+                        </li>
+                    {% else %}
+                        <li class="page-item">
+                            <a class="page-link" href="{{ url_for("archive.list_all_page", page=p) }}"> {{ 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("archive.list_all_page", page=pagination.next_num) }}"> 下一页 </a>
+                </li>
+            {% endif %}
+        </ul>
+
+    </div>
+{% endblock %}

+ 1 - 1
templates/base.html

@@ -51,7 +51,7 @@
         <div class="container mt-2">
             <a class="h3" href="/" style="text-decoration:none;color:#333;"> {{ conf["WEBSITE_TITLE"] }} </a>
             <a href="{{ url_for("auth.auth_page") }}" class="btn btn-success float-end text-white mx-2"> 用户管理 </a>
-            <a href="#" class="btn btn-dark float-end text-white mx-2"> 归档 </a>
+            <a href="{{ url_for("archive.list_all_page") }}" class="btn btn-dark float-end text-white mx-2"> 归档 </a>
             <a href="{{ url_for("base.index_page") }}" class="btn btn-danger float-end text-white mx-2"> 主页 </a>
         </div>
     {% endblock %}