0
0
Fork 0

AJAX载入文章数据

Bug509-XieQiuHan-WangZiming-HuangDan
huangdan 2023-05-08 14:10:16 +08:00
parent d30a434b2a
commit a0894f6901
2 changed files with 85 additions and 10 deletions

View File

@ -45,22 +45,25 @@
{# {% endfor %}#}
{% if result_of_generate_article != "had read all articles" %}
<a id="next_btn" class="btn btn-success" href="/{{ username }}/reset" role="button"> 下一篇 Next Article </a>
<button class="btn btn-success" id="load_next_article" onclick="load_next_article()"> 下一篇 Next Article </button>
<!-- <a id="next_btn" class="btn btn-success" href="/{{ username }}/reset" role="button"> 下一篇 Next Article </a>-->
{% endif %}
{% if session.get('visited_articles') and session.get('visited_articles')['index']>0 %}
<a id="pre_btn" class="btn btn-success" href="/{{ username }}/back" role="button"> 上一篇 Previous Article </a>
<!-- <a id="pre_btn" class="btn btn-success" href="/{{ username }}/back" role="button"> 上一篇 Previous Article </a>-->
<button class="btn btn-success" id="load_pre_article" onclick="load_pre_article()"> 上一篇 Previous Article </button>
{% endif %}
<p><b>阅读文章并回答问题</b></p>
<p><b>阅读文章并回答问题{{result_of_generate_article}}</b></p>
<div id="text-content">
{% if result_of_generate_article == 'found' %}
<div class="alert alert-success" role="alert">According to your word list, your level is <span class="badge bg-success">{{ today_article["user_level"] }}</span> and we have chosen an article with a difficulty level of <span class="badge bg-success">{{ today_article["text_level"] }}</span> for you.</div>
<p class="text-muted">Article added on: {{ today_article["date"] }}</p><br/>
<div id="found">
<div class="alert alert-success" role="alert">According to your word list, your level is <span class="badge bg-success" id="user_level">{{ today_article["user_level"] }}</span> and we have chosen an article with a difficulty level of <span class="badge bg-success" id="text-level">{{ today_article["text_level"] }}</span> for you.</div>
<p class="text-muted" id="date">Article added on: {{ today_article["date"] }}</p><br/>
<div class="p-3 mb-2 bg-light text-dark"><br/>
<p class="display-5">{{ today_article["article_title"] }}</p><br/>
<p class="display-5" id="article_title">{{ today_article["article_title"] }}</p><br/>
<p class="lead"><font id="article" size=2>{{ today_article["article_body"] }}</font></p><br/>
<p><small class="text-muted">{{ today_article['source'] }}</small></p><br/>
<p><b>{{ today_article['question'] }}</b></p><br/>
<p><small class="text-muted" id="source">{{ today_article['source'] }}</small></p><br/>
<p><b id="question">{{ today_article['question'] }}</b></p><br/>
<script type="text/javascript">
function toggle_visibility(id) { {# https://css-tricks.com/snippets/javascript/showhide-element/#}
const e = document.getElementById(id);
@ -73,12 +76,13 @@
<button onclick="toggle_visibility('answer');">ANSWER</button>
<div id="answer" style="display:none;">{{ today_article['answer'] }}</div><br/>
</div>
</div>
{% elif result_of_generate_article == "not found" %}
<div class="alert alert-success" role="alert">
<div class="alert alert-success" role="alert" id="not_found">
<p class="text-muted"><span class="badge bg-success">Notes:</span><br>No article is currently available for you. You can try again a few times or mark new words in the passage to improve your level.</p>
</div>
{% elif result_of_generate_article == "had read all articles" %}
<div class="alert alert-success" role="alert">
<div class="alert alert-success" role="alert" id="read_all">
<p class="text-muted"><span class="badge bg-success">Notes:</span><br>You've read all the articles.</p>
</div>
{% endif %}
@ -146,6 +150,50 @@
<script src="{{ js }}"></script>
{% endfor %}
{% endif %}
<script>
function load_next_article(){
$.ajax({
url: '/get_next_article/{{username}}',
dataType: 'json',
success: function(data) {
// 更新页面内容
if(data){
$('#user_level').html(data['user_level']);
$('#text_level').html(data["text_level"]);
$('#date').html('Article added on: '+data["date"]);
$('#article_title').html(data["article_title"]);
$('#article').html(data["article_body"]);
$('#source').html(data['source']);
$('#question').html(data["question"]);
$('#answer').html(data["answer"]);
}
}
});
}
function load_pre_article(){
$.ajax({
url: '/get_pre_article/{{username}}',
dataType: 'json',
success: function(data) {
// 更新页面内容
if(data){
$('#user_level').html(data['user_level']);
$('#text_level').html(data["text_level"]);
$('#date').html('Article added on: '+data["date"]);
$('#article_title').html(data["article_title"]);
$('#article').html(data["article_body"]);
$('#source').html(data['source']);
$('#question').html(data["question"]);
$('#answer').html(data["answer"]);
}
}
});
console.info({{visited_articles}})
}
</script>
</body>
<style>
mark {

View File

@ -21,6 +21,21 @@ userService = Blueprint("user_bp", __name__)
path_prefix = '/var/www/wordfreq/wordfreq/'
path_prefix = './' # comment this line in deployment
@userService.route("/get_next_article/<username>",methods=['GET','POST'])
def get_next_article(username):
user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
session['old_articleID'] = session.get('articleID')
if request.method == 'GET':
visited_articles = session.get("visited_articles")
if visited_articles['article_ids'][-1] == "null": # 如果当前还是“null”则将“null”pop出来,无需index+=1
visited_articles['article_ids'].pop()
else: # 当前不为“null”直接 index+=1
visited_articles["index"] += 1
session["visited_articles"] = visited_articles
visited_articles, data, result_of_generate_article = get_today_article(user_freq_record, session.get('visited_articles'))
else:
return 'Under construction'
return json.dumps(data)
@userService.route("/<username>/reset", methods=['GET', 'POST'])
def user_reset(username):
@ -40,6 +55,18 @@ def user_reset(username):
else:
return 'Under construction'
@userService.route("/get_pre_article/<username>",methods=['GET'])
def get_pre_article(username):
user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
if request.method == 'GET':
visited_articles = session.get("visited_articles")
visited_articles["index"] -= 1 # 上一篇index-=1
if visited_articles['article_ids'][-1] == "null": # 如果当前还是“null”则将“null”pop出来
visited_articles['article_ids'].pop()
session["visited_articles"] = visited_articles
visited_articles, data, result_of_generate_article = get_today_article(user_freq_record, session.get('visited_articles'))
return json.dumps(data)
@userService.route("/<username>/back", methods=['GET'])
def user_back(username):
'''