1
0
Fork 0

Resolve merge conflicts.

Alpha
Hui Lan 2023-03-21 16:08:55 +08:00
commit 9075fe9eea
3 changed files with 28 additions and 26 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,12 +45,13 @@ 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":
if delete_id := int(request.args.get("delete_id", 0)): # delete article delete_id = int(request.args.get("delete_id", 0))
if delete_id: # delete article
delete_article(delete_id) delete_article(delete_id)
_update_context() _update_context()
else: else:
@ -61,8 +60,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 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)
_update_context() _update_context()
if username: if username:
update_user_password(username) update_user_password(username)
@ -70,31 +75,27 @@ 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):
article_id &= 0xFFFFFFFF # max 32 bits article_id &= 0xFFFFFFFF # max 32 bits
with db_session: with db_session:
if article := Article.select(article_id=article_id): article = Article.select(article_id=article_id)
if article:
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()[:])
@ -114,5 +115,6 @@ def get_users():
def update_user_password(username, password="123456"): def update_user_password(username, password="123456"):
with db_session: with db_session:
if if user := User.select(name=username).first(): user = User.select(name=username)
user.password = md5(username + password) if user:
user.first().password = md5(username + password)

View File

@ -1,7 +1,7 @@
from pony.orm import * from pony.orm import *
db = Database() db = Database()
db.bind("sqlite", "./static/wordfreqapp.db", create_db=True) # bind sqlit file db.bind("sqlite", "./static/wordfreqapp.db", create_db=True) # bind sqlite file
class User(db.Entity): class User(db.Entity):

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>