Browse Source

feat: 主页显示列表

SongZihuan 2 years ago
parent
commit
5cd18955d3
9 changed files with 109 additions and 25 deletions
  1. 8 1
      app/__init__.py
  2. 20 0
      app/comment.py
  3. 5 2
      app/index.py
  4. 4 2
      main.py
  5. 6 6
      templates/auth/login.html
  6. 16 2
      templates/base.html
  7. 50 0
      templates/comment/list.html
  8. 0 3
      templates/index/index.html
  9. 0 9
      templates/macro.html

+ 8 - 1
app/__init__.py

@@ -4,6 +4,7 @@ import logging
 import logging.handlers
 import logging.handlers
 import os
 import os
 import sys
 import sys
+from datetime import datetime
 
 
 from .db import db, Role, User
 from .db import db, Role, User
 from .moment import moment
 from .moment import moment
@@ -35,7 +36,10 @@ class HTalkFlask(Flask):
         @self.context_processor
         @self.context_processor
         def inject_base():
         def inject_base():
             """ app默认模板变量 """
             """ app默认模板变量 """
-            return {"conf": conf, "Role": Role, "User": User}
+            return {"conf": conf,
+                    "Role": Role,
+                    "User": User,
+                    "datetime": datetime}
 
 
     def blueprint(self):
     def blueprint(self):
         from .index import index
         from .index import index
@@ -44,6 +48,9 @@ class HTalkFlask(Flask):
         from .auth import auth
         from .auth import auth
         self.register_blueprint(auth, url_prefix="/auth")
         self.register_blueprint(auth, url_prefix="/auth")
 
 
+        from .comment import comment
+        self.register_blueprint(comment, url_prefix="/cm")
+
     def profile_setting(self):
     def profile_setting(self):
         if conf["DEBUG_PROFILE"]:
         if conf["DEBUG_PROFILE"]:
             self.wsgi_app = ProfilerMiddleware(self.wsgi_app, sort_by=("cumtime",))
             self.wsgi_app = ProfilerMiddleware(self.wsgi_app, sort_by=("cumtime",))

+ 20 - 0
app/comment.py

@@ -0,0 +1,20 @@
+from flask import Blueprint, render_template, request
+
+from .db import Comment
+from datetime import datetime
+
+
+comment = Blueprint("comment", __name__)
+
+
+@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))
+    return render_template("comment/list.html",
+                           page=page,
+                           items=pagination.items,
+                           pagination=pagination)

+ 5 - 2
app/index.py

@@ -1,4 +1,6 @@
-from flask import Blueprint, render_template
+from flask import Blueprint, redirect, url_for, request
+
+from .db import Comment
 
 
 
 
 index = Blueprint("base", __name__)
 index = Blueprint("base", __name__)
@@ -6,4 +8,5 @@ index = Blueprint("base", __name__)
 
 
 @index.route("/")
 @index.route("/")
 def index_page():
 def index_page():
-    return render_template("index/index.html")
+    page = request.args.get("page", 1, type=int)
+    return redirect(url_for("comment.list_all_page", page=page))

+ 4 - 2
main.py

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

+ 6 - 6
templates/auth/login.html

@@ -22,8 +22,8 @@
     <div class="tab-pane container fade {% if on_passwd_login %} show active {% endif %}" id="passwd_login">
     <div class="tab-pane container fade {% if on_passwd_login %} show active {% endif %}" id="passwd_login">
         <form method="post" action="{{ url_for("auth.passwd_login_page") }}" class="was-validated">
         <form method="post" action="{{ url_for("auth.passwd_login_page") }}" class="was-validated">
             {{ passwd_login_form.hidden_tag() }}
             {{ passwd_login_form.hidden_tag() }}
-            {{ macro.render_field(passwd_login_form.email) }}
-            {{ macro.render_field(passwd_login_form.passwd) }}
+            {{ render_field(passwd_login_form.email) }}
+            {{ render_field(passwd_login_form.passwd) }}
 
 
             <div class="text-end">
             <div class="text-end">
                 {{ passwd_login_form.submit(class='btn btn-success me-2') }}
                 {{ passwd_login_form.submit(class='btn btn-success me-2') }}
