From 87fd5946364a43dac7c752b9aa1005fb7f51b407 Mon Sep 17 00:00:00 2001 From: Lan Hui <1348141770@qq.com> Date: Fri, 14 Jul 2023 08:26:37 +0800 Subject: [PATCH] admin_service.py: refactor view function article(). --- app/admin_service.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/app/admin_service.py b/app/admin_service.py index b0c18ff..f8f80f4 100644 --- a/app/admin_service.py +++ b/app/admin_service.py @@ -37,6 +37,22 @@ def admin(): @adminService.route("/admin/article", methods=["GET", "POST"]) def article(): + + def _make_title_and_content(article_lst): + for article in article_lst: + text = escape(article.text) # Fix XSS vulnerability, contributed by Xu Xuan + article.title = text.split("\n")[0] + article.content = '
'.join(text.split("\n")[1:]) + + + def _update_context(): + article_len = get_number_of_articles() + context["article_number"] = article_len + context["text_list"] = get_page_articles(_cur_page, _page_size) + _articles = get_page_articles(_cur_page, _page_size) + _make_title_and_content(_articles) + context["text_list"] = _articles + global _cur_page, _page_size is_admin = check_is_admin() @@ -55,10 +71,7 @@ def article(): return "page parmas must be int!" _articles = get_page_articles(_cur_page, _page_size) - for article in _articles: # 获取每篇文章的title - text = escape(article.text) # Fix XSS vulnerability, contributed by Xu Xuan - article.title = text.split("\n")[0] - article.content = '
'.join(text.split("\n")[1:]) + _make_title_and_content(_articles) context = { "article_number": _article_number, @@ -68,17 +81,6 @@ def article(): "username": session.get("username"), } - def _update_context(): - article_len = get_number_of_articles() - context["article_number"] = article_len - context["text_list"] = get_page_articles(_cur_page, _page_size) - _articles = get_page_articles(_cur_page, _page_size) - for article in _articles: # 获取每篇文章的title - text = escape(article.text) # Fix XSS vulnerability, contributed by Xu Xuan - article.title = text.split("\n")[0] - article.content = '
'.join(text.split("\n")[1:]) - - context["text_list"] = _articles if request.method == "GET": try: @@ -102,6 +104,7 @@ def article(): _update_context() title = content.split('\n')[0] flash(f'Article added. Title: {title}') + return render_template("admin_manage_article.html", **context)