From 0cef097dfac7a899625633a048524327dda32030 Mon Sep 17 00:00:00 2001 From: wzj <244142824@qq.com> Date: Sat, 14 Jun 2025 10:12:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 38 +++++++++++++++++++++++++++++++++----- templates/ca_detail.html | 10 ++++++++-- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/app.py b/app.py index b620aca..3b5b337 100644 --- a/app.py +++ b/app.py @@ -12,6 +12,11 @@ from io import BytesIO import zipfile from pypinyin import pinyin, Style import uuid + +app = Flask(__name__) +app.secret_key = 'your-secret-key-here' +from pypinyin import pinyin, Style + def to_pinyin(text): """将中文转换为拼音""" if not text: @@ -21,14 +26,11 @@ def to_pinyin(text): # 拼接成字符串 return "_".join([item[0] for item in pinyin_list]) -app = Flask(__name__) -app.secret_key = 'your-secret-key-here' - +# 注册模板过滤器 @app.template_filter('to_pinyin') def jinja2_to_pinyin(text): return to_pinyin(text) - # 数据库配置 db_config = { 'host': '192.168.31.11', @@ -631,6 +633,8 @@ def create_ca_view(): return render_template('create_ca.html') +from datetime import timedelta # 确保顶部已导入 + @app.route('/cas/') @login_required def ca_detail(ca_id): @@ -663,7 +667,14 @@ def ca_detail(ca_id): """, (ca_id,)) crl = cursor.fetchone() - return render_template('ca_detail.html', ca=ca, certificates=certificates, crl=crl) + return render_template( + 'ca_detail.html', + ca=ca, + certificates=certificates, + crl=crl, + timedelta=timedelta, # 传递timedelta到模板 + get_username=get_username # 确保这个函数已定义 + ) except Error as e: print(f"Database error: {e}") flash('获取CA详情失败', 'danger') @@ -674,6 +685,23 @@ def ca_detail(ca_id): conn.close() return redirect(url_for('ca_list')) +def get_username(user_id): + """根据用户ID获取用户名""" + conn = get_db_connection() + if conn: + try: + cursor = conn.cursor(dictionary=True) + cursor.execute("SELECT username FROM users WHERE id = %s", (user_id,)) + user = cursor.fetchone() + return user['username'] if user else str(user_id) + except Error as e: + print(f"Database error: {e}") + return str(user_id) + finally: + if conn.is_connected(): + cursor.close() + conn.close() + return str(user_id) @app.route('/cas//generate_crl') @login_required diff --git a/templates/ca_detail.html b/templates/ca_detail.html index 75b94bf..1d05498 100644 --- a/templates/ca_detail.html +++ b/templates/ca_detail.html @@ -75,8 +75,14 @@
密钥长度
{{ ca.key_size }}位
-
有效期
-
{{ ca.days_valid }}天 (至 {{ (ca.created_at + timedelta(days=ca.days_valid)).strftime('%Y-%m-%d') }})
+ +
有效期
+
+ {{ ca.days_valid }}天 + {% if ca.created_at %} + (至 {{ (ca.created_at + timedelta(days=ca.days_valid)).strftime('%Y-%m-%d') }}) + {% endif %} +
创建者
{{ get_username(ca.created_by) }}