comment.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. from flask import Blueprint, render_template, request, abort, flash, redirect, url_for
  2. from flask_wtf import FlaskForm
  3. from wtforms import TextAreaField, StringField, SelectMultipleField, SubmitField, ValidationError
  4. from wtforms.validators import DataRequired, Length
  5. from flask_login import current_user
  6. from .db import db, Comment, Archive, User
  7. comment = Blueprint("comment", __name__)
  8. class WriteCommentForm(FlaskForm):
  9. title = StringField("标题", description="讨论标题", validators=[Length(0, 20, message="标题长度1-20个字符")])
  10. content = TextAreaField("内容", validators=[DataRequired(message="必须输入内容")])
  11. archive = SelectMultipleField("归档", coerce=int)
  12. submit = SubmitField("提交")
  13. def __init__(self, **kwargs):
  14. super().__init__(**kwargs)
  15. archive = Archive.query.all()
  16. self.archive_res = []
  17. self.archive_choices = []
  18. for i in archive:
  19. self.archive_res.append(i.id)
  20. self.archive_choices.append((i.id, f"{i.name} ({i.comment_count})"))
  21. self.archive.choices = self.archive_choices
  22. def validate_archive(self, field):
  23. for i in field.data:
  24. if i not in self.archive_res:
  25. raise ValidationError("错误的归档被指定")
  26. @comment.route("/")
  27. def comment_page():
  28. comment_id = request.args.get("comment", None, type=int)
  29. if not comment_id:
  30. return abort(404)
  31. cm: Comment = Comment.query.filter_by(id=comment_id).first()
  32. if cm:
  33. return render_template("comment/comment.html",
  34. comment=cm,
  35. comment_son=cm.son)
  36. return abort(404)
  37. @comment.route("/all")
  38. def list_all_page():
  39. page = request.args.get("page", 1, type=int)
  40. archive_id = request.args.get("archive", None, type=int)
  41. if not archive_id == -1:
  42. pagination = (Comment.query
  43. .filter(Comment.title != None).filter(Comment.father_id == None)
  44. .order_by(Comment.create_time.desc(), Comment.title.desc())
  45. .paginate(page=page, per_page=8, error_out=False))
  46. return render_template("comment/list.html",
  47. page=page,
  48. archive=archive_id,
  49. items=pagination.items,
  50. pagination=pagination,
  51. archive_name="全部讨论",
  52. archive_describe="罗列了本站所有的讨论",
  53. title="主页")
  54. else:
  55. archive = Archive.query.filter_by(id=archive_id).first()
  56. if not archive:
  57. return abort(404)
  58. pagination = (archive.comment
  59. .filter(Comment.title != None).filter(Comment.father_id == None)
  60. .order_by(Comment.create_time.desc(), Comment.title.asc())
  61. .paginate(page=page, per_page=8, error_out=False))
  62. return render_template("comment/list.html",
  63. page=page,
  64. archive=archive_id,
  65. items=pagination.items,
  66. pagination=pagination,
  67. archive_name=archive.name,
  68. archive_describe=archive.describe,
  69. title=archive.name)
  70. @comment.route("/user")
  71. def user_page():
  72. page = request.args.get("page", 1, type=int)
  73. user_id = request.args.get("user", None, type=int)
  74. if not user_id:
  75. return abort(404)
  76. user: User = User.query.filter_by(id=user_id).first()
  77. if not user:
  78. return abort(404)
  79. pagination = (user.comment
  80. .order_by(Comment.create_time.desc(), Comment.title.asc())
  81. .paginate(page=page, per_page=8, error_out=False))
  82. return render_template("comment/user.html",
  83. page=page,
  84. user=user,
  85. items=pagination.items,
  86. pagination=pagination,
  87. title=user.email)
  88. @comment.route("/create", methods=["GET", "POST"])
  89. def create_page():
  90. father_id = request.args.get("father", None, type=int)
  91. if father_id:
  92. father = Comment.query.filter_by(id=father_id).first()
  93. if not father:
  94. return abort(404)
  95. else:
  96. father = None
  97. form = WriteCommentForm()
  98. if form.validate_on_submit():
  99. title = form.title.data if len(form.title.data) > 0 else None
  100. archive_list = []
  101. for i in form.archive.data:
  102. archive = Archive.query.filter_by(id=i).first()
  103. if not archive:
  104. return abort(404)
  105. archive_list.append(archive)
  106. cm = Comment(title=title, content=form.content.data, father=father, archive=archive_list, auth=current_user)
  107. db.session.add(cm)
  108. db.session.commit()
  109. flash("讨论发表成功")
  110. return redirect(url_for("comment.comment_page", comment=cm.id))
  111. return render_template("comment/create.html", form=form, father=father)