0
0
Fork 0

用ponyORM重构get_today_article函数的原生SQL语句

Alpha-snapshot20230511
何铮铮 2023-05-24 18:31:52 +08:00
parent b41e1044bc
commit 6210ca432a
2 changed files with 59 additions and 5 deletions

View File

@ -8,6 +8,7 @@ import hashlib
from datetime import datetime from datetime import datetime
from flask import Flask, request, redirect, render_template, url_for, session, abort, flash, get_flashed_messages from flask import Flask, request, redirect, render_template, url_for, session, abort, flash, get_flashed_messages
from difficulty import get_difficulty_level_for_user, text_difficulty_level, user_difficulty_level from difficulty import get_difficulty_level_for_user, text_difficulty_level, user_difficulty_level
from MyPony import *
path_prefix = '/var/www/wordfreq/wordfreq/' path_prefix = '/var/www/wordfreq/wordfreq/'
@ -32,22 +33,20 @@ def get_article_body(s):
return '\n'.join(lst) return '\n'.join(lst)
@db_session
def get_today_article(user_word_list, visited_articles): def get_today_article(user_word_list, visited_articles):
rq = RecordQuery(path_prefix + 'static/wordfreqapp.db')
if visited_articles is None: if visited_articles is None:
visited_articles = { visited_articles = {
"index" : 0, # 为 article_ids 的索引 "index" : 0, # 为 article_ids 的索引
"article_ids": [] # 之前显示文章的id列表越后越新 "article_ids": [] # 之前显示文章的id列表越后越新
} }
if visited_articles["index"] > len(visited_articles["article_ids"])-1: # 生成新的文章,因此查找所有的文章 if visited_articles["index"] > len(visited_articles["article_ids"])-1: # 生成新的文章,因此查找所有的文章
rq.instructions("SELECT * FROM article") result = list(selectAllArticles())
else: # 生成阅读过的文章,因此查询指定 article_id 的文章 else: # 生成阅读过的文章,因此查询指定 article_id 的文章
if visited_articles["article_ids"][visited_articles["index"]] == 'null': # 可能因为直接刷新页面导致直接去查询了'null',因此当刷新的页面的时候,需要直接进行“上一篇”操作 if visited_articles["article_ids"][visited_articles["index"]] == 'null': # 可能因为直接刷新页面导致直接去查询了'null',因此当刷新的页面的时候,需要直接进行“上一篇”操作
visited_articles["index"] -= 1 visited_articles["index"] -= 1
visited_articles["article_ids"].pop() visited_articles["article_ids"].pop()
rq.instructions('SELECT * FROM article WHERE article_id=%d' % (visited_articles["article_ids"][visited_articles["index"]])) result = list(selectArticlesById(visited_articles["article_ids"][visited_articles["index"]]))
rq.do()
result = rq.get_results()
random.shuffle(result) random.shuffle(result)
# Choose article according to reader's level # Choose article according to reader's level

55
app/MyPony.py Normal file
View File

@ -0,0 +1,55 @@
from pony.orm import *
db = Database()
# 数据库实体
class Article(db.Entity):
_table_ = "article" # table name
article_id = PrimaryKey(int, auto=True)
text = Optional(str)
source = Optional(str)
date = Optional(str)
level = Optional(str)
question = Optional(str)
db.bind(provider='sqlite', filename='./static/wordfreqapp.db', create_db=True)
db.generate_mapping(create_tables=True)
# 打印SQL错误
set_sql_debug(True)
@db_session
def selectAllArticles():
"""
获取全部文章
"""
articles = select(a for a in Article)
articles_list = []
for i in articles:
d = {"article_id": i.article_id,
"text": i.text,
"source": i.source,
"date": i.date,
"level": i.level,
"question": i.question}
articles_list.append(d)
return articles_list
@db_session
def selectArticlesById(id):
"""
根据文章id搜索
"""
article = Article.get(article_id=id)
li = []
d = {"article_id": article.article_id,
"text": article.text,
"source": article.source,
"date": article.date,
"level": article.level,
"question": article.question}
li.append(d)
return li