1
0
Fork 0
EnglishPal/app/admin_service.py

117 lines
3.5 KiB
Python
Raw Normal View History

# System Library
2023-03-20 20:16:48 +08:00
from flask import *
# Personal library
2023-03-20 20:16:48 +08:00
from Yaml import yml
from model.user import *
from model.article import *
2023-03-20 20:16:48 +08:00
2023-03-20 20:19:56 +08:00
ADMIN_NAME = "lanhui" # unique admin name
2023-03-20 20:16:48 +08:00
_cur_page = 1 # current article page
_page_size = 5 # article sizes per page
adminService = Blueprint("admin_service", __name__)
def check_is_admin():
2023-03-20 20:16:48 +08:00
# 未登录,跳转到未登录界面
if not session.get("logged_in"):
return render_template("not_login.html")
# 用户名不是admin_name
if session.get("username") != ADMIN_NAME:
2023-03-20 20:16:48 +08:00
return "You are not admin!"
return "pass"
@adminService.route("/admin", methods=["GET"])
def admin():
is_admin = check_is_admin()
if is_admin != "pass":
return is_admin
return render_template(
"admin_index.html", yml=yml, username=session.get("username")
)
@adminService.route("/admin/article", methods=["GET", "POST"])
def article():
global _cur_page, _page_size
is_admin = check_is_admin()
if is_admin != "pass":
return is_admin
article_number = get_number_of_articles()
2023-03-20 20:16:48 +08:00
try:
_page_size = min(
max(1, int(request.args.get("size", 5))), article_number
) # 最小的size是1
_cur_page = min(
max(1, int(request.args.get("page", 1))), article_number // _page_size + 1
) # 最小的page是1
2023-03-20 20:16:48 +08:00
except ValueError:
return "page parmas must be int!"
context = {
"article_number": article_number,
"text_list": get_page_articles(_cur_page, _page_size),
2023-03-20 20:16:48 +08:00
"page_size": _page_size,
"cur_page": _cur_page,
"username": session.get("username"),
2023-03-20 20:16:48 +08:00
}
def _update_context():
article_len = get_number_of_articles()
context["article_number"] = article_len
2023-03-20 20:16:48 +08:00
context["text_list"] = get_page_articles(_cur_page, _page_size)
if request.method == "GET":
try:
delete_id = int(request.args.get("delete_id", 0))
except:
return "Delete article ID must be int!"
if delete_id: # delete article
delete_article_by_id(delete_id)
2023-03-20 20:16:48 +08:00
_update_context()
elif request.method == "POST":
2023-03-20 20:16:48 +08:00
data = request.form
content = data.get("content", "")
source = data.get("source", "")
question = data.get("question", "")
level = data.get("level", "5")
2023-03-20 20:16:48 +08:00
if content:
try: # check level
if level not in [str(x + 1) for x in range(5)]:
raise ValueError
except ValueError:
return "Level must be between 1 and 5"
add_article(content, source, level, question)
2023-03-20 20:16:48 +08:00
_update_context()
return render_template("admin_manage_article.html", **context)
2023-03-20 20:16:48 +08:00
@adminService.route("/admin/user", methods=["GET", "POST"])
def user():
is_admin = check_is_admin()
if is_admin != "pass":
return is_admin
context = {
"user_list": get_users(),
"username": session.get("username"),
}
if request.method == "POST":
data = request.form
username = data.get("username","")
new_password = data.get("new_password", "")
expiry_time = data.get("expiry_time", "")
if username:
if new_password:
update_password_by_username(username, new_password)
if expiry_time:
update_expiry_time_by_username(username, "".join(expiry_time.split("-")))
return render_template("admin_manage_user.html", **context)