Fix bug 536
							parent
							
								
									8cbc7c9a0c
								
							
						
					
					
						commit
						ff6e20cabf
					
				| 
						 | 
				
			
			@ -2,12 +2,20 @@
 | 
			
		|||
venv/
 | 
			
		||||
app/__init__.py
 | 
			
		||||
app/__pycache__/
 | 
			
		||||
.DS_Store
 | 
			
		||||
app/.DS_Store
 | 
			
		||||
app/sqlite_commands.py
 | 
			
		||||
app/static/usr/*.jpg
 | 
			
		||||
app/static/img/
 | 
			
		||||
app/static/frequency/frequency_*.pickle
 | 
			
		||||
app/static/frequency/frequency.p
 | 
			
		||||
app/static/wordfreqapp.db
 | 
			
		||||
app/wordfreqapp.db
 | 
			
		||||
app/db/wordfreqapp.db
 | 
			
		||||
app/static/donate-the-author.jpg
 | 
			
		||||
app/static/donate-the-author-hidden.jpg
 | 
			
		||||
app/model/__pycache__/
 | 
			
		||||
app/model/__pycache__/
 | 
			
		||||
app/test/__pycache__/
 | 
			
		||||
app/test/.pytest_cache/
 | 
			
		||||
app/test/pytest_report.html
 | 
			
		||||
app/test/assets
 | 
			
		||||
app/log.txt
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,4 +1,5 @@
 | 
			
		|||
FROM tiangolo/uwsgi-nginx-flask:python3.6
 | 
			
		||||
COPY requirements.txt /app
 | 
			
		||||
RUN pip3 install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
 | 
			
		||||
COPY ./app /app
 | 
			
		||||
FROM tiangolo/uwsgi-nginx-flask:python3.8-alpine
 | 
			
		||||
COPY requirements.txt /tmp
 | 
			
		||||
COPY ./app/ /app/
 | 
			
		||||
RUN pip3 install -U pip -i https://mirrors.aliyun.com/pypi/simple/
 | 
			
		||||
RUN pip3 install -r /tmp/requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -10,8 +10,8 @@ pipeline {
 | 
			
		|||
    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'
 | 
			
		||||
	        sh 'touch ./app/wordfreqapp.db && rm -f ./app/wordfreqapp.db' 
 | 
			
		||||
	        sh 'cat ./app/static/wordfreqapp.sql | sqlite3 ./app/wordfreqapp.db'
 | 
			
		||||
	    }
 | 
			
		||||
	}
 | 
			
		||||
        stage('BuildIt') {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										30
									
								
								README.md
								
								
								
								
							
							
						
						
									
										30
									
								
								README.md
								
								
								
								
							| 
						 | 
				
			
			@ -61,15 +61,15 @@ My steps for deploying English on a Ubuntu server.
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
All articles are stored in the `article` table in a SQLite file called
 | 
			
		||||
`app/static/wordfreqapp.db`.
 | 
			
		||||
`app/db/wordfreqapp.db`.
 | 
			
		||||
 | 
			
		||||
### Adding new articles
 | 
			
		||||
 | 
			
		||||
To add articles, open and edit `app/static/wordfreqapp.db` using DB Browser for SQLite (https://sqlitebrowser.org).
 | 
			
		||||
To add articles, open and edit `app/db/wordfreqapp.db` using DB Browser for SQLite (https://sqlitebrowser.org).
 | 
			
		||||
 | 
			
		||||
### Extending an account's expiry date
 | 
			
		||||
 | 
			
		||||
By default, an account's expiry is 30 days after first sign-up.  To extend account's expiry date, open and edit `user` table in `app/static/wordfreqapp.db`.  Simply update field `expiry_date`.
 | 
			
		||||
By default, an account's expiry is 30 days after first sign-up.  To extend account's expiry date, open and edit `user` table in `app/db/wordfreqapp.db`.  Simply update field `expiry_date`.
 | 
			
		||||
 | 
			
		||||
### Exporting the database
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -95,7 +95,7 @@ sqlite3 wordfreqapp.db`.  Delete wordfreqapp.db first if it exists.
 | 
			
		|||
### Uploading wordfreqapp.db to the server
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
`pscp wordfreqapp.db lanhui@118.*.*.118:/home/lanhui/englishpal2/EnglishPal/app/static`
 | 
			
		||||
`pscp wordfreqapp.db lanhui@118.*.*.118:/home/lanhui/englishpal2/EnglishPal/app/db/`
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -129,6 +129,28 @@ We welcome feedback on EnglishPal.  Feedback examples:
 | 
			
		|||
EnglishPal's bugs and improvement suggestions are recorded in [Bugzilla](http://118.25.96.118/bugzilla/buglist.cgi?bug_status=__all__&list_id=1302&order=Importance&product=EnglishPal&query_format=specific).  Send (lanhui at zjnu.edu.cn) an email message for opening a Bugzilla account or reporting a bug.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## End-to-end testing
 | 
			
		||||
 | 
			
		||||
We use the Selenium test framework to test our app.
 | 
			
		||||
 | 
			
		||||
In order to run the test, first we need to download a webdriver executable.
 | 
			
		||||
 | 
			
		||||
Microsoft Edge's webdriver can be downloaded from [microsoft-edge-tools-webdriver](https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/). Make sure the version we download matches the version of the web browser installed on our laptop.
 | 
			
		||||
 | 
			
		||||
After extracting the downloaded zip file (e.g., edgedriver_win64.zip), rename msedgedriver.exe to MicrosoftWebDriver.exe.
 | 
			
		||||
 | 
			
		||||
Add MicrosoftWebDriver.exe's path to system's PATH variable.
 | 
			
		||||
 | 
			
		||||
Install the following dependencies too:
 | 
			
		||||
 | 
			
		||||
- pip install -U selenium==3.141.0
 | 
			
		||||
- pip install -U urllib3==1.26.2
 | 
			
		||||
 | 
			
		||||
Run English Pal first, then run the test using pytest as follows: pytest --html=pytest_report.html test_add_word.py
 | 
			
		||||
 | 
			
		||||
The above command will generate a HTML report file pytest_report.html after finishing executing test_add_word.py.  Note: you need to install pytest-html package first: pip install pytest-html.
 | 
			
		||||
 | 
			
		||||
You may also want to use [webdriver-manager](https://pypi.org/project/webdriver-manager/) from PyPI, so that you can avoid tediously installing a web driver executable manually.  However, my experience shows that webdriver-manager is too slow.  For example, it took me 16 minutes to run 9 tests, while with the pre-installed web driver executable, it took less than 2 minutes.
 | 
			
		||||
 | 
			
		||||
## TODO
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,5 @@
 | 
			
		|||
from WordFreq import WordFreq
 | 
			
		||||
from wordfreqCMD import youdao_link, sort_in_descending_order
 | 
			
		||||
from UseSqlite import InsertQuery, RecordQuery
 | 
			
		||||
import pickle_idea, pickle_idea2
 | 
			
		||||
import os
 | 
			
		||||
import random, glob
 | 
			
		||||
| 
						 | 
				
			
			@ -8,18 +7,15 @@ import hashlib
 | 
			
		|||
from datetime import datetime
 | 
			
		||||
from flask import Flask, request, redirect, render_template, url_for, session, abort, flash, get_flashed_messages
 | 
			
		||||
from difficulty import get_difficulty_level_for_user, text_difficulty_level, user_difficulty_level
 | 
			
		||||
from model.article import get_all_articles, get_article_by_id, get_number_of_articles
 | 
			
		||||
import logging
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
path_prefix = '/var/www/wordfreq/wordfreq/'
 | 
			
		||||
path_prefix = './'  # comment this line in deployment
 | 
			
		||||
path_prefix = './'
 | 
			
		||||
db_path_prefix = './db/'  # comment this line in deployment
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def total_number_of_essays():
 | 
			
		||||
    rq = RecordQuery(path_prefix + 'static/wordfreqapp.db')
 | 
			
		||||
    rq.instructions("SELECT * FROM article")
 | 
			
		||||
    rq.do()
 | 
			
		||||
    result = rq.get_results()
 | 
			
		||||
    return len(result)
 | 
			
		||||
    return get_number_of_articles()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_article_title(s):
 | 
			
		||||
| 
						 | 
				
			
			@ -33,32 +29,36 @@ def get_article_body(s):
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
def get_today_article(user_word_list, visited_articles):
 | 
			
		||||
    rq = RecordQuery(path_prefix + 'static/wordfreqapp.db')
 | 
			
		||||
    if visited_articles is None:
 | 
			
		||||
        visited_articles = {
 | 
			
		||||
            "index" : 0,  # 为 article_ids 的索引
 | 
			
		||||
            "article_ids": []  # 之前显示文章的id列表,越后越新
 | 
			
		||||
        }
 | 
			
		||||
    if visited_articles["index"] > len(visited_articles["article_ids"])-1:  # 生成新的文章,因此查找所有的文章
 | 
			
		||||
        rq.instructions("SELECT * FROM article")
 | 
			
		||||
        result = get_all_articles()
 | 
			
		||||
    else:  # 生成阅读过的文章,因此查询指定 article_id 的文章
 | 
			
		||||
        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()
 | 
			
		||||
        article_id = visited_articles["article_ids"][visited_articles["index"]]
 | 
			
		||||
        result = get_article_by_id(article_id)
 | 
			
		||||
    random.shuffle(result)
 | 
			
		||||
 | 
			
		||||
    # Choose article according to reader's level
 | 
			
		||||
    d1 = load_freq_history(path_prefix + 'static/frequency/frequency.p')
 | 
			
		||||
    logging.debug('* get_today_article(): start d1 = ... ')
 | 
			
		||||
    d1 = load_freq_history(user_word_list)
 | 
			
		||||
    d2 = load_freq_history(path_prefix + 'static/words_and_tests.p')
 | 
			
		||||
    logging.debug(' ... get_today_article(): get_difficulty_level_for_user() start')
 | 
			
		||||
    d3 = get_difficulty_level_for_user(d1, d2)
 | 
			
		||||
    logging.debug(' ... get_today_article(): done')
 | 
			
		||||
 | 
			
		||||
    d = None
 | 
			
		||||
    result_of_generate_article = "not found"
 | 
			
		||||
 | 
			
		||||
    d_user = load_freq_history(user_word_list)
 | 
			
		||||
    logging.debug('* get_today_article(): user_difficulty_level() start')
 | 
			
		||||
    user_level = user_difficulty_level(d_user, d3)  # more consideration as user's behaviour is dynamic. Time factor should be considered.
 | 
			
		||||
    logging.debug('* get_today_article(): done')
 | 
			
		||||
    text_level = 0
 | 
			
		||||
    if visited_articles["index"] > len(visited_articles["article_ids"])-1:  # 生成新的文章
 | 
			
		||||
        amount_of_visited_articles = len(visited_articles["article_ids"])
 | 
			
		||||
| 
						 | 
				
			
			@ -87,8 +87,8 @@ def get_today_article(user_word_list, visited_articles):
 | 
			
		|||
    today_article = None
 | 
			
		||||
    if d:
 | 
			
		||||
        today_article = {
 | 
			
		||||
            "user_level": '%4.2f' % user_level,
 | 
			
		||||
            "text_level": '%4.2f' % text_level,
 | 
			
		||||
            "user_level": '%4.1f' % user_level,
 | 
			
		||||
            "text_level": '%4.1f' % text_level,
 | 
			
		||||
            "date": d['date'],
 | 
			
		||||
            "article_title": get_article_title(d['text']),
 | 
			
		||||
            "article_body": get_article_body(d['text']),
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										62
									
								
								app/Login.py
								
								
								
								
							
							
						
						
									
										62
									
								
								app/Login.py
								
								
								
								
							| 
						 | 
				
			
			@ -1,8 +1,12 @@
 | 
			
		|||
import hashlib
 | 
			
		||||
import string
 | 
			
		||||
from datetime import datetime, timedelta
 | 
			
		||||
 | 
			
		||||
import unicodedata
 | 
			
		||||
 | 
			
		||||
from UseSqlite import InsertQuery, RecordQuery
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def md5(s):
 | 
			
		||||
    '''
 | 
			
		||||
    MD5摘要
 | 
			
		||||
| 
						 | 
				
			
			@ -12,14 +16,16 @@ def md5(s):
 | 
			
		|||
    h = hashlib.md5(s.encode(encoding='utf-8'))
 | 
			
		||||
    return h.hexdigest()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# import model.user after the defination of md5(s) to avoid circular import
 | 
			
		||||
from model.user import get_user_by_username, insert_user, update_password_by_username
 | 
			
		||||
 | 
			
		||||
path_prefix = '/var/www/wordfreq/wordfreq/'
 | 
			
		||||
path_prefix = './'  # comment this line in deployment
 | 
			
		||||
 | 
			
		||||
def verify_pass(newpass,oldpass):
 | 
			
		||||
    if(newpass==oldpass):
 | 
			
		||||
 | 
			
		||||
def verify_pass(newpass, oldpass):
 | 
			
		||||
    if (newpass == oldpass):
 | 
			
		||||
        return True
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -31,7 +37,7 @@ 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
 | 
			
		||||
    expiry_date = (datetime.now() + timedelta(days=30)).strftime('%Y%m%d')  # will expire after 30 days
 | 
			
		||||
    # 将用户名和密码一起加密,以免暴露不同用户的相同密码
 | 
			
		||||
    password = md5(username + password)
 | 
			
		||||
    insert_user(username=username, password=password, start_date=start_date, expiry_date=expiry_date)
 | 
			
		||||
| 
						 | 
				
			
			@ -53,7 +59,7 @@ def change_password(username, old_password, new_password):
 | 
			
		|||
    if not verify_user(username, old_password):  # 旧密码错误
 | 
			
		||||
        return False
 | 
			
		||||
    # 将用户名和密码一起加密,以免暴露不同用户的相同密码
 | 
			
		||||
    if verify_pass(new_password,old_password): #新旧密码一致
 | 
			
		||||
    if verify_pass(new_password, old_password):  #新旧密码一致
 | 
			
		||||
        return False
 | 
			
		||||
    update_password_by_username(username, new_password)
 | 
			
		||||
    return True
 | 
			
		||||
| 
						 | 
				
			
			@ -66,30 +72,64 @@ def get_expiry_date(username):
 | 
			
		|||
    else:
 | 
			
		||||
        return user.expiry_date
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class UserName:
 | 
			
		||||
    def __init__(self, username):
 | 
			
		||||
        self.username = username
 | 
			
		||||
 | 
			
		||||
    def contains_chinese(self):
 | 
			
		||||
        for char in self.username:
 | 
			
		||||
            # Check if the character is in the CJK (Chinese, Japanese, Korean) Unicode block
 | 
			
		||||
            if unicodedata.name(char).startswith('CJK UNIFIED IDEOGRAPH'):
 | 
			
		||||
                return True
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
    def validate(self):
 | 
			
		||||
        if len(self.username) > 20:
 | 
			
		||||
            return f'{self.username} is too long.  The user name cannot exceed 20 characters.'
 | 
			
		||||
        if self.username.startswith('.'): # a user name must not start with a dot
 | 
			
		||||
        if self.username.startswith('.'):  # a user name must not start with a dot
 | 
			
		||||
            return 'Period (.) is not allowed as the first letter in the user name.'
 | 
			
		||||
        if ' ' in self.username: # a user name must not include a whitespace
 | 
			
		||||
        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
 | 
			
		||||
        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 != '.' 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', 'admin']:
 | 
			
		||||
        if self.username in ['signup', 'login', 'logout', 'reset', 'mark', 'back', 'unfamiliar', 'familiar', 'del',
 | 
			
		||||
                             'admin']:
 | 
			
		||||
            return 'You used a restricted word as your user name.  Please come up with a better one.'
 | 
			
		||||
        if self.contains_chinese():
 | 
			
		||||
            return 'Chinese characters are not allowed in the user name.'
 | 
			
		||||
        return 'OK'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Password:
 | 
			
		||||
    def __init__(self, password):
 | 
			
		||||
        self.password = password
 | 
			
		||||
 | 
			
		||||
    def contains_chinese(self):
 | 
			
		||||
        for char in self.password:
 | 
			
		||||
            # Check if the character is in the CJK (Chinese, Japanese, Korean) Unicode block
 | 
			
		||||
            if unicodedata.name(char).startswith('CJK UNIFIED IDEOGRAPH'):
 | 
			
		||||
                return True
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
    def validate(self):
 | 
			
		||||
        if len(self.password) < 4:
 | 
			
		||||
            return 'Password must be at least 4 characters long.'
 | 
			
		||||
        if ' ' in self.password:
 | 
			
		||||
            return 'Password cannot contain spaces.'
 | 
			
		||||
        if self.contains_chinese():
 | 
			
		||||
            return 'Chinese characters are not allowed in the password.'
 | 
			
		||||
        return 'OK'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class WarningMessage:
 | 
			
		||||
    def __init__(self, s):
 | 
			
		||||
    def __init__(self, s, type='username'):
 | 
			
		||||
        self.s = s
 | 
			
		||||
        self.type = type
 | 
			
		||||
 | 
			
		||||
    def __str__(self):
 | 
			
		||||
        return UserName(self.s).validate()
 | 
			
		||||
 | 
			
		||||
        if self.type == 'username':
 | 
			
		||||
            return UserName(self.s).validate()
 | 
			
		||||
        if self.type == 'password':
 | 
			
		||||
            return Password(self.s).validate()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,10 +1,10 @@
 | 
			
		|||
from flask import *
 | 
			
		||||
from Login import check_username_availability, verify_user, add_user, get_expiry_date, change_password, WarningMessage
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# 初始化蓝图
 | 
			
		||||
accountService = Blueprint("accountService", __name__)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### Sign-up, login, logout ###
 | 
			
		||||
@accountService.route("/signup", methods=['GET', 'POST'])
 | 
			
		||||
def signup():
 | 
			
		||||
| 
						 | 
				
			
			@ -19,16 +19,20 @@ def signup():
 | 
			
		|||
        # POST方法需判断是否注册成功,再根据结果返回不同的内容
 | 
			
		||||
        username = escape(request.form['username'])
 | 
			
		||||
        password = escape(request.form['password'])
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        #! 添加如下代码为了过滤注册时的非法字符
 | 
			
		||||
        warn = WarningMessage(username)
 | 
			
		||||
        warn = WarningMessage(username, type='username')
 | 
			
		||||
        if str(warn) != 'OK':
 | 
			
		||||
            return jsonify({'status': '3', 'warn': str(warn)})
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        warn = WarningMessage(password, type='password')
 | 
			
		||||
        if str(warn) != 'OK':
 | 
			
		||||
            return jsonify({'status': '3', 'warn': str(warn)})
 | 
			
		||||
 | 
			
		||||
        available = check_username_availability(username)
 | 
			
		||||
        if not available: # 用户名不可用
 | 
			
		||||
        if not available:  # 用户名不可用
 | 
			
		||||
            return jsonify({'status': '0'})
 | 
			
		||||
        else: # 添加账户信息
 | 
			
		||||
        else:  # 添加账户信息
 | 
			
		||||
            add_user(username, password)
 | 
			
		||||
            verified = verify_user(username, password)
 | 
			
		||||
            if verified:
 | 
			
		||||
| 
						 | 
				
			
			@ -43,7 +47,6 @@ def signup():
 | 
			
		|||
                return jsonify({'status': '1'})
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@accountService.route("/login", methods=['GET', 'POST'])
 | 
			
		||||
def login():
 | 
			
		||||
    '''
 | 
			
		||||
| 
						 | 
				
			
			@ -102,9 +105,9 @@ def reset():
 | 
			
		|||
        # POST请求用于提交修改后信息
 | 
			
		||||
        old_password = escape(request.form['old-password'])
 | 
			
		||||
        new_password = escape(request.form['new-password'])
 | 
			
		||||
        flag = change_password(username, old_password, new_password) # flag表示是否修改成功
 | 
			
		||||
        flag = change_password(username, old_password, new_password)  # flag表示是否修改成功
 | 
			
		||||
        if flag:
 | 
			
		||||
            session['logged_in'] = False
 | 
			
		||||
            return jsonify({'status':'1'})  # 修改成功
 | 
			
		||||
            return jsonify({'status': '1'})  # 修改成功
 | 
			
		||||
        else:
 | 
			
		||||
            return jsonify({'status':'2'})  # 修改失败
 | 
			
		||||
            return jsonify({'status': '2'})  # 修改失败
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,6 @@
 | 
			
		|||
# System Library
 | 
			
		||||
from flask import *
 | 
			
		||||
from markupsafe import escape
 | 
			
		||||
 | 
			
		||||
# Personal library
 | 
			
		||||
from Yaml import yml
 | 
			
		||||
| 
						 | 
				
			
			@ -37,6 +38,22 @@ def admin():
 | 
			
		|||
 | 
			
		||||
@adminService.route("/admin/article", methods=["GET", "POST"])
 | 
			
		||||
def article():
 | 
			
		||||
 | 
			
		||||
    def _make_title_and_content(article_lst):
 | 
			
		||||
        for article in article_lst:
 | 
			
		||||
            text = escape(article.text) # Fix XSS vulnerability, contributed by Xu Xuan
 | 
			
		||||
            article.title = text.split("\n")[0]
 | 
			
		||||
            article.content = '<br/>'.join(text.split("\n")[1:])
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def _update_context():
 | 
			
		||||
        article_len = get_number_of_articles()
 | 
			
		||||
        context["article_number"] = article_len
 | 
			
		||||
        context["text_list"] = get_page_articles(_cur_page, _page_size)
 | 
			
		||||
        _articles = get_page_articles(_cur_page, _page_size)
 | 
			
		||||
        _make_title_and_content(_articles)
 | 
			
		||||
        context["text_list"] = _articles
 | 
			
		||||
 | 
			
		||||
    global _cur_page, _page_size
 | 
			
		||||
 | 
			
		||||
    is_admin = check_is_admin()
 | 
			
		||||
| 
						 | 
				
			
			@ -44,20 +61,15 @@ def article():
 | 
			
		|||
        return is_admin
 | 
			
		||||
 | 
			
		||||
    _article_number = get_number_of_articles()
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        _page_size = min(
 | 
			
		||||
            max(1, int(request.args.get("size", 5))), _article_number
 | 
			
		||||
        )  # 最小的size是1
 | 
			
		||||
        _cur_page = min(
 | 
			
		||||
            max(1, int(request.args.get("page", 1))), _article_number // _page_size + (_article_number % _page_size > 0)
 | 
			
		||||
        )  # 最小的page是1
 | 
			
		||||
        _page_size = min(max(1, int(request.args.get("size", 5))), _article_number)  # 最小的size是1
 | 
			
		||||
        _cur_page = min(max(1, int(request.args.get("page", 1))), _article_number // _page_size + (_article_number % _page_size > 0))  # 最小的page是1
 | 
			
		||||
    except ValueError:
 | 
			
		||||
        return "page parmas must be int!"
 | 
			
		||||
    
 | 
			
		||||
        return "page parameters must be integer!"
 | 
			
		||||
 | 
			
		||||
    _articles = get_page_articles(_cur_page, _page_size)
 | 
			
		||||
    for article in _articles:   # 获取每篇文章的title
 | 
			
		||||
        article.title = article.text.split("\n")[0]
 | 
			
		||||
        article.content = '<br/>'.join(article.text.split("\n")[1:])
 | 
			
		||||
    _make_title_and_content(_articles)
 | 
			
		||||
    
 | 
			
		||||
    context = {
 | 
			
		||||
        "article_number": _article_number,
 | 
			
		||||
| 
						 | 
				
			
			@ -67,23 +79,16 @@ def article():
 | 
			
		|||
        "username": session.get("username"),
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    def _update_context():
 | 
			
		||||
        article_len = get_number_of_articles()
 | 
			
		||||
        context["article_number"] = article_len
 | 
			
		||||
        context["text_list"] = get_page_articles(_cur_page, _page_size)
 | 
			
		||||
        _articles = get_page_articles(_cur_page, _page_size)
 | 
			
		||||
        for article in _articles:   # 获取每篇文章的title
 | 
			
		||||
            article.title = article.text.split("\n")[0]
 | 
			
		||||
        context["text_list"] = _articles
 | 
			
		||||
 | 
			
		||||
    if request.method == "GET":
 | 
			
		||||
        try:
 | 
			
		||||
            delete_id = int(request.args.get("delete_id", 0))
 | 
			
		||||
        except:
 | 
			
		||||
            return "Delete article ID must be int!"
 | 
			
		||||
            return "Delete article ID must be integer!"
 | 
			
		||||
        if delete_id:  # delete article
 | 
			
		||||
            delete_article_by_id(delete_id)
 | 
			
		||||
            _update_context()
 | 
			
		||||
 | 
			
		||||
    elif request.method == "POST":
 | 
			
		||||
        data = request.form
 | 
			
		||||
        content = data.get("content", "")
 | 
			
		||||
| 
						 | 
				
			
			@ -97,6 +102,7 @@ def article():
 | 
			
		|||
            _update_context()
 | 
			
		||||
            title = content.split('\n')[0]
 | 
			
		||||
            flash(f'Article added. Title: {title}')
 | 
			
		||||
 | 
			
		||||
    return render_template("admin_manage_article.html", **context)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1 @@
 | 
			
		|||
Put wordfreqapp.db here
 | 
			
		||||
| 
						 | 
				
			
			@ -18,6 +18,7 @@ def load_record(pickle_fname):
 | 
			
		|||
    return d
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
ENGLISH_WORD_DIFFICULTY_DICT = {}
 | 
			
		||||
def convert_test_type_to_difficulty_level(d):
 | 
			
		||||
    """
 | 
			
		||||
    对原本的单词库中的单词进行难度评级
 | 
			
		||||
| 
						 | 
				
			
			@ -39,8 +40,10 @@ def convert_test_type_to_difficulty_level(d):
 | 
			
		|||
        elif 'BBC' in d[k]:
 | 
			
		||||
            result[k] = 8
 | 
			
		||||
 | 
			
		||||
    return result  # {'apple': 4, ...}
 | 
			
		||||
    global ENGLISH_WORD_DIFFICULTY_DICT
 | 
			
		||||
    ENGLISH_WORD_DIFFICULTY_DICT = result
 | 
			
		||||
 | 
			
		||||
    return result  # {'apple': 4, ...}
 | 
			
		||||
 | 
			
		||||
def get_difficulty_level_for_user(d1, d2):
 | 
			
		||||
    """
 | 
			
		||||
| 
						 | 
				
			
			@ -49,7 +52,11 @@ def get_difficulty_level_for_user(d1, d2):
 | 
			
		|||
    在d2的后面添加单词,没有新建一个新的字典
 | 
			
		||||
    """
 | 
			
		||||
    # TODO: convert_test_type_to_difficulty_level() should not be called every time.  Each word's difficulty level should be pre-computed.
 | 
			
		||||
    d2 = convert_test_type_to_difficulty_level(d2)  # 根据d2的标记评级{'apple': 4, 'abandon': 4, ...}
 | 
			
		||||
    if ENGLISH_WORD_DIFFICULTY_DICT == {}:
 | 
			
		||||
        d2 = convert_test_type_to_difficulty_level(d2)  # 根据d2的标记评级{'apple': 4, 'abandon': 4, ...}
 | 
			
		||||
    else:
 | 
			
		||||
        d2 = ENGLISH_WORD_DIFFICULTY_DICT
 | 
			
		||||
 | 
			
		||||
    stemmer = snowballstemmer.stemmer('english')
 | 
			
		||||
 | 
			
		||||
    for k in d1:  # 用户的词
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										11
									
								
								app/main.py
								
								
								
								
							
							
						
						
									
										11
									
								
								app/main.py
								
								
								
								
							| 
						 | 
				
			
			@ -1,19 +1,19 @@
 | 
			
		|||
#! /usr/bin/python3
 | 
			
		||||
# -*- coding: utf-8 -*-
 | 
			
		||||
 | 
			
		||||
###########################################################################
 | 
			
		||||
# Copyright 2019 (C) Hui Lan <hui.lan@cantab.net>
 | 
			
		||||
# Written permission must be obtained from the author for commercial uses.
 | 
			
		||||
###########################################################################
 | 
			
		||||
from flask import escape
 | 
			
		||||
from flask import abort
 | 
			
		||||
from markupsafe import escape
 | 
			
		||||
from Login import *
 | 
			
		||||
from Article import *
 | 
			
		||||
import Yaml
 | 
			
		||||
from user_service import userService
 | 
			
		||||
from account_service import accountService
 | 
			
		||||
from admin_service import adminService, ADMIN_NAME
 | 
			
		||||
import os
 | 
			
		||||
 | 
			
		||||
app = Flask(__name__)
 | 
			
		||||
app.secret_key = 'lunch.time!'
 | 
			
		||||
app.secret_key = os.urandom(32)
 | 
			
		||||
 | 
			
		||||
# 将蓝图注册到Lab app
 | 
			
		||||
app.register_blueprint(userService)
 | 
			
		||||
| 
						 | 
				
			
			@ -54,7 +54,6 @@ def appears_in_test(word, d):
 | 
			
		|||
    else:
 | 
			
		||||
        return ','.join(d[word])
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@app.route("/mark", methods=['GET', 'POST'])
 | 
			
		||||
def mark_word():
 | 
			
		||||
    '''
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,7 @@
 | 
			
		|||
from pony.orm import *
 | 
			
		||||
 | 
			
		||||
db = Database()
 | 
			
		||||
db.bind("sqlite", "../static/wordfreqapp.db", create_db=True)  # bind sqlite file
 | 
			
		||||
db.bind("sqlite", "../db/wordfreqapp.db", create_db=True)  # bind sqlite file
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class User(db.Entity):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,7 +7,7 @@ def add_article(content, source="manual_input", level="5", question="No question
 | 
			
		|||
        Article(
 | 
			
		||||
            text=content,
 | 
			
		||||
            source=source,
 | 
			
		||||
            date=datetime.now().strftime("%-d %b %Y"),  # format style of `5 Oct 2022`
 | 
			
		||||
            date=datetime.now().strftime("%d %b %Y"),  # format style of `5 Oct 2022`
 | 
			
		||||
            level=level,
 | 
			
		||||
            question=question,
 | 
			
		||||
        )
 | 
			
		||||
| 
						 | 
				
			
			@ -32,3 +32,17 @@ def get_page_articles(num, size):
 | 
			
		|||
            x
 | 
			
		||||
            for x in Article.select().order_by(desc(Article.article_id)).page(num, size)
 | 
			
		||||
        ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_all_articles():
 | 
			
		||||
    articles = []
 | 
			
		||||
    with db_session:
 | 
			
		||||
        for article in Article.select():
 | 
			
		||||
            articles.append(article.to_dict())
 | 
			
		||||
    return articles
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_article_by_id(article_id):
 | 
			
		||||
    with db_session:
 | 
			
		||||
        article = Article.get(article_id=article_id)
 | 
			
		||||
    return [article.to_dict()]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,13 +2,14 @@
 | 
			
		|||
css:
 | 
			
		||||
  item:
 | 
			
		||||
    - ../static/css/bootstrap.css
 | 
			
		||||
 | 
			
		||||
    - ../static/css/highlighted.css
 | 
			
		||||
# 全局引入的js文件地址
 | 
			
		||||
js:
 | 
			
		||||
  head: # 在页面加载之前加载
 | 
			
		||||
    - ../static/js/jquery.js
 | 
			
		||||
    - ../static/js/read.js
 | 
			
		||||
    - ../static/js/word_operation.js
 | 
			
		||||
    - ../static/js/checkboxes.js
 | 
			
		||||
  bottom: # 在页面加载完之后加载
 | 
			
		||||
    - ../static/js/fillword.js
 | 
			
		||||
    - ../static/js/highlight.js
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,5 @@
 | 
			
		|||
 | 
			
		||||
.highlighted {
 | 
			
		||||
    color: red;
 | 
			
		||||
    font-weight: normal;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,5 @@
 | 
			
		|||
function toggleCheckboxSelection(checkStatus) {
 | 
			
		||||
    // used in userpage_post.html
 | 
			
		||||
    const checkBoxes = document.getElementsByName('marked');
 | 
			
		||||
    checkBoxes.forEach((checkbox) => { checkbox.checked = checkStatus;} );
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
let isRead = true;
 | 
			
		||||
let isChoose = true;
 | 
			
		||||
let isRead = localStorage.getItem('readChecked') !== 'false';  // default to true
 | 
			
		||||
let isChoose = localStorage.getItem('chooseChecked') !== 'false';
 | 
			
		||||
 | 
			
		||||
function getWord() {
 | 
			
		||||
    return window.getSelection ? window.getSelection() : document.selection.createRange().text;
 | 
			
		||||
| 
						 | 
				
			
			@ -10,7 +10,9 @@ function fillInWord() {
 | 
			
		|||
    if (isRead) Reader.read(word, inputSlider.value);
 | 
			
		||||
    if (!isChoose) return;
 | 
			
		||||
    const element = document.getElementById("selected-words");
 | 
			
		||||
    localStorage.setItem('nowWords', element.value);
 | 
			
		||||
    element.value = element.value + " " + word;
 | 
			
		||||
    localStorage.setItem('selectedWords', element.value);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
document.getElementById("text-content").addEventListener("click", fillInWord, false);
 | 
			
		||||
| 
						 | 
				
			
			@ -24,8 +26,16 @@ inputSlider.oninput = () => {
 | 
			
		|||
 | 
			
		||||
function onReadClick() {
 | 
			
		||||
    isRead = !isRead;
 | 
			
		||||
    localStorage.setItem('readChecked', isRead);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function onChooseClick() {
 | 
			
		||||
    isChoose = !isChoose;
 | 
			
		||||
    localStorage.setItem('chooseChecked', isChoose);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 如果网页刷新,停止播放声音
 | 
			
		||||
if (performance.getEntriesByType("navigation")[0].type == "reload") {
 | 
			
		||||
    Reader.stopRead();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,4 +1,4 @@
 | 
			
		|||
let isHighlight = true;
 | 
			
		||||
let isHighlight = localStorage.getItem('highlightChecked') !== 'false'; // default to true
 | 
			
		||||
 | 
			
		||||
function cancelBtnHandler() {
 | 
			
		||||
    cancelHighlighting();
 | 
			
		||||
| 
						 | 
				
			
			@ -22,62 +22,46 @@ function getWord() {
 | 
			
		|||
 | 
			
		||||
function highLight() {
 | 
			
		||||
    if (!isHighlight) return;
 | 
			
		||||
    let articleContent = document.getElementById("article").innerText; //将原来的.innerText改为.innerHtml,使用innerText会把原文章中所包含的<br>标签去除,导致处理后的文章内容失去了原来的格式
 | 
			
		||||
    let word = (getWord() + "").trim();
 | 
			
		||||
    let articleContent = document.getElementById("article").innerHTML; // innerHTML保留HTML标签来保持部分格式,且适配不同的浏览器
 | 
			
		||||
    let pickedWords = document.getElementById("selected-words");  // words picked to the text area
 | 
			
		||||
    let dictionaryWords = document.getElementById("selected-words2"); // words appearing in the user's new words list
 | 
			
		||||
    let allWords = "";  //初始化allWords的值,避免进入判断后编译器认为allWords未初始化的问题
 | 
			
		||||
    if(dictionaryWords != null){//增加一个判断,检查生词本里面是否为空,如果为空,allWords只添加选中的单词
 | 
			
		||||
        allWords = pickedWords.value + " " + dictionaryWords.value;
 | 
			
		||||
    let allWords = dictionaryWords === null ? pickedWords.value + " " : pickedWords.value + " " + dictionaryWords.value;
 | 
			
		||||
    const list = allWords.split(" "); // 将所有的生词放入一个list中
 | 
			
		||||
    if(word !== null && word !== "" && word !== " "){
 | 
			
		||||
        let articleContent_fb2 = articleContent;
 | 
			
		||||
        if(localStorage.getItem("nowWords").indexOf(word) !== -1 || localStorage.getItem("nowWords").indexOf(word.toLowerCase()) !== -1){
 | 
			
		||||
            articleContent = articleContent.replace(new RegExp('<span class="highlighted">' + word + '</span>', "gi"), word);
 | 
			
		||||
            pickedWords.value = localStorage.getItem("nowWords").replace(word,"");
 | 
			
		||||
            document.getElementById("article").innerHTML = articleContent;
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    else{
 | 
			
		||||
        allWords = pickedWords.value + " ";
 | 
			
		||||
    }
 | 
			
		||||
    const list = allWords.split(" ");//将所有的生词放入一个list中,用于后续处理
 | 
			
		||||
    let totalSet = new Set();
 | 
			
		||||
    for (let i = 0; i < list.length; ++i) {
 | 
			
		||||
        list[i] = list[i].replace(/(^\s*)|(\s*$)/g, ""); //消除单词两边的空字符
 | 
			
		||||
        list[i] = list[i].replace(/(^\W*)|(\W*$)/g, ""); // 消除单词两边的非单词字符
 | 
			
		||||
        list[i] = list[i].replace('|', "");
 | 
			
		||||
        list[i] = list[i].replace('?', "");
 | 
			
		||||
        if (list[i] !== "" && "<mark>".indexOf(list[i]) === -1 && "</mark>".indexOf(list[i]) === -1) {
 | 
			
		||||
           //将文章中所有出现该单词word的地方改为:"<mark>" + word + "<mark>"。 正则表达式RegExp()中,"\\b"代表单词边界匹配。
 | 
			
		||||
 | 
			
		||||
            //修改代码
 | 
			
		||||
            let articleContent_fb = articleContent;  //文章副本
 | 
			
		||||
            while(articleContent_fb.toLowerCase().indexOf(list[i].toLowerCase()) !== -1 && list[i]!=""){
 | 
			
		||||
                //找到副本中和list[i]匹配的第一个单词(第一种匹配情况),并赋值给list[i]。
 | 
			
		||||
                const index = articleContent_fb.toLowerCase().indexOf(list[i].toLowerCase());
 | 
			
		||||
                list[i] = articleContent_fb.substring(index, index + list[i].length);
 | 
			
		||||
 | 
			
		||||
                articleContent_fb = articleContent_fb.substring(index + list[i].length);    // 使用副本中list[i]之后的子串替换掉副本
 | 
			
		||||
                articleContent = articleContent.replace(new RegExp("\\b"+list[i]+"\\b","g"),"<mark>" + list[i] + "</mark>");
 | 
			
		||||
            }
 | 
			
		||||
        if (list[i] != "" && !totalSet.has(list[i])) {
 | 
			
		||||
            // 返回所有匹配单词的集合, 正则表达式RegExp()中, "\b"匹配一个单词的边界, g 表示全局匹配, i 表示对大小写不敏感。
 | 
			
		||||
            let matches = new Set(articleContent.match(new RegExp("\\b" + list[i] + "\\b", "gi")));
 | 
			
		||||
            totalSet = new Set([...totalSet, ...matches]);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 删除所有的"<span class='highlighted'>"标签,防止标签发生嵌套
 | 
			
		||||
    articleContent = articleContent.replace(new RegExp('<span class="highlighted">',"gi"), "")
 | 
			
		||||
    articleContent = articleContent.replace(new RegExp("</span>","gi"), "");
 | 
			
		||||
    // 将文章中所有出现该单词word的地方改为:"<span class='highlighted'>" + word + "</span>"。
 | 
			
		||||
    for (let word of totalSet) {
 | 
			
		||||
        articleContent = articleContent.replace(new RegExp("\\b" + word + "\\b", "g"), "<span class='highlighted'>" + word + "</span>");
 | 
			
		||||
    }
 | 
			
		||||
    document.getElementById("article").innerHTML = articleContent;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function cancelHighlighting() {
 | 
			
		||||
    let articleContent = document.getElementById("article").innerText;//将原来的.innerText改为.innerHtml,原因同上
 | 
			
		||||
    let pickedWords = document.getElementById("selected-words");
 | 
			
		||||
    const dictionaryWords = document.getElementById("selected-words2");    
 | 
			
		||||
    const list = pickedWords.value.split(" ");    
 | 
			
		||||
    if (pickedWords != null) {
 | 
			
		||||
        for (let i = 0; i < list.length; ++i) {
 | 
			
		||||
            list[i] = list[i].replace(/(^\s*)|(\s*$)/g, "");
 | 
			
		||||
            if (list[i] !== "") { //原来判断的代码中,替换的内容为“list[i]”这个字符串,这明显是错误的,我们需要替换的是list[i]里的内容
 | 
			
		||||
                articleContent = articleContent.replace(new RegExp("<mark>"+list[i]+"</mark>", "g"), list[i]);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    if (dictionaryWords != null) {
 | 
			
		||||
        let list2 = pickedWords.value.split(" ");
 | 
			
		||||
        for (let i = 0; i < list2.length; ++i) {
 | 
			
		||||
            list2 = dictionaryWords.value.split(" ");
 | 
			
		||||
            list2[i] = list2[i].replace(/(^\s*)|(\s*$)/g, "");
 | 
			
		||||
            if (list2[i] !== "") { //原来代码中,替换的内容为“list[i]”这个字符串,这明显是错误的,我们需要替换的是list[i]里的内容
 | 
			
		||||
                articleContent = articleContent.replace(new RegExp("<mark>"+list2[i]+"</mark>", "g"), list2[i]);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    let articleContent = document.getElementById("article").innerHTML;
 | 
			
		||||
    articleContent = articleContent.replace(new RegExp('<span class="highlighted">',"gi"), "")
 | 
			
		||||
    articleContent = articleContent.replace(new RegExp("</span>","gi"), "");
 | 
			
		||||
    document.getElementById("article").innerHTML = articleContent;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -97,6 +81,7 @@ function toggleHighlighting() {
 | 
			
		|||
        isHighlight = true;
 | 
			
		||||
        highLight();
 | 
			
		||||
    }
 | 
			
		||||
     localStorage.setItem('highlightChecked', isHighlight);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
showBtnHandler();
 | 
			
		||||
showBtnHandler();
 | 
			
		||||
| 
						 | 
				
			
			@ -9,7 +9,7 @@ var Reader = (function() {
 | 
			
		|||
        msg.rate = rate;
 | 
			
		||||
        msg.lang = "en-US";
 | 
			
		||||
        msg.onboundary = ev => {
 | 
			
		||||
            if (ev.name == "word") {
 | 
			
		||||
            if (ev.name === "word") {
 | 
			
		||||
                current_position = ev.charIndex;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			@ -32,4 +32,4 @@ var Reader = (function() {
 | 
			
		|||
        read: read,
 | 
			
		||||
        stopRead: stopRead
 | 
			
		||||
    };
 | 
			
		||||
})();
 | 
			
		||||
}) ();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,15 +5,14 @@ function familiar(theWord) {
 | 
			
		|||
    $.ajax({
 | 
			
		||||
        type:"GET",
 | 
			
		||||
        url:"/" + username + "/" + word + "/familiar",
 | 
			
		||||
        success:function(response){
 | 
			
		||||
        success:function(response) {
 | 
			
		||||
            let new_freq = freq - 1;
 | 
			
		||||
            const allow_move = document.getElementById("move_dynamiclly").checked;
 | 
			
		||||
            if (allow_move) {
 | 
			
		||||
 | 
			
		||||
                if (new_freq <= 0) {
 | 
			
		||||
                    removeWord(theWord);
 | 
			
		||||
                } else {
 | 
			
		||||
                    renderWord({ word: theWord, freq: new_freq });
 | 
			
		||||
                    renderWord({word: theWord, freq: new_freq});
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                if(new_freq <1) {
 | 
			
		||||
| 
						 | 
				
			
			@ -33,11 +32,11 @@ function unfamiliar(theWord) {
 | 
			
		|||
    $.ajax({
 | 
			
		||||
        type:"GET",
 | 
			
		||||
        url:"/" + username + "/" + word + "/unfamiliar",
 | 
			
		||||
        success:function(response){
 | 
			
		||||
        success:function(response) {
 | 
			
		||||
            let new_freq = parseInt(freq) + 1;
 | 
			
		||||
            const allow_move = document.getElementById("move_dynamiclly").checked;
 | 
			
		||||
            if (allow_move) {
 | 
			
		||||
                renderWord({ word: theWord, freq: new_freq });
 | 
			
		||||
                renderWord({word: theWord, freq: new_freq});
 | 
			
		||||
            } else {
 | 
			
		||||
                $("#freq_" + theWord).text(new_freq);
 | 
			
		||||
            }
 | 
			
		||||
| 
						 | 
				
			
			@ -51,7 +50,7 @@ function delete_word(theWord) {
 | 
			
		|||
    $.ajax({
 | 
			
		||||
        type:"GET",
 | 
			
		||||
        url:"/" + username + "/" + word + "/del",
 | 
			
		||||
        success:function(response){
 | 
			
		||||
        success:function(response) {
 | 
			
		||||
            const allow_move = document.getElementById("move_dynamiclly").checked;
 | 
			
		||||
            if (allow_move) {
 | 
			
		||||
                removeWord(theWord);
 | 
			
		||||
| 
						 | 
				
			
			@ -114,7 +113,7 @@ function removeWord(word) {
 | 
			
		|||
    // 根据词频信息删除元素
 | 
			
		||||
    word = word.replace('&', '&');
 | 
			
		||||
    const element_to_remove = document.getElementById(`p_${word}`);
 | 
			
		||||
    if (element_to_remove != null) {
 | 
			
		||||
    if (element_to_remove !== null) {
 | 
			
		||||
        element_to_remove.remove();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -129,7 +128,7 @@ function renderWord(word) {
 | 
			
		|||
    for (const current of container.children) {
 | 
			
		||||
        const cur_word = parseWord(current);
 | 
			
		||||
        // 找到第一个词频比它小的元素,插入到这个元素前面
 | 
			
		||||
        if (compareWord(cur_word, word) == -1) {
 | 
			
		||||
        if (compareWord(cur_word, word) === -1) {
 | 
			
		||||
            container.insertBefore(new_element, current);
 | 
			
		||||
            inserted = true;
 | 
			
		||||
            break;
 | 
			
		||||
| 
						 | 
				
			
			@ -165,17 +164,11 @@ function elementFromString(string) {
 | 
			
		|||
 *  当first大于second时返回1
 | 
			
		||||
 */
 | 
			
		||||
