Compare commits

...

2 Commits

2 changed files with 17 additions and 12 deletions

View File

@ -1,6 +1,5 @@
from WordFreq import WordFreq
from wordfreqCMD import youdao_link, sort_in_descending_order
from UseSqlite import InsertQuery, RecordQuery
import pickle_idea, pickle_idea2
import os
import random, glob
@ -8,6 +7,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 app.model import article
path_prefix = '/var/www/wordfreq/wordfreq/'
@ -15,11 +15,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)
return article.get_number_of_articles()
def get_article_title(s):
@ -33,22 +29,18 @@ def get_article_body(s):
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 = article.get_all_articles()
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()
random.shuffle(result)
result = article.get_article_by_id(visited_articles["article_ids"][visited_articles["index"]])
# Choose article according to reader's level
d1 = load_freq_history(path_prefix + 'static/frequency/frequency.p')

View File

@ -32,3 +32,16 @@ def get_page_articles(num, size):
x
for x in Article.select().order_by(desc(Article.article_id)).page(num, size)
]
def get_article_by_id(article_id):
article_id &= 0xFFFFFFFF # max 32 bits
with db_session:
article = Article.select(article_id=article_id).first()
if article:
return article.to_dict()
def get_all_articles():
with db_session:
return [art.to_dict() for art in Article.select()[:]]