From 1f150fc847d0fc70947261ff45843ccc116117c2 Mon Sep 17 00:00:00 2001 From: Awoodwhale Date: Fri, 31 Mar 2023 13:39:28 +0800 Subject: [PATCH] refactor: use ajax to get expiry_date --- app/admin_service.py | 17 +++++++++++++++++ app/model/user.py | 5 +++++ app/templates/admin_manage_user.html | 17 +++++++++-------- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/app/admin_service.py b/app/admin_service.py index 16e80db..efbaf6d 100644 --- a/app/admin_service.py +++ b/app/admin_service.py @@ -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 \ No newline at end of file diff --git a/app/model/user.py b/app/model/user.py index 6c030c2..28173b9 100644 --- a/app/model/user.py +++ b/app/model/user.py @@ -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: diff --git a/app/templates/admin_manage_user.html b/app/templates/admin_manage_user.html index 585fc82..a3f0ca0 100644 --- a/app/templates/admin_manage_user.html +++ b/app/templates/admin_manage_user.html @@ -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 %} + }) }