1
0
SongZihuan 2 жил өмнө
parent
commit
b2be87db37

+ 8 - 8
app/auth.py

@@ -259,8 +259,8 @@ def logout_page():
 
 @auth.route("/user")
 def user_page():
-    user_id = request.args.get("user", -1, type=int)
-    if user_id == -1:
+    user_id = request.args.get("user", None, type=int)
+    if not user_id:
         return abort(404)
     elif current_user.is_authenticated and current_user.id == user_id:
         return redirect(url_for("auth.auth_page"))
@@ -298,8 +298,8 @@ def followed_page():
 
 @auth.route("/followed/follow")
 def set_follow_page():
-    user_id = request.args.get("user", 1, type=int)
-    if user_id == current_user.id:
+    user_id = request.args.get("user", None, type=int)
+    if not user_id or user_id == current_user.id:
         return abort(404)
 
     user = User.query.filter_by(id=user_id).first()
@@ -319,8 +319,8 @@ def set_follow_page():
 
 @auth.route("/followed/unfollow")
 def set_unfollow_page():
-    user_id = request.args.get("user", 1, type=int)
-    if user_id == current_user.id:
+    user_id = request.args.get("user", None, type=int)
+    if not user_id or user_id == current_user.id:
         return abort(404)
 
     user = User.query.filter_by(id=user_id).first()
@@ -335,8 +335,8 @@ def set_unfollow_page():
 
 @auth.route("/block")
 def set_block_page():
-    user_id = request.args.get("user", 1, type=int)
-    if user_id == current_user.id:
+    user_id = request.args.get("user", None, type=int)
+    if not user_id or user_id == current_user.id:
         return abort(404)
 
     user = User.query.filter_by(id=user_id).first()

+ 67 - 8
app/comment.py

@@ -1,16 +1,44 @@
-from flask import Blueprint, render_template, request, abort
+from flask import Blueprint, render_template, request, abort, flash, redirect, url_for
+from flask_wtf import FlaskForm
+from wtforms import TextAreaField, StringField, SelectMultipleField, SubmitField, ValidationError
+from wtforms.validators import DataRequired, Length
+from flask_login import current_user
 
-from .db import Comment, Archive, User
+
+from .db import db, Comment, Archive, User
 
 
 comment = Blueprint("comment", __name__)
 
 
+class WriteCommentForm(FlaskForm):
+    title = StringField("标题", description="讨论标题", validators=[Length(0, 20, message="标题长度1-20个字符")])
+    content = TextAreaField("内容", validators=[DataRequired(message="必须输入内容")])
+    archive = SelectMultipleField("归档", coerce=int)
+    submit = SubmitField("提交")
+
+    def __init__(self, **kwargs):
+        super().__init__(**kwargs)
+        archive = Archive.query.all()
+        self.archive_res = []
+        self.archive_choices = []
+        for i in archive:
+            self.archive_res.append(i.id)
+            self.archive_choices.append((i.id, f"{i.name} ({i.comment_count})"))
+        self.archive.choices = self.archive_choices
+
+    def validate_archive(self, field):
+        for i in field.data:
+            if i not in self.archive_res:
+                raise ValidationError("错误的归档被指定")
+
+
 @comment.route("/")
 def comment_page():
-    comment_id = request.args.get("comment_id", -1, type=int)
-    if comment_id == -1:
+    comment_id = request.args.get("comment", None, type=int)
+    if not comment_id:
         return abort(404)
+
     cm: Comment = Comment.query.filter_by(id=comment_id).first()
     if cm:
         return render_template("comment/comment.html",
@@ -22,9 +50,9 @@ def comment_page():
 @comment.route("/all")
 def list_all_page():
     page = request.args.get("page", 1, type=int)
-    archive_id = request.args.get("archive", -1, type=int)
+    archive_id = request.args.get("archive", None, type=int)
 
-    if archive_id == -1:
+    if not archive_id == -1:
         pagination = (Comment.query
                       .filter(Comment.title != None).filter(Comment.father_id == None)
                       .order_by(Comment.create_time.desc(), Comment.title.desc())
@@ -58,8 +86,8 @@ def list_all_page():
 @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:
+    user_id = request.args.get("user", None, type=int)
+    if not user_id:
         return abort(404)
 
     user: User = User.query.filter_by(id=user_id).first()
@@ -75,3 +103,34 @@ def user_page():
                            items=pagination.items,
                            pagination=pagination,
                            title=user.email)
+
+
+@comment.route("/create", methods=["GET", "POST"])
+def create_page():
+    father_id = request.args.get("father", None, type=int)
+
+    if father_id:
+        father = Comment.query.filter_by(id=father_id).first()
+        if not father:
+            return abort(404)
+    else:
+        father = None
+
+    form = WriteCommentForm()
+    if form.validate_on_submit():
+        title = form.title.data if len(form.title.data) > 0 else None
+        archive_list = []
+        for i in form.archive.data:
+            archive = Archive.query.filter_by(id=i).first()
+            if not archive:
+                return abort(404)
+            archive_list.append(archive)
+        cm = Comment(title=title, content=form.content.data, father=father, archive=archive_list, auth=current_user)
+        db.session.add(cm)
+        db.session.commit()
+        flash("讨论发表成功")
+        return redirect(url_for("comment.comment_page", comment=cm.id))
+    return render_template("comment/create.html", form=form, father=father)
+
+
+

+ 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("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>
             </div>

+ 21 - 1
templates/base.html

@@ -1,6 +1,6 @@
 {% macro render_field(field) %}
     <div class="form-floating my-3">
-        {{ field(class="form-control", placeholder=field.label.text, **kwargs) | safe }}
+        {{ field(class="form-control", placeholder=field.label.text) | safe }}
         {{ field.label }}
         {% for error in field.errors %}
             <div class="invalid-feedback"> {{ error }} </div>
@@ -8,6 +8,26 @@
     </div>
 {% endmacro %}
 
+{% macro render_text_field(field) %}
+    <div class="form-floating my-3">
+        {{ field(class="form-control", placeholder=field.label.text, style="height: 40vh") | safe }}
+        {{ field.label }}
+        {% for error in field.errors %}
+            <div class="invalid-feedback"> {{ error }} </div>
+        {% endfor %}
+    </div>
+{% endmacro %}
+
+{% macro render_select_field(field) %}
+    <div class="my-3">
+        {{ field(class="form-select") | safe }}
+        {% 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 %}

+ 3 - 3
templates/comment/comment.html

@@ -6,7 +6,7 @@
     <div class="container mt-3">
         <div>
             <span class="h5"> 讨论ID:{{ comment.id }} </span>
-            <a class="btn btn-warning float-end"> 添加子讨论 </a>
+            <a class="btn btn-warning float-end" href="{{ url_for("comment.create_page", father=comment.id) }}"> 添加子讨论 </a>
         </div>
 
         <div class="card mt-4">
@@ -18,7 +18,7 @@
                 <a class="badge bg-info text-white" href="{{ url_for("auth.user_page", user=comment.auth.id) }}"> {{ comment.auth.email }} </a>
                 <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>
+                        <a class="btn btn-link" href="{{ url_for("comment.comment_page", comment=comment.father_id) }}"> 查看父讨论 </a>
                         <br>
                     {% endif %}
                     子讨论个数:{{ comment.son_count }}
@@ -38,7 +38,7 @@
                     <a class="badge bg-info text-white" href="{{ url_for("auth.user_page", user=i.auth.id) }}"> {{ i.auth.email }} </a>
 
                     <p class="text-end">
-                        <a class="btn btn-link" href="{{ url_for("comment.comment_page", comment_id=i.id) }}"> 前往查看 </a>
+                        <a class="btn btn-link" href="{{ url_for("comment.comment_page", comment=i.id) }}"> 前往查看 </a>
                         <br>
                         子讨论个数:{{ i.son_count }}
                         <br>

+ 24 - 0
templates/comment/create.html

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

+ 1 - 1
templates/comment/list.html

@@ -25,7 +25,7 @@
                         <a class="badge bg-info text-white" href="{{ url_for("auth.user_page", user=i.auth.id) }}"> {{ i.auth.email }} </a>
 
                         <p class="text-end">
-                            <a class="btn btn-link" href="{{ url_for("comment.comment_page", comment_id=i.id) }}"> 前往查看 </a>
+                            <a class="btn btn-link" href="{{ url_for("comment.comment_page", comment=i.id) }}"> 前往查看 </a>
                             <br>
                             子讨论个数:{{ i.son_count }}
                             <br>

+ 1 - 1
templates/comment/user.html

@@ -30,7 +30,7 @@
                         <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>
+                            <a class="btn btn-link" href="{{ url_for("comment.comment_page", comment=i.id) }}"> 前往查看 </a>
                             <br>
                             子讨论个数:{{ i.son_count }}
                             <br>