refactor(core): 批次 E——死 import/死正则清零、Project.sort_order 整链下线、分类 PATCH 改真部分更新
- 死代码(逐项 grep 复核后删):models.py 的 import json;security.py 的 datetime / merge_levels 引用
(merge_levels 本体活着,models/rbac/测试都在用);board.py 的 Project;rbac.py 的 LEVELS+LEVEL_RANK;
ai_parser.py 三条零引用正则/常量(_CN_NUM/_PCT_RE/_AMT_RE,真正在用的是行内正则)。
- Project.sort_order 下线:接口永不写、前端零读、恒 0 空转——模型列+to_dict 键删,
projects.py/weekly.py 排序去掉该级(保留 is_key desc + id);登记 STALE_COLUMNS。
Staff/Category 的同名列是活的(有排序语义+可编辑),未受牵连。
存量库验证:reload 重启后 PRAGMA 复核列已自动清。
- PATCH /api/categories 不再复用创建模型:新增 CategoryPatch(exclude_unset),
{} 不再 422、只改传了的字段、空名 400——与 README 两种请求体模式对齐;
新增 test_分类PATCH是部分更新。pytest 102 passed。
main
parent
c9ec19f7e6
commit
bde2a0959e
|
|
@ -61,9 +61,7 @@ def ai_enabled(db) -> bool:
|
|||
# --------------------------------------------------------------------------
|
||||
# 规则兜底解析
|
||||
# --------------------------------------------------------------------------
|
||||
_CN_NUM = {"一": 1, "二": 2, "三": 3, "四": 4, "五": 5, "六": 6, "七": 7, "八": 8, "九": 9, "十": 10}
|
||||
_SPLIT_RE = re.compile(r"(?:^|\n)\s*(?:\(?\d{1,2}[))、..::]|[一二三四五六七八九十]{1,2}[、..]|[-•*·]\s)")
|
||||
_PCT_RE = re.compile(r"(\d{1,3}(?:\.\d+)?)\s*%")
|
||||
|
||||
_STATUS_RULES = [
|
||||
(("阻塞", "卡住", "卡点", "停滞", "无法推进", "推不动"), "blocked"),
|
||||
|
|
@ -256,7 +254,6 @@ CUSTOM_USER_TEMPLATE = """定制项目:{project}
|
|||
---- 结束 ----
|
||||
"""
|
||||
|
||||
_AMT_RE = re.compile(r"(?:金额|合同额|合同金额|下单|订单|报价)?\s*[¥¥]?\s*(\d[\d,,]*(?:\.\d+)?)\s*(元|万|万元)?")
|
||||
_REVENUE_RE = re.compile(r"(?:计收|已计收|核销|回款)[^0-9]{0,8}([0-9][0-9,,]*(?:\.\d+)?)")
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@
|
|||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import (
|
||||
|
|
@ -154,7 +153,6 @@ class Project(Base, TimestampMixin):
|
|||
is_key: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False) # 重点标记
|
||||
progress: Mapped[float] = mapped_column(Float, default=0.0, nullable=False) # 最新进度 %
|
||||
archived: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
|
||||
sort_order: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
|
||||
|
||||
category: Mapped[Category | None] = relationship(back_populates="projects")
|
||||
tags: Mapped[list[Tag]] = relationship(secondary=project_tags, lazy="selectin")
|
||||
|
|
@ -178,7 +176,6 @@ class Project(Base, TimestampMixin):
|
|||
"is_key": self.is_key,
|
||||
"progress": round(self.progress or 0, 1),
|
||||
"archived": self.archived,
|
||||
"sort_order": self.sort_order,
|
||||
"tags": [t.to_dict() for t in self.tags],
|
||||
"updated_at": self.updated_at.isoformat(timespec="seconds") if self.updated_at else None,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ from sqlalchemy.orm import Session
|
|||
from .. import constants
|
||||
from ..constants import parse_period_label
|
||||
from ..db import get_db
|
||||
from ..models import Period, Project, Update
|
||||
from ..models import Period, Update
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ from ..schemas import (
|
|||
ProjectPatch,
|
||||
UpdateIn,
|
||||
UpdatePatch,
|
||||
CategoryPatch,
|
||||
)
|
||||
from .board import resolve_period_ids, worst_status
|
||||
|
||||
|
|
@ -96,7 +97,7 @@ def list_projects(
|
|||
if owner:
|
||||
# 负责人筛选连其他负责人一起命中(主负责人在 owner 列,其余在 other_owners 列)
|
||||
q = q.filter(or_(Project.owner == owner, Project.other_owners.like(people_like(owner))))
|
||||
projects = q.order_by(Project.is_key.desc(), Project.sort_order, Project.id).all()
|
||||
projects = q.order_by(Project.is_key.desc(), Project.id).all()
|
||||
|
||||
# 最近一次更新
|
||||
ups = (
|
||||
|
|
@ -264,12 +265,18 @@ def create_category(payload: CategoryIn, db: Session = Depends(get_db)):
|
|||
|
||||
|
||||
@router.patch("/categories/{cid}")
|
||||
def patch_category(cid: int, payload: CategoryIn, db: Session = Depends(get_db)):
|
||||
def patch_category(cid: int, payload: CategoryPatch, db: Session = Depends(get_db)):
|
||||
c = db.get(Category, cid)
|
||||
if not c:
|
||||
raise HTTPException(404, "分类不存在")
|
||||
c.name = payload.name
|
||||
c.color = payload.color
|
||||
data = payload.model_dump(exclude_unset=True)
|
||||
if "name" in data:
|
||||
name = (data["name"] or "").strip()
|
||||
if not name:
|
||||
raise HTTPException(400, "分类名称不能为空")
|
||||
c.name = name
|
||||
if data.get("color"):
|
||||
c.color = data["color"]
|
||||
db.commit()
|
||||
return c.to_dict()
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ from fastapi import APIRouter, Depends, HTTPException, Query, Request
|
|||
from sqlalchemy.orm import Session
|
||||
|
||||
from .. import audit, security
|
||||
from ..constants import LEVELS, LEVEL_RANK, MENU_KEYS, MENU_LABELS, LEVEL_LABELS, merge_levels
|
||||
from ..constants import MENU_KEYS, MENU_LABELS, LEVEL_LABELS, merge_levels
|
||||
from ..db import get_db
|
||||
from ..models import AuditLog, Role, RoleMenu, User, UserRole, UserSession
|
||||
from ..schemas import RoleIn, RolePatch, UserIn, UserPatch
|
||||
|
|
|
|||
|
|
@ -87,7 +87,7 @@ def weekly(
|
|||
|
||||
project_rows = []
|
||||
for p in db.query(Project).filter(Project.archived.is_(False)).order_by(
|
||||
Project.is_key.desc(), Project.sort_order, Project.id
|
||||
Project.is_key.desc(), Project.id
|
||||
).all():
|
||||
items = ups_by_project.get(p.id, [])
|
||||
if owner and owner not in all_owners(p.owner, p.other_owners):
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ STALE_COLUMNS: tuple[tuple[str, str], ...] = (
|
|||
("periods", "start_date"), # 周期起止由 year/month/week 推导,两列从未被读取
|
||||
("periods", "end_date"),
|
||||
("projects", "priority"), # 优先级无界面读写,导出恒为 P2(NOT NULL 列,必须清)
|
||||
("projects", "sort_order"), # 恒为 0 的排序位:无任何入口写过,排序语义空转(Staff/Category 的同名列是活的)
|
||||
("projects", "description"),
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -36,6 +36,12 @@ class CategoryIn(BaseModel):
|
|||
color: str = "#2563eb"
|
||||
|
||||
|
||||
class CategoryPatch(BaseModel):
|
||||
"""分类部分更新:只改传了的字段(README 两种请求体模式的既定语义)。"""
|
||||
name: str | None = None
|
||||
color: str | None = None
|
||||
|
||||
|
||||
class UpdateIn(BaseModel):
|
||||
project_id: int
|
||||
task_id: int | None = None
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ from __future__ import annotations
|
|||
import hashlib
|
||||
import hmac
|
||||
import secrets
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import timedelta
|
||||
|
||||
from fastapi import Depends, HTTPException, Request
|
||||
from sqlalchemy.orm import Session
|
||||
|
|
@ -28,7 +28,6 @@ from .constants import (
|
|||
LEVELS,
|
||||
MENU_KEYS,
|
||||
MENU_LABELS,
|
||||
merge_levels,
|
||||
)
|
||||
from .db import get_db
|
||||
from .models import Role, RoleMenu, User, UserRole, UserSession, now
|
||||
|
|
|
|||
|
|
@ -233,3 +233,14 @@ def test_设置只认AI相关的键(h):
|
|||
assert s["ai_model"] == "glm-4" and s["ai_api_key"] == "sk-x" and s["ai_temperature"] == "0.5"
|
||||
assert "偷改的键" not in s
|
||||
assert h.admin.get("/api/meta").json()["ai"]["has_key"] is True # 配了 Key 就显示已配置
|
||||
|
||||
|
||||
def test_分类PATCH是部分更新(h):
|
||||
"""README 既定:PATCH 只改传了的字段。曾经复用创建模型导致 PATCH {} 直接 422。"""
|
||||
c = h.category("交付项目", color="#111111")
|
||||
up = h.admin.patch(f"/api/categories/{c['id']}", json={"color": "#222222"}).json()
|
||||
assert up["color"] == "#222222" and up["name"] == c["name"] # 没传 name 不动
|
||||
up = h.admin.patch(f"/api/categories/{c['id']}", json={"name": "改名后"}).json()
|
||||
assert up["name"] == "改名后" and up["color"] == "#222222"
|
||||
assert h.admin.patch(f"/api/categories/{c['id']}", json={"name": " "}).status_code == 400
|
||||
assert h.admin.patch(f"/api/categories/{c['id']}", json={}).status_code == 200
|
||||
|
|
|
|||
Loading…
Reference in New Issue