用ponyORM重构get_today_article函数的原生SQL语句
parent
b41e1044bc
commit
6210ca432a
|
@ -8,6 +8,7 @@ import hashlib
|
|||
from datetime import datetime
|
||||
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 MyPony import *
|
||||
|
||||
|
||||
path_prefix = '/var/www/wordfreq/wordfreq/'
|
||||
|
@ -32,22 +33,20 @@ def get_article_body(s):
|
|||
return '\n'.join(lst)
|
||||
|
||||
|
||||
@db_session
|
||||
def get_today_article(user_word_list, visited_articles):
|
||||
rq = RecordQuery(path_prefix + 'static/wordfreqapp.db')
|
||||
if visited_articles is None:
|
||||
visited_articles = {
|
||||
"index" : 0, # 为 article_ids 的索引
|
||||
"article_ids": [] # 之前显示文章的id列表,越后越新
|
||||
}
|
||||
if visited_articles["index"] > len(visited_articles["article_ids"])-1: # 生成新的文章,因此查找所有的文章
|
||||
rq.instructions("SELECT * FROM article")
|
||||
result = list(selectAllArticles())
|
||||
else: # 生成阅读过的文章,因此查询指定 article_id 的文章
|
||||
if visited_articles["article_ids"][visited_articles["index"]] == 'null': # 可能因为直接刷新页面导致直接去查询了'null',因此当刷新的页面的时候,需要直接进行“上一篇”操作
|
||||
visited_articles["index"] -= 1
|
||||
visited_articles["article_ids"].pop()
|
||||
rq.instructions('SELECT * FROM article WHERE article_id=%d' % (visited_articles["article_ids"][visited_articles["index"]]))
|
||||
rq.do()
|
||||
result = rq.get_results()
|
||||
result = list(selectArticlesById(visited_articles["article_ids"][visited_articles["index"]]))
|
||||
random.shuffle(result)
|
||||
|
||||
# Choose article according to reader's level
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue