修复边界值问题(当刚开始就没有找到文章或者就根本被没有文章的时候,会出现上一篇按钮)

王梓铭 2023-04-21 02:36:51 +08:00
parent 70917df47b
commit 03145b57d9
3 changed files with 17 additions and 14 deletions

View File

@ -74,11 +74,12 @@ def get_today_article(user_word_list, had_read_articles):
break break
if result_of_generate_article == "found": # 用于成功找到文章后及时退出外层循环 if result_of_generate_article == "found": # 用于成功找到文章后及时退出外层循环
break break
if result_of_generate_article != "found": # 阅读完所有文章或者循环3次没有找到适合的文章对 index+=1 进行回滚 if result_of_generate_article != "found": # 阅读完所有文章或者循环3次没有找到适合的文章放入空“null”
had_read_articles["index"] -= 1 had_read_articles["article_ids"].append('null')
else: # 生成已经阅读过的文章 else: # 生成已经阅读过的文章
d = random.choice(result) d = random.choice(result)
text_level = text_difficulty_level(d['text'], d3) text_level = text_difficulty_level(d['text'], d3)
result_of_generate_article = "found"
today_article = None today_article = None
if d: if d:

View File

@ -44,14 +44,16 @@
{# <div class="alert alert-warning" role="alert">Congratulations! {{ message }}</div>#} {# <div class="alert alert-warning" role="alert">Congratulations! {{ message }}</div>#}
{# {% endfor %}#} {# {% 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> <a id="next_btn" class="btn btn-success" href="/{{ username }}/reset" role="button"> 下一篇 Next Article </a>
{% if session.get('had_read_articles') != None and session.get('had_read_articles')["index"] !=0 %} {% endif %}
{% if session.get('had_read_articles') and session.get('had_read_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>
{% endif %} {% endif %}
<p><b>阅读文章并回答问题</b></p> <p><b>阅读文章并回答问题</b></p>
<div id="text-content"> <div id="text-content">
{% if today_article %} {% 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> <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/> <p class="text-muted">Article added on: {{ today_article["date"] }}</p><br/>
<div class="p-3 mb-2 bg-light text-dark"><br/> <div class="p-3 mb-2 bg-light text-dark"><br/>

View File

@ -31,6 +31,9 @@ def user_reset(username):
''' '''
if request.method == 'GET': if request.method == 'GET':
had_read_articles = session.get("had_read_articles") had_read_articles = session.get("had_read_articles")
if had_read_articles['article_ids'][-1] == "null": # 如果当前还是“null”则将“null”pop出来,无需index+=1
had_read_articles['article_ids'].pop()
else: # 当前不为“null”直接 index+=1
had_read_articles["index"] += 1 had_read_articles["index"] += 1
session["had_read_articles"] = had_read_articles session["had_read_articles"] = had_read_articles
return redirect(url_for('user_bp.userpage', username=username)) return redirect(url_for('user_bp.userpage', username=username))
@ -45,9 +48,10 @@ def user_back(username):
:return: 返回页面内容 :return: 返回页面内容
''' '''
if request.method == 'GET': if request.method == 'GET':
if session.get("found_article"):
had_read_articles = session.get("had_read_articles") had_read_articles = session.get("had_read_articles")
had_read_articles["index"] -= 1 had_read_articles["index"] -= 1 # 上一篇index-=1
if had_read_articles['article_ids'][-1] == "null": # 如果当前还是“null”则将“null”pop出来
had_read_articles['article_ids'].pop()
session["had_read_articles"] = had_read_articles session["had_read_articles"] = had_read_articles
return redirect(url_for('user_bp.userpage', username=username)) return redirect(url_for('user_bp.userpage', username=username))
@ -137,10 +141,6 @@ def userpage(username):
words += x[0] + ' ' words += x[0] + ' '
had_read_articles, today_article, result_of_generate_article = get_today_article(user_freq_record, session.get('had_read_articles')) had_read_articles, today_article, result_of_generate_article = get_today_article(user_freq_record, session.get('had_read_articles'))
session['had_read_articles'] = had_read_articles session['had_read_articles'] = had_read_articles
if today_article is None:
session["found_article"] = False
else:
session["found_article"] = True
# 通过 today_article加载前端的显示页面 # 通过 today_article加载前端的显示页面
return render_template('userpage_get.html', return render_template('userpage_get.html',
username=username, username=username,