Coverage for app / router / app / projects / router.py: 91%

86 statements  

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

1from typing import Optional 

2from uuid import UUID 

3 

4from fastapi import APIRouter, Depends, HTTPException, status 

5from sqlalchemy.orm import Session 

6 

7from app.dependency.base import get_db 

8from app.dependency.workspace import owner_for 

9from app.dependency.user.context import get_current_user 

10from app.dependency.resource_access import ( 

11 shared_with, 

12 verify_resource_access, 

13 verify_resource_read, 

14 verify_list_scope, 

15 verify_owner_scope, 

16) 

17from app.common import exceptions 

18from app.db.postgres.enums import HistoryEnum 

19from app.db.postgres.models import Project, User 

20from app.db.postgres.crud_schemas import Schema 

21from app.router.app.history.router import register_history_routes 

22from app.services import history as history_service 

23from app.services import projects as projects_service 

24from app.services.membership import PROJECT, MemberRole 

25from app.router.app.membership_routes import register_membership_routes 

26from app.router.app.projects.inheritance.router import inheritance_router 

27from app.router.app.organisations.router import _access_of 

28from app.router.app.organisations.schemas import ProjectListItem 

29 

30project_router = APIRouter(prefix="/projects", tags=["App - Projects"]) 

31 

32 

33@project_router.post( 

34 "", 

35 response_model=Schema.Project.Read, 

36 status_code=status.HTTP_201_CREATED, 

37) 

38def create_project( 

39 data: Schema.Project.Create, 

40 db: Session = Depends(get_db), 

41 current_user: User = Depends(get_current_user), 

42): 

43 payload = data.model_dump() 

44 if not payload.get("owner_organisation_id") and not payload.get("owner_project_id"): 

45 # In the workspace the request was made in: an organisation's 

46 # projects page makes an org project (app/dependency/workspace.py). 

47 payload.update(owner_for(db, current_user, model=Project)) 

48 if payload.get("owner_project_id"): 

49 # Projects are one level deep (``app/services/workspace.py``). 

50 raise HTTPException( 

51 status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, 

52 detail="A project belongs to a person or an organisation, " 

53 "not to another project.", 

54 ) 

55 verify_owner_scope(payload, current_user, db, model=Project) 

56 # Its creator is its first admin (app/services/projects.py). 

57 project = projects_service.create(db, current_user, **payload) 

58 db.commit() 

59 db.refresh(project) 

60 return project 

61 

62 

63@project_router.get( 

64 "/{project_id}", 

65 response_model=Schema.Project.Read, 

66) 

67def get_project( 

68 project_id: UUID, 

69 db: Session = Depends(get_db), 

70 current_user: User = Depends(get_current_user), 

71): 

72 project = db.get(Project, project_id) 

73 if not project: 

74 raise exceptions.ProjectNotFoundException 

75 verify_resource_read(project, current_user, db) 

76 return project 

77 

78 

79@project_router.get( 

80 "", 

81 response_model=list[ProjectListItem], 

82) 

83def list_projects( 

84 workspace: Optional[str] = None, 

85 owner_organisation_id: Optional[UUID] = None, 

86 db: Session = Depends(get_db), 

87 current_user: User = Depends(get_current_user), 

88): 

89 """Projects. With ``workspace``, the projects of that personal workspace 

90 or organisation the caller can open: all of them for its admins (and a 

91 person's own), the ones they were added to otherwise.""" 

92 query = db.query(Project) 

93 if workspace: 

94 from app.services import workspace as workspace_service 

95 

96 ws = workspace_service.resolve(db, workspace, current_user) 

97 if ws.kind is workspace_service.WorkspaceKind.project: 

98 # Projects don't contain projects. 

99 return [] 

100 openable = { 

101 e.workspace.id 

102 for e in workspace_service.workspaces_of(db, current_user) 

103 if e.workspace.kind is workspace_service.WorkspaceKind.project 

104 and e.workspace.parent is not None 

105 and e.workspace.parent.key == ws.key 

106 } 

107 if not openable: 

108 return [] 

109 query = query.filter(Project.id.in_(list(openable))) 

110 elif owner_organisation_id: 

111 verify_list_scope(owner_organisation_id, "organisation", current_user, db) 

112 query = query.filter(Project.owner_organisation_id == owner_organisation_id) 

113 else: 

114 # Projects the caller's organisations own, and any they were invited 

115 # into directly. 

116 from sqlalchemy import or_, select 

117 

118 from app.db.postgres.models import UserProject 

119 

120 joined = select(UserProject.project_id).where( 

121 UserProject.user_id == current_user.id 

122 ) 

123 query = query.filter( 

124 or_(shared_with(Project, current_user), Project.id.in_(joined)) 

125 ) 

126 projects = query.all() 

127 # The caller's role and access in each, where they are a member, so a 

128 # form can offer a project as an owner only where they may create. 

129 from app.db.postgres.models import Role, UserProject 

130 

131 rows = ( 

132 db.query(UserProject.project_id, Role.name, UserProject.access) 

133 .join(Role, Role.id == UserProject.role_id) 

134 .filter( 

135 UserProject.user_id == current_user.id, 

136 UserProject.project_id.in_([p.id for p in projects]), 

137 ) 

138 .all() 

139 if projects 

140 else [] 

141 ) 

142 mine = {pid: (role, access) for pid, role, access in rows} 

143 # Validated whole, never ``model_copy(update=…)``, which skips 

144 # validation (see ``organisations/router.py``). 

145 return [ 

146 ProjectListItem.model_validate( 

147 { 

148 **Schema.Project.Read.model_validate(p).model_dump(), 

149 "role": MemberRole(mine[p.id][0]) if p.id in mine else None, 

150 "access": _access_of(*mine[p.id]) if p.id in mine else None, 

151 } 

152 ) 

153 for p in projects 

154 ] 

155 

156 

157@project_router.patch( 

158 "/{project_id}", 

159 response_model=Schema.Project.Read, 

160) 

161def update_project( 

162 project_id: UUID, 

163 data: Schema.Project.Update, 

164 db: Session = Depends(get_db), 

165 current_user: User = Depends(get_current_user), 

166): 

167 project = db.get(Project, project_id) 

168 if not project: 

169 raise exceptions.ProjectNotFoundException 

170 verify_resource_access(project, current_user, db) 

171 

172 for field, value in data.model_dump(exclude_unset=True).items(): 

173 setattr(project, field, value) 

174 

175 history_service.record_version( 

176 db, 

177 resource_type=HistoryEnum.ResourceType.project, 

178 model=Project, 

179 resource=project, 

180 action=HistoryEnum.Action.updated, 

181 actor=current_user, 

182 ) 

183 db.commit() 

184 db.refresh(project) 

185 return project 

186 

187 

188@project_router.delete( 

189 "/{project_id}", 

190 status_code=status.HTTP_204_NO_CONTENT, 

191) 

192def delete_project( 

193 project_id: UUID, 

194 db: Session = Depends(get_db), 

195 current_user: User = Depends(get_current_user), 

196): 

197 project = db.get(Project, project_id) 

198 if not project: 

199 raise exceptions.ProjectNotFoundException 

200 verify_resource_access(project, current_user, db) 

201 # Its agents go with it, and then what they remember, in svc-ai. 

202 projects_service.delete(db, project, current_user) 

203 

204 

205register_history_routes( 

206 project_router, 

207 resource_type=HistoryEnum.ResourceType.project, 

208 model=Project, 

209 not_found_detail="Project not found", 

210) 

211 

212# Members and invites: the same routes organisations mount. 

213register_membership_routes(project_router, PROJECT) 

214 

215# What a project inherits from its parent (app/services/inheritance.py). 

216project_router.include_router(inheritance_router)