2022-01-26 21:10:09 +08:00
|
|
|
|
from datetime import datetime
|
2023-03-20 20:15:58 +08:00
|
|
|
|
from admin_service import ADMIN_NAME
|
2022-01-26 21:10:09 +08:00
|
|
|
|
from flask import *
|
|
|
|
|
|
|
|
|
|
# from app import Yaml
|
|
|
|
|
# from app.Article import get_today_article, load_freq_history
|
|
|
|
|
# from app.WordFreq import WordFreq
|
|
|
|
|
# from app.wordfreqCMD import sort_in_descending_order
|
|
|
|
|
|
|
|
|
|
import Yaml
|
|
|
|
|
from Article import get_today_article, load_freq_history
|
|
|
|
|
from WordFreq import WordFreq
|
|
|
|
|
from wordfreqCMD import sort_in_descending_order
|
|
|
|
|
|
|
|
|
|
import pickle_idea
|
|
|
|
|
import pickle_idea2
|
|
|
|
|
|
2024-07-04 09:29:55 +08:00
|
|
|
|
import logging
|
|
|
|
|
logging.basicConfig(filename='log.txt', format='%(asctime)s %(message)s', level=logging.DEBUG)
|
|
|
|
|
|
2022-01-26 21:10:09 +08:00
|
|
|
|
# 初始化蓝图
|
|
|
|
|
userService = Blueprint("user_bp", __name__)
|
|
|
|
|
|
|
|
|
|
path_prefix = '/var/www/wordfreq/wordfreq/'
|
|
|
|
|
path_prefix = './' # comment this line in deployment
|
|
|
|
|
|
2023-05-08 14:33:48 +08:00
|
|
|
|
@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')
|
2022-01-26 21:10:09 +08:00
|
|
|
|
if request.method == 'GET':
|
2023-04-25 17:47:51 +08:00
|
|
|
|
visited_articles = session.get("visited_articles")
|
|
|
|
|
if visited_articles['article_ids'][-1] == "null": # 如果当前还是“null”,则将“null”pop出来,无需index+=1
|
|
|
|
|
visited_articles['article_ids'].pop()
|
2023-04-21 02:36:51 +08:00
|
|
|
|
else: # 当前不为“null”,直接 index+=1
|
2023-04-25 17:47:51 +08:00
|
|
|
|
visited_articles["index"] += 1
|
|
|
|
|
session["visited_articles"] = visited_articles
|
2024-07-04 09:29:55 +08:00
|
|
|
|
logging.debug('/get_next_article: start calling get_today_arcile()')
|
2023-05-11 15:51:10 +08:00
|
|
|
|
visited_articles, today_article, result_of_generate_article = get_today_article(user_freq_record, session.get('visited_articles'))
|
2024-07-04 09:29:55 +08:00
|
|
|
|
logging.debug('/get_next_arcile: done.')
|
2023-05-11 15:51:10 +08:00
|
|
|
|
data = {
|
|
|
|
|
'visited_articles': visited_articles,
|
|
|
|
|
'today_article': today_article,
|
|
|
|
|
'result_of_generate_article': result_of_generate_article
|
|
|
|
|
}
|
2022-01-26 21:10:09 +08:00
|
|
|
|
else:
|
|
|
|
|
return 'Under construction'
|
2023-05-08 14:33:48 +08:00
|
|
|
|
return json.dumps(data)
|
2022-01-26 21:10:09 +08:00
|
|
|
|
|
2023-05-08 14:33:48 +08:00
|
|
|
|
@userService.route("/get_pre_article/<username>",methods=['GET'])
|
|
|
|
|
def get_pre_article(username):
|
|
|
|
|
user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
|
2022-07-21 23:13:33 +08:00
|
|
|
|
if request.method == 'GET':
|
2023-04-25 17:47:51 +08:00
|
|
|
|
visited_articles = session.get("visited_articles")
|
2023-05-11 15:51:10 +08:00
|
|
|
|
if(visited_articles["index"]==0):
|
|
|
|
|
data=''
|
|
|
|
|
else:
|
|
|
|
|
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, today_article, result_of_generate_article = get_today_article(user_freq_record, session.get('visited_articles'))
|
|
|
|
|
data = {
|
|
|
|
|
'visited_articles': visited_articles,
|
|
|
|
|
'today_article': today_article,
|
|
|
|
|
'result_of_generate_article':result_of_generate_article
|
|
|
|
|
}
|
2023-05-08 14:33:48 +08:00
|
|
|
|
return json.dumps(data)
|
2022-01-26 21:10:09 +08:00
|
|
|
|
|
|
|
|
|
@userService.route("/<username>/<word>/unfamiliar", methods=['GET', 'POST'])
|
|
|
|
|
def unfamiliar(username, word):
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
:param username:
|
|
|
|
|
:param word:
|
|
|
|
|
:return:
|
|
|
|
|
'''
|
|
|
|
|
user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
|
|
|
|
|
pickle_idea.unfamiliar(user_freq_record, word)
|
|
|
|
|
session['thisWord'] = word # 1. put a word into session
|
|
|
|
|
session['time'] = 1
|
2022-07-18 13:29:04 +08:00
|
|
|
|
return "success"
|
2022-01-26 21:10:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@userService.route("/<username>/<word>/familiar", methods=['GET', 'POST'])
|
|
|
|
|
def familiar(username, word):
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
:param username:
|
|
|
|
|
:param word:
|
|
|
|
|
:return:
|
|
|
|
|
'''
|
|
|
|
|
user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
|
|
|
|
|
pickle_idea.familiar(user_freq_record, word)
|
|
|
|
|
session['thisWord'] = word # 1. put a word into session
|
|
|
|
|
session['time'] = 1
|
2022-07-18 13:29:04 +08:00
|
|
|
|
return "success"
|
2022-01-26 21:10:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@userService.route("/<username>/<word>/del", methods=['GET', 'POST'])
|
|
|
|
|
def deleteword(username, word):
|
|
|
|
|
'''
|
|
|
|
|
删除单词
|
|
|
|
|
:param username: 用户名
|
|
|
|
|
:param word: 单词
|
|
|
|
|
:return: 重定位到用户界面
|
|
|
|
|
'''
|
|
|
|
|
user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
|
|
|
|
|
pickle_idea2.deleteRecord(user_freq_record, word)
|
2023-03-27 14:28:54 +08:00
|
|
|
|
# 模板userpage_get.html中删除单词是异步执行,而flash的信息后续是同步执行的,所以注释这段代码;同时如果这里使用flash但不提取信息,则会影响 signup.html的显示。bug复现:删除单词后,点击退出,点击注册,注册页面就会出现提示信息
|
|
|
|
|
# flash(f'{word} is no longer in your word list.')
|
2022-07-18 13:29:04 +08:00
|
|
|
|
return "success"
|
2022-01-26 21:10:09 +08:00
|
|
|
|
|
|
|
|
|
|
2023-04-20 20:30:14 +08:00
|
|
|
|
@userService.route("/<username>/userpage", methods=['GET', 'POST'])
|
2022-01-26 21:10:09 +08:00
|
|
|
|
def userpage(username):
|
|
|
|
|
'''
|
|
|
|
|
用户界面
|
|
|
|
|
:param username: 用户名
|
|
|
|
|
:return: 返回用户界面
|
|
|
|
|
'''
|
|
|
|
|
# 未登录,跳转到未登录界面
|
|
|
|
|
if not session.get('logged_in'):
|
|
|
|
|
return render_template('not_login.html')
|
|
|
|
|
|
|
|
|
|
# 用户过期
|
|
|
|
|
user_expiry_date = session.get('expiry_date')
|
2022-01-27 12:45:09 +08:00
|
|
|
|
if datetime.now().strftime('%Y%m%d') > user_expiry_date:
|
2023-01-29 10:57:58 +08:00
|
|
|
|
return render_template('expiry.html', expiry_date=user_expiry_date)
|
2022-01-26 21:10:09 +08:00
|
|
|
|
|
|
|
|
|
# 获取session里的用户名
|
|
|
|
|
username = session.get('username')
|
|
|
|
|
|
|
|
|
|
user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
|
|
|
|
|
|
|
|
|
|
if request.method == 'POST': # when we submit a form
|
2024-07-04 09:29:55 +08:00
|
|
|
|
content = request.form['content']
|
2022-01-26 21:10:09 +08:00
|
|
|
|
f = WordFreq(content)
|
|
|
|
|
lst = f.get_freq()
|
|
|
|
|
return render_template('userpage_post.html',username=username,lst = lst, yml=Yaml.yml)
|
|
|
|
|
|
|
|
|
|
elif request.method == 'GET': # when we load a html page
|
|
|
|
|
d = load_freq_history(user_freq_record)
|
|
|
|
|
lst = pickle_idea2.dict2lst(d)
|
|
|
|
|
lst2 = []
|
|
|
|
|
for t in lst:
|
|
|
|
|
lst2.append((t[0], len(t[1])))
|
|
|
|
|
lst3 = sort_in_descending_order(lst2)
|
|
|
|
|
words = ''
|
|
|
|
|
for x in lst3:
|
|
|
|
|
words += x[0] + ' '
|
2023-04-25 17:47:51 +08:00
|
|
|
|
visited_articles, today_article, result_of_generate_article = get_today_article(user_freq_record, session.get('visited_articles'))
|
|
|
|
|
session['visited_articles'] = visited_articles
|
2023-03-08 16:33:13 +08:00
|
|
|
|
# 通过 today_article,加载前端的显示页面
|
2022-01-26 21:10:09 +08:00
|
|
|
|
return render_template('userpage_get.html',
|
2023-03-20 20:15:58 +08:00
|
|
|
|
admin_name=ADMIN_NAME,
|
2022-01-26 21:10:09 +08:00
|
|
|
|
username=username,
|
|
|
|
|
session=session,
|
2023-03-27 14:28:54 +08:00
|
|
|
|
# flashed_messages=get_flashed_messages(), 仅有删除单词的时候使用到flash,而删除单词是异步执行,这里的信息提示是同步执行,所以就没有存在的必要了
|
2023-03-08 16:33:13 +08:00
|
|
|
|
today_article=today_article,
|
2023-04-20 22:53:30 +08:00
|
|
|
|
result_of_generate_article=result_of_generate_article,
|
2022-01-26 21:10:09 +08:00
|
|
|
|
d_len=len(d),
|
|
|
|
|
lst3=lst3,
|
|
|
|
|
yml=Yaml.yml,
|
|
|
|
|
words=words)
|
|
|
|
|
|
|
|
|
|
@userService.route("/<username>/mark", methods=['GET', 'POST'])
|
|
|
|
|
def user_mark_word(username):
|
|
|
|
|
'''
|
|
|
|
|
标记单词
|
|
|
|
|
:param username: 用户名
|
|
|
|
|
:return: 重定位到用户界面
|
|
|
|
|
'''
|
|
|
|
|
username = session[username]
|
|
|
|
|
user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
# 提交标记的单词
|
|
|
|
|
d = load_freq_history(user_freq_record)
|
|
|
|
|
lst_history = pickle_idea2.dict2lst(d)
|
|
|
|
|
lst = []
|
|
|
|
|
for word in request.form.getlist('marked'):
|
|
|
|
|
lst.append((word, [get_time()]))
|
|
|
|
|
d = pickle_idea2.merge_frequency(lst, lst_history)
|
2024-07-04 09:29:55 +08:00
|
|
|
|
if len(lst_history) > 999:
|
|
|
|
|
flash('You have way too many words in your difficult-words book. Delete some first.')
|
|
|
|
|
else:
|
|
|
|
|
pickle_idea2.save_frequency_to_pickle(d, user_freq_record)
|
|
|
|
|
flash('Added %s.' % (', '.join(request.form.getlist('marked'))))
|
2022-01-26 21:10:09 +08:00
|
|
|
|
return redirect(url_for('user_bp.userpage', username=username))
|
|
|
|
|
else:
|
|
|
|
|
return 'Under construction'
|
|
|
|
|
|
|
|
|
|
def get_time():
|
|
|
|
|
'''
|
|
|
|
|
获取当前时间
|
|
|
|
|
:return: 当前时间
|
|
|
|
|
'''
|
|
|
|
|
return datetime.now().strftime('%Y%m%d%H%M') # upper to minutes
|
|
|
|
|
|