refactor(routes): one supersession seam for REST and MCP; PUT/PATCH notes share a handler; shared mask/not-found/caller helpers (#2829, milestone 296 area 5)
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 9s
CI & Build / integration (push) Successful in 25s
CI & Build / TypeScript typecheck (push) Successful in 32s
CI & Build / Python tests (push) Failing after 37s
CI & Build / Build & push image (push) Skipped
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 9s
CI & Build / integration (push) Successful in 25s
CI & Build / TypeScript typecheck (push) Successful in 32s
CI & Build / Python tests (push) Failing after 37s
CI & Build / Build & push image (push) Skipped
Reading the 28 route modules against each other and against the MCP tools: - routes/notes.py carried a PUT and a PATCH handler that were the same function minus the supersedes contract on one of them — one handler now serves both verbs, so both carry it. - The two _attach_supersession copies (REST + MCP) become supersession_svc.attach_relations(uid, note_id, data, hint=) — the seam the two surfaces must agree through; only the agent surface adds the one-sentence reading hint. - Three local _uid() wrappers over g.user.id → scribe.auth.get_current_user_id like every other module; design_systems' private _not_found → routes.utils. not_found; the four "********" literals → settings_svc.SECRET_MASK with the read/write contract written once. - routes/plugin.py: the project_id/repo resolution block and the comma-separated id parse were copied into three endpoints — _project_scope() and _int_list() now. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,29 +1,26 @@
|
||||
"""Rulebook / topic REST endpoints.
|
||||
|
||||
Wraps services/rulebooks.py. Standard Scribe auth: g.user.id is the
|
||||
Wraps services/rulebooks.py. Standard Scribe auth: get_current_user_id() is the
|
||||
authenticated owner; the service enforces ownership scoping.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from quart import Blueprint, g, jsonify, request
|
||||
from quart import Blueprint, jsonify, request
|
||||
|
||||
from scribe.auth import login_required
|
||||
from scribe.auth import get_current_user_id, login_required
|
||||
import scribe.services.rulebooks as rulebooks_svc
|
||||
from scribe.services.trash import delete as trash_delete
|
||||
|
||||
rulebooks_bp = Blueprint("rulebooks", __name__, url_prefix="/api")
|
||||
|
||||
|
||||
def _uid() -> int:
|
||||
return g.user.id
|
||||
|
||||
|
||||
# ── Rulebooks ───────────────────────────────────────────────────────────
|
||||
|
||||
@rulebooks_bp.get("/rulebooks")
|
||||
@login_required
|
||||
async def list_rulebooks():
|
||||
rows = await rulebooks_svc.list_rulebooks(_uid())
|
||||
rows = await rulebooks_svc.list_rulebooks(get_current_user_id())
|
||||
return jsonify({"rulebooks": [rb.to_dict() for rb in rows]})
|
||||
|
||||
|
||||
@@ -35,7 +32,7 @@ async def create_rulebook():
|
||||
if not title:
|
||||
return jsonify({"error": "title is required"}), 400
|
||||
rb = await rulebooks_svc.create_rulebook(
|
||||
user_id=_uid(),
|
||||
user_id=get_current_user_id(),
|
||||
title=title,
|
||||
description=data.get("description", ""),
|
||||
)
|
||||
@@ -45,7 +42,7 @@ async def create_rulebook():
|
||||
@rulebooks_bp.get("/rulebooks/<int:rulebook_id>")
|
||||
@login_required
|
||||
async def get_rulebook(rulebook_id: int):
|
||||
rb = await rulebooks_svc.get_rulebook(rulebook_id, _uid())
|
||||
rb = await rulebooks_svc.get_rulebook(rulebook_id, get_current_user_id())
|
||||
if rb is None:
|
||||
return jsonify({"error": "rulebook not found"}), 404
|
||||
return jsonify(rb.to_dict())
|
||||
@@ -56,7 +53,7 @@ async def get_rulebook(rulebook_id: int):
|
||||
async def update_rulebook(rulebook_id: int):
|
||||
data = await request.get_json() or {}
|
||||
fields = {k: v for k, v in data.items() if k in ("title", "description", "always_on")}
|
||||
rb = await rulebooks_svc.update_rulebook(rulebook_id, _uid(), **fields)
|
||||
rb = await rulebooks_svc.update_rulebook(rulebook_id, get_current_user_id(), **fields)
|
||||
if rb is None:
|
||||
return jsonify({"error": "rulebook not found"}), 404
|
||||
return jsonify(rb.to_dict())
|
||||
@@ -65,7 +62,7 @@ async def update_rulebook(rulebook_id: int):
|
||||
@rulebooks_bp.delete("/rulebooks/<int:rulebook_id>")
|
||||
@login_required
|
||||
async def delete_rulebook(rulebook_id: int):
|
||||
await trash_delete(_uid(), "rulebook", rulebook_id)
|
||||
await trash_delete(get_current_user_id(), "rulebook", rulebook_id)
|
||||
return "", 204
|
||||
|
||||
|
||||
@@ -75,7 +72,7 @@ async def delete_rulebook(rulebook_id: int):
|
||||
@login_required
|
||||
async def list_topics(rulebook_id: int):
|
||||
try:
|
||||
rows = await rulebooks_svc.list_topics(rulebook_id, _uid())
|
||||
rows = await rulebooks_svc.list_topics(rulebook_id, get_current_user_id())
|
||||
except ValueError as exc:
|
||||
return jsonify({"error": str(exc)}), 404
|
||||
return jsonify({"topics": [t.to_dict() for t in rows]})
|
||||
@@ -91,7 +88,7 @@ async def create_topic(rulebook_id: int):
|
||||
try:
|
||||
topic = await rulebooks_svc.create_topic(
|
||||
rulebook_id=rulebook_id,
|
||||
user_id=_uid(),
|
||||
user_id=get_current_user_id(),
|
||||
title=title,
|
||||
description=data.get("description", ""),
|
||||
order_index=data.get("order_index", 0),
|
||||
@@ -109,7 +106,7 @@ async def update_topic(topic_id: int):
|
||||
k: v for k, v in data.items()
|
||||
if k in ("title", "description", "order_index")
|
||||
}
|
||||
topic = await rulebooks_svc.update_topic(topic_id, _uid(), **fields)
|
||||
topic = await rulebooks_svc.update_topic(topic_id, get_current_user_id(), **fields)
|
||||
if topic is None:
|
||||
return jsonify({"error": "topic not found"}), 404
|
||||
return jsonify(topic.to_dict())
|
||||
@@ -118,7 +115,7 @@ async def update_topic(topic_id: int):
|
||||
@rulebooks_bp.delete("/rulebook-topics/<int:topic_id>")
|
||||
@login_required
|
||||
async def delete_topic(topic_id: int):
|
||||
if await trash_delete(_uid(), "topic", topic_id) is None:
|
||||
if await trash_delete(get_current_user_id(), "topic", topic_id) is None:
|
||||
return jsonify({"error": "topic not found"}), 404
|
||||
return "", 204
|
||||
|
||||
@@ -140,7 +137,7 @@ async def list_rules():
|
||||
return jsonify({"error": "rulebook_id, topic_id, project_id must be integers"}), 400
|
||||
|
||||
rows = await rulebooks_svc.list_rules(
|
||||
user_id=_uid(),
|
||||
user_id=get_current_user_id(),
|
||||
rulebook_id=rulebook_id,
|
||||
topic_id=topic_id,
|
||||
project_id=project_id,
|
||||
@@ -159,7 +156,7 @@ async def create_rule(topic_id: int):
|
||||
try:
|
||||
rule = await rulebooks_svc.create_rule(
|
||||
topic_id=topic_id,
|
||||
user_id=_uid(),
|
||||
user_id=get_current_user_id(),
|
||||
title=title,
|
||||
statement=statement,
|
||||
why=data.get("why", ""),
|
||||
@@ -174,7 +171,7 @@ async def create_rule(topic_id: int):
|
||||
@rulebooks_bp.get("/rules/<int:rule_id>")
|
||||
@login_required
|
||||
async def get_rule(rule_id: int):
|
||||
rule = await rulebooks_svc.get_rule(rule_id, _uid())
|
||||
rule = await rulebooks_svc.get_rule(rule_id, get_current_user_id())
|
||||
if rule is None:
|
||||
return jsonify({"error": "rule not found"}), 404
|
||||
return jsonify(rule.to_dict())
|
||||
@@ -188,7 +185,7 @@ async def update_rule(rule_id: int):
|
||||
k: v for k, v in data.items()
|
||||
if k in ("title", "statement", "why", "how_to_apply", "order_index")
|
||||
}
|
||||
rule = await rulebooks_svc.update_rule(rule_id, _uid(), **fields)
|
||||
rule = await rulebooks_svc.update_rule(rule_id, get_current_user_id(), **fields)
|
||||
if rule is None:
|
||||
return jsonify({"error": "rule not found"}), 404
|
||||
return jsonify(rule.to_dict())
|
||||
@@ -197,7 +194,7 @@ async def update_rule(rule_id: int):
|
||||
@rulebooks_bp.delete("/rules/<int:rule_id>")
|
||||
@login_required
|
||||
async def delete_rule(rule_id: int):
|
||||
if await trash_delete(_uid(), "rule", rule_id) is None:
|
||||
if await trash_delete(get_current_user_id(), "rule", rule_id) is None:
|
||||
return jsonify({"error": "rule not found"}), 404
|
||||
return "", 204
|
||||
|
||||
@@ -213,7 +210,7 @@ async def subscribe_project(project_id: int):
|
||||
return jsonify({"error": "rulebook_id is required"}), 400
|
||||
try:
|
||||
await rulebooks_svc.subscribe_project(
|
||||
project_id=project_id, rulebook_id=int(rulebook_id), user_id=_uid(),
|
||||
project_id=project_id, rulebook_id=int(rulebook_id), user_id=get_current_user_id(),
|
||||
)
|
||||
except ValueError as exc:
|
||||
return jsonify({"error": str(exc)}), 404
|
||||
@@ -227,7 +224,7 @@ async def subscribe_project(project_id: int):
|
||||
async def unsubscribe_project(project_id: int, rulebook_id: int):
|
||||
try:
|
||||
await rulebooks_svc.unsubscribe_project(
|
||||
project_id=project_id, rulebook_id=rulebook_id, user_id=_uid(),
|
||||
project_id=project_id, rulebook_id=rulebook_id, user_id=get_current_user_id(),
|
||||
)
|
||||
except ValueError as exc:
|
||||
return jsonify({"error": str(exc)}), 404
|
||||
@@ -238,7 +235,7 @@ async def unsubscribe_project(project_id: int, rulebook_id: int):
|
||||
@login_required
|
||||
async def get_project_rules(project_id: int):
|
||||
result = await rulebooks_svc.get_applicable_rules(
|
||||
project_id=project_id, user_id=_uid(),
|
||||
project_id=project_id, user_id=get_current_user_id(),
|
||||
)
|
||||
return jsonify(result)
|
||||
|
||||
@@ -248,7 +245,7 @@ async def get_project_rules(project_id: int):
|
||||
async def suppress_project_rule(project_id: int, rule_id: int):
|
||||
try:
|
||||
await rulebooks_svc.suppress_rule_for_project(
|
||||
project_id=project_id, rule_id=rule_id, user_id=_uid(),
|
||||
project_id=project_id, rule_id=rule_id, user_id=get_current_user_id(),
|
||||
)
|
||||
except ValueError as exc:
|
||||
return jsonify({"error": str(exc)}), 404
|
||||
@@ -260,7 +257,7 @@ async def suppress_project_rule(project_id: int, rule_id: int):
|
||||
async def unsuppress_project_rule(project_id: int, rule_id: int):
|
||||
try:
|
||||
await rulebooks_svc.unsuppress_rule_for_project(
|
||||
project_id=project_id, rule_id=rule_id, user_id=_uid(),
|
||||
project_id=project_id, rule_id=rule_id, user_id=get_current_user_id(),
|
||||
)
|
||||
except ValueError as exc:
|
||||
return jsonify({"error": str(exc)}), 404
|
||||
@@ -272,7 +269,7 @@ async def unsuppress_project_rule(project_id: int, rule_id: int):
|
||||
async def suppress_project_topic(project_id: int, topic_id: int):
|
||||
try:
|
||||
await rulebooks_svc.suppress_topic_for_project(
|
||||
project_id=project_id, topic_id=topic_id, user_id=_uid(),
|
||||
project_id=project_id, topic_id=topic_id, user_id=get_current_user_id(),
|
||||
)
|
||||
except ValueError as exc:
|
||||
return jsonify({"error": str(exc)}), 404
|
||||
@@ -284,7 +281,7 @@ async def suppress_project_topic(project_id: int, topic_id: int):
|
||||
async def unsuppress_project_topic(project_id: int, topic_id: int):
|
||||
try:
|
||||
await rulebooks_svc.unsuppress_topic_for_project(
|
||||
project_id=project_id, topic_id=topic_id, user_id=_uid(),
|
||||
project_id=project_id, topic_id=topic_id, user_id=get_current_user_id(),
|
||||
)
|
||||
except ValueError as exc:
|
||||
return jsonify({"error": str(exc)}), 404
|
||||
@@ -303,7 +300,7 @@ async def create_project_rule(project_id: int):
|
||||
try:
|
||||
rule = await rulebooks_svc.create_project_rule(
|
||||
project_id=project_id,
|
||||
user_id=_uid(),
|
||||
user_id=get_current_user_id(),
|
||||
title=title,
|
||||
statement=statement,
|
||||
why=data.get("why", ""),
|
||||
|
||||
Reference in New Issue
Block a user