From 848eaafc81db74f49e7e50b9c6d8d62f39dbb0a6 Mon Sep 17 00:00:00 2001 From: zenovio Date: Fri, 12 May 2023 01:14:59 +0800 Subject: [PATCH] Refactor: raw SQL statements had been replaced with Pony ORM approach in Article.py (functions: total_number_of_essays and get_today_article) --- app/Article.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/app/Article.py b/app/Article.py index e40717f..4d704be 100644 --- a/app/Article.py +++ b/app/Article.py @@ -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')