2023-03-23 17:09:25 +08:00
|
|
|
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,
|
2024-07-04 10:40:35 +08:00
|
|
|
date=datetime.now().strftime("%d %b %Y"), # format style of `5 Oct 2022`
|
2023-03-23 17:09:25 +08:00
|
|
|
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)
|
|
|
|
]
|
2024-07-04 10:40:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
def get_all_articles():
|
|
|
|
articles = []
|
|
|
|
with db_session:
|
|
|
|
for article in Article.select():
|
|
|
|
articles.append(article.to_dict())
|
|
|
|
return articles
|
|
|
|
|
|
|
|
|
|
|
|
def get_article_by_id(article_id):
|
|
|
|
with db_session:
|
|
|
|
article = Article.get(article_id=article_id)
|
|
|
|
return [article.to_dict()]
|