forked from mrlan/EnglishPal
Refactor: remove raw sql sentences in Article.py
parent
89773bdcf1
commit
6d2191f727
|
@ -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, text_difficulty_level, user_difficulty_level
|
||||
from model.article import get_number_of_articles, get_article, get_article_by_id
|
||||
|
||||
|
||||
path_prefix = '/var/www/wordfreq/wordfreq/'
|
||||
|
@ -15,11 +16,7 @@ path_prefix = './' # comment this line in deployment
|
|||
|
||||
|
||||
def total_number_of_essays():
|
||||
rq = RecordQuery(path_prefix + 'static/wordfreqapp.db')
|
||||
rq.instructions("SELECT * FROM article")
|
||||
rq.do()
|
||||
result = rq.get_results()
|
||||
return len(result)
|
||||
get_number_of_articles()
|
||||
|
||||
|
||||
def get_article_title(s):
|
||||
|
@ -33,18 +30,15 @@ def get_article_body(s):
|
|||
|
||||
|
||||
def get_today_article(user_word_list, existing_articles):
|
||||
rq = RecordQuery(path_prefix + 'static/wordfreqapp.db')
|
||||
if existing_articles is None:
|
||||
existing_articles = {
|
||||
"index" : 0, # 为 article_ids 的索引
|
||||
"article_ids": [] # 之前显示文章的id列表,越后越新
|
||||
}
|
||||
if existing_articles["index"] > len(existing_articles["article_ids"])-1:
|
||||
rq.instructions("SELECT * FROM article")
|
||||
result = list(get_article()) # 转为一个list
|
||||
else:
|
||||
rq.instructions('SELECT * FROM article WHERE article_id=%d' % (existing_articles["article_ids"][existing_articles["index"]]))
|
||||
rq.do()
|
||||
result = rq.get_results()
|
||||
result = [get_article_by_id(existing_articles["article_ids"][existing_articles["index"]])]
|
||||
random.shuffle(result)
|
||||
|
||||
# Choose article according to reader's level
|
||||
|
@ -59,31 +53,31 @@ def get_today_article(user_word_list, existing_articles):
|
|||
if existing_articles["index"] > len(existing_articles["article_ids"])-1: # 下一篇
|
||||
flag_get_article = False
|
||||
for reading in result:
|
||||
text_level = text_difficulty_level(reading['text'], d3)
|
||||
text_level = text_difficulty_level(reading.text, d3)
|
||||
factor = random.gauss(0.8,
|
||||
0.1) # a number drawn from Gaussian distribution with a mean of 0.8 and a stand deviation of 1
|
||||
if reading['article_id'] not in existing_articles["article_ids"] and within_range(text_level, user_level, (8.0 - user_level) * factor): # 新的文章之前没有出现过且符合一定范围的水平
|
||||
if reading.article_id not in existing_articles["article_ids"] and within_range(text_level, user_level, (8.0 - user_level) * factor): # 新的文章之前没有出现过且符合一定范围的水平
|
||||
d = reading
|
||||
existing_articles["article_ids"].append(d['article_id']) # 列表添加新的文章id;下面进行
|
||||
existing_articles["article_ids"].append(d.article_id) # 列表添加新的文章id;下面进行
|
||||
flag_get_article = True
|
||||
break
|
||||
if not flag_get_article:
|
||||
existing_articles["index"] -= 1
|
||||
else: # 上一篇
|
||||
d = random.choice(result)
|
||||
text_level = text_difficulty_level(d['text'], d3)
|
||||
text_level = text_difficulty_level(d.text, d3)
|
||||
|
||||
today_article = None
|
||||
if d:
|
||||
today_article = {
|
||||
"user_level": '%4.2f' % user_level,
|
||||
"text_level": '%4.2f' % text_level,
|
||||
"date": d['date'],
|
||||
"article_title": get_article_title(d['text']),
|
||||
"article_body": get_article_body(d['text']),
|
||||
"source": d["source"],
|
||||
"question": get_question_part(d['question']),
|
||||
"answer": get_answer_part(d['question'])
|
||||
"date": d.date,
|
||||
"article_title": get_article_title(d.text),
|
||||
"article_body": get_article_body(d.text),
|
||||
"source": d.source,
|
||||
"question": get_question_part(d.question),
|
||||
"answer": get_answer_part(d.question)
|
||||
}
|
||||
|
||||
return existing_articles, today_article
|
||||
|
|
|
@ -32,3 +32,14 @@ def get_page_articles(num, size):
|
|||
x
|
||||
for x in Article.select().order_by(desc(Article.article_id)).page(num, size)
|
||||
]
|
||||
|
||||
def get_article():
|
||||
with db_session:
|
||||
return Article.select()[:]
|
||||
|
||||
def get_article_by_id(article_id):
|
||||
article_id &= 0xFFFFFFFF # max 32 bits
|
||||
with db_session:
|
||||
article = Article.select(article_id=article_id)
|
||||
if article:
|
||||
return article.first()
|
||||
|
|
Loading…
Reference in New Issue