Coverage for app / router / app / workspaces / router.py: 98%
45 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
1"""The workspaces a user can open (``app/services/workspace.py``): what
2fe-core's workspace switcher lists, and how a deep link's workspace is
3resolved to a name and the caller's place in it."""
5from typing import Literal
6from uuid import UUID
8from fastapi import APIRouter, Depends
9from pydantic import BaseModel
10from sqlalchemy.orm import Session
12from app.db.postgres.models import User
13from app.dependency.base import get_db
14from app.dependency.user.context import get_current_user
15from app.router.app.organisations.schemas import AccessMap
16from app.services import workspace as workspace_service
17from app.services.membership import MemberRole
19workspace_router = APIRouter(prefix="/workspaces", tags=["App - Workspaces"])
21Kind = Literal["personal", "organisation", "project"]
24class WorkspaceParent(BaseModel):
25 key: str
26 kind: Kind
27 id: UUID
28 name: str | None = None
31class WorkspaceRead(BaseModel):
32 #: ``user:<id>``, ``organisation:<id>`` or ``project:<id>`` — what a
33 #: request's ``workspace`` parameter takes.
34 key: str
35 kind: Kind
36 id: UUID
37 name: str | None = None
38 #: A project's owner: the personal workspace or organisation it sits in.
39 parent: WorkspaceParent | None = None
40 #: The caller's role there — None for a superuser who is not a member.
41 role: MemberRole | None = None
42 #: The caller's level per area there.
43 access: AccessMap | None = None
44 #: An admin by position (of the organisation that owns the project, or
45 #: as a personal project's owner), not by being added to it.
46 implicit: bool = False
49def _parent(ws) -> WorkspaceParent | None:
50 if ws.parent is None:
51 return None
52 p = ws.parent
53 return WorkspaceParent(key=p.key, kind=p.kind.value, id=p.id, name=p.name)
56def _read(entry) -> WorkspaceRead:
57 ws = entry.workspace
58 return WorkspaceRead(
59 key=ws.key,
60 kind=ws.kind.value,
61 id=ws.id,
62 name=ws.name,
63 parent=_parent(ws),
64 role=MemberRole(entry.role) if entry.role else None,
65 access=entry.access,
66 implicit=entry.implicit,
67 )
70@workspace_router.get("", response_model=list[WorkspaceRead])
71def list_workspaces(
72 db: Session = Depends(get_db),
73 current_user: User = Depends(get_current_user),
74):
75 """Every workspace the caller can open: Personal and its projects, then
76 each organisation followed by its projects they belong to (all of them,
77 for its admins), then other people's personal projects they joined."""
78 return [_read(e) for e in workspace_service.workspaces_of(db, current_user)]
81@workspace_router.get("/resolve", response_model=WorkspaceRead)
82def resolve_workspace(
83 key: str,
84 db: Session = Depends(get_db),
85 current_user: User = Depends(get_current_user),
86):
87 """One workspace by key — ``personal`` for the caller's own — with the
88 caller's role and access there. 404 for one that doesn't exist, 403 for
89 one they don't belong to."""
90 ws = workspace_service.resolve(db, key, current_user)
91 for entry in workspace_service.workspaces_of(db, current_user):
92 if entry.workspace.key == ws.key:
93 return _read(entry)
94 # A superuser opening a workspace they don't belong to.
95 return _read(workspace_service.Entry(ws, None, None))