diff --git a/apps/admin/views.py b/apps/admin/views.py index beeaaa4..7f76a71 100644 --- a/apps/admin/views.py +++ b/apps/admin/views.py @@ -856,6 +856,7 @@ async def update_file( if not file_code: raise HTTPException(status_code=404, detail="文件不存在") target_name = file_service._build_file_activity_name(file_code) + previous_code = file_code.code update_data = {} if data.code is not None and data.code != file_code.code: @@ -879,6 +880,11 @@ async def update_file( update_data["expired_count"] = data.expired_count await file_code.update_from_dict(update_data).save() + if "code" in update_data: + await ShareCodeReservations.get_or_create( + code=previous_code, + defaults={"file_code_id": file_code.id, "reason": "manual_update"}, + ) if update_data: await file_service.record_admin_activity( action="file.update", diff --git a/apps/base/migrations/migrations_009_reserve_historical_codes.py b/apps/base/migrations/migrations_009_reserve_historical_codes.py new file mode 100644 index 0000000..4ee818a --- /dev/null +++ b/apps/base/migrations/migrations_009_reserve_historical_codes.py @@ -0,0 +1,14 @@ +from tortoise import connections + + +async def migrate(): + """把既有审计日志中的取件码也登记为永久占用。""" + conn = connections.get("default") + await conn.execute_query( + """ + INSERT OR IGNORE INTO share_code_reservations (code, file_code_id, reason) + SELECT DISTINCT TRIM(code), file_code_id, 'historical_audit' + FROM transfer_stats + WHERE code IS NOT NULL AND TRIM(code) <> '' + """ + ) diff --git a/tests/test_share_code_reservations.py b/tests/test_share_code_reservations.py index f5ac4f7..9312440 100644 --- a/tests/test_share_code_reservations.py +++ b/tests/test_share_code_reservations.py @@ -32,6 +32,13 @@ class ShareCodeReservationTests(unittest.TestCase): reservation = await ShareCodeReservations.get(code="OLD_CODE") self.assertEqual(reservation.file_code_id, item.id) self.assertEqual(reservation.reason, "restored") + + # 模拟管理员手工改码:被替换的旧码也必须永久封存。 + await ShareCodeReservations.get_or_create( + code="MANUALOLD", + defaults={"file_code_id": item.id, "reason": "manual_update"}, + ) + self.assertTrue(await ShareCodeReservations.filter(code="MANUALOLD").exists()) finally: await Tortoise.close_connections()