forked from mrlan/EnglishPal
				
			refactor: use ajax to get expiry_date
							parent
							
								
									0ce1c6eb6e
								
							
						
					
					
						commit
						1f150fc847
					
				| 
						 | 
				
			
			@ -126,3 +126,20 @@ def user():
 | 
			
		|||
                update_expiry_time_by_username(username, "".join(expiry_time.split("-")))
 | 
			
		||||
                flash(f'Expiry date updated to {expiry_time}.')
 | 
			
		||||
    return render_template("admin_manage_user.html", **context)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@adminService.route("/admin/expiry", methods=["GET"])
 | 
			
		||||
def user_expiry_time():
 | 
			
		||||
    is_admin = check_is_admin()
 | 
			
		||||
    if is_admin != "pass":
 | 
			
		||||
        return is_admin
 | 
			
		||||
    
 | 
			
		||||
    username = request.args.get("username", "")
 | 
			
		||||
    if not username:
 | 
			
		||||
        return "Username can't empty"
 | 
			
		||||
    
 | 
			
		||||
    user = get_user_by_username(username)
 | 
			
		||||
    if not user:
 | 
			
		||||
        return "User not exist"
 | 
			
		||||
    
 | 
			
		||||
    return user.expiry_date
 | 
			
		||||
| 
						 | 
				
			
			@ -5,6 +5,11 @@ def get_users():
 | 
			
		|||
    with db_session:
 | 
			
		||||
        return User.select().order_by(User.name)[:]
 | 
			
		||||
 | 
			
		||||
def get_user_by_username(username):
 | 
			
		||||
    with db_session:
 | 
			
		||||
        user = User.select(name=username)
 | 
			
		||||
        if user:
 | 
			
		||||
            return user.first()
 | 
			
		||||
 | 
			
		||||
def update_password_by_username(username, password="123456"):
 | 
			
		||||
    with db_session:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -82,16 +82,17 @@
 | 
			
		|||
    })
 | 
			
		||||
    // 选择用户后更新其过期时间
 | 
			
		||||
    function loadUserExpiryDate() {
 | 
			
		||||
        let cur_user = $('#username').val();
 | 
			
		||||
        {% for user in user_list %}
 | 
			
		||||
            if (cur_user == "{{ user.name }}") {
 | 
			
		||||
                const dateString = "{{ user.expiry_date }}"
 | 
			
		||||
                const year = dateString.substr(0,4);
 | 
			
		||||
                const month = dateString.substr(4,2);
 | 
			
		||||
                const day = dateString.substr(6,2);
 | 
			
		||||
        const cur_user = $('#username').val();
 | 
			
		||||
        $.ajax({
 | 
			
		||||
            type: "GET",
 | 
			
		||||
            url: `/admin/expiry?username=${cur_user}`,
 | 
			
		||||
            success: function(resp) {
 | 
			
		||||
                const year = resp.substr(0,4);
 | 
			
		||||
                const month = resp.substr(4,2);
 | 
			
		||||
                const day = resp.substr(6,2);
 | 
			
		||||
                document.getElementById("expiry_date").value = year + '-' + month + '-' + day
 | 
			
		||||
            }
 | 
			
		||||
        {% endfor %}
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue