feat: 翻译记录添加不共享开关功能

- Translation 模型新增 no_share 字段
- 管理后台翻译记录页面添加共享状态列和切换按钮
- 不共享的翻译不会被其他用户使用缓存
- 缓存匹配时检查是否有 no_share 标记
This commit is contained in:
2026-04-16 19:06:43 +08:00
parent 504fed6c3e
commit 44077796f8
4 changed files with 68 additions and 6 deletions

View File

@@ -255,6 +255,28 @@ def delete_translation(trans_id):
return jsonify({'success': True})
@admin_bp.route('/translation/<int:trans_id>/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

7
app.py
View File

@@ -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:

View File

@@ -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,

View File

@@ -69,13 +69,14 @@
<th>大小</th>
<th>状态</th>
<th>缓存</th>
<th>共享</th>
<th>时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for t in translations.items %}
<tr>
<tr id="trans-row-{{ t.id }}">
<td>{{ t.id }}</td>
<td>{{ t.original_filename[:25] }}{% if t.original_filename|length > 25 %}...{% endif %}</td>
<td>{% if t.user_id %}ID:{{ t.user_id }}{% else %}访客{% endif %}</td>
@@ -87,14 +88,26 @@
</span>
</td>
<td>{% if t.from_cache %}<i class="bi bi-check-circle text-success"></i>{% else %}-{% endif %}</td>
<td>
{% if t.no_share %}
<span class="badge bg-secondary" id="share-badge-{{ t.id }}">不共享</span>
{% else %}
<span class="badge bg-success" id="share-badge-{{ t.id }}">共享</span>
{% endif %}
</td>
<td>{{ t.created_at.strftime('%m-%d %H:%M') }}</td>
<td>
<a href="{{ url_for('admin.translation_detail', trans_id=t.id) }}" class="btn btn-sm btn-outline-primary"><i class="bi bi-eye"></i></a>
<button class="btn btn-sm btn-outline-danger" onclick="deleteTrans({{ t.id }})"><i class="bi bi-trash"></i></button>
<div class="d-flex gap-1">
<button class="btn btn-sm btn-outline-{% if t.no_share %}success{% else %}warning{% endif %}" onclick="toggleShare({{ t.id }})" title="切换共享状态">
<i class="bi bi-share{% if t.no_share %}-fill{% endif %}"></i>
</button>
<a href="{{ url_for('admin.translation_detail', trans_id=t.id) }}" class="btn btn-sm btn-outline-primary"><i class="bi bi-eye"></i></a>
<button class="btn btn-sm btn-outline-danger" onclick="deleteTrans({{ t.id }})"><i class="bi bi-trash"></i></button>
</div>
</td>
</tr>
{% else %}
<tr><td colspan="9" class="text-center text-muted py-4">暂无数据</td></tr>
<tr><td colspan="10" class="text-center text-muted py-4">暂无数据</td></tr>
{% endfor %}
</tbody>
</table>
@@ -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(); // 简化处理,刷新页面
}
});
}
</script>
</body>
</html>