Coverage for app / router / app / ownership / router.py: 94%

53 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-10-31 11:21 +0000

1"""Moving personal work into an organisation or project. 

2 

3Preview first: the plan lists what goes with it, what is already there and 

4what blocks the move. See ``app/services/ownership.py``. 

5""" 

6 

7from uuid import UUID 

8 

9from fastapi import APIRouter, Depends 

10from pydantic import BaseModel 

11from sqlalchemy.orm import Session 

12 

13from app.db.postgres.models import User 

14from app.dependency.base import get_db 

15from app.dependency.user.context import get_current_user 

16from app.services import ownership 

17 

18ownership_router = APIRouter(prefix="/ownership", tags=["App - Ownership"]) 

19 

20 

21class MoveRequest(BaseModel): 

22 kind: str 

23 resource_id: UUID 

24 #: Exactly one: where it goes. 

25 organisation_id: UUID | None = None 

26 project_id: UUID | None = None 

27 

28 

29class MoveItem(BaseModel): 

30 kind: str 

31 label: str 

32 id: UUID 

33 name: str 

34 reason: str | None = None 

35 

36 

37class MovePlan(BaseModel): 

38 moves: list[MoveItem] 

39 already_there: list[MoveItem] 

40 blockers: list[MoveItem] 

41 

42 

43def _args(body: MoveRequest): 

44 return body.kind, body.resource_id, ownership.destination(body.model_dump()) 

45 

46 

47@ownership_router.post("/preview", response_model=MovePlan) 

48def ownership_preview( 

49 body: MoveRequest, 

50 db: Session = Depends(get_db), 

51 current_user: User = Depends(get_current_user), 

52): 

53 """What a move would carry with it, and what would stop it. Writes nothing.""" 

54 return ownership.plan(db, current_user, *_args(body)).as_json() 

55 

56 

57@ownership_router.post("/move", response_model=MovePlan) 

58def ownership_move( 

59 body: MoveRequest, 

60 db: Session = Depends(get_db), 

61 current_user: User = Depends(get_current_user), 

62): 

63 """Move it, and the personal work it depends on, as the preview listed.""" 

64 result = ownership.move(db, current_user, *_args(body)) 

65 db.commit() 

66 return result.as_json() 

67 

68 

69class CopyRequest(BaseModel): 

70 kind: str 

71 resource_id: UUID 

72 #: Where the copy goes — ``personal`` / ``organisation:<id>`` / 

73 #: ``project:<id>``. Defaults to the workspace the request is made in. 

74 workspace: str | None = None 

75 

76 

77class CopyResult(BaseModel): 

78 kind: str 

79 id: UUID 

80 name: str | None = None 

81 #: The key of the workspace it was made in. 

82 workspace: str 

83 

84 

85@ownership_router.post("/copy", response_model=CopyResult) 

86def ownership_copy( 

87 body: CopyRequest, 

88 db: Session = Depends(get_db), 

89 current_user: User = Depends(get_current_user), 

90): 

91 """Copy an agent, skill, skillset, rule, dataset or dashboard into a 

92 workspace as an editable copy of its own (``app/services/copying.py``): 

93 from a project's parent into the project, or within one workspace. What 

94 a copied agent uses comes along by reference — the project inherits it.""" 

95 from app.dependency.workspace import requested_workspace 

96 from app.services import copying 

97 from app.services import workspace as workspace_service 

98 

99 key = body.workspace or requested_workspace() or workspace_service.PERSONAL 

100 ws = workspace_service.resolve(db, key, current_user) 

101 copy = copying.copy(db, current_user, body.kind, body.resource_id, ws) 

102 db.commit() 

103 return CopyResult( 

104 kind=body.kind, id=copy.id, name=getattr(copy, "name", None), workspace=ws.key 

105 )