docx.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. from flask import Blueprint, render_template, abort, redirect, url_for, flash, make_response
  2. from flask_wtf import FlaskForm
  3. from flask_login import login_required, current_user
  4. from wtforms import TextAreaField, StringField, SubmitField
  5. from wtforms.validators import DataRequired
  6. import app
  7. from sql.base import DBBit
  8. from object.blog import BlogArticle, load_blog_by_id
  9. from object.comment import Comment
  10. from object.archive import load_archive_by_name
  11. docx = Blueprint("docx", __name__)
  12. class WriteBlogForm(FlaskForm):
  13. title = StringField("标题", validators=[DataRequired()])
  14. subtitle = StringField("副标题", validators=[DataRequired()])
  15. archive = StringField("归档")
  16. context = TextAreaField("博客内容", validators=[DataRequired()])
  17. submit = SubmitField("提交博客")
  18. def __init__(self, **kwargs):
  19. super().__init__(**kwargs)
  20. self.context.data = "# Blog Title\n## Blog subtitle\nHello, World"
  21. class WriteCommentForm(FlaskForm):
  22. context = TextAreaField(validators=[DataRequired()])
  23. submit = SubmitField("评论")
  24. @docx.route('/<int:page>')
  25. def docx_page(page: int = 1):
  26. if page < 1:
  27. app.HBlogFlask.print_user_opt_fail_log(f"Load docx list with error page({page})")
  28. abort(404)
  29. return
  30. blog_list = BlogArticle.get_blog_list(limit=20, offset=(page - 1) * 20)
  31. max_page = app.HBlogFlask.get_max_page(BlogArticle.get_blog_count(), 20)
  32. page_list = app.HBlogFlask.get_page("docx.docx_page", page, max_page)
  33. app.HBlogFlask.print_load_page_log(f"docx list (page: {page})")
  34. return render_template("docx/docx.html",
  35. blog_list=blog_list,
  36. is_top=DBBit.BIT_1,
  37. page_list=page_list,
  38. form=WriteBlogForm(),
  39. show_delete=current_user.check_role("DeleteBlog"))
  40. @docx.route('/<int:archive>/<int:page>')
  41. def archive_page(archive: int, page: int = 1):
  42. if page < 1:
  43. app.HBlogFlask.print_user_opt_fail_log(f"Load archive-docx list with error page({page}) archive: {archive}")
  44. abort(404)
  45. return
  46. blog_list = BlogArticle.get_blog_list(archive_id=archive, limit=20, offset=(page - 1) * 20)
  47. max_page = app.HBlogFlask.get_max_page(BlogArticle.get_blog_count(archive_id=archive), 20)
  48. page_list = app.HBlogFlask.get_page("docx.archive_page", page, max_page)
  49. app.HBlogFlask.print_load_page_log(f"archive-docx list (archive-id: {archive} page: {page})")
  50. return render_template("docx/docx.html",
  51. blog_list=blog_list,
  52. is_top=DBBit.BIT_1,
  53. page_list=page_list,
  54. form=None)
  55. @docx.route('/article/<int:blog_id>')
  56. def article_page(blog_id: int):
  57. article = load_blog_by_id(blog_id)
  58. if article is None:
  59. app.HBlogFlask.print_user_opt_fail_log(f"Load article with error id({blog_id})")
  60. abort(404)
  61. return
  62. app.HBlogFlask.print_load_page_log(f"article (id: {blog_id})")
  63. return render_template("docx/article.html",
  64. article=article,
  65. archive_list=article.archive,
  66. form=WriteCommentForm(),
  67. show_delete=current_user.check_role("DeleteComment"),
  68. show_email=current_user.check_role("ReadUserInfo"))
  69. @docx.route('/down/<int:blog_id>')
  70. def article_down_page(blog_id: int):
  71. article = load_blog_by_id(blog_id)
  72. if article is None:
  73. app.HBlogFlask.print_user_opt_fail_log(f"Download article with error id({blog_id})")
  74. abort(404)
  75. return
  76. response = make_response(article.context)
  77. response.headers["Content-Disposition"] = f"attachment;filename={article.title.encode().decode('latin-1')}.md"
  78. app.HBlogFlask.print_load_page_log(f"download article (id: {blog_id})")
  79. return response
  80. @docx.route('/comment/<int:blog>', methods=["POST"])
  81. @login_required
  82. @app.form_required(WriteCommentForm, "write comment")
  83. @app.role_required("WriteComment", "write comment")
  84. def comment_page(blog: int, form: WriteCommentForm):
  85. context = form.context.data
  86. if Comment(None, blog, current_user, context).create():
  87. app.HBlogFlask.print_user_opt_success_log("comment")
  88. flash("评论成功")
  89. else:
  90. app.HBlogFlask.print_user_opt_error_log("comment")
  91. flash("评论失败")
  92. return redirect(url_for("docx.article_page", blog_id=blog))
  93. @docx.route('/create-docx', methods=["POST"])
  94. @login_required
  95. @app.form_required(WriteBlogForm, "write blog")
  96. @app.role_required("WriteBlog", "write blog")
  97. def create_docx_page(form: WriteBlogForm):
  98. title = form.title.data
  99. if len(title) > 10:
  100. flash("标题太长了")
  101. abort(400)
  102. subtitle = form.subtitle.data
  103. if len(subtitle) > 10:
  104. flash("副标题太长了")
  105. abort(400)
  106. archive = set(str(form.archive.data).replace(" ", "").split(";"))
  107. archive_list = []
  108. for f in archive:
  109. f_ = load_archive_by_name(f)
  110. if f_ is not None:
  111. archive_list.append(f_)
  112. if BlogArticle(None, current_user, title, subtitle, form.context.data, archive=archive_list).create():
  113. app.HBlogFlask.print_sys_opt_success_log("write blog")
  114. flash(f"博客 {title} 发表成功")
  115. else:
  116. app.HBlogFlask.print_sys_opt_fail_log("write blog")
  117. flash(f"博客 {title} 发表失败")
  118. return redirect(url_for("docx.docx_page", page=1))
  119. @docx.route("delete/<int:blog_id>")
  120. @login_required
  121. @app.role_required("DeleteBlog", "delete blog")
  122. def delete_blog_page(blog_id: int):
  123. if BlogArticle(blog_id, None, None, None, None).delete():
  124. app.HBlogFlask.print_sys_opt_success_log("delete blog")
  125. flash("博文删除成功")
  126. else:
  127. app.HBlogFlask.print_sys_opt_fail_log("delete blog")
  128. flash("博文删除失败")
  129. return redirect(url_for("docx.docx_page", page=1))
  130. @docx.route("delete_comment/<int:comment_id>")
  131. @login_required
  132. @app.role_required("DeleteComment", "delete comment")
  133. def delete_comment_page(comment_id: int):
  134. if Comment(comment_id, None, None, None).delete():
  135. app.HBlogFlask.print_sys_opt_success_log("delete comment")
  136. flash("博文评论成功")
  137. else:
  138. app.HBlogFlask.print_sys_opt_fail_log("delete comment")
  139. flash("博文评论失败")
  140. return redirect(url_for("docx.docx_page", page=1))
  141. @docx.context_processor
  142. def inject_base():
  143. return {"top_nav": ["", "", "active", "", "", ""]}