物品借出免审批并扩大上传限制
This commit is contained in:
+43
-8
@@ -43,7 +43,7 @@ from . import duty_receipts
|
||||
from . import center
|
||||
from . import center_export
|
||||
from . import timetables as timetable_routes
|
||||
from .models import center_activity_meta
|
||||
from .models import center_activity_meta, center_items, center_loans
|
||||
import json
|
||||
|
||||
BASE_DIR = Path(__file__).resolve().parent
|
||||
@@ -635,7 +635,7 @@ async def lifespan(_app: FastAPI):
|
||||
|
||||
async def _migrate_legacy(conn) -> None:
|
||||
"""老库平滑升级:users 补 phone 列;username 列允许为空(新注册只靠邮箱识别);
|
||||
activities 的历史 pending 全部放行为 approved。"""
|
||||
activities 的历史 pending 全部放行为 approved;一楼旧借用记录按库存安全迁移。"""
|
||||
def _reflect(sync_conn):
|
||||
insp = inspect(sync_conn)
|
||||
names = insp.get_table_names()
|
||||
@@ -668,6 +668,8 @@ async def _migrate_legacy(conn) -> None:
|
||||
await conn.execute(
|
||||
text("UPDATE activities SET status='approved' WHERE status IN ('pending','rejected')")
|
||||
)
|
||||
if "center_loans" in tables and "center_items" in tables:
|
||||
await _migrate_center_loans(conn)
|
||||
|
||||
if "category" in activity_cols:
|
||||
# SQLite 3.35+ 支持直接 DROP COLUMN
|
||||
@@ -684,13 +686,46 @@ async def _migrate_legacy(conn) -> None:
|
||||
await conn.execute(update(table).where(table.c.token == token).values(token=token_digest(token)))
|
||||
|
||||
|
||||
app = FastAPI(title="WorkLog 活动记录", version="2.0.0", lifespan=lifespan)
|
||||
def center_approvers():
|
||||
values = os.getenv('WORKLOG_CENTER_APPROVERS')
|
||||
emails = values.split(',') if values is not None else cfg('center', 'approver_emails', [])
|
||||
return {email.strip().lower() for email in emails if email.strip()}
|
||||
async def _migrate_center_loans(conn) -> None:
|
||||
"""把旧待审批借用按提交顺序转为已借出,库存不足的保留为历史未借出。"""
|
||||
rows = (await conn.execute(
|
||||
select(center_loans)
|
||||
.where(center_loans.c.status == 'pending')
|
||||
.order_by(center_loans.c.created_at, center_loans.c.id)
|
||||
)).mappings().all()
|
||||
if not rows:
|
||||
return
|
||||
totals = dict((await conn.execute(select(center_items.c.id, center_items.c.quantity))).all())
|
||||
active = dict((await conn.execute(
|
||||
select(center_loans.c.item_id, func.coalesce(func.sum(center_loans.c.quantity), 0))
|
||||
.where(center_loans.c.status == 'approved', center_loans.c.returned_at.is_(None))
|
||||
.group_by(center_loans.c.item_id)
|
||||
)).all())
|
||||
for row in rows:
|
||||
if row['returned_at'] is not None:
|
||||
await conn.execute(
|
||||
update(center_loans).where(center_loans.c.id == row['id']).values(status='returned')
|
||||
)
|
||||
continue
|
||||
used = active.get(row['item_id'], 0)
|
||||
available = max(0, totals.get(row['item_id'], 0) - used)
|
||||
if row['quantity'] > 0 and row['quantity'] <= available:
|
||||
await conn.execute(
|
||||
update(center_loans).where(center_loans.c.id == row['id']).values(status='approved')
|
||||
)
|
||||
active[row['item_id']] = used + row['quantity']
|
||||
else:
|
||||
await conn.execute(
|
||||
update(center_loans).where(center_loans.c.id == row['id']).values(
|
||||
status='rejected',
|
||||
reviewed_at=None,
|
||||
review_note='免审批迁移:库存不足,原待审批记录未借出',
|
||||
)
|
||||
)
|
||||
|
||||
center.install(app, get_db, current_user, lambda: _db_path.parent / 'uploads', visible_clause, center_approvers)
|
||||
|
||||
app = FastAPI(title="WorkLog 活动记录", version="2.0.0", lifespan=lifespan)
|
||||
center.install(app, get_db, current_user, lambda: _db_path.parent / 'uploads', visible_clause)
|
||||
center_export.install(app, get_db, current_user, visible_clause)
|
||||
|
||||
def timetable_collectors():
|
||||
|
||||
Reference in New Issue
Block a user