Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main.py: Login error message bug fix, page not found error handling a… #3

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ed991f9
main.py: Login error message bug fix, page not found error handling a…
GaoxingAbdullah May 21, 2021
9b7b527
Update main.py
vicky-ZhuWenqi May 31, 2021
643e0c4
Update pickle_idea.py
vicky-ZhuWenqi May 31, 2021
d7ca484
添加README.md文件
vicky-ZhuWenqi Jun 2, 2021
c88a5dd
上传效果演示视频
vicky-ZhuWenqi Jun 2, 2021
3b6c5bf
main.py: refactor main page and added its mainpage.html file
GaoxingAbdullah Jun 7, 2021
9e59a45
Created Jenkinsfile and Dockerfile.
Jun 8, 2021
760d2d5
README.md: add README author name.
Jun 8, 2021
68e8102
Add app/static/wordfreqapp.sql for reconstructing the database.
Jun 8, 2021
f4a6921
app/test/test_add_word_and_essay_does_not_change.py: remove unnecessa…
Jun 8, 2021
3f0006f
Merge pull request #5 from lanlab-org/SPM-Spring2021-2600-朱文琦20183690…
spm2020spring Jun 8, 2021
783a0b2
app/test/test_next_essay.py: correct the variable name 'differ'.
Jun 9, 2021
3cc5105
Jenkinsfile: generate an HTML test report.
Jun 9, 2021
3056cb8
Jenkinsfile: automatic deployment has not been implemented yet.
Jun 9, 2021
a6328be
Merge branch 'master' of https://github.com/lanlab-org/EnglishPal
Jun 9, 2021
24ffed7
Jenkinsfile: correct command line argument.
Jun 9, 2021
3ef96ae
Jenkinsfile: correct command line argument 2.
Jun 9, 2021
90ab0be
app/test/test_next_essay.py: click Next 5 times.
Jun 9, 2021
d02da7b
Merge pull request #7 from lanlab-org/Lanhui-update-test_next_essay
spm2020spring Jun 9, 2021
71c39b7
Create Jenkinsfile
kpodjison Jun 11, 2021
2e62daf
Delete Jenkinsfile
GaoxingAbdullah Jun 20, 2021
cdf183f
main.py: Login error message bug fix, page not found error handling a…
GaoxingAbdullah May 21, 2021
5d19c22
main.py: refactor main page and added its mainpage.html file
GaoxingAbdullah Jun 7, 2021
87cf3b6
update master branch
GaoxingAbdullah Jun 20, 2021
7deef27
update master branch
GaoxingAbdullah Jun 20, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ app/static/img/
app/static/frequency/frequency_*.pickle
app/static/frequency/frequency.p
app/static/wordfreqapp.db
app/static/wordfreqapp.sql
app/static/donate-the-author.jpg
app/static/donate-the-author-hidden.jpg
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM tiangolo/uwsgi-nginx-flask:python3.6
COPY ./app /app
34 changes: 34 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
pipeline {
agent any

stages {
stage('MakeDatabasefile') {
steps {
sh 'touch ./app/static/wordfreqapp.db && rm -f ./app/static/wordfreqapp.db'
sh 'cat ./app/static/wordfreqapp.sql | sqlite3 ./app/static/wordfreqapp.db'
}
}
stage('BuildIt') {
steps {
echo 'Building..'
sh 'sudo docker build -t englishpal .'
sh 'sudo docker stop $(docker ps -aq)'
sh 'sudo docker run -d -p 91:80 -v /var/lib/jenkins/workspace/EnglishPal_Pipeline_master/app/static/frequency:/app/static/frequency -t englishpal'
}
}
stage('TestIt') {
steps {
echo 'Testing..'
sh 'sudo docker run -d -p 4444:4444 selenium/standalone-chrome'
sh 'pip3 install pytest -U -q'
sh 'pip3 install selenium -U -q'
sh 'pytest -v -s --html=EnglishPalTestReport.html ./app/test'
}
}
stage('DeployIt') {
steps {
echo 'Deploying (TBD)'
}
}
}
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
在生词簿每个单词后面,加上两个按钮,熟悉与不熟悉:
1.如果点熟悉,就将生词簿中该单词后面记录的添加次数减一,直至减为0,就将该单词从生词簿中移除。
2.如果点不熟悉,就将生词簿中该单词后面记录的添加次数加一。

- 朱文绮

77 changes: 41 additions & 36 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_random_image(path):

def get_random_ads():
ads = random.choice(['个性化分析精准提升', '你的专有单词本', '智能捕捉阅读弱点,针对性提高你的阅读水平'])
return ads + '。 <a href="/signup">试试</a>吧!'
return ads

def load_freq_history(path):
d = {}
Expand Down Expand Up @@ -75,7 +75,7 @@ def within_range(x, y, r):


def get_today_article(user_word_list, articleID):

rq = RecordQuery(path_prefix + 'static/wordfreqapp.db')
if articleID == None:
rq.instructions("SELECT * FROM article")
Expand All @@ -102,7 +102,6 @@ def get_today_article(user_word_list, articleID):
if within_range(text_level, user_level, 0.5):
d = reading
break

s = '<p><i>According to your word list, your level is <b>%4.2f</b> and we have chosen an article with a difficulty level of <b>%4.2f</b> for you.</i></p>' % (user_level, text_level)
s += '<p><b>%s</b></p>' % (d['date'])
s += '<p><font size=+2>%s</font></p>' % (d['text'])
Expand Down Expand Up @@ -218,39 +217,25 @@ def mainpage():

return page
elif request.method == 'GET': # when we load a html page
page = '''
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=3.0, user-scalable=yes" />
<title>EnglishPal 英文单词高效记</title>

</head>
<body>
'''
page += '<p><b><font size="+3" color="red">English Pal - Learn English in a smart way!</font></b></p>'
user = ''
youdao = {}
getWord = {}
getwordNum = {}
if session.get('logged_in'):
page += ' <a href="%s">%s</a></p>\n' % (session['username'], session['username'])
else:
page += '<p><a href="/login">登录</a> <a href="/signup">成为会员</a> <a href="/static/usr/instructions.html">使用说明</a></p>\n'
#page += '<p><img src="%s" width="400px" alt="advertisement"/></p>' % (get_random_image(path_prefix + 'static/img/'))
page += '<p><b>%s</b></p>' % (get_random_ads())
page += '<p>粘帖1篇文章 (English only)</p>'
page += '<form method="post" action="/">'
page += ' <textarea name="content" rows="10" cols="120"></textarea><br/>'
page += ' <input type="submit" value="get文章中的词频"/>'
page += ' <input type="reset" value="清除"/>'
page += '</form>'
user = session['username']
random_ads = get_random_ads()
d = load_freq_history(path_prefix + 'static/frequency/frequency.p')
word = 1
if len(d) > 0:
page += '<p><b>最常见的词</b></p>'
for x in sort_in_descending_order(pickle_idea.dict2lst(d)):
if x[1] <= 99:
break
page += '<a href="%s">%s</a> %d\n' % (youdao_link(x[0]), x[0], x[1])

page += '</body></html>'
return page
youdao[word] = youdao_link(x[0])
getWord[word] = x[0]
getwordNum[word] = x[1]
word = word + 1

return render_template('mainpage.html', user=user, random_ads=random_ads, youdao=youdao, getWord=getWord, getwordNum=getwordNum, word=word)


@app.route("/<username>/mark", methods=['GET', 'POST'])
Expand All @@ -270,6 +255,17 @@ def user_mark_word(username):
return 'Under construction'


@app.route("/<username>/<word>/unfamiliar", methods=['GET', 'POST'])
def unfamiliar(username,word):
user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
pickle_idea.unfamiliar(user_freq_record,word)
return redirect(url_for('userpage', username=username))

@app.route("/<username>/<word>/familiar", methods=['GET', 'POST'])
def familiar(username,word):
user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
pickle_idea.familiar(user_freq_record,word)
return redirect(url_for('userpage', username=username))

@app.route("/<username>", methods=['GET', 'POST'])
def userpage(username):
Expand Down Expand Up @@ -345,13 +341,11 @@ def userpage(username):
freq = x[1]
if isinstance(d[word], list): # d[word] is a list of dates
if freq > 1:
page += '<p class="new-word"> <a href="%s">%s</a> (<a title="%s">%d</a>) </p>\n' % (youdao_link(word), word, '; '.join(d[word]), freq)
page += '<p class="new-word"> <a href="%s">%s</a>(<a title="%s">%d</a>) <a href="%s/%s/familiar">熟悉</a> <a href="%s/%s/unfamiliar">不熟悉</a> </p>\n' % (youdao_link(word), word, '; '.join(d[word]), freq,username, word,username,word)
else:
page += '<p class="new-word"> <a href="%s">%s</a> <font color="white">(<a title="%s">%d</a>)</font> </p>\n' % (youdao_link(word), word, '; '.join(d[word]), freq)
page += '<p class="new-word"> <a href="%s">%s</a>(<a title="%s">%d</a>) <a href="%s/%s/familiar">熟悉</a> <a href="%s/%s/unfamiliar">不熟悉</a> </p>\n' % (youdao_link(word), word, '; '.join(d[word]), freq,username, word,username,word)
elif isinstance(d[word], int): # d[word] is a frequency. to migrate from old format.
page += '<a href="%s">%s</a>%d\n' % (youdao_link(word), word, freq)


page += '<a href="%s">%s</a>%d\n' % (youdao_link(word), word, freq)
return page

### Sign-up, login, logout ###
Expand Down Expand Up @@ -403,7 +397,18 @@ def login():
session['articleID'] = None
return redirect(url_for('userpage', username=username))
else:
return '无法通过验证。'
error = "无法通过验证"
return render_template('login.html', error=error)

#error page handling
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404

#internal server error handling
@app.errorhandler(500)
def internal_server_error(e):
return render_template('500.html'), 500


@app.route("/logout", methods=['GET', 'POST'])
Expand Down
18 changes: 17 additions & 1 deletion app/pickle_idea.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# Task: incorporate the functions into wordfreqCMD.py such that it will also show cumulative frequency.

import pickle
from datetime import datetime


def lst2dict(lst, d):
Expand Down Expand Up @@ -53,7 +54,22 @@ def save_frequency_to_pickle(d, pickle_fname):
pickle.dump(d2, f)
f.close()


def unfamiliar(path,word):
f = open(path,"rb")
dic = pickle.load(f)
dic[word] += [datetime.now().strftime('%Y%m%d%H%M')]
fp = open(path,"wb")
pickle.dump(dic,fp)

def familiar(path,word):
f = open(path,"rb")
dic = pickle.load(f)
if len(dic[word])>1:
del dic[word][0]
else:
dic.pop(word)
fp = open(path,"wb")
pickle.dump(dic,fp)

if __name__ == '__main__':

Expand Down
106 changes: 106 additions & 0 deletions app/static/wordfreqapp.sql

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions app/templates/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>English Pal - Page not found </title>
</head>
<body>
<h1 style="color: red; text-align: center;">English Pal - Learn English in a smart way!</h1>
<hr>
<p style="font-size: 25px; text-align: center;">Page Not Found </p>
<P style="text-align: center;"><a href="/">Home page </a></P>
</body>
</html>
15 changes: 15 additions & 0 deletions app/templates/500.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>English Pal - Internal server error</title>
</head>
<body>
<h1 style="color: red; text-align: center;">English Pal - Learn English in a smart way!</h1>
<hr>
<p style="font-size: 25px; text-align: center;">Internal server error</p>
<P><a href="/">Home page </a></P>
</body>
</html>
4 changes: 4 additions & 0 deletions app/templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
<form action="/login" method="POST">
<p><input type="username" name="username" placeholder="邮箱地址、电话号码"></p>
<p><input type="password" name="password" placeholder="密码"></p>
<!--Login error message -->
{% if error %}
<div style="color: red;">{{ error }} </div>
{% endif %}
<p><input type="submit" value="登录"></p>
</form>
{% endif %}
Expand Down
30 changes: 30 additions & 0 deletions app/templates/mainpage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>EnglishPal 英文单词高效记</title>
</head>
<body>
<p><b><font size="+3" color="red">English Pal - Learn English in a smart way!</font></b></p>
{% if username %}
<a href="user">user</a>
{% else %}
<p><a href="/login">登录</a> | <a href="/signup">成为会员</a> | <a href="/static/usr/instructions.html">使用说明</a></p>
{% endif %}
<p><b>{{random_ads}}. <a href="/signup">试试</a> 吧!</b></p>
<p>粘帖1篇文章 (English only)</p>
<form method="post" action="/">
<textarea name="content" rows="10" cols="120"></textarea><br/>
<input type="submit" value="get文章中的词频"/>
<input type="reset" value="清除"/>
</form>
<p><b>最常见的词</b></p>
<div style="margin-left: 5px; margin-right: 5px; padding: 5px;">
{% for i in range(word) %}
<a style="padding: 5px;" href="{{youdao[i]}}">{{getWord[i]}}</a> {{getwordNum[i]}}
{% endfor %}
</div>
</body>
</html>
10 changes: 0 additions & 10 deletions app/test/test_add_word_and_essay_does_not_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,5 @@ def test_add_word_and_essay_does_not_change():
index = current_essay_content.find('for you.')
assert current_essay_content[index:] == essay_content[index:]

# click the Next button. Now the essay should change.
elem = driver.find_element_by_link_text('下一篇') # 找到get所有词频按钮
elem.click()

# compare again
driver.save_screenshot('./app/test/test_add_word_and_essay_does_not_change_pic2.png')
elem = driver.find_element_by_id('text-content')
next_essay_content = elem.text

assert current_essay_content[index:] != next_essay_content[index:]
finally:
driver.quit()
4 changes: 2 additions & 2 deletions app/test/test_next_essay.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def test_next():
essay_content = elem.text

# click Next
differ = 0
for i in range(3):
diff = 0
for i in range(5):
elem = driver.find_element_by_link_text('下一篇')
elem.click()
driver.save_screenshot('./app/test/test_next_essay_pic1.png')
Expand Down
Binary file added 效果演示.mp4
Binary file not shown.