Refactor: raw SQL statements had been replaced with Pony ORM approach in Article.py (functions: total_number_of_essays and get_today_article)

SOFTARCH2023S-ZENOVIO
Nze Avomo Zenovio Ndong 2023-05-12 01:14:59 +08:00
parent 75d43137e0
commit 848eaafc81
1 changed files with 4 additions and 12 deletions

View File

@ -1,6 +1,5 @@
from WordFreq import WordFreq from WordFreq import WordFreq
from wordfreqCMD import youdao_link, sort_in_descending_order from wordfreqCMD import youdao_link, sort_in_descending_order
from UseSqlite import InsertQuery, RecordQuery
import pickle_idea, pickle_idea2 import pickle_idea, pickle_idea2
import os import os
import random, glob import random, glob
@ -8,6 +7,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, text_difficulty_level, user_difficulty_level from difficulty import get_difficulty_level, text_difficulty_level, user_difficulty_level
from app.model import article
path_prefix = '/var/www/wordfreq/wordfreq/' path_prefix = '/var/www/wordfreq/wordfreq/'
@ -15,11 +15,7 @@ path_prefix = './' # comment this line in deployment
def total_number_of_essays(): def total_number_of_essays():
rq = RecordQuery(path_prefix + 'static/wordfreqapp.db') return article.get_number_of_articles()
rq.instructions("SELECT * FROM article")
rq.do()
result = rq.get_results()
return len(result)
def get_article_title(s): def get_article_title(s):
@ -33,22 +29,18 @@ def get_article_body(s):
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 = article.get_all_articles()
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 = article.get_article_by_id(visited_articles["article_ids"][visited_articles["index"]])
rq.do()
result = rq.get_results()
random.shuffle(result)
# Choose article according to reader's level # Choose article according to reader's level
d1 = load_freq_history(path_prefix + 'static/frequency/frequency.p') d1 = load_freq_history(path_prefix + 'static/frequency/frequency.p')