diff --git a/app/Article.py b/app/Article.py index 04a32ea..824df0c 100644 --- a/app/Article.py +++ b/app/Article.py @@ -1,3 +1,6 @@ +""" +This module provides functions about article +""" from WordFreq import WordFreq from wordfreqCMD import youdao_link, sort_in_descending_order from UseSqlite import InsertQuery, RecordQuery @@ -46,7 +49,6 @@ def get_today_article(user_word_list, articleID): d1 = load_freq_history(path_prefix + 'static/frequency/frequency.p') d2 = load_freq_history(path_prefix + 'static/words_and_tests.p') d3 = get_difficulty_level(d1, d2) - d = {} 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. @@ -133,4 +135,4 @@ def get_answer_part(s): html_code += '\n' html_code += '\n' html_code += '
\n' % ('\n'.join(result)) - return html_code \ No newline at end of file + return html_code diff --git a/app/Login.py b/app/Login.py index 8e0030b..5ada69f 100644 --- a/app/Login.py +++ b/app/Login.py @@ -1,3 +1,6 @@ +""" +This module provides methods for Login +""" import hashlib import string from datetime import datetime, timedelta @@ -7,15 +10,17 @@ path_prefix = '/var/www/wordfreq/wordfreq/' path_prefix = './' # comment this line in deployment def verify_pass(newpass,oldpass): - if(newpass==oldpass): + if newpass==oldpass: return True - def verify_user(username, password): rq = RecordQuery(path_prefix + 'static/wordfreqapp.db') password = md5(username + password) - rq.instructions_with_parameters("SELECT * FROM user WHERE name=:username AND password=:password", dict( - username=username, password=password)) # the named style https://docs.python.org/3/library/sqlite3.html + rq.instructions_with_parameters( + "SELECT * FROM user WHERE name=:username AND password=:password", + dict( # the named style https://docs.python.org/3/library/sqlite3.html + username=username, + password=password)) rq.do_with_parameters() result = rq.get_results() return result != [] @@ -23,12 +28,19 @@ def verify_user(username, password): def add_user(username, password): start_date = datetime.now().strftime('%Y%m%d') - expiry_date = (datetime.now() + timedelta(days=30)).strftime('%Y%m%d') # will expire after 30 days + # will expire after 30 days + expiry_date = (datetime.now() + timedelta(days=30)).strftime('%Y%m%d') # 将用户名和密码一起加密,以免暴露不同用户的相同密码 password = md5(username + password) rq = InsertQuery(path_prefix + 'static/wordfreqapp.db') - rq.instructions_with_parameters("INSERT INTO user VALUES (:username, :password, :start_date, :expiry_date)", dict( - username=username, password=password, start_date=start_date, expiry_date=expiry_date)) + rq.instructions_with_parameters( + "INSERT INTO user VALUES (:username, :password, :start_date, :expiry_date)", + dict( + username=username, + password=password, + start_date=start_date, + expiry_date=expiry_date + )) rq.do_with_parameters() @@ -96,7 +108,7 @@ class UserName: if ' ' in self.username: # a user name must not include a whitespace return 'Whitespace is not allowed in the user name.' for c in self.username: # a user name must not include special characters, except non-leading periods or underscores - if c in string.punctuation and c is not '.' and c is not '_': + if c in string.punctuation and c != '.' and c != '_': return f'{c} is not allowed in the user name.' if self.username in ['signup', 'login', 'logout', 'reset', 'mark', 'back', 'unfamiliar', 'familiar', 'del']: return 'You used a restricted word as your user name. Please come up with a better one.' @@ -110,4 +122,3 @@ class WarningMessage: def __str__(self): return UserName(self.s).validate() - diff --git a/app/WordFreq.py b/app/WordFreq.py index 3620a41..766ecaf 100644 --- a/app/WordFreq.py +++ b/app/WordFreq.py @@ -2,9 +2,12 @@ # Copyright 2019 (C) Hui Lan恭喜,你已成功注册, 你的用户名是 %s。
\ - ' % (username, username, username) - else: - return '用户名密码验证失败。' - + add_user(username, password)# 添加账户信息 + verified = verify_user(username, password) + if verified: + # 写入session + session['logged_in'] = True + session[username] = username + session['username'] = username + session['expiry_date'] = get_expiry_date(username) + session['articleID'] = None + return '恭喜,你已成功注册, 你的用户名是 %s。
\ + ' % (username, username, username) + return '用户名密码验证失败。' @accountService.route("/login", methods=['GET', 'POST']) @@ -62,11 +62,10 @@ def login(): if not session.get('logged_in'): # 未登录,返回登录页面 return render_template('login.html') - else: - # 已登录,提示信息并显示登出按钮 - return '你已登录 %s。 登出点击这里。' % ( + # 已登录,提示信息并显示登出按钮 + return '你已登录 %s。 登出点击这里。' % ( session['username'], session['username']) - elif request.method == 'POST': + if request.method == 'POST': # POST方法用于判断登录是否成功 # check database and verify user username = escape(request.form['username']) @@ -81,8 +80,7 @@ def login(): session['expiry_date'] = user_expiry_date session['articleID'] = None return redirect(url_for('user_bp.userpage', username=username)) - else: - return '无法通过验证。' + return '无法通过验证。' @accountService.route("/logout", methods=['GET', 'POST']) @@ -111,21 +109,18 @@ def reset(): if request.method == 'GET': # GET请求返回修改密码页面 return render_template('reset.html', username=session['username'], state='wait') - else: - # POST请求用于提交修改后信息 - old_password = escape(request.form['old-password']) - new_password = escape(request.form['new-password']) - - re_new_password = escape(request.form['re-new-password']) # 确认新密码 - if re_new_password != new_password: #验证新密码两次输入是否相同 - return '新密码不匹配,请重新输入' - if len(new_password) < 4: #验证新密码长度,原则参照注册模块 - return '密码过于简单。(密码长度至少4位)' - - flag = change_password(username, old_password, new_password) # flag表示是否修改成功 - if flag: - session['logged_in'] = False - return \ + # POST请求用于提交修改后信息 + old_password = escape(request.form['old-password']) + new_password = escape(request.form['new-password']) + re_new_password = escape(request.form['re-new-password']) # 确认新密码 + if re_new_password != new_password: #验证新密码两次输入是否相同 + return '新密码不匹配,请重新输入' + if len(new_password) < 4: #验证新密码长度,原则参照注册模块 + return '密码过于简单。(密码长度至少4位)' + flag = change_password(username, old_password, new_password) # flag表示是否修改成功 + if flag: + session['logged_in'] = False + return \ '''