refactor: refactor the model

Alpha
吴宇涵 2023-03-23 17:09:25 +08:00
parent 82896de336
commit 3e35679a91
3 changed files with 54 additions and 1 deletions

View File

@ -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):

34
app/model/article.py Normal file
View File

@ -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)
]

19
app/model/user.py Normal file
View File

@ -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