feat(project): add cell moves and width menus

This commit is contained in:
2026-10-03 08:55:15 +09:00
parent 2c8542339d
commit 7647e4b20f
18 changed files with 695 additions and 26 deletions
+79 -3
View File
@@ -2275,8 +2275,13 @@ class ProjectStore:
def set_goal_cell(self, *, row_id: int, month: int, body: str, updated_by: str = "") -> None:
m = store.validate_goal_month(month, allow_year=True)
text = store.validate_goal_text(body)
with self._pool.connection() as conn:
if not conn.execute("SELECT 1 FROM goal_rows WHERE id = %s", (row_id,)).fetchone():
with self._pool.connection() as conn, conn.transaction():
# 빈 대상 칸을 포함해 일반 저장·이동·병합이 같은 영역 잠금으로 직렬화된다.
area = conn.execute(
"SELECT a.id AS area_id FROM goal_rows r JOIN goal_areas a ON a.id = r.area_id "
"WHERE r.id = %s FOR UPDATE OF a", (row_id,),
).fetchone()
if not area:
raise KeyError(row_id)
halves = self._table_exists(conn, "goal_cell_merges")
if m > 12 and not halves:
@@ -2285,7 +2290,7 @@ class ProjectStore:
raise store.GoalMigration017Needed()
if halves:
# 병합에 가려진 칸(글은 왼쪽 위 칸에만 둔다) — 다른 창에서 병합된 뒤의 늦은 저장
area_id = conn.execute("SELECT area_id FROM goal_rows WHERE id = %s", (row_id,)).fetchone()["area_id"]
area_id = area["area_id"]
ids, merges, _ = self._area_geometry(conn, int(area_id))
if self._covered_by(merges, ids.index(row_id), store.GOAL_COL_ORDER.index(m)):
raise ValueError("병합된 칸입니다. 새로고침해 주세요.")
@@ -2299,6 +2304,77 @@ class ProjectStore:
(row_id, m, text, store.norm_str(updated_by, lower=True)),
)
def move_goal_cell(
self, *, source_row: Any, source_month: Any, target_row: Any, target_month: Any,
source_body: str, target_body: str, body: str, updated_by: str = "",
) -> str:
"""같은 연도 안에서 셀의 내용·서식을 이동한다. 두 값의 비교와 이동은 한 트랜잭션.
병합 시작 칸끼리도 내용만 이동하며 병합 배치·영역 색·열 너비는 유지한다.
응답만 유실된 요청을 다시 보내면 이미 같은 이동이 완료된 상태는 성공으로 돌려준다.
"""
try:
sr, tr = int(source_row), int(target_row)
except (TypeError, ValueError):
raise ValueError("이동할 셀이 올바르지 않습니다.")
sm = store.validate_goal_month(source_month, allow_year=True)
tm = store.validate_goal_month(target_month, allow_year=True)
if sr <= 0 or tr <= 0 or (sr, sm) == (tr, tm):
raise ValueError("서로 다른 두 셀을 골라 주세요.")
expected_source, expected_target = store.validate_goal_text(source_body), store.validate_goal_text(target_body)
moved = store.validate_goal_text(body)
if not expected_source or not moved or store.goal_text_plain(moved).strip() != store.goal_text_plain(expected_source).strip():
raise ValueError("원본 내용과 이동할 내용이 일치하지 않습니다.")
with self._pool.connection() as conn:
with conn.transaction():
rows = conn.execute(
"SELECT r.id, r.area_id, a.year FROM goal_rows r JOIN goal_areas a ON a.id = r.area_id "
"WHERE r.id = ANY(%s)", ([sr, tr],),
).fetchall()
by_id = {int(r["id"]): r for r in rows}
if sr not in by_id or tr not in by_id:
raise KeyError(sr if sr not in by_id else tr)
if by_id[sr]["year"] != by_id[tr]["year"]:
raise ValueError("같은 연도의 셀로만 이동할 수 있습니다.")
areas = sorted({int(r["area_id"]) for r in rows})
# 교차 영역 이동도 같은 순서로 잠가 교착을 피하고, 행 삭제도 함께 차단한다.
conn.execute("SELECT id FROM goal_areas WHERE id = ANY(%s) ORDER BY id FOR UPDATE", (areas,)).fetchall()
locked = conn.execute(
"SELECT id, area_id FROM goal_rows WHERE id = ANY(%s) ORDER BY id FOR UPDATE", ([sr, tr],),
).fetchall()
if len(locked) != len(by_id):
raise KeyError("이동할 줄이 삭제되었습니다.")
halves = self._table_exists(conn, "goal_cell_merges")
if (sm > 12 or tm > 12) and not halves:
raise store.GoalMigration014Needed()
if (sm in store.GOAL_Q_NEW or tm in store.GOAL_Q_NEW) and not self._goal_quarters(conn):
raise store.GoalMigration017Needed()
geometry = {a: self._area_geometry(conn, a) for a in areas} if halves else {}
for rid, month in ((sr, sm), (tr, tm)):
if halves:
ids, merges, _ = geometry[int(by_id[rid]["area_id"])]
if rid not in ids or self._covered_by(merges, ids.index(rid), store.GOAL_COL_ORDER.index(month)):
raise store.GoalCellConflict("병합 배치가 바뀌었습니다. 새로고침한 뒤 다시 이동해 주세요.")
cells = conn.execute(
"SELECT row_id, month, body FROM goal_cells "
"WHERE (row_id = %s AND month = %s) OR (row_id = %s AND month = %s) FOR UPDATE",
(sr, sm, tr, tm),
).fetchall()
values = {(int(c["row_id"]), int(c["month"])): store.validate_goal_text(c["body"]) for c in cells}
source, target = values.get((sr, sm), ""), values.get((tr, tm), "")
if not source and target == moved:
return moved
if source != expected_source or target != expected_target:
raise store.GoalCellConflict("원본 또는 대상 내용이 바뀌었습니다. 새로고침한 뒤 다시 이동해 주세요.")
conn.execute(
"INSERT INTO goal_cells (row_id, month, body, updated_by) VALUES (%s,%s,%s,%s) "
"ON CONFLICT (row_id, month) DO UPDATE "
"SET body = EXCLUDED.body, updated_by = EXCLUDED.updated_by, updated_at = now()",
(tr, tm, moved, store.norm_str(updated_by, lower=True)),
)
conn.execute("DELETE FROM goal_cells WHERE row_id = %s AND month = %s", (sr, sm))
return moved
# ── 엑셀식 사각형 병합 ─────────────────────────────────────────
@staticmethod
def _column_exists(conn: Any, table: str, column: str) -> bool: