fix: add one way to set article level & rename some functions and vars

Alpha
吴宇涵 2023-03-20 23:37:27 +08:00
parent b34f260d98
commit cabf6702a7
2 changed files with 20 additions and 21 deletions

View File

@ -5,8 +5,6 @@ from Yaml import yml
from Login import md5 from Login import md5
from datetime import datetime from datetime import datetime
# ? from difficulty import text_difficulty_level
ADMIN_NAME = "lanhui" # unique admin name ADMIN_NAME = "lanhui" # unique admin name
_cur_page = 1 # current article page _cur_page = 1 # current article page
_page_size = 5 # article sizes per page _page_size = 5 # article sizes per page
@ -25,19 +23,19 @@ def admin():
if username != ADMIN_NAME: if username != ADMIN_NAME:
return "You are not admin!" return "You are not admin!"
article_len = get_articles_len() article_number = get_number_of_articles()
try: try:
_page_size = min(int(request.args.get("size", 5)), article_len) _page_size = min(int(request.args.get("size", 5)), article_number)
if _page_size <= 0: if _page_size <= 0:
raise ZeroDivisionError raise ZeroDivisionError
_cur_page = min(int(request.args.get("page", 1)), article_len // _page_size) _cur_page = min(int(request.args.get("page", 1)), article_number // _page_size)
except ValueError: except ValueError:
return "page parmas must be int!" return "page parmas must be int!"
except ZeroDivisionError: except ZeroDivisionError:
return "page size must bigger than zero" return "page size must bigger than zero"
context = { context = {
"text_len": article_len, "article_number": article_number,
"page_size": _page_size, "page_size": _page_size,
"cur_page": _cur_page, "cur_page": _cur_page,
"text_list": get_page_articles(_cur_page, _page_size), "text_list": get_page_articles(_cur_page, _page_size),
@ -47,8 +45,8 @@ def admin():
} }
def _update_context(): def _update_context():
article_len = get_articles_len() article_len = get_number_of_articles()
context["text_len"] = article_len context["article_number"] = article_len
context["text_list"] = get_page_articles(_cur_page, _page_size) context["text_list"] = get_page_articles(_cur_page, _page_size)
if request.method == "GET": if request.method == "GET":
@ -61,8 +59,14 @@ def admin():
source = data.get("source", "") source = data.get("source", "")
question = data.get("question", "") question = data.get("question", "")
username = data.get("username", "") username = data.get("username", "")
level = data.get("level", "5")
if content: if content:
add_article(content, source, question) try: # check level
if not (0 < int(level) <= 5):
raise ValueError
except ValueError:
return "level must be between 1 and 5"
add_article(content, source, level, question)
_update_context() _update_context()
if username: if username:
update_user_password(username) update_user_password(username)
@ -70,21 +74,16 @@ def admin():
return render_template("admin_index.html", **context) return render_template("admin_index.html", **context)
def add_article(content, source="manual_input", question="No question"): def add_article(content, source="manual_input", level="5", question="No question"):
with db_session: with db_session:
# add one atricle to sqlite # add one article to sqlite
Article( Article(
text=content, text=content,
source=source, source=source,
date=datetime.now().strftime("%-d %b %Y"), # format style of `5 Oct 2022` date=datetime.now().strftime("%-d %b %Y"), # format style of `5 Oct 2022`
level="1", level=level,
question=question, 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): def delete_article(article_id):
@ -94,7 +93,7 @@ def delete_article(article_id):
article.first().delete() article.first().delete()
def get_articles_len(): def get_number_of_articles():
with db_session: with db_session:
return len(Article.select()[:]) return len(Article.select()[:])

View File

@ -70,10 +70,10 @@
<label for="exampleInputEmail1" class="form-label">文章内容</label> <label for="exampleInputEmail1" class="form-label">文章内容</label>
<textarea id="content" name="content" class="form-control" placeholder="请输入文章内容"></textarea> <textarea id="content" name="content" class="form-control" placeholder="请输入文章内容"></textarea>
<label for="exampleInputEmail1" class="form-label">文章来源</label> <label for="exampleInputEmail1" class="form-label">文章来源</label>
<textarea id="source" name="source" class="form-control" placeholder="请输入来源"></textarea> <textarea id="source" name="source" class="form-control" placeholder="请输入来源"></textarea>
<label for="exampleInputEmail1" class="form-label">文章等级</label>
<textarea id="level" name="level" class="form-control" placeholder="请输入等级"></textarea>
<label for="exampleInputEmail1" class="form-label">文章问题</label> <label for="exampleInputEmail1" class="form-label">文章问题</label>
<textarea id="question" name="question" class="form-control" placeholder="请输入问题"></textarea> <textarea id="question" name="question" class="form-control" placeholder="请输入问题"></textarea>
</div> </div>
<input type="submit" value="保存" class="btn btn-outline-primary"> <input type="submit" value="保存" class="btn btn-outline-primary">
@ -103,7 +103,7 @@
<ul class="pagination pagination-sm justify-content-center"> <ul class="pagination pagination-sm justify-content-center">
<li class="page-item"><a class="page-link" href="/admin?page={{cur_page-1}}&size={{page_size}}">Previous</a> <li class="page-item"><a class="page-link" href="/admin?page={{cur_page-1}}&size={{page_size}}">Previous</a>
</li> </li>
{% for i in range(1, text_len//page_size+1) %} {% for i in range(1, article_number//page_size+1) %}
{% if cur_page == i %} {% if cur_page == i %}
<li class="page-item active"><a class="page-link" href="/admin?page={{i}}&size={{page_size}}">{{ i }}</a> <li class="page-item active"><a class="page-link" href="/admin?page={{i}}&size={{page_size}}">{{ i }}</a>
</li> </li>