forked from mrlan/EnglishPal
				
			AJAX载入文章数据
							parent
							
								
									becef7e343
								
							
						
					
					
						commit
						5039f5710e
					
				| 
						 | 
				
			
			@ -50,22 +50,22 @@
 | 
			
		|||
{#    {% endfor %}#}
 | 
			
		||||
 | 
			
		||||
    {% if result_of_generate_article != "had read all articles" %}
 | 
			
		||||
        <a id="next_btn" class="btn btn-success" href="/{{ username }}/reset" role="button"> 下一篇 Next Article </a>
 | 
			
		||||
        <button class="btn btn-success" id="load_next_article" onclick="load_next_article()"> 下一篇 Next Article </button>
 | 
			
		||||
    {% endif %}
 | 
			
		||||
    {% if session.get('visited_articles') and session.get('visited_articles')['index']>0 %}
 | 
			
		||||
        <a id="pre_btn" class="btn btn-success" href="/{{ username }}/back" role="button"> 上一篇 Previous Article </a>
 | 
			
		||||
        <button class="btn btn-success" id="load_pre_article" onclick="load_pre_article()"> 上一篇 Previous Article </button>
 | 
			
		||||
    {% endif %}
 | 
			
		||||
 | 
			
		||||
    <p><b>阅读文章并回答问题</b></p>
 | 
			
		||||
    <div id="text-content">
 | 
			
		||||
        {% if result_of_generate_article == 'found' %}
 | 
			
		||||
            <div class="alert alert-success" role="alert">According to your word list, your level is <span class="badge bg-success">{{ today_article["user_level"] }}</span>  and we have chosen an article with a difficulty level of <span class="badge bg-success">{{ today_article["text_level"] }}</span> for you.</div>
 | 
			
		||||
                <p class="text-muted">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="badge bg-success" id="user-level">{{ today_article["user_level"] }}</span>  and we have chosen an article with a difficulty level of <span class="badge bg-success" 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"><br/>
 | 
			
		||||
            <p class="display-5">{{ today_article["article_title"] }}</p><br/>
 | 
			
		||||
            <p class="display-5" id="article_title">{{ today_article["article_title"] }}</p><br/>
 | 
			
		||||
            <p class="lead"><font id="article" size=2>{{ today_article["article_body"] }}</font></p><br/>
 | 
			
		||||
            <p><small class="text-muted">{{ today_article['source'] }}</small></p><br/>
 | 
			
		||||
            <p><b>{{ today_article['question'] }}</b></p><br/>
 | 
			
		||||
            <p><small class="text-muted" id="source">{{ today_article['source'] }}</small></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);
 | 
			
		||||
| 
						 | 
				
			
			@ -151,6 +151,50 @@
 | 
			
		|||
        <script src="{{ js }}"></script>
 | 
			
		||||
    {% endfor %}
 | 
			
		||||
{% endif %}
 | 
			
		||||
