comment.py 5.3 KB

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