From 3e35679a91515425ec77573dc9a710860081dd2a Mon Sep 17 00:00:00 2001 From: Awoodwhale Date: Thu, 23 Mar 2023 17:09:25 +0800 Subject: [PATCH] refactor: refactor the model --- app/{model.py => model/__init__.py} | 2 +- app/model/article.py | 34 +++++++++++++++++++++++++++++ app/model/user.py | 19 ++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) rename app/{model.py => model/__init__.py} (89%) create mode 100644 app/model/article.py create mode 100644 app/model/user.py diff --git a/app/model.py b/app/model/__init__.py similarity index 89% rename from app/model.py rename to app/model/__init__.py index bc8a08a..9526313 100644 --- a/app/model.py +++ b/app/model/__init__.py @@ -1,7 +1,7 @@ from pony.orm import * db = Database() -db.bind("sqlite", "./static/wordfreqapp.db", create_db=True) # bind sqlite file +db.bind("sqlite", "../static/wordfreqapp.db", create_db=True) # bind sqlite file class User(db.Entity): diff --git a/app/model/article.py b/app/model/article.py new file mode 100644 index 0000000..a3b4bf7 --- /dev/null +++ b/app/model/article.py @@ -0,0 +1,34 @@ +from model import * +from datetime import datetime + +def add_article(content, source="manual_input", level="5", question="No question"): + with db_session: + # add one article to sqlite + Article( + text=content, + source=source, + date=datetime.now().strftime("%-d %b %Y"), # format style of `5 Oct 2022` + level=level, + question=question, + ) + + +def delete_article_by_id(article_id): + article_id &= 0xFFFFFFFF # max 32 bits + with db_session: + article = Article.select(article_id=article_id) + if article: + article.first().delete() + + +def get_number_of_articles(): + 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) + ] diff --git a/app/model/user.py b/app/model/user.py new file mode 100644 index 0000000..6c030c2 --- /dev/null +++ b/app/model/user.py @@ -0,0 +1,19 @@ +from model import * +from Login import md5 + +def get_users(): + with db_session: + return User.select().order_by(User.name)[:] + + +def update_password_by_username(username, password="123456"): + with db_session: + user = User.select(name=username) + if user: + user.first().password = md5(username + password) + +def update_expiry_time_by_username(username, expiry_time="20230323"): + with db_session: + user = User.select(name=username) + if user: + user.first().expiry_date = expiry_time