@@ -34,7 +34,7 @@
     <div class="tab-pane container fade {% if on_email_login %} show active {% endif %}" id="email_login">
     <div class="tab-pane container fade {% if on_email_login %} show active {% endif %}" id="email_login">
         <form method="post" action="{{ url_for("auth.email_login_page") }}" class="was-validated">
         <form method="post" action="{{ url_for("auth.email_login_page") }}" class="was-validated">
             {{ email_login_form.hidden_tag() }}
             {{ email_login_form.hidden_tag() }}
-            {{ macro.render_field(email_login_form.email) }}
+            {{ render_field(email_login_form.email) }}
 
 
             <div class="text-end">
             <div class="text-end">
                 {{ email_login_form.submit(class='btn btn-success me-2') }}
                 {{ email_login_form.submit(class='btn btn-success me-2') }}
@@ -45,9 +45,9 @@
     <div class="tab-pane container fade {% if on_register %} show active {% endif %}" id="register">
     <div class="tab-pane container fade {% if on_register %} show active {% endif %}" id="register">
         <form method="post" action="{{ url_for("auth.register_page") }}" class="was-validated">
         <form method="post" action="{{ url_for("auth.register_page") }}" class="was-validated">
             {{ register_form.hidden_tag() }}
             {{ register_form.hidden_tag() }}
-            {{ macro.render_field(register_form.email) }}
-            {{ macro.render_field(register_form.passwd) }}
-            {{ macro.render_field(register_form.passwd_again) }}
+            {{ render_field(register_form.email) }}
+            {{ render_field(register_form.passwd) }}
+            {{ render_field(register_form.passwd_again) }}
 
 
 
 
             <div class="text-end">
             <div class="text-end">

+ 16 - 2
templates/base.html

@@ -1,4 +1,16 @@
-{% import "macro.html" as macro %}
+{% macro render_field(field) %}
+    <div class="form-floating my-3">
+        {{ field(class="form-control", placeholder=field.label.text, **kwargs) | safe }}
+        {{ field.label }}
+        {% for error in field.errors %}
+            <div class="invalid-feedback"> {{ error }} </div>
+        {% endfor %}
+    </div>
+{% endmacro %}
+
+{% macro show_time(time) %}
+    {{ moment(datetime.utcfromtimestamp(datetime.timestamp(time))).format('YYYY-MM-DD HH:mm:ss') }}
+{% endmacro %}
 
 
 <!DOCTYPE html>
 <!DOCTYPE html>
 <html lang="zh">
 <html lang="zh">
@@ -38,7 +50,9 @@
     {% block nav %}
     {% block nav %}
         <div class="container mt-2">
         <div class="container mt-2">
             <a class="h3" href="/" style="text-decoration:none;color:#333;"> {{ conf["WEBSITE_TITLE"] }} </a>
             <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"> 用户管理 </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("base.index_page") }}" class="btn btn-danger float-end text-white mx-2"> 主页 </a>
         </div>
         </div>
     {% endblock %}
     {% endblock %}
 
 

+ 50 - 0
templates/comment/list.html

@@ -0,0 +1,50 @@
+{% extends "base.html" %}
+
+{% block title %} 主页 {% endblock %}
+
+{% block content %}
+    <div class="container text-center">
+        <div class="list-group 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>
+            {% 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) }}"> 上一页 </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) }}"> {{ p }} </a>
+                        </li>
+                    {% else %}
+                        <li class="page-item">
+                            <a class="page-link" href="{{ url_for("comment.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("comment.list_all_page", page=pagination.next_num) }}"> 下一页 </a>
+                </li>
+            {% endif %}
+        </ul>
+
+    </div>
+{% endblock %}

+ 0 - 3
templates/index/index.html

@@ -1,3 +0,0 @@
-{% extends "base.html" %}
-
-{% block title %} 主页 {% endblock %}

+ 0 - 9
templates/macro.html

@@ -1,9 +0,0 @@
-{% macro render_field(field) %}
-    <div class="form-floating my-3">
-        {{ field(class="form-control", placeholder=field.label.text, **kwargs) | safe }}
-        {{ field.label }}
-        {% for error in field.errors %}
-            <div class="invalid-feedback"> {{ error }} </div>
-        {% endfor %}
-    </div>
-{% endmacro %}