diff --git a/app/Article.py b/app/Article.py index 35a06a4..e40717f 100644 --- a/app/Article.py +++ b/app/Article.py @@ -32,20 +32,20 @@ def get_article_body(s): return '\n'.join(lst) -def get_today_article(user_word_list, had_read_articles): +def get_today_article(user_word_list, visited_articles): rq = RecordQuery(path_prefix + 'static/wordfreqapp.db') - if had_read_articles is None: - had_read_articles = { + if visited_articles is None: + visited_articles = { "index" : 0, # 为 article_ids 的索引 "article_ids": [] # 之前显示文章的id列表,越后越新 } - if had_read_articles["index"] > len(had_read_articles["article_ids"])-1: # 生成新的文章,因此查找所有的文章 + if visited_articles["index"] > len(visited_articles["article_ids"])-1: # 生成新的文章,因此查找所有的文章 rq.instructions("SELECT * FROM article") else: # 生成阅读过的文章,因此查询指定 article_id 的文章 - if had_read_articles["article_ids"][had_read_articles["index"]] == 'null': # 可能因为直接刷新页面导致直接去查询了'null',因此当刷新的页面的时候,需要直接进行“上一篇”操作 - had_read_articles["index"] -= 1 - had_read_articles["article_ids"].pop() - rq.instructions('SELECT * FROM article WHERE article_id=%d' % (had_read_articles["article_ids"][had_read_articles["index"]])) + 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) @@ -60,25 +60,25 @@ def get_today_article(user_word_list, had_read_articles): d_user = load_freq_history(user_word_list) user_level = user_difficulty_level(d_user, d3) # more consideration as user's behaviour is dynamic. Time factor should be considered. text_level = 0 - if had_read_articles["index"] > len(had_read_articles["article_ids"])-1: # 生成新的文章 - amount_of_had_read_articles = len(had_read_articles["article_ids"]) + if visited_articles["index"] > len(visited_articles["article_ids"])-1: # 生成新的文章 + amount_of_visited_articles = len(visited_articles["article_ids"]) amount_of_existing_articles = result.__len__() - if amount_of_had_read_articles == amount_of_existing_articles: # 如果当前阅读过的文章的数量 == 存在的文章的数量,即所有的书本都阅读过了 + if amount_of_visited_articles == amount_of_existing_articles: # 如果当前阅读过的文章的数量 == 存在的文章的数量,即所有的书本都阅读过了 result_of_generate_article = "had read all articles" else: for k in range(3): # 最多尝试3次 for reading in result: 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 had_read_articles["article_ids"] and within_range(text_level, user_level, (8.0 - user_level) * factor): # 新的文章之前没有出现过且符合一定范围的水平 + if reading['article_id'] not in visited_articles["article_ids"] and within_range(text_level, user_level, (8.0 - user_level) * factor): # 新的文章之前没有出现过且符合一定范围的水平 d = reading - had_read_articles["article_ids"].append(d['article_id']) # 列表添加新的文章id;下面进行 + visited_articles["article_ids"].append(d['article_id']) # 列表添加新的文章id;下面进行 result_of_generate_article = "found" break if result_of_generate_article == "found": # 用于成功找到文章后及时退出外层循环 break if result_of_generate_article != "found": # 阅读完所有文章,或者循环3次没有找到适合的文章,则放入空(“null”) - had_read_articles["article_ids"].append('null') + visited_articles["article_ids"].append('null') else: # 生成已经阅读过的文章 d = random.choice(result) text_level = text_difficulty_level(d['text'], d3) @@ -97,7 +97,7 @@ def get_today_article(user_word_list, had_read_articles): "answer": get_answer_part(d['question']) } - return had_read_articles, today_article, result_of_generate_article + return visited_articles, today_article, result_of_generate_article def load_freq_history(path): diff --git a/app/account_service.py b/app/account_service.py index 8de9a03..a7ed0c4 100644 --- a/app/account_service.py +++ b/app/account_service.py @@ -37,7 +37,7 @@ def signup(): session[username] = username session['username'] = username session['expiry_date'] = get_expiry_date(username) - session['had_read_articles'] = None + session['visited_articles'] = None return jsonify({'status': '2'}) else: return jsonify({'status': '1'}) @@ -66,7 +66,7 @@ def login(): session['username'] = username user_expiry_date = get_expiry_date(username) session['expiry_date'] = user_expiry_date - session['had_read_articles'] = None + session['visited_articles'] = None return jsonify({'status': '1'}) else: return jsonify({'status': '0'}) diff --git a/app/templates/userpage_get.html b/app/templates/userpage_get.html index 098674d..ae11a1c 100644 --- a/app/templates/userpage_get.html +++ b/app/templates/userpage_get.html @@ -52,7 +52,7 @@ {% if result_of_generate_article != "had read all articles" %} 下一篇 Next Article {% endif %} - {% if session.get('had_read_articles') and session.get('had_read_articles')['index']>0 %} + {% if session.get('visited_articles') and session.get('visited_articles')['index']>0 %} 上一篇 Previous Article {% endif %} diff --git a/app/user_service.py b/app/user_service.py index 787acf4..6a6bd97 100644 --- a/app/user_service.py +++ b/app/user_service.py @@ -30,12 +30,12 @@ def user_reset(username): :return: 返回页面内容 ''' if request.method == 'GET': - 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() + 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 - had_read_articles["index"] += 1 - session["had_read_articles"] = had_read_articles + visited_articles["index"] += 1 + session["visited_articles"] = visited_articles return redirect(url_for('user_bp.userpage', username=username)) else: return 'Under construction' @@ -48,11 +48,11 @@ def user_back(username): :return: 返回页面内容 ''' if request.method == 'GET': - had_read_articles = session.get("had_read_articles") - 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 + 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 return redirect(url_for('user_bp.userpage', username=username)) @@ -139,8 +139,8 @@ def userpage(username): words = '' for x in lst3: words += x[0] + ' ' - 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 + visited_articles, today_article, result_of_generate_article = get_today_article(user_freq_record, session.get('visited_articles')) + session['visited_articles'] = visited_articles # 通过 today_article,加载前端的显示页面 return render_template('userpage_get.html', admin_name=ADMIN_NAME,