Selaa lähdekoodia

feat: 增加archive

SongZihuan 2 vuotta sitten
vanhempi
sitoutus
117b63909d
4 muutettua tiedostoa jossa 52 lisäystä ja 4 poistoa
  1. 33 3
      app/archive.py
  2. 1 1
      app/comment.py
  3. 17 0
      templates/archive/create.html
  4. 1 0
      templates/auth/yours.html

+ 33 - 3
app/archive.py

@@ -1,11 +1,29 @@
-from flask import Blueprint, render_template, request
-
-from .db import Archive
+from flask import Blueprint, render_template, request, url_for, redirect
+from flask_wtf import FlaskForm
+from wtforms import StringField, SubmitField
+from wtforms.validators import DataRequired, Length, ValidationError
 
+from .db import db, Archive
 
 archive = Blueprint("archive", __name__)
 
 
+class CreateArchiveForm(FlaskForm):
+    name = StringField("名字", description="归档名字",
+                       validators=[
+                           DataRequired(f"必须填写归档名字"),
+                           Length(1, 32, message=f"归档名字长度为0-32位")])
+    describe = StringField("描述", description="归档描述",
+                           validators=[
+                               Length(0, 100, message=f"归档描述长度为0-100位")])
+    submit = SubmitField("提交")
+
+    def validate_name(self, field):
+        """ 检验email是否合法 """
+        if Archive.query.filter_by(name=field.data).first():
+            raise ValidationError("归档已存在")
+
+
 @archive.route("/all")
 def list_all_page():
     page = request.args.get("page", 1, type=int)
@@ -16,3 +34,15 @@ def list_all_page():
                            page=page,
                            items=pagination.items,
                            pagination=pagination)
+
+
+
+@archive.route("/create", methods=["GET", "POST"])
+def create_page():
+    form = CreateArchiveForm()
+    if form.validate_on_submit():
+        ac = Archive(name=form.name.data, describe=form.describe.data)
+        db.session.add(ac)
+        db.session.commit()
+        return redirect(url_for("comment.list_all_page", archive=ac.id, page=1))
+    return render_template("archive/create.html", form=form)

+ 1 - 1
app/comment.py

@@ -52,7 +52,7 @@ def list_all_page():
     page = request.args.get("page", 1, type=int)
     archive_id = request.args.get("archive", None, type=int)
 
-    if not archive_id == -1:
+    if not archive_id:
         pagination = (Comment.query
                       .filter(Comment.title != None).filter(Comment.father_id == None)
                       .order_by(Comment.create_time.desc(), Comment.title.desc())

+ 17 - 0
templates/archive/create.html

@@ -0,0 +1,17 @@
+{% extends "base.html" %}
+
+{% block title %} 创建 {% endblock %}
+
+{% block content %}
+    <div class="container">
+        <form method="post" action="{{ url_for("archive.create_page") }}" class="was-validated">
+            {{ form.hidden_tag() }}
+            {{ render_field(form.name) }}
+            {{ render_field(form.describe) }}
+
+            <div class="text-end">
+                {{ form.submit(class='btn btn-success me-2') }}
+            </div>
+        </form>
+    </div>
+{% endblock %}

+ 1 - 0
templates/auth/yours.html

@@ -18,6 +18,7 @@
 
         <div class="text-end">
             <div class="btn-group">
+                <a class="btn btn-outline-danger" href="{{ url_for("archive.create_page") }}"> 创建新归档 </a>
                 <a class="btn btn-outline-danger" href="{{ url_for("comment.create_page") }}"> 创建新讨论 </a>
                 <a class="btn btn-outline-danger" href="{{ url_for("auth.change_passwd_page") }}"> 修改密码 </a>
                 <a class="btn btn-outline-danger" href="{{ url_for("auth.logout_page") }}"> 退出登录 </a>