From f98da2696d76100123ebe8027377526cc4f8abe5 Mon Sep 17 00:00:00 2001
From: Luna <3282295753@qq.com>
Date: Thu, 8 Dec 2022 14:45:24 +0800
Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8DBug-504:=E9=AB=98?=
=?UTF-8?q?=E4=BA=AE=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
app/static/js/highlight.js | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/app/static/js/highlight.js b/app/static/js/highlight.js
index 1003918..5c3d5f4 100644
--- a/app/static/js/highlight.js
+++ b/app/static/js/highlight.js
@@ -22,7 +22,7 @@ function getWord() {
function highLight() {
if (!isHighlight) return;
- let articleContent = document.getElementById("article").innerText;
+ let articleContent = document.getElementById("text-content").innerText;
let pickedWords = document.getElementById("selected-words"); // words picked to the text area
let dictionaryWords = document.getElementById("selected-words2"); // words appearing in the user's new words list
let allWords = pickedWords.value + " " + dictionaryWords.value;
@@ -34,11 +34,11 @@ function highLight() {
articleContent = articleContent.replace(new RegExp("\\s"+list[i]+"\\s", "g"), " " + list[i] + " ");
}
}
- document.getElementById("article").innerHTML = articleContent;
+ document.getElementById("text-content").innerHTML = articleContent;
}
function cancelHighlighting() {
- let articleContent = document.getElementById("article").innerText;
+ let articleContent = document.getElementById("text-content").innerText;
let pickedWords = document.getElementById("selected-words");
const dictionaryWords = document.getElementById("selected-words2");
const list = pickedWords.value.split(" ");
@@ -60,7 +60,7 @@ function cancelHighlighting() {
}
}
}
- document.getElementById("article").innerHTML = articleContent;
+ document.getElementById("text-content").innerHTML = articleContent;
}
function fillInWord() {
--
2.17.1
From 6d2191f7273b5f056f58e155baeab66c8d61179f Mon Sep 17 00:00:00 2001
From: your name
Date: Mon, 24 Apr 2023 22:20:35 +0800
Subject: [PATCH 2/3] Refactor: remove raw sql sentences in Article.py
---
app/Article.py | 34 ++++++++++++++--------------------
app/model/article.py | 11 +++++++++++
2 files changed, 25 insertions(+), 20 deletions(-)
diff --git a/app/Article.py b/app/Article.py
index e0f006a..e395114 100644
--- a/app/Article.py
+++ b/app/Article.py
@@ -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
diff --git a/app/model/article.py b/app/model/article.py
index a3b4bf7..ca12a01 100644
--- a/app/model/article.py
+++ b/app/model/article.py
@@ -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()
--
2.17.1
From 1fff921d7947310c628ab9167cba4ffa4cb228d0 Mon Sep 17 00:00:00 2001
From: your name
Date: Tue, 25 Apr 2023 22:32:02 +0800
Subject: [PATCH 3/3] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86Bug504=E2=80=9C?=
=?UTF-8?q?=E6=A0=87=E8=AE=B0=E6=96=87=E7=AB=A0=E6=A0=87=E9=A2=98=E5=92=8C?=
=?UTF-8?q?=E6=96=87=E7=AB=A0=E4=B8=8B=E6=96=B9=E9=97=AE=E9=A2=98=E4=B8=8D?=
=?UTF-8?q?=E4=BC=9A=E9=AB=98=E4=BA=AE=E2=80=9D=E7=9A=84=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
app/static/js/highlight.js | 14 ++++++++++++++
app/templates/userpage_get.html | 4 ++--
2 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/app/static/js/highlight.js b/app/static/js/highlight.js
index 5ec9663..17cd2ad 100644
--- a/app/static/js/highlight.js
+++ b/app/static/js/highlight.js
@@ -23,6 +23,8 @@ function getWord() {
function highLight() {
if (!isHighlight) return;
let articleContent = document.getElementById("article").innerText; //将原来的.innerText改为.innerHtml,使用innerText会把原文章中所包含的 标签去除,导致处理后的文章内容失去了原来的格式
+ let articleTitle = document.getElementById("article_title").innerText;//获取文章标题对象的引用
+ let articleQuestion = document.getElementById("question").innerText;//获取文章问题对象的引用
let pickedWords = document.getElementById("selected-words"); // words picked to the text area
let dictionaryWords = document.getElementById("selected-words2"); // words appearing in the user's new words list
let allWords = ""; //初始化allWords的值,避免进入判断后编译器认为allWords未初始化的问题
@@ -40,13 +42,19 @@ function highLight() {
if (list[i] !== "" && "".indexOf(list[i]) === -1 && "".indexOf(list[i]) === -1) {
//将文章中所有出现该单词word的地方改为:" " + word + " "。 正则表达式RegExp()中,"\\s"代表单词前后必须要有空格,以防止只对单词中的部分字符高亮的情况出现。
articleContent = articleContent.replace(new RegExp("\\s"+list[i]+"\\s", "g"), " " + list[i] + " ");
+ articleTitle = articleTitle.replace(new RegExp("\\s"+list[i]+"\\s", "g"), " " + list[i] + " ");
+ articleQuestion = articleQuestion.replace(new RegExp("\\s"+list[i]+"\\s", "g"), " " + list[i] + " ");
}
}
document.getElementById("article").innerHTML = articleContent;
+ document.getElementById("article_title").innerHTML = articleTitle;
+ document.getElementById("question").innerHTML = articleQuestion;
}
function cancelHighlighting() {
let articleContent = document.getElementById("article").innerText;//将原来的.innerText改为.innerHtml,原因同上
+ let articleTitle = document.getElementById("article_title").innerText;//获取文章标题对象的引用
+ let articleQuestion = document.getElementById("question").innerText;//获取文章问题对象的引用
let pickedWords = document.getElementById("selected-words");
const dictionaryWords = document.getElementById("selected-words2");
const list = pickedWords.value.split(" ");
@@ -55,6 +63,8 @@ function cancelHighlighting() {
list[i] = list[i].replace(/(^\s*)|(\s*$)/g, "");
if (list[i] !== "") { //原来判断的代码中,替换的内容为“list[i]”这个字符串,这明显是错误的,我们需要替换的是list[i]里的内容
articleContent = articleContent.replace(new RegExp(""+list[i]+"", "g"), list[i]);
+ articleTitle = articleTitle.replace(new RegExp(""+list[i]+"", "g"), list[i]);
+ articleQuestion = articleQuestion.replace(new RegExp(""+list[i]+"", "g"), list[i]);
}
}
}
@@ -65,10 +75,14 @@ function cancelHighlighting() {
list2[i] = list2[i].replace(/(^\s*)|(\s*$)/g, "");
if (list2[i] !== "") { //原来代码中,替换的内容为“list[i]”这个字符串,这明显是错误的,我们需要替换的是list[i]里的内容
articleContent = articleContent.replace(new RegExp(""+list2[i]+"", "g"), list2[i]);
+ articleTitle = articleTitle.replace(new RegExp(""+list2[i]+"", "g"), list2[i]);
+ articleQuestion = articleQuestion.replace(new RegExp(""+list2[i]+"", "g"), list2[i]);
}
}
}
document.getElementById("article").innerHTML = articleContent;
+ document.getElementById("article_title").innerHTML = articleTitle;
+ document.getElementById("question").innerHTML = articleQuestion;
}
function fillInWord() {
diff --git a/app/templates/userpage_get.html b/app/templates/userpage_get.html
index b5e16aa..59e6ee1 100644
--- a/app/templates/userpage_get.html
+++ b/app/templates/userpage_get.html
@@ -58,10 +58,10 @@
According to your word list, your level is {{ today_article["user_level"] }} and we have chosen an article with a difficulty level of {{ today_article["text_level"] }} for you.