diff --git a/app/admin_service.py b/app/admin_service.py new file mode 100644 index 0000000..87d2610 --- /dev/null +++ b/app/admin_service.py @@ -0,0 +1,118 @@ +from flask import * +from model import * +from pony.orm import * +from Yaml import yml +from Login import md5 +from datetime import datetime + +# ? from difficulty import text_difficulty_level + +ADMIN_NAME = "114514" # unique admin name +_cur_page = 1 # current article page +_page_size = 5 # article sizes per page +adminService = Blueprint("admin_service", __name__) + + +@adminService.route("/admin", methods=["GET", "POST"]) +def admin(): + global _cur_page, _page_size + # 未登录,跳转到未登录界面 + if not session.get("logged_in"): + return render_template("not_login.html") + + # 获取session里的用户名 + username = session.get("username") + if username != ADMIN_NAME: + return "You are not admin!" + + article_len = get_articles_len() + try: + _page_size = min(int(request.args.get("size", 5)), article_len) + if _page_size <= 0: + raise ZeroDivisionError + _cur_page = min(int(request.args.get("page", 1)), article_len // _page_size) + except ValueError: + return "page parmas must be int!" + except ZeroDivisionError: + return "page size must bigger than zero" + + context = { + "text_len": article_len, + "page_size": _page_size, + "cur_page": _cur_page, + "text_list": get_page_articles(_cur_page, _page_size), + "user_list": get_users(), + "username": username, + "yml": yml, + } + + def _update_context(): + article_len = get_articles_len() + context["text_len"] = article_len + context["text_list"] = get_page_articles(_cur_page, _page_size) + + if request.method == "GET": + if delete_id := int(request.args.get("delete_id", 0)): # delete article + delete_article(delete_id) + _update_context() + else: + data = request.form + content = data.get("content", "") + source = data.get("source", "") + question = data.get("question", "") + username = data.get("username", "") + if content: + add_article(content, source, question) + _update_context() + if username: + update_user_password(username) + + return render_template("admin_index.html", **context) + + +def add_article(content, source="manual_input", question="No question"): + with db_session: + # add one atricle to sqlite + Article( + text=content, + source=source, + date=datetime.now().strftime("%-d %b %Y"), # format style of `5 Oct 2022` + level="1", + question=question, + ) + # ? There is a question that: + # ? How can i get one article level? + # ? I try to use the function `text_difficulty_level(content,{"test":1})` + # ? However, i lose one dict parma from pickle + # ? So I temporarily fixed the level to 1 + + +def delete_article(article_id): + article_id &= 0xFFFFFFFF # max 32 bits + with db_session: + if article := Article.select(article_id=article_id): + article.first().delete() + + +def get_articles_len(): + with db_session: + return len(Article.select()[:]) + + +def get_page_articles(num, size): + with db_session: + return [ + x + for x in Article.select().order_by(desc(Article.article_id)).page(num, size) + ] + + +def get_users(): + with db_session: + return User.select()[:] + + +def update_user_password(username, password="123456"): + with db_session: + if user := User.select(name=username).first(): + user.password = md5(username + password) diff --git a/app/main.py b/app/main.py index bcbd3f8..2af57e3 100644 --- a/app/main.py +++ b/app/main.py @@ -5,24 +5,24 @@ # Copyright 2019 (C) Hui Lan # Written permission must be obtained from the author for commercial uses. ########################################################################### -from datetime import datetime from flask import escape from Login import * from Article import * import Yaml from user_service import userService from account_service import accountService +from admin_service import adminService app = Flask(__name__) app.secret_key = 'lunch.time!' # 将蓝图注册到Lab app app.register_blueprint(userService) app.register_blueprint(accountService) +app.register_blueprint(adminService) path_prefix = '/var/www/wordfreq/wordfreq/' path_prefix = './' # comment this line in deployment - def get_random_image(path): ''' 返回随机图 @@ -102,79 +102,6 @@ def mainpage(): d_len=d_len, lst=lst, yml=Yaml.yml) -def insert_article(content, source='manual_input', level=5, question=''): - sql = f"INSERT into article (text,source, date, level, question) VALUES " \ - f"('{content}','{source}','{datetime.now().strftime('%Y-%m-%d')}', '{level}', '{question}');" - print(sql) - rq = RecordQuery('./static/wordfreqapp.db') - rq.instructions(sql) - rq.do() - - -def get_articles(): - sql = f"SELECT * from article order by -article_id;" - rq = RecordQuery('./static/wordfreqapp.db') - rq.instructions(sql) - rq.do() - result = rq.get_results() - return result - - -def get_users(): - sql = f"SELECT * from user;" - rq = RecordQuery('./static/wordfreqapp.db') - rq.instructions(sql) - rq.do() - result = rq.get_results() - return result - - -def update_user_password(username, password='123456'): - password = md5(username + password) - sql = f"UPDATE user SET password='{password}' where name='{username}';" - rq = RecordQuery('./static/wordfreqapp.db') - rq.instructions(sql) - rq.do() - - -@app.route("/admin", methods=['GET', 'POST']) -def admin(): - ''' - ''' - # 未登录,跳转到未登录界面 - if not session.get('logged_in'): - return render_template('not_login.html') - - # 获取session里的用户名 - username = session.get('username') - - context = { - # 'user': request.user, - 'text_list': get_articles(), - 'user_list': get_users(), - 'username': username - } - if request.method == 'GET': - return render_template('admin_index.html', **context) - else: - data = request.form - content = data.get('content') - source = data.get('source', '') - question = data.get('question', '') - username = data.get('username') - if content: - insert_article( - content=content, - source=source, - question=question - ) - context['text_list'] = get_articles() - if username: - update_user_password(username) - return render_template('admin_index.html', **context) - - - if __name__ == '__main__': ''' 运行程序 diff --git a/app/templates/admin_index.html b/app/templates/admin_index.html index be88556..03d6ee4 100644 --- a/app/templates/admin_index.html +++ b/app/templates/admin_index.html @@ -1,112 +1,120 @@ - + + - + + + + {{ yml['header'] | safe }} + {% if yml['css']['item'] %} + {% for css in yml['css']['item'] %} + + {% endfor %} + {% endif %} + {% if yml['js']['head'] %} + {% for js in yml['js']['head'] %} + + {% endfor %} + {% endif %} - + +
+
重置选中用户密码
+
+
+ + +
+ +
- -
-
重置选中用户密码
-
-
- - -
- - -
-
- -
- {% if tips %} +
+ {% if tips %} - {% endif %} + {% endif %} +
+
录入文章
+
+
+ + + -
-
录入文章
- -
- - - + + - - - - -
- - - + +
+ + +
-
-
-
文章列表
-
- {% for text in text_list %} +
+
文章列表
+
+ {% for text in text_list %}
-
{{ text['source'] }}
- Date:{{ text['date'] }} Level:{{ text['level'] }} +
{{ text.source }}
+ Date:{{ text.date }} Level:{{ text.level }}
-

{{ text['text'] }}

+ +

{{ text.text }}

- {% endfor %} - + {% endfor %} +
+
+
+
    +
  • Previous +
  • + {% for i in range(1, text_len//page_size+1) %} + {% if cur_page == i %} +
  • {{ i }} +
  • + {% else %} +
  • {{ i }}
  • + {% endif %} + {% endfor %} +
  • Next +
  • +
-
- - - - - - + \ No newline at end of file