From 44077796f8e512bb8ae5a365c28a2de9c622519c Mon Sep 17 00:00:00 2001 From: coder Date: Thu, 16 Apr 2026 19:06:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BF=BB=E8=AF=91=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=8D=E5=85=B1=E4=BA=AB=E5=BC=80=E5=85=B3?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Translation 模型新增 no_share 字段 - 管理后台翻译记录页面添加共享状态列和切换按钮 - 不共享的翻译不会被其他用户使用缓存 - 缓存匹配时检查是否有 no_share 标记 --- admin.py | 22 +++++++++++++++++ app.py | 7 ++++-- models.py | 4 +++ templates/admin/translations.html | 41 ++++++++++++++++++++++++++++--- 4 files changed, 68 insertions(+), 6 deletions(-) diff --git a/admin.py b/admin.py index 7de51e8..b6f1be7 100644 --- a/admin.py +++ b/admin.py @@ -255,6 +255,28 @@ def delete_translation(trans_id): return jsonify({'success': True}) +@admin_bp.route('/translation//toggle-share', methods=['POST']) +@admin_required +def toggle_translation_share(trans_id): + """切换翻译共享状态""" + translation = Translation.query.get_or_404(trans_id) + translation.no_share = not translation.no_share + db.session.commit() + + # 记录日志 + log = OperationLog( + user_id=session.get('user_id'), + username='admin', + action='toggle_translation_share', + target=f'翻译#{trans_id}', + detail=f'设置共享状态为: {not translation.no_share}' + ) + db.session.add(log) + db.session.commit() + + return jsonify({'success': True, 'no_share': translation.no_share}) + + # ==================== 缓存管理 ==================== @admin_bp.route('/cache') @admin_required diff --git a/app.py b/app.py index 7a50179..2230b2b 100644 --- a/app.py +++ b/app.py @@ -365,8 +365,11 @@ def upload_pdf(): cache_path = cache_service.get_cache(file_hash) from_cache = False - if cache_path and ENABLE_CACHE and not instruction: - # 有缓存且无特殊翻译要求,直接使用缓存 + # 检查是否有用户设置了不共享此文件 + no_share_check = Translation.query.filter_by(file_hash=file_hash, no_share=True).first() + + if cache_path and ENABLE_CACHE and not instruction and not no_share_check: + # 有缓存且无特殊翻译要求且无不共享标记,直接使用缓存 from_cache = True output_path = cache_path else: diff --git a/models.py b/models.py index 2c72eaa..c6a1a82 100644 --- a/models.py +++ b/models.py @@ -178,6 +178,9 @@ class Translation(db.Model): # 是否来自缓存 from_cache = db.Column(db.Boolean, default=False) + # 不共享缓存 + no_share = db.Column(db.Boolean, default=False) # 不共享此翻译给其他用户 + # 重译信息 retranslate_request = db.Column(db.Text, nullable=True) # 重译要求 parent_id = db.Column(db.Integer, db.ForeignKey('translations.id'), nullable=True) # 原翻译ID @@ -190,6 +193,7 @@ class Translation(db.Model): 'status': self.status, 'progress': self.progress, 'from_cache': self.from_cache, + 'no_share': self.no_share, 'file_size': self.file_size, 'created_at': self.created_at.isoformat() if self.created_at else None, 'completed_at': self.completed_at.isoformat() if self.completed_at else None, diff --git a/templates/admin/translations.html b/templates/admin/translations.html index 659a3dd..fe95492 100644 --- a/templates/admin/translations.html +++ b/templates/admin/translations.html @@ -69,13 +69,14 @@ 大小 状态 缓存 + 共享 时间 操作 {% for t in translations.items %} - + {{ t.id }} {{ t.original_filename[:25] }}{% if t.original_filename|length > 25 %}...{% endif %} {% if t.user_id %}ID:{{ t.user_id }}{% else %}访客{% endif %} @@ -87,14 +88,26 @@ {% if t.from_cache %}{% else %}-{% endif %} + + {% if t.no_share %} + 不共享 + {% else %} + 共享 + {% endif %} + {{ t.created_at.strftime('%m-%d %H:%M') }} - - +
+ + + +
{% else %} - 暂无数据 + 暂无数据 {% endfor %} @@ -122,6 +135,26 @@ .then(r => r.json()) .then(d => { if (d.success) location.reload(); }); } + + function toggleShare(id) { + fetch(`/admin/translation/${id}/toggle-share`, { method: 'POST' }) + .then(r => r.json()) + .then(d => { + if (d.success) { + // 更新显示状态 + const badge = document.getElementById(`share-badge-${id}`); + if (d.no_share) { + badge.className = 'badge bg-secondary'; + badge.textContent = '不共享'; + } else { + badge.className = 'badge bg-success'; + badge.textContent = '共享'; + } + // 更新按钮样式 + location.reload(); // 简化处理,刷新页面 + } + }); + } \ No newline at end of file