function compareWord(first, second) {
 | 
			
		||||
    if (first.freq < second.freq) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    if (first.freq !== second.freq) {
 | 
			
		||||
        return first.freq < second.freq ? -1 : 1;
 | 
			
		||||
    }
 | 
			
		||||
    if (first.freq > second.freq) {
 | 
			
		||||
        return 1;
 | 
			
		||||
    }
 | 
			
		||||
    if (first.word < second.word) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    if (first.word > second.word) {
 | 
			
		||||
        return 1;
 | 
			
		||||
    if (first.word !== second.word) {
 | 
			
		||||
        return first.word < second.word ? -1 : 1;
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,7 +1,7 @@
 | 
			
		|||
{% block body %}
 | 
			
		||||
{% if session['logged_in'] %}
 | 
			
		||||
 | 
			
		||||
你已登录 <a href="/{{ session['username'] }}">{{ session['username'] }}</a>。 登出点击<a href="/logout">这里</a>。
 | 
			
		||||
你已登录 <a href="/{{ session['username'] }}/userpage">{{ session['username'] }}</a>。 登出点击<a href="/logout">这里</a>。
 | 
			
		||||
 | 
			
		||||
{% else %}
 | 
			
		||||
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=3.0, user-scalable=yes" />
 | 
			
		||||
| 
						 | 
				
			
			@ -15,6 +15,10 @@
 | 
			
		|||
            alert('输入不能为空!');
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        if (password.includes(' ')) {
 | 
			
		||||
            alert('输入不能包含空格!');
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        $.post(
 | 
			
		||||
            "/login", {'username': username, 'password': password},
 | 
			
		||||
            function (response) {
 | 
			
		||||
| 
						 | 
				
			
			@ -32,14 +36,13 @@
 | 
			
		|||
<div class="container">
 | 
			
		||||
 | 
			
		||||
  <section class="signin-heading">
 | 
			
		||||
    <h1>Sign In</h1>
 | 
			
		||||
    <h1>Sign in</h1>
 | 
			
		||||
  </section>
 | 
			
		||||
 | 
			
		||||
  <input type="text" placeholder="用户名" class="username" id="username">
 | 
			
		||||
  <input type="password" placeholder="密码" class="password"  id="password">
 | 
			
		||||
  <button type="button" class="btn" onclick="login()">登录</button>
 | 
			
		||||
  <a class="signup" href="/signup">注册</a>
 | 
			
		||||
 | 
			
		||||
</div>
 | 
			
		||||
 | 
			
		||||
{% endif %}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -34,9 +34,9 @@
 | 
			
		|||
        <div class="alert alert-success" role="alert">共有文章 <span class="badge bg-success"> {{ number_of_essays }} </span> 篇</div>
 | 
			
		||||
        <p>粘贴1篇文章 (English only)</p>
 | 
			
		||||
        <form method="post" action="/">
 | 
			
		||||
            <textarea name="content" rows="10" cols="120"></textarea><br/>
 | 
			
		||||
            <textarea name="content" id="article" rows="10" cols="120"></textarea><br/>
 | 
			
		||||
            <input type="submit" value="get文章中的词频"/>
 | 
			
		||||
            <input type="reset" value="清除"/>
 | 
			
		||||
            <input type="reset" value="清除" onclick="clearArticle()"/>
 | 
			
		||||
        </form>
 | 
			
		||||
        {% if d_len > 0 %}
 | 
			
		||||
            <p><b>最常见的词</b></p>
 | 
			
		||||
| 
						 | 
				
			
			@ -44,6 +44,7 @@
 | 
			
		|||
                <a href="http://youdao.com/w/eng/{{x[0]}}/#keyfrom=dict2.index">{{x[0]}}</a> {{x[1]}}
 | 
			
		||||
            {% endfor %}
 | 
			
		||||
        {% endif %}
 | 
			
		||||
        <p class="text-muted">Version: 20230810</p>
 | 
			
		||||
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
 | 
			
		||||
    </div>
 | 
			
		||||
    {{ yml['footer'] | safe }}
 | 
			
		||||
| 
						 | 
				
			
			@ -52,5 +53,22 @@
 | 
			
		|||
            <script src="{{ js }}" ></script>
 | 
			
		||||
        {% endfor %}
 | 
			
		||||
    {% endif %}
 | 
			
		||||
<script type="text/javascript">
 | 
			
		||||
    // IIFE, avoid polluting the global scope
 | 
			
		||||
    (function() {
 | 
			
		||||
        const articleInput = document.querySelector('#article');
 | 
			
		||||
        articleInput.value = localStorage.getItem('article') || '';
 | 
			
		||||
 | 
			
		||||
        articleInput.addEventListener('input', function() {
 | 
			
		||||
            localStorage.setItem('article', articleInput.value);
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        window.clearArticle = function() {
 | 
			
		||||
            localStorage.removeItem('article');
 | 
			
		||||
            articleInput.value = '';
 | 
			
		||||
        };
 | 
			
		||||
    })();
 | 
			
		||||
 | 
			
		||||
</script>
 | 
			
		||||
</body>
 | 
			
		||||
</html>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,7 +2,7 @@
 | 
			
		|||
<html lang="en">
 | 
			
		||||
<head>
 | 
			
		||||
    <meta charset="UTF-8">
 | 
			
		||||
    <title>Title</title>
 | 
			
		||||
    <title>单词词频</title>
 | 
			
		||||
 | 
			
		||||
    {{ yml['header'] | safe }}
 | 
			
		||||
    {% if yml['css']['item'] %}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -12,6 +12,10 @@
 | 
			
		|||
                alert('输入不能为空!');
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
            if (old_password.includes(' ') || new_password.includes(' ')) {
 | 
			
		||||
                alert('输入不能包含空格!');
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
            if (new_password !== re_new_password) {
 | 
			
		||||
                alert('新密码不匹配,请重新输入');
 | 
			
		||||
                return false;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -16,6 +16,10 @@ You're logged in already! <a href="/logout">Logout</a>.
 | 
			
		|||
                alert('输入不能为空!');
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
            if (password.includes(' ') || password2.includes(' ')) {
 | 
			
		||||
                alert('输入不能包含空格!');
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
            if (password !== password2) {
 | 
			
		||||
                alert('确认密码与输入密码不一致!');
 | 
			
		||||
                return false;
 | 
			
		||||
| 
						 | 
				
			
			@ -53,7 +57,7 @@ You're logged in already! <a href="/logout">Logout</a>.
 | 
			
		|||
<div class="container">
 | 
			
		||||
 | 
			
		||||
  <section class="signin-heading">
 | 
			
		||||
    <h1>Sign Up</h1>
 | 
			
		||||
    <h1>Sign up</h1>
 | 
			
		||||
  </section>
 | 
			
		||||
 | 
			
		||||
  <p><input type="username" id="username" placeholder="输入用户名" class="username"></p>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -23,44 +23,34 @@
 | 
			
		|||
    <title>EnglishPal Study Room for {{ username }}</title>
 | 
			
		||||
 | 
			
		||||
    <style>
 | 
			
		||||
        .shaking {
 | 
			
		||||
            animation: shakes 1600ms ease-in-out;
 | 
			
		||||
        }
 | 
			
		||||
      .shaking {
 | 
			
		||||
          animation: shakes 1600ms ease-in-out;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
        @keyframes shakes {
 | 
			
		||||
            10%, 90% {
 | 
			
		||||
                transform: translate3d(-1px, 0, 0);
 | 
			
		||||
            }
 | 
			
		||||
            20%, 50% {
 | 
			
		||||
                transform: translate3d(+2px, 0, 0);
 | 
			
		||||
            }
 | 
			
		||||
            30%, 70% {
 | 
			
		||||
                transform: translate3d(-4px, 0, 0);
 | 
			
		||||
            }
 | 
			
		||||
            40%, 60% {
 | 
			
		||||
                transform: translate3d(+4px, 0, 0);
 | 
			
		||||
            }
 | 
			
		||||
            50% {
 | 
			
		||||
                transform: translate3d(-4px, 0, 0);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
      @keyframes shakes {
 | 
			
		||||
          10%, 90% { transform: translate3d(-1px, 0, 0); }
 | 
			
		||||
          20%, 50% { transform: translate3d(+2px, 0, 0); }
 | 
			
		||||
          30%, 70% { transform: translate3d(-4px, 0, 0); }
 | 
			
		||||
          40%, 60% { transform: translate3d(+4px, 0, 0); }
 | 
			
		||||
          50% { transform: translate3d(-4px, 0, 0); }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
        .lead {
 | 
			
		||||
            font-size: 22px;
 | 
			
		||||
            font-family: Helvetica, sans-serif;
 | 
			
		||||
            white-space: pre-wrap;
 | 
			
		||||
        }
 | 
			
		||||
      .lead{
 | 
			
		||||
          font-size: 22px;
 | 
			
		||||
          font-family: Helvetica, sans-serif;
 | 
			
		||||
          white-space: pre-wrap;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
        .arrow {
 | 
			
		||||
            padding: 0;
 | 
			
		||||
            font-size: 20px;
 | 
			
		||||
            line-height: 21px;
 | 
			
		||||
            display: inline-block;
 | 
			
		||||
        }
 | 
			
		||||
      .arrow {
 | 
			
		||||
	  padding: 0;
 | 
			
		||||
	  font-size: 20px;
 | 
			
		||||
	  line-height: 21px;
 | 
			
		||||
	  display: inline-block;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
        .arrow:hover {
 | 
			
		||||
            cursor: pointer;
 | 
			
		||||
        }
 | 
			
		||||
      .arrow:hover {
 | 
			
		||||
	  cursor: pointer;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
    </style>
 | 
			
		||||
</head>
 | 
			
		||||
| 
						 | 
				
			
			@ -69,83 +59,72 @@
 | 
			
		|||
    <p><b>English Pal for <font id="username" color="red">{{ username }}</font></b>
 | 
			
		||||
 | 
			
		||||
        {% if username ==  admin_name %}
 | 
			
		||||
            <a class="btn btn-secondary" href="/admin" role="button" onclick="stopRead()">管理</a>
 | 
			
		||||
        <a class="btn btn-secondary" href="/admin" role="button" onclick="stopRead()">管理</a>
 | 
			
		||||
        {% endif %}
 | 
			
		||||
        <a id="quit" class="btn btn-secondary" href="/logout" role="button" onclick="stopRead()">退出</a>
 | 
			
		||||
        <a class="btn btn-secondary" href="/reset" role="button" onclick="stopRead()">重设密码</a>
 | 
			
		||||
 | 
			
		||||
    </p>
 | 
			
		||||
    {% for message in get_flashed_messages() %}
 | 
			
		||||
        <div class="alert alert-warning alert-dismissible fade show" role="alert">
 | 
			
		||||
            {{ message }}
 | 
			
		||||
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
 | 
			
		||||
        </div>
 | 
			
		||||
    <div class="alert alert-warning alert-dismissible fade show" role="alert">
 | 
			
		||||
	{{ message }}
 | 
			
		||||
	<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
 | 
			
		||||
    </div>
 | 
			
		||||
    {% endfor %}
 | 
			
		||||
 | 
			
		||||
    <button class="arrow" id="load_next_article" onclick="load_next_article();Reader.stopRead()"
 | 
			
		||||
            title="下一篇 Next Article">⇨
 | 
			
		||||
    </button>
 | 
			
		||||
    <button class="arrow" id="load_pre_article" onclick="load_pre_article();Reader.stopRead()" style="display: none"
 | 
			
		||||
            title="上一篇 Previous Article">⇦
 | 
			
		||||
    </button>
 | 
			
		||||
        <button class="arrow" id="load_next_article" onclick="load_next_article();Reader.stopRead()" title="下一篇 Next Article">⇨</button>
 | 
			
		||||
        <button class="arrow" id="load_pre_article" onclick="load_pre_article();Reader.stopRead()" style="display: none" title="上一篇 Previous Article">⇦</button>
 | 
			
		||||
 | 
			
		||||
    <p><b>阅读文章并回答问题</b></p>
 | 
			
		||||
    <div id="text-content">
 | 
			
		||||
        <div id="found">
 | 
			
		||||
            <div class="alert alert-success" role="alert">According to your word list, your level is <span
 | 
			
		||||
                    class="text-decoration-underline" id="user_level">{{ today_article["user_level"] }}</span> and we
 | 
			
		||||
                have chosen an article with a difficulty level of <span class="text-decoration-underline"
 | 
			
		||||
                                                                        id="text_level">{{ today_article["text_level"] }}</span>
 | 
			
		||||
                for you.
 | 
			
		||||
            </div>
 | 
			
		||||
            <p class="text-muted" id="date">Article added on: {{ today_article["date"] }}</p><br/>
 | 
			
		||||
            <div class="alert alert-success" role="alert">According to your word list, your level is <span class="text-decoration-underline" id="user_level">{{ today_article["user_level"] }}</span>  and we have chosen an article with a difficulty level of <span class="text-decoration-underline" id="text_level">{{ today_article["text_level"] }}</span> for you.</div>
 | 
			
		||||
                <p class="text-muted" id="date">Article added on: {{ today_article["date"] }}</p><br/>
 | 
			
		||||
            <div class="p-3 mb-2 bg-light text-dark" style="margin: 0 0.5%;"><br/>
 | 
			
		||||
                <p class="display-6" id="article_title">{{ today_article["article_title"] }}</p><br/>
 | 
			
		||||
                <p class="lead"><font id="article">{{ today_article["article_body"] }}</font></p><br/>
 | 
			
		||||
                <div>
 | 
			
		||||
                    <p><small class="text-muted" id="source">{{ today_article['source'] }}</small></p><br/>
 | 
			
		||||
                </div>
 | 
			
		||||
            <p class="display-6" id="article_title">{{ today_article["article_title"] }}</p><br/>
 | 
			
		||||
            <p class="lead"><font id="article">{{ today_article["article_body"] }}</font></p><br/>
 | 
			
		||||
            <div>
 | 
			
		||||
                <p><small class="text-muted" id="source">{{ today_article['source'] }}</small></p><br/>
 | 
			
		||||
            </div>
 | 
			
		||||
 | 
			
		||||
                <p><b id="question">{{ today_article['question'] }}</b></p><br/>
 | 
			
		||||
            <p><b id="question">{{ today_article['question'] }}</b></p><br/>
 | 
			
		||||
                <script type="text/javascript">
 | 
			
		||||
                    function toggle_visibility(id) { {# https://css-tricks.com/snippets/javascript/showhide-element/#}
 | 
			
		||||
                        const e = document.getElementById(id);
 | 
			
		||||
                        if (e.style.display === 'block')
 | 
			
		||||
                        if(e.style.display === 'block')
 | 
			
		||||
                            e.style.display = 'none';
 | 
			
		||||
                        else
 | 
			
		||||
                            e.style.display = 'block';
 | 
			
		||||
                    }
 | 
			
		||||
                </script>
 | 
			
		||||
                <button onclick="toggle_visibility('answer');">ANSWER</button>
 | 
			
		||||
                <div id="answer" style="display:none;">{{ today_article['answer'] }}</div>
 | 
			
		||||
                <br/>
 | 
			
		||||
                <div id="answer" style="display:none;">{{ today_article['answer'] }}</div><br/>
 | 
			
		||||
            </div>
 | 
			
		||||
        </div>
 | 
			
		||||
        <div class="alert alert-success" role="alert" id="not_found" style="display:none;">
 | 
			
		||||
            <p class="text-muted"><span class="badge bg-success">Notes:</span><br>No article is currently available for
 | 
			
		||||
                you. You can try again a few times or mark new words in the passage to improve your level.</p>
 | 
			
		||||
            <p class="text-muted"><span class="badge bg-success">Notes:</span><br>No article is currently available for you. You can try again a few times or mark new words in the passage to improve your level.</p>
 | 
			
		||||
        </div>
 | 
			
		||||
        <div class="alert alert-success" role="alert" id="read_all" style="display:none;">
 | 
			
		||||
            <p class="text-muted"><span class="badge bg-success">Notes:</span><br>You've read all the articles.</p>
 | 
			
		||||
        </div>
 | 
			
		||||
    </div>
 | 
			
		||||
 | 
			
		||||
    <input type="checkbox" id="highlightCheckbox" onclick="toggleHighlighting()"/>生词高亮
 | 
			
		||||
    <input type="checkbox" id="readCheckbox" onclick="onReadClick()"/>大声朗读
 | 
			
		||||
    <input type="checkbox" id="chooseCheckbox" onclick="onChooseClick()"/>划词入库
 | 
			
		||||
    <input type="checkbox" id="highlightCheckbox" onclick="toggleHighlighting()" />生词高亮
 | 
			
		||||
    <input type="checkbox" id="readCheckbox" onclick="onReadClick()" />大声朗读
 | 
			
		||||
    <input type="checkbox" id="chooseCheckbox" onclick="onChooseClick()" />划词入库
 | 
			
		||||
    <div class="range">
 | 
			
		||||
        <div class="field">
 | 
			
		||||
            <div class="sliderValue">
 | 
			
		||||
                <span id="rangeValue">1×</span>
 | 
			
		||||
            </div>
 | 
			
		||||
            <input type="range" id="rangeComponent" min="0.5" max="2" value="1" step="0.25"/>
 | 
			
		||||
            <input type="range" id="rangeComponent" min="0.5" max="2" value="1" step="0.25" />
 | 
			
		||||
        </div>
 | 
			
		||||
    </div>
 | 
			
		||||
    <p><b>收集生词吧</b> (可以在正文中划词,也可以复制黏贴)</p>
 | 
			
		||||
    <form method="post" action="/{{ username }}/userpage">
 | 
			
		||||
        <textarea name="content" id="selected-words" rows="10" cols="120"></textarea><br/>
 | 
			
		||||
        <button class="btn btn-primary btn-lg" type="submit" onclick="Reader.stopRead()">把生词加入我的生词库</button>
 | 
			
		||||
        <button class="btn btn-primary btn-lg" type="reset" onclick="clearSelectedWords()">清除</button>
 | 
			
		||||
        <button class="btn btn-primary btn-lg" type="reset"  onclick="clearSelectedWords()">清除</button>
 | 
			
		||||
    </form>
 | 
			
		||||
    {% if session.get['thisWord'] %}
 | 
			
		||||
        <script type="text/javascript">
 | 
			
		||||
| 
						 | 
				
			
			@ -173,10 +152,9 @@
 | 
			
		|||
                {% set freq = x[1] %}
 | 
			
		||||
                {% if session.get('thisWord') == x[0] and session.get('time') == 1 %}
 | 
			
		||||
                {% endif %}
 | 
			
		||||
                <p id='p_{{ word }}' class="new-word">
 | 
			
		||||
                    <a id="word_{{ word }}" class="btn btn-light"
 | 
			
		||||
                       href='http://youdao.com/w/eng/{{ word }}/#keyfrom=dict2.index'
 | 
			
		||||
                       role="button">{{ word }}</a>
 | 
			
		||||
                <p id='p_{{ word }}' class="new-word" >
 | 
			
		||||
                    <a id="word_{{ word }}"  class="btn btn-light" href='http://youdao.com/w/eng/{{ word }}/#keyfrom=dict2.index'
 | 
			
		||||
                    role="button">{{ word }}</a>
 | 
			
		||||
                    ( <a id="freq_{{ word }}" title="{{ word }}">{{ freq }}</a> )
 | 
			
		||||
                    <a class="btn btn-success" onclick="familiar('{{ word }}')" role="button">熟悉</a>
 | 
			
		||||
                    <a class="btn btn-warning" onclick="unfamiliar('{{ word }}')" role="button">不熟悉</a>
 | 
			
		||||
| 
						 | 
				
			
			@ -244,78 +222,66 @@
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    function load_next_article() {
 | 
			
		||||
        $("#load_next_article").prop("disabled", true)
 | 
			
		||||
    function load_next_article(){
 | 
			
		||||
        $.ajax({
 | 
			
		||||
            url: '/get_next_article/{{username}}',
 | 
			
		||||
            dataType: 'json',
 | 
			
		||||
            success: function (data) {
 | 
			
		||||
            success: function(data) {
 | 
			
		||||
                // 更新页面内容
 | 
			
		||||
                if (data['today_article']) {
 | 
			
		||||
                if(data['today_article']){
 | 
			
		||||
                    update(data['today_article']);
 | 
			
		||||
                    check_pre(data['visited_articles']);
 | 
			
		||||
                    check_next(data['result_of_generate_article']);
 | 
			
		||||
                }
 | 
			
		||||
            }, complete: function (xhr, status) {
 | 
			
		||||
                $("#load_next_article").prop("disabled", false)
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function load_pre_article() {
 | 
			
		||||
    function load_pre_article(){
 | 
			
		||||
        $.ajax({
 | 
			
		||||
            url: '/get_pre_article/{{username}}',
 | 
			
		||||
            dataType: 'json',
 | 
			
		||||
            success: function (data) {
 | 
			
		||||
            success: function(data) {
 | 
			
		||||
                // 更新页面内容
 | 
			
		||||
                if (data['today_article']) {
 | 
			
		||||
                if(data['today_article']){
 | 
			
		||||
                    update(data['today_article']);
 | 
			
		||||
                    check_pre(data['visited_articles']);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function update(today_article) {
 | 
			
		||||
    function update(today_article){
 | 
			
		||||
        $('#user_level').html(today_article['user_level']);
 | 
			
		||||
        $('#text_level').html(today_article["text_level"]);
 | 
			
		||||
        $('#date').html('Article added on: ' + today_article["date"]);
 | 
			
		||||
        $('#date').html('Article added on: '+today_article["date"]);
 | 
			
		||||
        $('#article_title').html(today_article["article_title"]);
 | 
			
		||||
        $('#article').html(today_article["article_body"]);
 | 
			
		||||
        $('#source').html(today_article['source']);
 | 
			
		||||
        $('#question').html(today_article["question"]);
 | 
			
		||||
        $('#answer').html(today_article["answer"]);
 | 
			
		||||
        document.querySelector('#text_level').classList.add('mark'); // highlight text difficult level for 2 seconds
 | 
			
		||||
        setTimeout(() => {
 | 
			
		||||
            document.querySelector('#text_level').classList.remove('mark');
 | 
			
		||||
        }, 2000);
 | 
			
		||||
        setTimeout(() => {document.querySelector('#text_level').classList.remove('mark');}, 2000);
 | 
			
		||||
        document.querySelector('#user_level').classList.add('mark'); // do the same thing for user difficulty level
 | 
			
		||||
        setTimeout(() => {
 | 
			
		||||
            document.querySelector('#user_level').classList.remove('mark');
 | 
			
		||||
        }, 2000);
 | 
			
		||||
        setTimeout(() => {document.querySelector('#user_level').classList.remove('mark');}, 2000);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    <!-- 检查是否存在上一篇或下一篇,不存在则对应按钮隐藏-->
 | 
			
		||||
    function check_pre(visited_articles) {
 | 
			
		||||
        if ((visited_articles == '') || (visited_articles['index'] <= 0)) {
 | 
			
		||||
<!-- 检查是否存在上一篇或下一篇,不存在则对应按钮隐藏-->
 | 
			
		||||
    function check_pre(visited_articles){
 | 
			
		||||
        if((visited_articles=='')||(visited_articles['index']<=0)){
 | 
			
		||||
            $('#load_pre_article').hide();
 | 
			
		||||
            sessionStorage.setItem('pre_page_button', 'display')
 | 
			
		||||
        } else {
 | 
			
		||||
        }else{
 | 
			
		||||
            $('#load_pre_article').show();
 | 
			
		||||
            sessionStorage.setItem('pre_page_button', 'show')
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    function check_next(result_of_generate_article) {
 | 
			
		||||
        if (result_of_generate_article == "found") {
 | 
			
		||||
            $('#found').show();
 | 
			
		||||
            $('#not_found').hide();
 | 
			
		||||
    function check_next(result_of_generate_article){
 | 
			
		||||
        if(result_of_generate_article == "found"){
 | 
			
		||||
            $('#found').show();$('#not_found').hide();
 | 
			
		||||
            $('#read_all').hide();
 | 
			
		||||
        } else if (result_of_generate_article == "not found") {
 | 
			
		||||
        }else if(result_of_generate_article == "not found"){
 | 
			
		||||
            $('#found').hide();
 | 
			
		||||
            $('#not_found').show();
 | 
			
		||||
            $('#read_all').hide();
 | 
			
		||||
        } else {
 | 
			
		||||
        }else{
 | 
			
		||||
            $('#found').hide();
 | 
			
		||||
            $('#not_found').hide();
 | 
			
		||||
            $('#read_all').show();
 | 
			
		||||
| 
						 | 
				
			
			@ -325,7 +291,7 @@
 | 
			
		|||
</body>
 | 
			
		||||
<style>
 | 
			
		||||
    mark {
 | 
			
		||||
        color: #{{ yml['highlight']['color'] }};
 | 
			
		||||
        color: red;
 | 
			
		||||
        background-color: rgba(0, 0, 0, 0);
 | 
			
		||||
    }
 | 
			
		||||
</style>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,45 +1,50 @@
 | 
			
		|||
<!DOCTYPE html>
 | 
			
		||||
<html lang="en">
 | 
			
		||||
<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" />
 | 
			
		||||
    <meta name="format-detection" content="telephone=no" />
 | 
			
		||||
    <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" />
 | 
			
		||||
       <meta name="format-detection" content="telephone=no" />
 | 
			
		||||
 | 
			
		||||
    {{ yml['header'] | safe }}
 | 
			
		||||
    {% if yml['css']['item'] %}
 | 
			
		||||
        {% for css in yml['css']['item'] %}
 | 
			
		||||
        <link href="{{ css }}" rel="stylesheet">
 | 
			
		||||
        {% endfor %}
 | 
			
		||||
    {% endif %}
 | 
			
		||||
    {% if yml['js']['head'] %}
 | 
			
		||||
        {% for js in yml['js']['head'] %}
 | 
			
		||||
            <script src="{{ js }}" ></script>
 | 
			
		||||
        {% endfor %}
 | 
			
		||||
    {% endif %}
 | 
			
		||||
       {{ yml['header'] | safe }}
 | 
			
		||||
       {% if yml['css']['item'] %}
 | 
			
		||||
         {% for css in yml['css']['item'] %}
 | 
			
		||||
         <link href="{{ css }}" rel="stylesheet">
 | 
			
		||||
         {% endfor %}
 | 
			
		||||
        {% endif %}
 | 
			
		||||
        {% if yml['js']['head'] %}
 | 
			
		||||
         {% for js in yml['js']['head'] %}
 | 
			
		||||
         <script src="{{ js }}" ></script>
 | 
			
		||||
         {% endfor %}
 | 
			
		||||
        {% endif %}
 | 
			
		||||
 | 
			
		||||
    <title>EnglishPal Study Room for {{username}}</title>
 | 
			
		||||
</head>
 | 
			
		||||
<body>
 | 
			
		||||
     <p>取消勾选认识的单词</p>
 | 
			
		||||
      <form method="post" action="/{{username}}/mark">
 | 
			
		||||
       <input type="submit" name="add-btn" value="加入我的生词簿"/>
 | 
			
		||||
       {% for x in lst %}
 | 
			
		||||
          {% set word = x[0]%}
 | 
			
		||||
        <p>
 | 
			
		||||
            <font color="grey">{{loop.index}}</font>
 | 
			
		||||
            :
 | 
			
		||||
            <a href='http://youdao.com/w/eng/{{word}}/#keyfrom=dict2.index' title={{word}}>{{word}}</a>
 | 
			
		||||
            ({{x[1]}})
 | 
			
		||||
            <input type="checkbox" name="marked" value="{{word}}" checked>
 | 
			
		||||
        </p>
 | 
			
		||||
       <title>EnglishPal Study Room for {{username}}</title>
 | 
			
		||||
    </head>
 | 
			
		||||
    <body>
 | 
			
		||||
      <div class="container-fluid">
 | 
			
		||||
          <p class="mt-md-3">
 | 
			
		||||
               <input type="button" id="btn-cancel-selection" value="取消勾选" onclick="toggleCheckboxSelection(false)" />
 | 
			
		||||
               <input type="button" id="btn-selection" value="全部勾选" onclick="toggleCheckboxSelection(true)" />
 | 
			
		||||
          </p>
 | 
			
		||||
          <form method="post" action="/{{username}}/mark">
 | 
			
		||||
              <button type="submit" name="add-btn" class="btn btn-outline-primary btn-lg" onclick="clearSelectedWords()">加入我的生词簿</button>
 | 
			
		||||
              {% for x in lst %}
 | 
			
		||||
              {% set word = x[0]%}
 | 
			
		||||
              <p>
 | 
			
		||||
                    <font color="grey">{{loop.index}}</font>
 | 
			
		||||
                    :
 | 
			
		||||
                    <a href='http://youdao.com/w/eng/{{word}}/#keyfrom=dict2.index' title={{word}}>{{word}}</a>
 | 
			
		||||
                    ({{x[1]}})
 | 
			
		||||
                    <input type="checkbox" name="marked" value="{{word}}" checked>
 | 
			
		||||
              </p>
 | 
			
		||||
 | 
			
		||||
       {% endfor %}
 | 
			
		||||
       </form>
 | 
			
		||||
    {{ yml['footer'] | safe }}
 | 
			
		||||
    {% if yml['js']['bottom'] %}
 | 
			
		||||
        {% for js in yml['js']['bottom'] %}
 | 
			
		||||
            <script src="{{ js }}" ></script>
 | 
			
		||||
        {% endfor %}
 | 
			
		||||
    {% endif %}
 | 
			
		||||
</body>
 | 
			
		||||
               {% endfor %}
 | 
			
		||||
           </form>
 | 
			
		||||
           {{ yml['footer'] | safe }}
 | 
			
		||||
           {% if yml['js']['bottom'] %}
 | 
			
		||||
            {% for js in yml['js']['bottom'] %}
 | 
			
		||||
             <script src="{{ js }}" ></script>
 | 
			
		||||
            {% endfor %}
 | 
			
		||||
        {% endif %}
 | 
			
		||||
      </div>
 | 
			
		||||
    </body>
 | 
			
		||||
</html>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,9 @@
 | 
			
		|||
import pytest
 | 
			
		||||
import sqlite3
 | 
			
		||||
import time
 | 
			
		||||
from selenium import webdriver
 | 
			
		||||
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
 | 
			
		||||
 | 
			
		||||
from pathlib import Path
 | 
			
		||||
 | 
			
		||||
@pytest.fixture
 | 
			
		||||
def URL():
 | 
			
		||||
| 
						 | 
				
			
			@ -9,5 +12,24 @@ def URL():
 | 
			
		|||
 | 
			
		||||
@pytest.fixture
 | 
			
		||||
def driver():
 | 
			
		||||
    my_driver = webdriver.Edge()  # uncomment this line if you wish to run the test on your laptop    
 | 
			
		||||
    return my_driver
 | 
			
		||||
    return webdriver.Edge()  # follow the "End-to-end testing" section in README.md to install the web driver executable
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@pytest.fixture
 | 
			
		||||
def restore_sqlite_database():
 | 
			
		||||
    '''
 | 
			
		||||
    Automatically restore SQLite database file app/db/wordfreqapp.db
 | 
			
		||||
    using SQL statements from app/static/wordfreqapp.sql
 | 
			
		||||
    '''
 | 
			
		||||
    con = sqlite3.connect('../db/wordfreqapp.db')
 | 
			
		||||
    with con:
 | 
			
		||||
        con.executescript('DROP TABLE IF EXISTS user;')
 | 
			
		||||
        con.executescript('DROP TABLE IF EXISTS article;')
 | 
			
		||||
        con.executescript(open('../static/wordfreqapp.sql', encoding='utf8').read())
 | 
			
		||||
    con.close()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@pytest.fixture(autouse=True)
 | 
			
		||||
def restart_englishpal(restore_sqlite_database):
 | 
			
		||||
    (Path(__file__).parent / '../main.py').touch()
 | 
			
		||||
    time.sleep(1)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,33 @@
 | 
			
		|||
import uuid
 | 
			
		||||
from selenium.webdriver.support.ui import WebDriverWait
 | 
			
		||||
from selenium.webdriver.support import expected_conditions as EC
 | 
			
		||||
from selenium.common.exceptions import UnexpectedAlertPresentException, NoAlertPresentException
 | 
			
		||||
 | 
			
		||||
def signup(URL, driver):
 | 
			
		||||
    username = 'TestUser' + str(uuid.uuid1()).split('-')[0].title()
 | 
			
		||||
    password = '[Abc+123]'
 | 
			
		||||
 | 
			
		||||
    driver.get(URL)
 | 
			
		||||
        
 | 
			
		||||
    elem = driver.find_element_by_link_text('注册')
 | 
			
		||||
    elem.click()
 | 
			
		||||
    
 | 
			
		||||
    elem = driver.find_element_by_id('username')
 | 
			
		||||
    elem.send_keys(username)
 | 
			
		||||
    
 | 
			
		||||
    elem = driver.find_element_by_id('password')
 | 
			
		||||
    elem.send_keys(password)
 | 
			
		||||
 | 
			
		||||
    elem = driver.find_element_by_id('password2')
 | 
			
		||||
    elem.send_keys(password)    
 | 
			
		||||
    
 | 
			
		||||
    elem = driver.find_element_by_class_name('btn') # 找到"注册"按钮
 | 
			
		||||
    elem.click()
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        WebDriverWait(driver, 1).until(EC.alert_is_present())
 | 
			
		||||
        driver.switch_to.alert.accept()
 | 
			
		||||
    except (UnexpectedAlertPresentException, NoAlertPresentException):
 | 
			
		||||
        pass
 | 
			
		||||
    
 | 
			
		||||
    return username, password
 | 
			
		||||
| 
						 | 
				
			
			@ -1,76 +1,31 @@
 | 
			
		|||
# -*- coding: utf-8 -*-
 | 
			
		||||
# Run the docker image using the following command:
 | 
			
		||||
# docker run -d -p 4444:4444 selenium/standalone-chrome
 | 
			
		||||
from selenium import webdriver
 | 
			
		||||
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
 | 
			
		||||
 | 
			
		||||
import random, time
 | 
			
		||||
import string
 | 
			
		||||
 | 
			
		||||
driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.FIREFOX)
 | 
			
		||||
driver.implicitly_wait(10)
 | 
			
		||||
 | 
			
		||||
HOME_PAGE = 'http://121.4.94.30:91/'
 | 
			
		||||
import time
 | 
			
		||||
from helper import signup
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def has_punctuation(s):
 | 
			
		||||
    return [c for c in s if c in string.punctuation] != []
 | 
			
		||||
    
 | 
			
		||||
def test_add_word():
 | 
			
		||||
def test_add_word(URL, driver):
 | 
			
		||||
    try:
 | 
			
		||||
        driver.get(HOME_PAGE)
 | 
			
		||||
        assert 'English Pal -' in driver.page_source
 | 
			
		||||
    
 | 
			
		||||
        # login
 | 
			
		||||
        elem = driver.find_element_by_link_text('登录')
 | 
			
		||||
        elem.click()
 | 
			
		||||
    
 | 
			
		||||
        uname = 'lanhui'
 | 
			
		||||
        password = 'l0ve1t'
 | 
			
		||||
        elem = driver.find_element_by_name('username')
 | 
			
		||||
        elem.send_keys(uname)
 | 
			
		||||
    
 | 
			
		||||
        elem = driver.find_element_by_name('password')
 | 
			
		||||
        elem.send_keys(password)
 | 
			
		||||
        
 | 
			
		||||
        elem = driver.find_element_by_xpath('//form[1]/p[3]/input[1]') # 找到登录按钮
 | 
			
		||||
        elem.click()
 | 
			
		||||
    
 | 
			
		||||
        assert 'EnglishPal Study Room for ' + uname in  driver.title
 | 
			
		||||
    
 | 
			
		||||
        # get essay content
 | 
			
		||||
        elem = driver.find_element_by_id('text-content')
 | 
			
		||||
        essay_content = elem.text
 | 
			
		||||
    
 | 
			
		||||
        elem = driver.find_element_by_id('selected-words')
 | 
			
		||||
        word = random.choice(essay_content.split())
 | 
			
		||||
        while 'font>' in word or 'br>' in word or 'p>' in word or len(word) < 6 or has_punctuation(word):
 | 
			
		||||
            word = random.choice(essay_content.split())
 | 
			
		||||
        username, password = signup(URL, driver) # sign up a new account and automatically log in
 | 
			
		||||
        time.sleep(1)
 | 
			
		||||
 | 
			
		||||
        # enter the word in the text area
 | 
			
		||||
        elem = driver.find_element_by_id('selected-words')
 | 
			
		||||
        word = 'devour'
 | 
			
		||||
        elem.send_keys(word)
 | 
			
		||||
 | 
			
		||||
        elem = driver.find_element_by_xpath('//form[1]//input[1]') # 找到get所有词频按钮
 | 
			
		||||
        elem.click()
 | 
			
		||||
    
 | 
			
		||||
        elems = driver.find_elements_by_xpath("//input[@type='checkbox']")
 | 
			
		||||
        for elem in elems:
 | 
			
		||||
            if elem.get_attribute('name') == 'marked':
 | 
			
		||||
                elem.click()
 | 
			
		||||
    
 | 
			
		||||
        elem = driver.find_element_by_name('add-btn') # 找到加入我的生词簿按钮
 | 
			
		||||
        elem = driver.find_element_by_xpath('//form[1]//button[1]') # 找到"把生词加入我的生词库"按钮
 | 
			
		||||
        elem.click()
 | 
			
		||||
 | 
			
		||||
        elem = driver.find_element_by_name('add-btn') # 找到"加入我的生词簿"按钮
 | 
			
		||||
        elem.click()
 | 
			
		||||
 | 
			
		||||
        driver.refresh()
 | 
			
		||||
        driver.refresh()
 | 
			
		||||
        driver.refresh()        
 | 
			
		||||
        elems = driver.find_elements_by_xpath("//p[@class='new-word']/a")
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
        found = 0
 | 
			
		||||
        for elem in elems:
 | 
			
		||||
            if word in elem.text:
 | 
			
		||||
                found = 1
 | 
			
		||||
                break
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
        assert found == 1
 | 
			
		||||
    finally:    
 | 
			
		||||
    finally:
 | 
			
		||||
        driver.quit()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,95 @@
 | 
			
		|||
import pytest
 | 
			
		||||
from selenium import webdriver
 | 
			
		||||
from selenium.webdriver.common.by import By
 | 
			
		||||
from selenium.webdriver.common.keys import Keys
 | 
			
		||||
from selenium.webdriver.support.ui import WebDriverWait
 | 
			
		||||
from selenium.webdriver.support import expected_conditions as EC
 | 
			
		||||
from selenium.common.exceptions import NoSuchElementException, TimeoutException
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# 测试登录页面输入密码包含空格的情况
 | 
			
		||||
def test_login_password_with_space(driver, URL):
 | 
			
		||||
    try:
 | 
			
		||||
        driver.get(URL+"/login")
 | 
			
		||||
 | 
			
		||||
        # 输入用户名
 | 
			
		||||
        username_elem = driver.find_element_by_id('username')
 | 
			
		||||
        username_elem.send_keys("test_user")
 | 
			
		||||
 | 
			
		||||
        # 输入包含空格的密码
 | 
			
		||||
        password_elem = driver.find_element_by_id('password')
 | 
			
		||||
        password_elem.send_keys("password with space")
 | 
			
		||||
 | 
			
		||||
        # 提交登录表单
 | 
			
		||||
        elem = driver.find_element_by_class_name('btn')  # 找到提交按钮
 | 
			
		||||
        elem.click()
 | 
			
		||||
 | 
			
		||||
        # 显式等待直到警告框出现
 | 
			
		||||
        WebDriverWait(driver, 10).until(EC.alert_is_present())
 | 
			
		||||
 | 
			
		||||
        # 检查是否弹出警告框
 | 
			
		||||
        alert = driver.switch_to.alert
 | 
			
		||||
        assert "输入不能包含空格!" in alert.text
 | 
			
		||||
    except (NoSuchElementException, TimeoutException) as e:
 | 
			
		||||
        pytest.fail("页面元素未找到或超时: {}".format(e))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# 测试注册页面输入密码包含空格的情况
 | 
			
		||||
 | 
			
		||||
def test_signup_password_with_space(driver, URL):
 | 
			
		||||
    try:
 | 
			
		||||
        driver.get(URL+"/signup")
 | 
			
		||||
 | 
			
		||||
        # 输入用户名
 | 
			
		||||
        username_elem = driver.find_element_by_id('username')
 | 
			
		||||
        username_elem.send_keys("new_user")
 | 
			
		||||
 | 
			
		||||
        # 输入包含空格的密码
 | 
			
		||||
        password_elem = driver.find_element_by_id('password')
 | 
			
		||||
        password_elem.send_keys("password with space")
 | 
			
		||||
 | 
			
		||||
        # 再次输入密码
 | 
			
		||||
        password2_elem = driver.find_element_by_id('password2')
 | 
			
		||||
        password2_elem.send_keys("password with space")
 | 
			
		||||
 | 
			
		||||
        # 提交注册表单
 | 
			
		||||
        elem = driver.find_element_by_class_name('btn')  # 找到提交按钮
 | 
			
		||||
        elem.click()
 | 
			
		||||
 | 
			
		||||
        # 显式等待直到警告框出现
 | 
			
		||||
        WebDriverWait(driver, 10).until(EC.alert_is_present())
 | 
			
		||||
 | 
			
		||||
        # 检查是否弹出警告框
 | 
			
		||||
        alert = driver.switch_to.alert
 | 
			
		||||
        assert "输入不能包含空格!" in alert.text
 | 
			
		||||
    except (NoSuchElementException, TimeoutException) as e:
 | 
			
		||||
        pytest.fail("页面元素未找到或超时: {}".format(e))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# 测试重设密码页面输入新密码包含空格的情况
 | 
			
		||||
 | 
			
		||||
def test_reset_password_with_space(driver, URL):
 | 
			
		||||
    try:
 | 
			
		||||
        driver.get(URL+"/reset")
 | 
			
		||||
 | 
			
		||||
        # 输入用户名
 | 
			
		||||
        username_elem = driver.find_element_by_id('username')
 | 
			
		||||
        username_elem.send_keys("test_user")
 | 
			
		||||
 | 
			
		||||
        # 输入包含空格的密码
 | 
			
		||||
        password_elem = driver.find_element_by_id('password')
 | 
			
		||||
        password_elem.send_keys("password with space")
 | 
			
		||||
 | 
			
		||||
        # 提交重设密码表单
 | 
			
		||||
        elem = driver.find_element_by_class_name('btn')  # 找到提交按钮
 | 
			
		||||
        elem.click()
 | 
			
		||||
 | 
			
		||||
        # 显式等待直到警告框出现
 | 
			
		||||
        WebDriverWait(driver, 10).until(EC.alert_is_present())
 | 
			
		||||
 | 
			
		||||
        # 检查是否弹出警告框
 | 
			
		||||
        alert = driver.switch_to.alert
 | 
			
		||||
        assert "输入不能包含空格!" in alert.text
 | 
			
		||||
    except (NoSuchElementException, TimeoutException) as e:
 | 
			
		||||
        pytest.fail("页面元素未找到或超时: {}".format(e))
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,88 @@
 | 
			
		|||
from selenium.webdriver.common.alert import Alert
 | 
			
		||||
from selenium.webdriver.common.by import By
 | 
			
		||||
from selenium.webdriver.support.ui import WebDriverWait
 | 
			
		||||
from selenium.webdriver.support import expected_conditions as EC
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# 对用户名不能为中文进行测试
 | 
			
		||||
def test_register_username_with_chinese(driver, URL):
 | 
			
		||||
    try:
 | 
			
		||||
        driver.get(URL + "/signup")
 | 
			
		||||
 | 
			
		||||
        # 等待用户名输入框出现
 | 
			
		||||
        username_elem = WebDriverWait(driver, 10).until(
 | 
			
		||||
            EC.presence_of_element_located((By.ID, 'username'))
 | 
			
		||||
        )
 | 
			
		||||
        username_elem.send_keys("测试用户")  # 输入中文用户名
 | 
			
		||||
 | 
			
		||||
        # 等待密码输入框出现
 | 
			
		||||
        password_elem = WebDriverWait(driver, 10).until(
 | 
			
		||||
            EC.presence_of_element_located((By.ID, 'password'))
 | 
			
		||||
        )
 | 
			
		||||
        password_elem.send_keys("validPassword123")  # 输入有效密码
 | 
			
		||||
 | 
			
		||||
        # 等待确认密码输入框出现
 | 
			
		||||
        password2_elem = WebDriverWait(driver, 10).until(
 | 
			
		||||
            EC.presence_of_element_located((By.ID, 'password2'))
 | 
			
		||||
        )
 | 
			
		||||
        password2_elem.send_keys("validPassword123")  # 输入有效确认密码
 | 
			
		||||
 | 
			
		||||
        # 等待注册按钮出现并点击
 | 
			
		||||
        signup_button = WebDriverWait(driver, 10).until(
 | 
			
		||||
            EC.element_to_be_clickable((By.XPATH, '//button[@onclick="signup()"]'))
 | 
			
		||||
        )
 | 
			
		||||
        signup_button.click()
 | 
			
		||||
 | 
			
		||||
        # 等待警告框出现并接受
 | 
			
		||||
        WebDriverWait(driver, 10).until(EC.alert_is_present())
 | 
			
		||||
        alert = driver.switch_to.alert
 | 
			
		||||
        alert_text = alert.text
 | 
			
		||||
        print(f"警告文本: {alert_text}")
 | 
			
		||||
        assert alert_text == "Chinese characters are not allowed in the user name."  # 根据实际的警告文本进行断言
 | 
			
		||||
        alert.accept()
 | 
			
		||||
 | 
			
		||||
    except Exception as e:
 | 
			
		||||
        print(f"发生错误: {e}")
 | 
			
		||||
        raise
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# 对注册时密码不能是中文进行测试
 | 
			
		||||
def test_register_password_with_chinese(driver, URL):
 | 
			
		||||
    try:
 | 
			
		||||
        driver.get(URL + "/signup")
 | 
			
		||||
 | 
			
		||||
        # 等待用户名输入框出现
 | 
			
		||||
        username_elem = WebDriverWait(driver, 10).until(
 | 
			
		||||
            EC.presence_of_element_located((By.ID, 'username'))
 | 
			
		||||
        )
 | 
			
		||||
        username_elem.send_keys("validUsername123")  # 输入有效用户名
 | 
			
		||||
 | 
			
		||||
        # 等待密码输入框出现
 | 
			
		||||
        password_elem = WebDriverWait(driver, 10).until(
 | 
			
		||||
            EC.presence_of_element_located((By.ID, 'password'))
 | 
			
		||||
        )
 | 
			
		||||
        password_elem.send_keys("测试密码")  # 输入中文密码
 | 
			
		||||
 | 
			
		||||
        # 等待确认密码输入框出现
 | 
			
		||||
        password2_elem = WebDriverWait(driver, 10).until(
 | 
			
		||||
            EC.presence_of_element_located((By.ID, 'password2'))
 | 
			
		||||
        )
 | 
			
		||||
        password2_elem.send_keys("测试密码")  # 输入中文确认密码
 | 
			
		||||
 | 
			
		||||
        # 等待注册按钮出现并点击
 | 
			
		||||
        signup_button = WebDriverWait(driver, 10).until(
 | 
			
		||||
            EC.element_to_be_clickable((By.XPATH, '//button[@onclick="signup()"]'))
 | 
			
		||||
        )
 | 
			
		||||
        signup_button.click()
 | 
			
		||||
 | 
			
		||||
        # 等待警告框出现并接受
 | 
			
		||||
        WebDriverWait(driver, 10).until(EC.alert_is_present())
 | 
			
		||||
        alert = driver.switch_to.alert
 | 
			
		||||
        alert_text = alert.text
 | 
			
		||||
        print(f"警告文本: {alert_text}")
 | 
			
		||||
        assert alert_text == "Chinese characters are not allowed in the password."  # 根据实际的警告文本进行断言
 | 
			
		||||
        alert.accept()
 | 
			
		||||
 | 
			
		||||
    except Exception as e:
 | 
			
		||||
        print(f"发生错误: {e}")
 | 
			
		||||
        raise
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,55 @@
 | 
			
		|||
import random
 | 
			
		||||
import string
 | 
			
		||||
import time
 | 
			
		||||
 | 
			
		||||
from selenium.webdriver.common.by import By
 | 
			
		||||
from selenium.webdriver.support.ui import WebDriverWait
 | 
			
		||||
from selenium.webdriver.support import expected_conditions as EC
 | 
			
		||||
 | 
			
		||||
from helper import signup
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def has_punctuation(s):
 | 
			
		||||
    return any(c in string.punctuation for c in s)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def login(driver, home, uname, password):
 | 
			
		||||
    driver.get(home)
 | 
			
		||||
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, '登录'))).click()
 | 
			
		||||
    driver.find_element(By.ID, 'username').send_keys(uname)
 | 
			
		||||
    driver.find_element(By.ID, 'password').send_keys(password)
 | 
			
		||||
    driver.find_element(By.XPATH, '//button[text()="登录"]').click()
 | 
			
		||||
    WebDriverWait(driver, 10).until(EC.title_is(f"EnglishPal Study Room for {uname}"))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def select_valid_word(driver):
 | 
			
		||||
    elem = driver.find_element(By.ID, 'text-content')
 | 
			
		||||
    essay_content = elem.text
 | 
			
		||||
    valid_word = random.choice([word for word in essay_content.split() if len(word) >= 6 and not has_punctuation(
 | 
			
		||||
        word) and 'font>' not in word and 'br>' not in word and 'p>' not in word])
 | 
			
		||||
    driver.find_element(By.ID, 'selected-words').send_keys(valid_word)
 | 
			
		||||
    return valid_word
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_save_selected_word(driver, URL):
 | 
			
		||||
    try:
 | 
			
		||||
        username, password = signup(URL, driver)
 | 
			
		||||
        word = select_valid_word(driver)
 | 
			
		||||
        stored_words = driver.execute_script('return localStorage.getItem("selectedWords");')
 | 
			
		||||
        assert word == stored_words, "Selected word not saved to localStorage correctly"
 | 
			
		||||
        # 退出并重新登录以检查存储的单词
 | 
			
		||||
        driver.find_element(By.LINK_TEXT, '退出').click()
 | 
			
		||||
        driver.execute_script("window.open('');window.close();")
 | 
			
		||||
 | 
			
		||||
        # 等待一会儿,让浏览器有足够的时间关闭标签页
 | 
			
		||||
        WebDriverWait(driver, 2)
 | 
			
		||||
 | 
			
		||||
        # 重新打开一个新的标签页
 | 
			
		||||
        driver.execute_script("window.open('');")
 | 
			
		||||
        driver.switch_to.window(driver.window_handles[-1])  # 切换到新打开的标签页
 | 
			
		||||
 | 
			
		||||
        login(driver, URL, username, password)
 | 
			
		||||
        textarea_content = driver.find_element(By.ID, 'selected-words').get_attribute('value')
 | 
			
		||||
        assert word == textarea_content, "Selected word not preserved after re-login"
 | 
			
		||||
    finally:
 | 
			
		||||
        driver.quit()
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,44 @@
 | 
			
		|||
import random
 | 
			
		||||
import string
 | 
			
		||||
import time
 | 
			
		||||
from selenium.webdriver.common.by import By
 | 
			
		||||
from selenium.webdriver.support.ui import WebDriverWait
 | 
			
		||||
from selenium.webdriver.support import expected_conditions as EC
 | 
			
		||||
from selenium.webdriver.common.action_chains import ActionChains
 | 
			
		||||
 | 
			
		||||
from helper import signup
 | 
			
		||||
 | 
			
		||||
def has_punctuation(s):
 | 
			
		||||
    return any(c in string.punctuation for c in s)
 | 
			
		||||
 | 
			
		||||
def select_one(driver):
 | 
			
		||||
    elem = driver.find_element(By.ID, 'article')
 | 
			
		||||
    essay_content = elem.text
 | 
			
		||||
    valid_word = random.choice([word for word in essay_content.split() if len(word) >= 6 and not has_punctuation(
 | 
			
		||||
        word) and 'font>' not in word and 'br>' not in word and 'p>' not in word])
 | 
			
		||||
    driver.find_element(By.ID, 'selected-words').send_keys(valid_word)
 | 
			
		||||
    driver.find_element(By.ID, 'article').click()
 | 
			
		||||
    return valid_word
 | 
			
		||||
 | 
			
		||||
def select_two(driver):
 | 
			
		||||
    word = driver.find_element(By.CLASS_NAME, 'highlighted')
 | 
			
		||||
 | 
			
		||||
    # 创建ActionChains对象
 | 
			
		||||
    actions = ActionChains(driver)
 | 
			
		||||
    actions.move_to_element(word)
 | 
			
		||||
 | 
			
		||||
    # 模拟鼠标按下并拖动以选择文本
 | 
			
		||||
    actions.double_click()
 | 
			
		||||
    actions.perform()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_selected_second_word(driver, URL):
 | 
			
		||||
    try:
 | 
			
		||||
        signup(URL, driver)
 | 
			
		||||
        selected_words = select_one(driver);
 | 
			
		||||
        assert selected_words.strip() != "", "选中的单词被放置框中"
 | 
			
		||||
        select_two(driver)
 | 
			
		||||
        selected_second_words = driver.find_element(By.ID, 'selected-words').get_attribute('value')
 | 
			
		||||
        assert selected_second_words.strip() == "", "选中的单词被删除"
 | 
			
		||||
    finally:
 | 
			
		||||
        driver.quit()
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,37 @@
 | 
			
		|||
import time
 | 
			
		||||
import pytest
 | 
			
		||||
from selenium import webdriver
 | 
			
		||||
from selenium.webdriver import ActionChains
 | 
			
		||||
from selenium.webdriver.common.by import By
 | 
			
		||||
from selenium.webdriver.common.alert import Alert
 | 
			
		||||
from selenium.webdriver.support import expected_conditions as EC
 | 
			
		||||
from selenium.webdriver.support.wait import WebDriverWait
 | 
			
		||||
from helper import signup
 | 
			
		||||
 | 
			
		||||
def test_bug551(driver, URL):
 | 
			
		||||
    driver.maximize_window()
 | 
			
		||||
    driver.get(URL)
 | 
			
		||||
 | 
			
		||||
    username, password = signup(URL, driver)
 | 
			
		||||
 | 
			
		||||
    article = driver.find_element(By.ID, 'article')
 | 
			
		||||
    actions = ActionChains(driver)
 | 
			
		||||
 | 
			
		||||
    actions.move_to_element(article)
 | 
			
		||||
    actions.click_and_hold()
 | 
			
		||||
    actions.move_by_offset(450, 200)
 | 
			
		||||
    actions.release()
 | 
			
		||||
    actions.perform()
 | 
			
		||||
 | 
			
		||||
    # 获取选中高亮部分的单词的元素
 | 
			
		||||
    highlighted_words = driver.find_elements(By.CLASS_NAME, 'highlighted')
 | 
			
		||||
 | 
			
		||||
    # 验证选中部分的单词是否同时应用了需求样式
 | 
			
		||||
    expected_font_weight = "400"  
 | 
			
		||||
 | 
			
		||||
    for word in highlighted_words:
 | 
			
		||||
        font_weight = word.value_of_css_property("font-weight")
 | 
			
		||||
        assert font_weight == expected_font_weight, f"选中部分的单词的字体样式错误"
 | 
			
		||||
 | 
			
		||||
    time.sleep(5)
 | 
			
		||||
    driver.quit()
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,58 @@
 | 
			
		|||
from selenium import webdriver
 | 
			
		||||
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
 | 
			
		||||
from selenium.webdriver.support import expected_conditions as EC
 | 
			
		||||
from selenium import webdriver
 | 
			
		||||
from selenium.webdriver.support.wait import WebDriverWait
 | 
			
		||||
from selenium.webdriver.common.by import By
 | 
			
		||||
from selenium.webdriver.common.keys import Keys
 | 
			
		||||
import logging
 | 
			
		||||
import time
 | 
			
		||||
import pytest
 | 
			
		||||
 | 
			
		||||
@pytest.mark.parametrize("test_input,expected",
 | 
			
		||||
                         [("‘test1’", "test1"),
 | 
			
		||||
                          ("'test2'", "test2"),
 | 
			
		||||
                          ("“test3”", "test3"),
 | 
			
		||||
                          ("it's", "it's"),
 | 
			
		||||
                          ("hello,I'm linshan", ["hello","i'm","linshan"]),
 | 
			
		||||
                          ("Happy New Year!?", ["happy","new","year"]),
 | 
			
		||||
                          ("My favorite book is 《Harry Potter》。", ["potter","harry","my","favorite","book","is"])])
 | 
			
		||||
def test_bug553_LinShan(test_input,expected, driver, URL):
 | 
			
		||||
    try:
 | 
			
		||||
        # 打开对应地址的网页
 | 
			
		||||
        driver.get(URL)
 | 
			
		||||
 | 
			
		||||
        # 浏览器最大窗口化
 | 
			
		||||
        driver.maximize_window()
 | 
			
		||||
 | 
			
		||||
        # 判断网页源代码中是否有English Pal -文字
 | 
			
		||||
        assert 'English Pal -' in driver.page_source
 | 
			
		||||
 | 
			
		||||
        # 将测试的数据输入到主页的textarea里
 | 
			
		||||
        driver.find_element_by_xpath("//textarea[@name='content']").send_keys(Keys.CONTROL, "a")
 | 
			
		||||
        driver.find_element_by_xpath("//textarea[@name='content']").send_keys(test_input)
 | 
			
		||||
        time.sleep(1)
 | 
			
		||||
 | 
			
		||||
        # 点击按钮获取单词
 | 
			
		||||
        driver.find_element_by_xpath("//input[@value='get文章中的词频']").click()
 | 
			
		||||
        time.sleep(1)
 | 
			
		||||
 | 
			
		||||
        # 获取筛选后的单词
 | 
			
		||||
        words = driver.find_elements_by_xpath("//p/a")
 | 
			
		||||
 | 
			
		||||
        # 遍历获取到的单词,并判断单词与预期的相同
 | 
			
		||||
        for word in words:
 | 
			
		||||
            # 判断单词是否在预期结果中
 | 
			
		||||
            assert word.text in expected
 | 
			
		||||
            
 | 
			
		||||
        # 返回上一页网页
 | 
			
		||||
        driver.find_element_by_xpath("//input[@value='确定并返回']").click()
 | 
			
		||||
        time.sleep(0.1)
 | 
			
		||||
 | 
			
		||||
    except Exception as e:
 | 
			
		||||
        # 输出异常信息
 | 
			
		||||
        logging.error(e)
 | 
			
		||||
        # 关闭浏览器
 | 
			
		||||
        driver.quit()
 | 
			
		||||
    finally:
 | 
			
		||||
        driver.quit()
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,43 @@
 | 
			
		|||
''' Contributed by Lin Junhong et al. 2023-06.'''
 | 
			
		||||
 | 
			
		||||
import requests
 | 
			
		||||
import multiprocessing
 | 
			
		||||
import time
 | 
			
		||||
 | 
			
		||||
def stress(username):
 | 
			
		||||
    try:
 | 
			
		||||
        data = {
 | 
			
		||||
            'username': username,
 | 
			
		||||
            'password': '123123'
 | 
			
		||||
        }
 | 
			
		||||
        headers = {
 | 
			
		||||
            'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Mobile Safari/537.36 Edg/114.0.1823.51'
 | 
			
		||||
        }
 | 
			
		||||
        session = requests.session()
 | 
			
		||||
        response = session.post(url='http://127.0.0.1:5000/signup', data=data, headers=headers)
 | 
			
		||||
        print('Sign up ', response.status_code)
 | 
			
		||||
        time.sleep(0.5)
 | 
			
		||||
        response = session.post(url='http://127.0.0.1:5000/login', data=data, headers=headers)
 | 
			
		||||
        print('Sign in ', response.status_code)
 | 
			
		||||
        time.sleep(0.5)
 | 
			
		||||
        response = session.get(url=f'http://127.0.0.1:5000/{username}/userpage', headers=headers)
 | 
			
		||||
        print('User page', response.status_code)
 | 
			
		||||
        time.sleep(0.5)
 | 
			
		||||
        print(session.cookies)
 | 
			
		||||
        for i in range(5):
 | 
			
		||||
            response = session.get(url=f'http://127.0.0.1:5000/get_next_article/{username}', headers=headers, cookies=session.cookies)
 | 
			
		||||
            time.sleep(0.5)
 | 
			
		||||
            print(f'Next page ({i}) [{username}]')
 | 
			
		||||
            print(response.status_code)
 | 
			
		||||
            print(response.json()['today_article']['article_title'])
 | 
			
		||||
    except Exception as e:
 | 
			
		||||
        print(e)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    username = 'Learner'
 | 
			
		||||
    pool = multiprocessing.Pool(processes=10)
 | 
			
		||||
    for i in range(10):
 | 
			
		||||
        pool.apply_async(stress, (f'{username}{i}',))
 | 
			
		||||
    pool.close()
 | 
			
		||||
    pool.join()
 | 
			
		||||
| 
						 | 
				
			
			@ -15,6 +15,9 @@ from wordfreqCMD import sort_in_descending_order
 | 
			
		|||
import pickle_idea
 | 
			
		||||
import pickle_idea2
 | 
			
		||||
 | 
			
		||||
import logging
 | 
			
		||||
logging.basicConfig(filename='log.txt', format='%(asctime)s %(message)s', level=logging.DEBUG)
 | 
			
		||||
 | 
			
		||||
# 初始化蓝图
 | 
			
		||||
userService = Blueprint("user_bp", __name__)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -32,7 +35,9 @@ def get_next_article(username):
 | 
			
		|||
        else:  # 当前不为“null”,直接 index+=1
 | 
			
		||||
            visited_articles["index"] += 1
 | 
			
		||||
        session["visited_articles"] = visited_articles
 | 
			
		||||
        logging.debug('/get_next_article: start calling get_today_arcile()')
 | 
			
		||||
        visited_articles, today_article, result_of_generate_article = get_today_article(user_freq_record, session.get('visited_articles'))
 | 
			
		||||
        logging.debug('/get_next_arcile: done.')
 | 
			
		||||
        data = {
 | 
			
		||||
            'visited_articles': visited_articles,
 | 
			
		||||
            'today_article': today_article,
 | 
			
		||||
| 
						 | 
				
			
			@ -129,7 +134,7 @@ def userpage(username):
 | 
			
		|||
    user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
 | 
			
		||||
 | 
			
		||||
    if request.method == 'POST':  # when we submit a form
 | 
			
		||||
        content = escape(request.form['content'])
 | 
			
		||||
        content = request.form['content']
 | 
			
		||||
        f = WordFreq(content)
 | 
			
		||||
        lst = f.get_freq()
 | 
			
		||||
        return render_template('userpage_post.html',username=username,lst = lst, yml=Yaml.yml)
 | 
			
		||||
| 
						 | 
				
			
			@ -176,7 +181,11 @@ def user_mark_word(username):
 | 
			
		|||
        for word in request.form.getlist('marked'):
 | 
			
		||||
            lst.append((word, [get_time()]))
 | 
			
		||||
        d = pickle_idea2.merge_frequency(lst, lst_history)
 | 
			
		||||
        pickle_idea2.save_frequency_to_pickle(d, user_freq_record)
 | 
			
		||||
        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'))))
 | 
			
		||||
        return redirect(url_for('user_bp.userpage', username=username))
 | 
			
		||||
    else:
 | 
			
		||||
        return 'Under construction'
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,6 +4,7 @@
 | 
			
		|||
###########################################################################
 | 
			
		||||
 | 
			
		||||
import collections
 | 
			
		||||
import html
 | 
			
		||||
import string
 | 
			
		||||
import operator
 | 
			
		||||
import os, sys # 引入模块sys,因为我要用里面的sys.argv列表中的信息来读取命令行参数。
 | 
			
		||||
| 
						 | 
				
			
			@ -39,7 +40,8 @@ def file2str(fname):#文件转字符
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
def remove_punctuation(s): # 这里是s是形参 (parameter)。函数被调用时才给s赋值。
 | 
			
		||||
    special_characters = '\_©~<=>+/[]*&$%^@.,?!:;#()"“”—‘’{}|' # 把里面的字符都去掉
 | 
			
		||||
    special_characters = '\_©~<=>+/[]*&$%^@.,?!:;#()"“”—‘’{}|,。?!¥……()、《》:;·' # 把里面的字符都去掉
 | 
			
		||||
    s = html.unescape(s) # 将HTML实体转换为对应的字符,比如<会被识别为小于号
 | 
			
		||||
    for c in special_characters:
 | 
			
		||||
        s = s.replace(c, ' ') # 防止出现把 apple,apple 移掉逗号后变成 appleapple 情况
 | 
			
		||||
    s = s.replace('--', ' ')
 | 
			
		||||
| 
						 | 
				
			
			@ -104,7 +106,7 @@ if __name__ == '__main__':
 | 
			
		|||
        print('%s\t%d\t%s' % (x[0], x[1], youdao_link(x[0])))#函数导出
 | 
			
		||||
 | 
			
		||||
    # 把频率的结果放result.html中
 | 
			
		||||
    make_html_page(sort_in_descending_order(L), 'result.html') 
 | 
			
		||||
    make_html_page(sort_in_descending_order(L), 'result.html')
 | 
			
		||||
 | 
			
		||||
    print('\nHistory:\n')
 | 
			
		||||
    if os.path.exists('frequency.p'):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,319 @@
 | 
			
		|||
body {
 | 
			
		||||
  font-family: Helvetica, Arial, sans-serif;
 | 
			
		||||
  font-size: 12px;
 | 
			
		||||
  /* do not increase min-width as some may use split screens */
 | 
			
		||||
  min-width: 800px;
 | 
			
		||||
  color: #999;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
h1 {
 | 
			
		||||
  font-size: 24px;
 | 
			
		||||
  color: black;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
h2 {
 | 
			
		||||
  font-size: 16px;
 | 
			
		||||
  color: black;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
p {
 | 
			
		||||
  color: black;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
a {
 | 
			
		||||
  color: #999;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
table {
 | 
			
		||||
  border-collapse: collapse;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/******************************
 | 
			
		||||
 * SUMMARY INFORMATION
 | 
			
		||||
 ******************************/
 | 
			
		||||
#environment td {
 | 
			
		||||
  padding: 5px;
 | 
			
		||||
  border: 1px solid #e6e6e6;
 | 
			
		||||
  vertical-align: top;
 | 
			
		||||
}
 | 
			
		||||
#environment tr:nth-child(odd) {
 | 
			
		||||
  background-color: #f6f6f6;
 | 
			
		||||
}
 | 
			
		||||
#environment ul {
 | 
			
		||||
  margin: 0;
 | 
			
		||||
  padding: 0 20px;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/******************************
 | 
			
		||||
 * TEST RESULT COLORS
 | 
			
		||||
 ******************************/
 | 
			
		||||
span.passed,
 | 
			
		||||
.passed .col-result {
 | 
			
		||||
  color: green;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
span.skipped,
 | 
			
		||||
span.xfailed,
 | 
			
		||||
span.rerun,
 | 
			
		||||
.skipped .col-result,
 | 
			
		||||
.xfailed .col-result,
 | 
			
		||||
.rerun .col-result {
 | 
			
		||||
  color: orange;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
span.error,
 | 
			
		||||
span.failed,
 | 
			
		||||
span.xpassed,
 | 
			
		||||
.error .col-result,
 | 
			
		||||
.failed .col-result,
 | 
			
		||||
.xpassed .col-result {
 | 
			
		||||
  color: red;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.col-links__extra {
 | 
			
		||||
  margin-right: 3px;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/******************************
 | 
			
		||||
 * RESULTS TABLE
 | 
			
		||||
 *
 | 
			
		||||
 * 1. Table Layout
 | 
			
		||||
 * 2. Extra
 | 
			
		||||
 * 3. Sorting items
 | 
			
		||||
 *
 | 
			
		||||
 ******************************/
 | 
			
		||||
/*------------------
 | 
			
		||||
 * 1. Table Layout
 | 
			
		||||
 *------------------*/
 | 
			
		||||
#results-table {
 | 
			
		||||
  border: 1px solid #e6e6e6;
 | 
			
		||||
  color: #999;
 | 
			
		||||
  font-size: 12px;
 | 
			
		||||
  width: 100%;
 | 
			
		||||
}
 | 
			
		||||
#results-table th,
 | 
			
		||||
#results-table td {
 | 
			
		||||
  padding: 5px;
 | 
			
		||||
  border: 1px solid #e6e6e6;
 | 
			
		||||
  text-align: left;
 | 
			
		||||
}
 | 
			
		||||
#results-table th {
 | 
			
		||||
  font-weight: bold;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*------------------
 | 
			
		||||
 * 2. Extra
 | 
			
		||||
 *------------------*/
 | 
			
		||||
.logwrapper {
 | 
			
		||||
  max-height: 230px;
 | 
			
		||||
  overflow-y: scroll;
 | 
			
		||||
  background-color: #e6e6e6;
 | 
			
		||||
}
 | 
			
		||||
.logwrapper.expanded {
 | 
			
		||||
  max-height: none;
 | 
			
		||||
}
 | 
			
		||||
.logwrapper.expanded .logexpander:after {
 | 
			
		||||
  content: "collapse [-]";
 | 
			
		||||
}
 | 
			
		||||
.logwrapper .logexpander {
 | 
			
		||||
  z-index: 1;
 | 
			
		||||
  position: sticky;
 | 
			
		||||
  top: 10px;
 | 
			
		||||
  width: max-content;
 | 
			
		||||
  border: 1px solid;
 | 
			
		||||
  border-radius: 3px;
 | 
			
		||||
  padding: 5px 7px;
 | 
			
		||||
  margin: 10px 0 10px calc(100% - 80px);
 | 
			
		||||
  cursor: pointer;
 | 
			
		||||
  background-color: #e6e6e6;
 | 
			
		||||
}
 | 
			
		||||
.logwrapper .logexpander:after {
 | 
			
		||||
  content: "expand [+]";
 | 
			
		||||
}
 | 
			
		||||
.logwrapper .logexpander:hover {
 | 
			
		||||
  color: #000;
 | 
			
		||||
  border-color: #000;
 | 
			
		||||
}
 | 
			
		||||
.logwrapper .log {
 | 
			
		||||
  min-height: 40px;
 | 
			
		||||
  position: relative;
 | 
			
		||||
  top: -50px;
 | 
			
		||||
  height: calc(100% + 50px);
 | 
			
		||||
  border: 1px solid #e6e6e6;
 | 
			
		||||
  color: black;
 | 
			
		||||
  display: block;
 | 
			
		||||
  font-family: "Courier New", Courier, monospace;
 | 
			
		||||
  padding: 5px;
 | 
			
		||||
  padding-right: 80px;
 | 
			
		||||
  white-space: pre-wrap;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
div.media {
 | 
			
		||||
  border: 1px solid #e6e6e6;
 | 
			
		||||
  float: right;
 | 
			
		||||
  height: 240px;
 | 
			
		||||
  margin: 0 5px;
 | 
			
		||||
  overflow: hidden;
 | 
			
		||||
  width: 320px;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.media-container {
 | 
			
		||||
  display: grid;
 | 
			
		||||
  grid-template-columns: 25px auto 25px;
 | 
			
		||||
  align-items: center;
 | 
			
		||||
  flex: 1 1;
 | 
			
		||||
  overflow: hidden;
 | 
			
		||||
  height: 200px;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.media-container--fullscreen {
 | 
			
		||||
  grid-template-columns: 0px auto 0px;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.media-container__nav--right,
 | 
			
		||||
.media-container__nav--left {
 | 
			
		||||
  text-align: center;
 | 
			
		||||
  cursor: pointer;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.media-container__viewport {
 | 
			
		||||
  cursor: pointer;
 | 
			
		||||
  text-align: center;
 | 
			
		||||
  height: inherit;
 | 
			
		||||
}
 | 
			
		||||
.media-container__viewport img,
 | 
			
		||||
.media-container__viewport video {
 | 
			
		||||
  object-fit: cover;
 | 
			
		||||
  width: 100%;
 | 
			
		||||
  max-height: 100%;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.media__name,
 | 
			
		||||
.media__counter {
 | 
			
		||||
  display: flex;
 | 
			
		||||
  flex-direction: row;
 | 
			
		||||
  justify-content: space-around;
 | 
			
		||||
  flex: 0 0 25px;
 | 
			
		||||
  align-items: center;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.collapsible td:not(.col-links) {
 | 
			
		||||
  cursor: pointer;
 | 
			
		||||
}
 | 
			
		||||
.collapsible td:not(.col-links):hover::after {
 | 
			
		||||
  color: #bbb;
 | 
			
		||||
  font-style: italic;
 | 
			
		||||
  cursor: pointer;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.col-result {
 | 
			
		||||
  width: 130px;
 | 
			
		||||
}
 | 
			
		||||
.col-result:hover::after {
 | 
			
		||||
  content: " (hide details)";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.col-result.collapsed:hover::after {
 | 
			
		||||
  content: " (show details)";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#environment-header h2:hover::after {
 | 
			
		||||
  content: " (hide details)";
 | 
			
		||||
  color: #bbb;
 | 
			
		||||
  font-style: italic;
 | 
			
		||||
  cursor: pointer;
 | 
			
		||||
  font-size: 12px;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#environment-header.collapsed h2:hover::after {
 | 
			
		||||
  content: " (show details)";
 | 
			
		||||
  color: #bbb;
 | 
			
		||||
  font-style: italic;
 | 
			
		||||
  cursor: pointer;
 | 
			
		||||
  font-size: 12px;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*------------------
 | 
			
		||||
 * 3. Sorting items
 | 
			
		||||
 *------------------*/
 | 
			
		||||
.sortable {
 | 
			
		||||
  cursor: pointer;
 | 
			
		||||
}
 | 
			
		||||
.sortable.desc:after {
 | 
			
		||||
  content: " ";
 | 
			
		||||
  position: relative;
 | 
			
		||||
  left: 5px;
 | 
			
		||||
  bottom: -12.5px;
 | 
			
		||||
  border: 10px solid #4caf50;
 | 
			
		||||
  border-bottom: 0;
 | 
			
		||||
  border-left-color: transparent;
 | 
			
		||||
  border-right-color: transparent;
 | 
			
		||||
}
 | 
			
		||||
.sortable.asc:after {
 | 
			
		||||
  content: " ";
 | 
			
		||||
  position: relative;
 | 
			
		||||
  left: 5px;
 | 
			
		||||
  bottom: 12.5px;
 | 
			
		||||
  border: 10px solid #4caf50;
 | 
			
		||||
  border-top: 0;
 | 
			
		||||
  border-left-color: transparent;
 | 
			
		||||
  border-right-color: transparent;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.hidden, .summary__reload__button.hidden {
 | 
			
		||||
  display: none;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.summary__data {
 | 
			
		||||
  flex: 0 0 550px;
 | 
			
		||||
}
 | 
			
		||||
.summary__reload {
 | 
			
		||||
  flex: 1 1;
 | 
			
		||||
  display: flex;
 | 
			
		||||
  justify-content: center;
 | 
			
		||||
}
 | 
			
		||||
.summary__reload__button {
 | 
			
		||||
  flex: 0 0 300px;
 | 
			
		||||
  display: flex;
 | 
			
		||||
  color: white;
 | 
			
		||||
  font-weight: bold;
 | 
			
		||||
  background-color: #4caf50;
 | 
			
		||||
  text-align: center;
 | 
			
		||||
  justify-content: center;
 | 
			
		||||
  align-items: center;
 | 
			
		||||
  border-radius: 3px;
 | 
			
		||||
  cursor: pointer;
 | 
			
		||||
}
 | 
			
		||||
.summary__reload__button:hover {
 | 
			
		||||
  background-color: #46a049;
 | 
			
		||||
}
 | 
			
		||||
.summary__spacer {
 | 
			
		||||
  flex: 0 0 550px;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.controls {
 | 
			
		||||
  display: flex;
 | 
			
		||||
  justify-content: space-between;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.filters,
 | 
			
		||||
.collapse {
 | 
			
		||||
  display: flex;
 | 
			
		||||
  align-items: center;
 | 
			
		||||
}
 | 
			
		||||
.filters button,
 | 
			
		||||
.collapse button {
 | 
			
		||||
  color: #999;
 | 
			
		||||
  border: none;
 | 
			
		||||
  background: none;
 | 
			
		||||
  cursor: pointer;
 | 
			
		||||
  text-decoration: underline;
 | 
			
		||||
}
 | 
			
		||||
.filters button:hover,
 | 
			
		||||
.collapse button:hover {
 | 
			
		||||
  color: #ccc;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.filter__label {
 | 
			
		||||
  margin-right: 10px;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										7
									
								
								build.sh
								
								
								
								
							
							
						
						
									
										7
									
								
								build.sh
								
								
								
								
							| 
						 | 
				
			
			@ -2,10 +2,7 @@
 | 
			
		|||
 | 
			
		||||
DEPLOYMENT_DIR=/home/lanhui/englishpal2/EnglishPal
 | 
			
		||||
cd $DEPLOYMENT_DIR
 | 
			
		||||
 | 
			
		||||
# Install dependencies
 | 
			
		||||
 | 
			
		||||
pip3 install -r requirements.txt
 | 
			
		||||
pwd
 | 
			
		||||
 | 
			
		||||
# Stop service
 | 
			
		||||
sudo docker stop EnglishPal
 | 
			
		||||
| 
						 | 
				
			
			@ -15,7 +12,7 @@ sudo docker rm EnglishPal
 | 
			
		|||
sudo docker build -t englishpal .
 | 
			
		||||
 | 
			
		||||
# Run the application
 | 
			
		||||
sudo docker run --restart=always -d --name EnglishPal -p 90:80 -v ${DEPLOYMENT_DIR}/app/static/frequency:/app/static/frequency -v ${DEPLOYMENT_DIR}/app/static/:/app/static/ -t englishpal  # for permanently saving data
 | 
			
		||||
sudo docker run --restart=always -d --name EnglishPal -p 90:80  -v ${DEPLOYMENT_DIR}/app/static/frequency:/app/static/frequency --mount type=volume,src=englishpal-db,target=/app/db -t englishpal # for permanently saving data
 | 
			
		||||
 | 
			
		||||
# Save space.  Run it after sudo docker run
 | 
			
		||||
sudo docker system prune -a -f
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,770 @@
 | 
			
		|||
<!DOCTYPE html>
 | 
			
		||||
<html>
 | 
			
		||||
  <head>
 | 
			
		||||
    <meta charset="utf-8"/>
 | 
			
		||||
    <title id="head-title">pytest_report.html</title>
 | 
			
		||||
      <link href="assets\style.css" rel="stylesheet" type="text/css"/>
 | 
			
		||||
  </head>
 | 
			
		||||
  <body>
 | 
			
		||||
    <h1 id="title">pytest_report.html</h1>
 | 
			
		||||
    <p>Report generated on 04-Jun-2024 at 13:06:16 by <a href="https://pypi.python.org/pypi/pytest-html">pytest-html</a>
 | 
			
		||||
        v4.1.1</p>
 | 
			
		||||
    <div id="environment-header">
 | 
			
		||||
      <h2>Environment</h2>
 | 
			
		||||
    </div>
 | 
			
		||||
    <table id="environment"></table>
 | 
			
		||||
    <!-- TEMPLATES -->
 | 
			
		||||
      <template id="template_environment_row">
 | 
			
		||||
      <tr>
 | 
			
		||||
        <td></td>
 | 
			
		||||
        <td></td>
 | 
			
		||||
      </tr>
 | 
			
		||||
    </template>
 | 
			
		||||
    <template id="template_results-table__body--empty">
 | 
			
		||||
      <tbody class="results-table-row">
 | 
			
		||||
        <tr id="not-found-message">
 | 
			
		||||
          <td colspan="4">No results found. Check the filters.</th>
 | 
			
		||||
        </tr>
 | 
			
		||||
    </template>
 | 
			
		||||
    <template id="template_results-table__tbody">
 | 
			
		||||
      <tbody class="results-table-row">
 | 
			
		||||
        <tr class="collapsible">
 | 
			
		||||
        </tr>
 | 
			
		||||
        <tr class="extras-row">
 | 
			
		||||
          <td class="extra" colspan="4">
 | 
			
		||||
            <div class="extraHTML"></div>
 | 
			
		||||
            <div class="media">
 | 
			
		||||
              <div class="media-container">
 | 
			
		||||
                  <div class="media-container__nav--left"><</div>
 | 
			
		||||
                  <div class="media-container__viewport">
 | 
			
		||||
                    <img src="" />
 | 
			
		||||
                    <video controls>
 | 
			
		||||
                      <source src="" type="video/mp4">
 | 
			
		||||
                    </video>
 | 
			
		||||
                  </div>
 | 
			
		||||
                  <div class="media-container__nav--right">></div>
 | 
			
		||||
                </div>
 | 
			
		||||
                <div class="media__name"></div>
 | 
			
		||||
                <div class="media__counter"></div>
 | 
			
		||||
            </div>
 | 
			
		||||
            <div class="logwrapper">
 | 
			
		||||
              <div class="logexpander"></div>
 | 
			
		||||
              <div class="log"></div>
 | 
			
		||||
            </div>
 | 
			
		||||
          </td>
 | 
			
		||||
        </tr>
 | 
			
		||||
      </tbody>
 | 
			
		||||
    </template>
 | 
			
		||||
    <!-- END TEMPLATES -->
 | 
			
		||||
    <div class="summary">
 | 
			
		||||
      <div class="summary__data">
 | 
			
		||||
        <h2>Summary</h2>
 | 
			
		||||
        <div class="additional-summary prefix">
 | 
			
		||||
        </div>
 | 
			
		||||
        <p class="run-count">0 test took 0 ms.</p>
 | 
			
		||||
        <p class="filter">(Un)check the boxes to filter the results.</p>
 | 
			
		||||
        <div class="summary__reload">
 | 
			
		||||
          <div class="summary__reload__button hidden" onclick="location.reload()">
 | 
			
		||||
            <div>There are still tests running. <br />Reload this page to get the latest results!</div>
 | 
			
		||||
          </div>
 | 
			
		||||
        </div>
 | 
			
		||||
        <div class="summary__spacer"></div>
 | 
			
		||||
        <div class="controls">
 | 
			
		||||
          <div class="filters">
 | 
			
		||||
            <input checked="true" class="filter" name="filter_checkbox" type="checkbox" data-test-result="failed" disabled/>
 | 
			
		||||
            <span class="failed">0 Failed,</span>
 | 
			
		||||
            <input checked="true" class="filter" name="filter_checkbox" type="checkbox" data-test-result="passed" disabled/>
 | 
			
		||||
            <span class="passed">0 Passed,</span>
 | 
			
		||||
            <input checked="true" class="filter" name="filter_checkbox" type="checkbox" data-test-result="skipped" disabled/>
 | 
			
		||||
            <span class="skipped">0 Skipped,</span>
 | 
			
		||||
            <input checked="true" class="filter" name="filter_checkbox" type="checkbox" data-test-result="xfailed" disabled/>
 | 
			
		||||
            <span class="xfailed">0 Expected failures,</span>
 | 
			
		||||
            <input checked="true" class="filter" name="filter_checkbox" type="checkbox" data-test-result="xpassed" disabled/>
 | 
			
		||||
            <span class="xpassed">0 Unexpected passes,</span>
 | 
			
		||||
            <input checked="true" class="filter" name="filter_checkbox" type="checkbox" data-test-result="error" disabled/>
 | 
			
		||||
            <span class="error">0 Errors,</span>
 | 
			
		||||
            <input checked="true" class="filter" name="filter_checkbox" type="checkbox" data-test-result="rerun" disabled/>
 | 
			
		||||
            <span class="rerun">0 Reruns</span>
 | 
			
		||||
          </div>
 | 
			
		||||
          <div class="collapse">
 | 
			
		||||
            <button id="show_all_details">Show all details</button> / <button id="hide_all_details">Hide all details</button>
 | 
			
		||||
          </div>
 | 
			
		||||
        </div>
 | 
			
		||||
      </div>
 | 
			
		||||
      <div class="additional-summary summary">
 | 
			
		||||
      </div>
 | 
			
		||||
      <div class="additional-summary postfix">
 | 
			
		||||
      </div>
 | 
			
		||||
    </div>
 | 
			
		||||
    <table id="results-table">
 | 
			
		||||
      <thead id="results-table-head">
 | 
			
		||||
        <tr>
 | 
			
		||||
          <th class="sortable" data-column-type="result">Result</th>
 | 
			
		||||
          <th class="sortable" data-column-type="testId">Test</th>
 | 
			
		||||
          <th class="sortable" data-column-type="duration">Duration</th>
 | 
			
		||||
          <th>Links</th>
 | 
			
		||||
        </tr>
 | 
			
		||||
      </thead>
 | 
			
		||||
    </table>
 | 
			
		||||
  </body>
 | 
			
		||||
  <footer>
 | 
			
		||||
    <div id="data-container" data-jsonblob="{"environment": {"Python": "3.12.2", "Platform": "Windows-11-10.0.22631-SP0", "Packages": {"pytest": "8.1.2", "pluggy": "1.5.0"}, "Plugins": {"html": "4.1.1", "metadata": "3.1.1"}}, "tests": {}, "renderCollapsed": ["passed"], "initialSort": "result", "title": "pytest_report.html"}"></div>
 | 
			
		||||
    <script>
 | 
			
		||||
      (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
 | 
			
		||||
const { getCollapsedCategory, setCollapsedIds } = require('./storage.js')
 | 
			
		||||
 | 
			
		||||
class DataManager {
 | 
			
		||||
    setManager(data) {
 | 
			
		||||
        const collapsedCategories = [...getCollapsedCategory(data.renderCollapsed)]
 | 
			
		||||
        const collapsedIds = []
 | 
			
		||||
        const tests = Object.values(data.tests).flat().map((test, index) => {
 | 
			
		||||
            const collapsed = collapsedCategories.includes(test.result.toLowerCase())
 | 
			
		||||
            const id = `test_${index}`
 | 
			
		||||
            if (collapsed) {
 | 
			
		||||
                collapsedIds.push(id)
 | 
			
		||||
            }
 | 
			
		||||
            return {
 | 
			
		||||
                ...test,
 | 
			
		||||
                id,
 | 
			
		||||
                collapsed,
 | 
			
		||||
            }
 | 
			
		||||
        })
 | 
			
		||||
        const dataBlob = { ...data, tests }
 | 
			
		||||
        this.data = { ...dataBlob }
 | 
			
		||||
        this.renderData = { ...dataBlob }
 | 
			
		||||
        setCollapsedIds(collapsedIds)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    get allData() {
 | 
			
		||||
        return { ...this.data }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    resetRender() {
 | 
			
		||||
        this.renderData = { ...this.data }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    setRender(data) {
 | 
			
		||||
        this.renderData.tests = [...data]
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    toggleCollapsedItem(id) {
 | 
			
		||||
        this.renderData.tests = this.renderData.tests.map((test) =>
 | 
			
		||||
            test.id === id ? { ...test, collapsed: !test.collapsed } : test,
 | 
			
		||||
        )
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    set allCollapsed(collapsed) {
 | 
			
		||||
        this.renderData = { ...this.renderData, tests: [...this.renderData.tests.map((test) => (
 | 
			
		||||
            { ...test, collapsed }
 | 
			
		||||
        ))] }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    get testSubset() {
 | 
			
		||||
        return [...this.renderData.tests]
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    get environment() {
 | 
			
		||||
        return this.renderData.environment
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    get initialSort() {
 | 
			
		||||
        return this.data.initialSort
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
    manager: new DataManager(),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
},{"./storage.js":8}],2:[function(require,module,exports){
 | 
			
		||||
const mediaViewer = require('./mediaviewer.js')
 | 
			
		||||
const templateEnvRow = document.getElementById('template_environment_row')
 | 
			
		||||
const templateResult = document.getElementById('template_results-table__tbody')
 | 
			
		||||
 | 
			
		||||
function htmlToElements(html) {
 | 
			
		||||
    const temp = document.createElement('template')
 | 
			
		||||
    temp.innerHTML = html
 | 
			
		||||
    return temp.content.childNodes
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const find = (selector, elem) => {
 | 
			
		||||
    if (!elem) {
 | 
			
		||||
        elem = document
 | 
			
		||||
    }
 | 
			
		||||
    return elem.querySelector(selector)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const findAll = (selector, elem) => {
 | 
			
		||||
    if (!elem) {
 | 
			
		||||
        elem = document
 | 
			
		||||
    }
 | 
			
		||||
    return [...elem.querySelectorAll(selector)]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const dom = {
 | 
			
		||||
    getStaticRow: (key, value) => {
 | 
			
		||||
        const envRow = templateEnvRow.content.cloneNode(true)
 | 
			
		||||
        const isObj = typeof value === 'object' && value !== null
 | 
			
		||||
        const values = isObj ? Object.keys(value).map((k) => `${k}: ${value[k]}`) : null
 | 
			
		||||
 | 
			
		||||
        const valuesElement = htmlToElements(
 | 
			
		||||
            values ? `<ul>${values.map((val) => `<li>${val}</li>`).join('')}<ul>` : `<div>${value}</div>`)[0]
 | 
			
		||||
        const td = findAll('td', envRow)
 | 
			
		||||
        td[0].textContent = key
 | 
			
		||||
        td[1].appendChild(valuesElement)
 | 
			
		||||
 | 
			
		||||
        return envRow
 | 
			
		||||
    },
 | 
			
		||||
    getResultTBody: ({ testId, id, log, extras, resultsTableRow, tableHtml, result, collapsed }) => {
 | 
			
		||||
        const resultBody = templateResult.content.cloneNode(true)
 | 
			
		||||
        resultBody.querySelector('tbody').classList.add(result.toLowerCase())
 | 
			
		||||
        resultBody.querySelector('tbody').id = testId
 | 
			
		||||
        resultBody.querySelector('.collapsible').dataset.id = id
 | 
			
		||||
 | 
			
		||||
        resultsTableRow.forEach((html) => {
 | 
			
		||||
            const t = document.createElement('template')
 | 
			
		||||
            t.innerHTML = html
 | 
			
		||||
            resultBody.querySelector('.collapsible').appendChild(t.content)
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
        if (log) {
 | 
			
		||||
            // Wrap lines starting with "E" with span.error to color those lines red
 | 
			
		||||
            const wrappedLog = log.replace(/^E.*$/gm, (match) => `<span class="error">${match}</span>`)
 | 
			
		||||
            resultBody.querySelector('.log').innerHTML = wrappedLog
 | 
			
		||||
        } else {
 | 
			
		||||
            resultBody.querySelector('.log').remove()
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (collapsed) {
 | 
			
		||||
            resultBody.querySelector('.collapsible > td')?.classList.add('collapsed')
 | 
			
		||||
            resultBody.querySelector('.extras-row').classList.add('hidden')
 | 
			
		||||
        } else {
 | 
			
		||||
            resultBody.querySelector('.collapsible > td')?.classList.remove('collapsed')
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const media = []
 | 
			
		||||
        extras?.forEach(({ name, format_type, content }) => {
 | 
			
		||||
            if (['image', 'video'].includes(format_type)) {
 | 
			
		||||
                media.push({ path: content, name, format_type })
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (format_type === 'html') {
 | 
			
		||||
                resultBody.querySelector('.extraHTML').insertAdjacentHTML('beforeend', `<div>${content}</div>`)
 | 
			
		||||
            }
 | 
			
		||||
        })
 | 
			
		||||
        mediaViewer.setup(resultBody, media)
 | 
			
		||||
 | 
			
		||||
        // Add custom html from the pytest_html_results_table_html hook
 | 
			
		||||
        tableHtml?.forEach((item) => {
 | 
			
		||||
            resultBody.querySelector('td[class="extra"]').insertAdjacentHTML('beforeend', item)
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
        return resultBody
 | 
			
		||||
    },
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
    dom,
 | 
			
		||||
    htmlToElements,
 | 
			
		||||
    find,
 | 
			
		||||
    findAll,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
},{"./mediaviewer.js":6}],3:[function(require,module,exports){
 | 
			
		||||
const { manager } = require('./datamanager.js')
 | 
			
		||||
const { doSort } = require('./sort.js')
 | 
			
		||||
const storageModule = require('./storage.js')
 | 
			
		||||
 | 
			
		||||
const getFilteredSubSet = (filter) =>
 | 
			
		||||
    manager.allData.tests.filter(({ result }) => filter.includes(result.toLowerCase()))
 | 
			
		||||
 | 
			
		||||
const doInitFilter = () => {
 | 
			
		||||
    const currentFilter = storageModule.getVisible()
 | 
			
		||||
    const filteredSubset = getFilteredSubSet(currentFilter)
 | 
			
		||||
    manager.setRender(filteredSubset)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const doFilter = (type, show) => {
 | 
			
		||||
    if (show) {
 | 
			
		||||
        storageModule.showCategory(type)
 | 
			
		||||
    } else {
 | 
			
		||||
        storageModule.hideCategory(type)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const currentFilter = storageModule.getVisible()
 | 
			
		||||
    const filteredSubset = getFilteredSubSet(currentFilter)
 | 
			
		||||
    manager.setRender(filteredSubset)
 | 
			
		||||
 | 
			
		||||
    const sortColumn = storageModule.getSort()
 | 
			
		||||
    doSort(sortColumn, true)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
    doFilter,
 | 
			
		||||
    doInitFilter,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
},{"./datamanager.js":1,"./sort.js":7,"./storage.js":8}],4:[function(require,module,exports){
 | 
			
		||||
const { redraw, bindEvents, renderStatic } = require('./main.js')
 | 
			
		||||
const { doInitFilter } = require('./filter.js')
 | 
			
		||||
const { doInitSort } = require('./sort.js')
 | 
			
		||||
const { manager } = require('./datamanager.js')
 | 
			
		||||
const data = JSON.parse(document.getElementById('data-container').dataset.jsonblob)
 | 
			
		||||
 | 
			
		||||
function init() {
 | 
			
		||||
    manager.setManager(data)
 | 
			
		||||
    doInitFilter()
 | 
			
		||||
    doInitSort()
 | 
			
		||||
    renderStatic()
 | 
			
		||||
    redraw()
 | 
			
		||||
    bindEvents()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
init()
 | 
			
		||||
 | 
			
		||||
},{"./datamanager.js":1,"./filter.js":3,"./main.js":5,"./sort.js":7}],5:[function(require,module,exports){
 | 
			
		||||
const { dom, find, findAll } = require('./dom.js')
 | 
			
		||||
const { manager } = require('./datamanager.js')
 | 
			
		||||
const { doSort } = require('./sort.js')
 | 
			
		||||
const { doFilter } = require('./filter.js')
 | 
			
		||||
const {
 | 
			
		||||
    getVisible,
 | 
			
		||||
    getCollapsedIds,
 | 
			
		||||
    setCollapsedIds,
 | 
			
		||||
    getSort,
 | 
			
		||||
    getSortDirection,
 | 
			
		||||
    possibleFilters,
 | 
			
		||||
} = require('./storage.js')
 | 
			
		||||
 | 
			
		||||
const removeChildren = (node) => {
 | 
			
		||||
    while (node.firstChild) {
 | 
			
		||||
        node.removeChild(node.firstChild)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const renderStatic = () => {
 | 
			
		||||
    const renderEnvironmentTable = () => {
 | 
			
		||||
        const environment = manager.environment
 | 
			
		||||
        const rows = Object.keys(environment).map((key) => dom.getStaticRow(key, environment[key]))
 | 
			
		||||
        const table = document.getElementById('environment')
 | 
			
		||||
        removeChildren(table)
 | 
			
		||||
        rows.forEach((row) => table.appendChild(row))
 | 
			
		||||
    }
 | 
			
		||||
    renderEnvironmentTable()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const addItemToggleListener = (elem) => {
 | 
			
		||||
    elem.addEventListener('click', ({ target }) => {
 | 
			
		||||
        const id = target.parentElement.dataset.id
 | 
			
		||||
        manager.toggleCollapsedItem(id)
 | 
			
		||||
 | 
			
		||||
        const collapsedIds = getCollapsedIds()
 | 
			
		||||
        if (collapsedIds.includes(id)) {
 | 
			
		||||
            const updated = collapsedIds.filter((item) => item !== id)
 | 
			
		||||
            setCollapsedIds(updated)
 | 
			
		||||
        } else {
 | 
			
		||||
            collapsedIds.push(id)
 | 
			
		||||
            setCollapsedIds(collapsedIds)
 | 
			
		||||
        }
 | 
			
		||||
        redraw()
 | 
			
		||||
    })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const renderContent = (tests) => {
 | 
			
		||||
    const sortAttr = getSort(manager.initialSort)
 | 
			
		||||
    const sortAsc = JSON.parse(getSortDirection())
 | 
			
		||||
    const rows = tests.map(dom.getResultTBody)
 | 
			
		||||
    const table = document.getElementById('results-table')
 | 
			
		||||
    const tableHeader = document.getElementById('results-table-head')
 | 
			
		||||
 | 
			
		||||
    const newTable = document.createElement('table')
 | 
			
		||||
    newTable.id = 'results-table'
 | 
			
		||||
 | 
			
		||||
    // remove all sorting classes and set the relevant
 | 
			
		||||
    findAll('.sortable', tableHeader).forEach((elem) => elem.classList.remove('asc', 'desc'))
 | 
			
		||||
    tableHeader.querySelector(`.sortable[data-column-type="${sortAttr}"]`)?.classList.add(sortAsc ? 'desc' : 'asc')
 | 
			
		||||
    newTable.appendChild(tableHeader)
 | 
			
		||||
 | 
			
		||||
    if (!rows.length) {
 | 
			
		||||
        const emptyTable = document.getElementById('template_results-table__body--empty').content.cloneNode(true)
 | 
			
		||||
        newTable.appendChild(emptyTable)
 | 
			
		||||
    } else {
 | 
			
		||||
        rows.forEach((row) => {
 | 
			
		||||
            if (!!row) {
 | 
			
		||||
                findAll('.collapsible td:not(.col-links', row).forEach(addItemToggleListener)
 | 
			
		||||
                find('.logexpander', row).addEventListener('click',
 | 
			
		||||
                    (evt) => evt.target.parentNode.classList.toggle('expanded'),
 | 
			
		||||
                )
 | 
			
		||||
                newTable.appendChild(row)
 | 
			
		||||
            }
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    table.replaceWith(newTable)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const renderDerived = () => {
 | 
			
		||||
    const currentFilter = getVisible()
 | 
			
		||||
    possibleFilters.forEach((result) => {
 | 
			
		||||
        const input = document.querySelector(`input[data-test-result="${result}"]`)
 | 
			
		||||
        input.checked = currentFilter.includes(result)
 | 
			
		||||
    })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const bindEvents = () => {
 | 
			
		||||
    const filterColumn = (evt) => {
 | 
			
		||||
        const { target: element } = evt
 | 
			
		||||
        const { testResult } = element.dataset
 | 
			
		||||
 | 
			
		||||
        doFilter(testResult, element.checked)
 | 
			
		||||
        const collapsedIds = getCollapsedIds()
 | 
			
		||||
        const updated = manager.renderData.tests.map((test) => {
 | 
			
		||||
            return {
 | 
			
		||||
                ...test,
 | 
			
		||||
                collapsed: collapsedIds.includes(test.id),
 | 
			
		||||
            }
 | 
			
		||||
        })
 | 
			
		||||
        manager.setRender(updated)
 | 
			
		||||
        redraw()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const header = document.getElementById('environment-header')
 | 
			
		||||
    header.addEventListener('click', () => {
 | 
			
		||||
        const table = document.getElementById('environment')
 | 
			
		||||
        table.classList.toggle('hidden')
 | 
			
		||||
        header.classList.toggle('collapsed')
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    findAll('input[name="filter_checkbox"]').forEach((elem) => {
 | 
			
		||||
        elem.addEventListener('click', filterColumn)
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    findAll('.sortable').forEach((elem) => {
 | 
			
		||||
        elem.addEventListener('click', (evt) => {
 | 
			
		||||
            const { target: element } = evt
 | 
			
		||||
            const { columnType } = element.dataset
 | 
			
		||||
            doSort(columnType)
 | 
			
		||||
            redraw()
 | 
			
		||||
        })
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    document.getElementById('show_all_details').addEventListener('click', () => {
 | 
			
		||||
        manager.allCollapsed = false
 | 
			
		||||
        setCollapsedIds([])
 | 
			
		||||
        redraw()
 | 
			
		||||
    })
 | 
			
		||||
    document.getElementById('hide_all_details').addEventListener('click', () => {
 | 
			
		||||
        manager.allCollapsed = true
 | 
			
		||||
        const allIds = manager.renderData.tests.map((test) => test.id)
 | 
			
		||||
        setCollapsedIds(allIds)
 | 
			
		||||
        redraw()
 | 
			
		||||
    })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const redraw = () => {
 | 
			
		||||
    const { testSubset } = manager
 | 
			
		||||
 | 
			
		||||
    renderContent(testSubset)
 | 
			
		||||
    renderDerived()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
    redraw,
 | 
			
		||||
    bindEvents,
 | 
			
		||||
    renderStatic,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
},{"./datamanager.js":1,"./dom.js":2,"./filter.js":3,"./sort.js":7,"./storage.js":8}],6:[function(require,module,exports){
 | 
			
		||||
class MediaViewer {
 | 
			
		||||
    constructor(assets) {
 | 
			
		||||
        this.assets = assets
 | 
			
		||||
        this.index = 0
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    nextActive() {
 | 
			
		||||
        this.index = this.index === this.assets.length - 1 ? 0 : this.index + 1
 | 
			
		||||
        return [this.activeFile, this.index]
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    prevActive() {
 | 
			
		||||
        this.index = this.index === 0 ? this.assets.length - 1 : this.index -1
 | 
			
		||||
        return [this.activeFile, this.index]
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    get currentIndex() {
 | 
			
		||||
        return this.index
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    get activeFile() {
 | 
			
		||||
        return this.assets[this.index]
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
const setup = (resultBody, assets) => {
 | 
			
		||||
    if (!assets.length) {
 | 
			
		||||
        resultBody.querySelector('.media').classList.add('hidden')
 | 
			
		||||
        return
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const mediaViewer = new MediaViewer(assets)
 | 
			
		||||
    const container = resultBody.querySelector('.media-container')
 | 
			
		||||
    const leftArrow = resultBody.querySelector('.media-container__nav--left')
 | 
			
		||||
    const rightArrow = resultBody.querySelector('.media-container__nav--right')
 | 
			
		||||
    const mediaName = resultBody.querySelector('.media__name')
 | 
			
		||||
    const counter = resultBody.querySelector('.media__counter')
 | 
			
		||||
    const imageEl = resultBody.querySelector('img')
 | 
			
		||||
    const sourceEl = resultBody.querySelector('source')
 | 
			
		||||
    const videoEl = resultBody.querySelector('video')
 | 
			
		||||
 | 
			
		||||
    const setImg = (media, index) => {
 | 
			
		||||
        if (media?.format_type === 'image') {
 | 
			
		||||
            imageEl.src = media.path
 | 
			
		||||
 | 
			
		||||
            imageEl.classList.remove('hidden')
 | 
			
		||||
            videoEl.classList.add('hidden')
 | 
			
		||||
        } else if (media?.format_type === 'video') {
 | 
			
		||||
            sourceEl.src = media.path
 | 
			
		||||
 | 
			
		||||
            videoEl.classList.remove('hidden')
 | 
			
		||||
            imageEl.classList.add('hidden')
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        mediaName.innerText = media?.name
 | 
			
		||||
        counter.innerText = `${index + 1} / ${assets.length}`
 | 
			
		||||
    }
 | 
			
		||||
    setImg(mediaViewer.activeFile, mediaViewer.currentIndex)
 | 
			
		||||
 | 
			
		||||
    const moveLeft = () => {
 | 
			
		||||
        const [media, index] = mediaViewer.prevActive()
 | 
			
		||||
        setImg(media, index)
 | 
			
		||||
    }
 | 
			
		||||
    const doRight = () => {
 | 
			
		||||
        const [media, index] = mediaViewer.nextActive()
 | 
			
		||||
        setImg(media, index)
 | 
			
		||||
    }
 | 
			
		||||
    const openImg = () => {
 | 
			
		||||
        window.open(mediaViewer.activeFile.path, '_blank')
 | 
			
		||||
    }
 | 
			
		||||
    if (assets.length === 1) {
 | 
			
		||||
        container.classList.add('media-container--fullscreen')
 | 
			
		||||
    } else {
 | 
			
		||||
        leftArrow.addEventListener('click', moveLeft)
 | 
			
		||||
        rightArrow.addEventListener('click', doRight)
 | 
			
		||||
    }
 | 
			
		||||
    imageEl.addEventListener('click', openImg)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
    setup,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
},{}],7:[function(require,module,exports){
 | 
			
		||||
const { manager } = require('./datamanager.js')
 | 
			
		||||
const storageModule = require('./storage.js')
 | 
			
		||||
 | 
			
		||||
const genericSort = (list, key, ascending, customOrder) => {
 | 
			
		||||
    let sorted
 | 
			
		||||
    if (customOrder) {
 | 
			
		||||
        sorted = list.sort((a, b) => {
 | 
			
		||||
            const aValue = a.result.toLowerCase()
 | 
			
		||||
            const bValue = b.result.toLowerCase()
 | 
			
		||||
 | 
			
		||||
            const aIndex = customOrder.findIndex((item) => item.toLowerCase() === aValue)
 | 
			
		||||
            const bIndex = customOrder.findIndex((item) => item.toLowerCase() === bValue)
 | 
			
		||||
 | 
			
		||||
            // Compare the indices to determine the sort order
 | 
			
		||||
            return aIndex - bIndex
 | 
			
		||||
        })
 | 
			
		||||
    } else {
 | 
			
		||||
        sorted = list.sort((a, b) => a[key] === b[key] ? 0 : a[key] > b[key] ? 1 : -1)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (ascending) {
 | 
			
		||||
        sorted.reverse()
 | 
			
		||||
    }
 | 
			
		||||
    return sorted
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const durationSort = (list, ascending) => {
 | 
			
		||||
    const parseDuration = (duration) => {
 | 
			
		||||
        if (duration.includes(':')) {
 | 
			
		||||
            // If it's in the format "HH:mm:ss"
 | 
			
		||||
            const [hours, minutes, seconds] = duration.split(':').map(Number)
 | 
			
		||||
            return (hours * 3600 + minutes * 60 + seconds) * 1000
 | 
			
		||||
        } else {
 | 
			
		||||
            // If it's in the format "nnn ms"
 | 
			
		||||
            return parseInt(duration)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    const sorted = list.sort((a, b) => parseDuration(a['duration']) - parseDuration(b['duration']))
 | 
			
		||||
    if (ascending) {
 | 
			
		||||
        sorted.reverse()
 | 
			
		||||
    }
 | 
			
		||||
    return sorted
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const doInitSort = () => {
 | 
			
		||||
    const type = storageModule.getSort(manager.initialSort)
 | 
			
		||||
    const ascending = storageModule.getSortDirection()
 | 
			
		||||
    const list = manager.testSubset
 | 
			
		||||
    const initialOrder = ['Error', 'Failed', 'Rerun', 'XFailed', 'XPassed', 'Skipped', 'Passed']
 | 
			
		||||
 | 
			
		||||
    storageModule.setSort(type)
 | 
			
		||||
    storageModule.setSortDirection(ascending)
 | 
			
		||||
 | 
			
		||||
    if (type?.toLowerCase() === 'original') {
 | 
			
		||||
        manager.setRender(list)
 | 
			
		||||
    } else {
 | 
			
		||||
        let sortedList
 | 
			
		||||
        switch (type) {
 | 
			
		||||
        case 'duration':
 | 
			
		||||
            sortedList = durationSort(list, ascending)
 | 
			
		||||
            break
 | 
			
		||||
        case 'result':
 | 
			
		||||
            sortedList = genericSort(list, type, ascending, initialOrder)
 | 
			
		||||
            break
 | 
			
		||||
        default:
 | 
			
		||||
            sortedList = genericSort(list, type, ascending)
 | 
			
		||||
            break
 | 
			
		||||
        }
 | 
			
		||||
        manager.setRender(sortedList)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const doSort = (type, skipDirection) => {
 | 
			
		||||
    const newSortType = storageModule.getSort(manager.initialSort) !== type
 | 
			
		||||
    const currentAsc = storageModule.getSortDirection()
 | 
			
		||||
    let ascending
 | 
			
		||||
    if (skipDirection) {
 | 
			
		||||
        ascending = currentAsc
 | 
			
		||||
    } else {
 | 
			
		||||
        ascending = newSortType ? false : !currentAsc
 | 
			
		||||
    }
 | 
			
		||||
    storageModule.setSort(type)
 | 
			
		||||
    storageModule.setSortDirection(ascending)
 | 
			
		||||
 | 
			
		||||
    const list = manager.testSubset
 | 
			
		||||
    const sortedList = type === 'duration' ? durationSort(list, ascending) : genericSort(list, type, ascending)
 | 
			
		||||
    manager.setRender(sortedList)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
    doInitSort,
 | 
			
		||||
    doSort,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
},{"./datamanager.js":1,"./storage.js":8}],8:[function(require,module,exports){
 | 
			
		||||
const possibleFilters = [
 | 
			
		||||
    'passed',
 | 
			
		||||
    'skipped',
 | 
			
		||||
    'failed',
 | 
			
		||||
    'error',
 | 
			
		||||
    'xfailed',
 | 
			
		||||
    'xpassed',
 | 
			
		||||
    'rerun',
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
const getVisible = () => {
 | 
			
		||||
    const url = new URL(window.location.href)
 | 
			
		||||
    const settings = new URLSearchParams(url.search).get('visible')
 | 
			
		||||
    const lower = (item) => {
 | 
			
		||||
        const lowerItem = item.toLowerCase()
 | 
			
		||||
        if (possibleFilters.includes(lowerItem)) {
 | 
			
		||||
            return lowerItem
 | 
			
		||||
        }
 | 
			
		||||
        return null
 | 
			
		||||
    }
 | 
			
		||||
    return settings === null ?
 | 
			
		||||
        possibleFilters :
 | 
			
		||||
        [...new Set(settings?.split(',').map(lower).filter((item) => item))]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const hideCategory = (categoryToHide) => {
 | 
			
		||||
    const url = new URL(window.location.href)
 | 
			
		||||
    const visibleParams = new URLSearchParams(url.search).get('visible')
 | 
			
		||||
    const currentVisible = visibleParams ? visibleParams.split(',') : [...possibleFilters]
 | 
			
		||||
    const settings = [...new Set(currentVisible)].filter((f) => f !== categoryToHide).join(',')
 | 
			
		||||
 | 
			
		||||
    url.searchParams.set('visible', settings)
 | 
			
		||||
    window.history.pushState({}, null, unescape(url.href))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const showCategory = (categoryToShow) => {
 | 
			
		||||
    if (typeof window === 'undefined') {
 | 
			
		||||
        return
 | 
			
		||||
    }
 | 
			
		||||
    const url = new URL(window.location.href)
 | 
			
		||||
    const currentVisible = new URLSearchParams(url.search).get('visible')?.split(',').filter(Boolean) ||
 | 
			
		||||
        [...possibleFilters]
 | 
			
		||||
    const settings = [...new Set([categoryToShow, ...currentVisible])]
 | 
			
		||||
    const noFilter = possibleFilters.length === settings.length || !settings.length
 | 
			
		||||
 | 
			
		||||
    noFilter ? url.searchParams.delete('visible') : url.searchParams.set('visible', settings.join(','))
 | 
			
		||||
    window.history.pushState({}, null, unescape(url.href))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const getSort = (initialSort) => {
 | 
			
		||||
    const url = new URL(window.location.href)
 | 
			
		||||
    let sort = new URLSearchParams(url.search).get('sort')
 | 
			
		||||
    if (!sort) {
 | 
			
		||||
        sort = initialSort || 'result'
 | 
			
		||||
    }
 | 
			
		||||
    return sort
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const setSort = (type) => {
 | 
			
		||||
    const url = new URL(window.location.href)
 | 
			
		||||
    url.searchParams.set('sort', type)
 | 
			
		||||
    window.history.pushState({}, null, unescape(url.href))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const getCollapsedCategory = (renderCollapsed) => {
 | 
			
		||||
    let categories
 | 
			
		||||
    if (typeof window !== 'undefined') {
 | 
			
		||||
        const url = new URL(window.location.href)
 | 
			
		||||
        const collapsedItems = new URLSearchParams(url.search).get('collapsed')
 | 
			
		||||
        switch (true) {
 | 
			
		||||
        case !renderCollapsed && collapsedItems === null:
 | 
			
		||||
            categories = ['passed']
 | 
			
		||||
            break
 | 
			
		||||
        case collapsedItems?.length === 0 || /^["']{2}$/.test(collapsedItems):
 | 
			
		||||
            categories = []
 | 
			
		||||
            break
 | 
			
		||||
        case /^all$/.test(collapsedItems) || collapsedItems === null && /^all$/.test(renderCollapsed):
 | 
			
		||||
            categories = [...possibleFilters]
 | 
			
		||||
            break
 | 
			
		||||
        default:
 | 
			
		||||
            categories = collapsedItems?.split(',').map((item) => item.toLowerCase()) || renderCollapsed
 | 
			
		||||
            break
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        categories = []
 | 
			
		||||
    }
 | 
			
		||||
    return categories
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const getSortDirection = () => JSON.parse(sessionStorage.getItem('sortAsc')) || false
 | 
			
		||||
const setSortDirection = (ascending) => sessionStorage.setItem('sortAsc', ascending)
 | 
			
		||||
 | 
			
		||||
const getCollapsedIds = () => JSON.parse(sessionStorage.getItem('collapsedIds')) || []
 | 
			
		||||
const setCollapsedIds = (list) => sessionStorage.setItem('collapsedIds', JSON.stringify(list))
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
    getVisible,
 | 
			
		||||
    hideCategory,
 | 
			
		||||
    showCategory,
 | 
			
		||||
    getCollapsedIds,
 | 
			
		||||
    setCollapsedIds,
 | 
			
		||||
    getSort,
 | 
			
		||||
    setSort,
 | 
			
		||||
    getSortDirection,
 | 
			
		||||
    setSortDirection,
 | 
			
		||||
    getCollapsedCategory,
 | 
			
		||||
    possibleFilters,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
},{}]},{},[4]);
 | 
			
		||||
    </script>
 | 
			
		||||
  </footer>
 | 
			
		||||
</html>
 | 
			
		||||
		Loading…
	
		Reference in New Issue