Coverage for app / services / statements / scope.py: 84%

44 statements  

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

1"""Which rows a workspace's statement counts. 

2 

3The tables a statement reads say where their rows belong in three different 

4ways, and each is asked the way it is written: 

5 

6``stamped`` the usage ledger and the activity log carry the workspace's 

7 stamp (``Workspace.stamped``): a project's row names its parent 

8 too, so a parent's counts its projects. 

9``owned`` a resource (an evaluation, a dataset) names one owner: the 

10 workspace, or one of its projects. 

11``filed`` a review flag names the person, the organisation or the project 

12 whose queue holds it. 

13``made`` a verdict names the workspace its turn happened in 

14 (``workspace_organisation_id`` / ``workspace_project_id``). 

15 

16An organisation's and a Personal workspace's statement count their projects, 

17as their budgets do; a project's counts itself. 

18""" 

19 

20from __future__ import annotations 

21 

22from dataclasses import dataclass 

23from typing import Any 

24from uuid import UUID 

25 

26from sqlalchemy import and_, or_ 

27from sqlalchemy.orm import Session 

28 

29from app.db.postgres.models import Project 

30from app.services.workspace import Workspace, WorkspaceKind 

31 

32 

33@dataclass(frozen=True) 

34class Here: 

35 """A workspace, with the projects its statement counts.""" 

36 

37 ws: Workspace 

38 projects: tuple[UUID, ...] = () 

39 

40 @classmethod 

41 def of(cls, db: Session, ws: Workspace) -> "Here": 

42 if ws.kind is WorkspaceKind.project: 

43 return cls(ws) 

44 column = ( 

45 Project.owner_organisation_id 

46 if ws.kind is WorkspaceKind.organisation 

47 else Project.owner_user_id 

48 ) 

49 ids = tuple( 

50 row[0] for row in db.query(Project.id).filter(column == ws.id).all() 

51 ) 

52 return cls(ws, ids) 

53 

54 @property 

55 def counts_projects(self) -> bool: 

56 return self.ws.kind is not WorkspaceKind.project 

57 

58 def stamped(self, model: Any): 

59 return self.ws.stamped(model, projects=True) 

60 

61 def owned(self, model: Any): 

62 clauses = [self.ws.owns(model)] 

63 if self.projects: 

64 clauses.append(model.owner_project_id.in_(self.projects)) 

65 return or_(*clauses) 

66 

67 def filed(self, model: Any): 

68 if self.ws.kind is WorkspaceKind.project: 

69 return model.owner_project_id == self.ws.id 

70 if self.ws.kind is WorkspaceKind.organisation: 

71 # A project's flag names its organisation too. 

72 return model.owner_organisation_id == self.ws.id 

73 return and_( 

74 model.owner_user_id == self.ws.id, 

75 model.owner_organisation_id.is_(None), 

76 ) 

77 

78 def made(self, model: Any): 

79 if self.ws.kind is WorkspaceKind.project: 

80 return model.workspace_project_id == self.ws.id 

81 if self.ws.kind is WorkspaceKind.organisation: 

82 return model.workspace_organisation_id == self.ws.id 

83 projects = model.workspace_project_id.is_(None) 

84 if self.projects: 

85 projects = or_(projects, model.workspace_project_id.in_(self.projects)) 

86 return and_( 

87 model.owner_user_id == self.ws.id, 

88 model.workspace_organisation_id.is_(None), 

89 projects, 

90 )