1
1
"""
2
2
Organisation service with integrated permissions
3
3
"""
4
+
4
5
from typing import Dict , Optional , Set
5
- from django . core . exceptions import PermissionDenied
6
+
6
7
from django .db .models import Count
7
8
from django .db .models .query import QuerySet
8
9
9
10
from ..constants import ROLE_ADMIN , ROLE_PROJECT_MANAGER
10
- from ..models import (
11
- Organisation ,
12
- OrganisationMembership ,
13
- Project ,
14
- User
15
- )
11
+ from ..models import Organisation , OrganisationMembership , Project , User
16
12
from .base import BasePermissionService , requires_permission
17
13
18
14
@@ -21,7 +17,9 @@ class OrganisationService(BasePermissionService):
21
17
22
18
def get_user_role (self , user : User , organisation : Organisation ) -> Optional [str ]:
23
19
try :
24
- membership = organisation .organisationmembership_set .filter (user = user ).first ()
20
+ membership = organisation .organisationmembership_set .filter (
21
+ user = user
22
+ ).first ()
25
23
return membership .role if membership else None
26
24
except AttributeError : # In case user is AnonymousUser
27
25
return None
@@ -77,7 +75,7 @@ def get_user_organisation_ids(self, user: User) -> Set[int]:
77
75
78
76
@requires_permission ("edit" , obj_param = "organisation" )
79
77
def update_organisation (
80
- self , user : User , organisation : Organisation , data : Dict
78
+ self , user : User , organisation : Organisation , data : Dict
81
79
) -> Organisation :
82
80
"""Update organisation with provided data"""
83
81
for key , value in data .items ():
@@ -86,7 +84,7 @@ def update_organisation(
86
84
return organisation
87
85
88
86
def create_organisation (
89
- self , user : User , name : str , description : str = None
87
+ self , user : User , name : str , description : str = None
90
88
) -> Organisation :
91
89
"""
92
90
Create a new organisation, and add the creator to it.
@@ -101,11 +99,11 @@ def create_organisation(
101
99
102
100
@requires_permission ("edit" , obj_param = "organisation" )
103
101
def add_user_to_organisation (
104
- self ,
105
- user : User ,
106
- user_to_add : User ,
107
- organisation : Organisation ,
108
- role : str ,
102
+ self ,
103
+ user : User ,
104
+ user_to_add : User ,
105
+ organisation : Organisation ,
106
+ role : str ,
109
107
) -> OrganisationMembership :
110
108
"""Add a user to an organisation with specified role"""
111
109
if role not in [ROLE_ADMIN , ROLE_PROJECT_MANAGER ]:
@@ -119,15 +117,15 @@ def add_user_to_organisation(
119
117
120
118
@requires_permission ("edit" , obj_param = "organisation" )
121
119
def remove_user_from_organisation (
122
- self , user : User , organisation : Organisation , removed_user : User
120
+ self , user : User , organisation : Organisation , removed_user : User
123
121
) -> None :
124
122
"""Remove user from organisation"""
125
123
OrganisationMembership .objects .filter (
126
124
user = removed_user , organisation = organisation
127
125
).delete ()
128
126
129
127
def get_organisation_projects (
130
- self , organisation : Organisation , user : User = None , with_metrics : bool = True
128
+ self , organisation : Organisation , user : User = None , with_metrics : bool = True
131
129
) -> QuerySet [Project ]:
132
130
"""Get projects for an organisation with optional metrics"""
133
131
if not self .can_view (user , organisation ):
@@ -137,15 +135,15 @@ def get_organisation_projects(
137
135
138
136
# Add metrics
139
137
if with_metrics :
140
- projects = base_query .annotate (
138
+ base_query = base_query .annotate (
141
139
survey_count = Count ("survey__id" , distinct = True ),
142
140
).select_related ("created_by" , "organisation" )
143
141
144
142
return base_query .order_by ("-created_at" )
145
143
146
144
@requires_permission ("view" , obj_param = "organisation" )
147
145
def get_organisation_members (
148
- self , user : User , organisation : Organisation
146
+ self , user : User , organisation : Organisation
149
147
) -> QuerySet [OrganisationMembership ]:
150
148
"""Get all members of an organisation with their roles"""
151
149
return OrganisationMembership .objects .filter (
0 commit comments