<script>
 | 
			
		||||
    function load_next_article(){
 | 
			
		||||
        $.ajax({
 | 
			
		||||
            url: '/get_next_article/{{username}}',
 | 
			
		||||
            dataType: 'json',
 | 
			
		||||
            success: function(data) {
 | 
			
		||||
 | 
			
		||||
                // 更新页面内容
 | 
			
		||||
                    if(data){
 | 
			
		||||
                        $('#user_level').html(data['user_level']);
 | 
			
		||||
                        $('#text_level').html(data["text_level"]);
 | 
			
		||||
                        $('#date').html('Article added on: '+data["date"]);
 | 
			
		||||
                        $('#article_title').html(data["article_title"]);
 | 
			
		||||
                        $('#article').html(data["article_body"]);
 | 
			
		||||
                        $('#source').html(data['source']);
 | 
			
		||||
                        $('#question').html(data["question"]);
 | 
			
		||||
                        $('#answer').html(data["answer"]);
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
    function load_pre_article(){
 | 
			
		||||
        $.ajax({
 | 
			
		||||
            url: '/get_pre_article/{{username}}',
 | 
			
		||||
            dataType: 'json',
 | 
			
		||||
            success: function(data) {
 | 
			
		||||
                // 更新页面内容
 | 
			
		||||
                if(data){
 | 
			
		||||
                    $('#user_level').html(data['user_level']);
 | 
			
		||||
                    $('#text_level').html(data["text_level"]);
 | 
			
		||||
                    $('#date').html('Article added on: '+data["date"]);
 | 
			
		||||
                    $('#article_title').html(data["article_title"]);
 | 
			
		||||
                    $('#article').html(data["article_body"]);
 | 
			
		||||
                    $('#source').html(data['source']);
 | 
			
		||||
                    $('#question').html(data["question"]);
 | 
			
		||||
                    $('#answer').html(data["answer"]);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
        console.info({{visited_articles}})
 | 
			
		||||
    }
 | 
			
		||||
</script>
 | 
			
		||||
</body>
 | 
			
		||||
<style>
 | 
			
		||||
    mark {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -21,14 +21,10 @@ userService = Blueprint("user_bp", __name__)
 | 
			
		|||
path_prefix = '/var/www/wordfreq/wordfreq/'
 | 
			
		||||
path_prefix = './'  # comment this line in deployment
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@userService.route("/<username>/reset", methods=['GET', 'POST'])
 | 
			
		||||
def user_reset(username):
 | 
			
		||||
    '''
 | 
			
		||||
    用户界面
 | 
			
		||||
    :param username: 用户名
 | 
			
		||||
    :return: 返回页面内容
 | 
			
		||||
    '''
 | 
			
		||||
@userService.route("/get_next_article/<username>",methods=['GET','POST'])
 | 
			
		||||
def get_next_article(username):
 | 
			
		||||
    user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
 | 
			
		||||
    session['old_articleID'] = session.get('articleID')
 | 
			
		||||
    if request.method == 'GET':
 | 
			
		||||
        visited_articles = session.get("visited_articles")
 | 
			
		||||
        if visited_articles['article_ids'][-1] == "null":  # 如果当前还是“null”,则将“null”pop出来,无需index+=1
 | 
			
		||||
| 
						 | 
				
			
			@ -36,26 +32,22 @@ def user_reset(username):
 | 
			
		|||
        else:  # 当前不为“null”,直接 index+=1
 | 
			
		||||
            visited_articles["index"] += 1
 | 
			
		||||
        session["visited_articles"] = visited_articles
 | 
			
		||||
        return redirect(url_for('user_bp.userpage', username=username))
 | 
			
		||||
        visited_articles, data, result_of_generate_article = get_today_article(user_freq_record, session.get('visited_articles'))
 | 
			
		||||
    else:
 | 
			
		||||
        return 'Under construction'
 | 
			
		||||
    return json.dumps(data)
 | 
			
		||||
 | 
			
		||||
@userService.route("/<username>/back", methods=['GET'])
 | 
			
		||||
def user_back(username):
 | 
			
		||||
    '''
 | 
			
		||||
    用户界面
 | 
			
		||||
    :param username: 用户名
 | 
			
		||||
    :return: 返回页面内容
 | 
			
		||||
    '''
 | 
			
		||||
@userService.route("/get_pre_article/<username>",methods=['GET'])
 | 
			
		||||
def get_pre_article(username):
 | 
			
		||||
    user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
 | 
			
		||||
    if request.method == 'GET':
 | 
			
		||||
        visited_articles = session.get("visited_articles")
 | 
			
		||||
        visited_articles["index"] -= 1  # 上一篇,index-=1
 | 
			
		||||
        if visited_articles['article_ids'][-1] == "null":  # 如果当前还是“null”,则将“null”pop出来
 | 
			
		||||
            visited_articles['article_ids'].pop()
 | 
			
		||||
        session["visited_articles"] = visited_articles
 | 
			
		||||
        return redirect(url_for('user_bp.userpage', username=username))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        visited_articles, data, result_of_generate_article = get_today_article(user_freq_record, session.get('visited_articles'))
 | 
			
		||||
        return json.dumps(data)
 | 
			
		||||
 | 
			
		||||
@userService.route("/<username>/<word>/unfamiliar", methods=['GET', 'POST'])
 | 
			
		||||
def unfamiliar(username, word):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue