Skip to content

Commit e30590f

Browse files
committed
Update routes, layers and table files to work with new permissions
1 parent e859d07 commit e30590f

33 files changed

+491
-575
lines changed

api/routes/analysis.py

+34-46
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,16 @@
1111
from api.utils.dates import parse_date_only_string
1212
from api.utils.db import (
1313
Connection,
14-
get_project_read_connection,
15-
get_project_write_connection,
14+
get_project_db_connection,
1615
get_projectless_db_connection,
1716
)
1817
from api.utils.export import ExportType
1918
from db.python.layers.analysis import AnalysisLayer
2019
from db.python.tables.analysis import AnalysisFilter
21-
from db.python.tables.project import ProjectPermissionsTable
2220
from db.python.utils import GenericFilter
2321
from models.enums import AnalysisStatus
24-
from models.models.analysis import (
25-
Analysis,
26-
AnalysisInternal,
27-
ProportionalDateTemporalMethod,
28-
)
29-
from models.models.group import ReadAccessRoles
22+
from models.models.analysis import Analysis, ProportionalDateTemporalMethod
23+
from models.models.project import FullWriteAccessRoles, ReadAccessRoles
3024
from models.utils.sequencing_group_id_format import (
3125
sequencing_group_id_format,
3226
sequencing_group_id_format_list,
@@ -96,7 +90,8 @@ def to_filter(self, project_id_map: dict[str, int]) -> AnalysisFilter:
9690

9791
@router.put('/{project}/', operation_id='createAnalysis', response_model=int)
9892
async def create_analysis(
99-
analysis: Analysis, connection: Connection = get_project_write_connection
93+
analysis: Analysis,
94+
connection: Connection = get_project_db_connection(FullWriteAccessRoles),
10095
) -> int:
10196
"""Create a new analysis"""
10297

@@ -136,14 +131,14 @@ async def update_analysis(
136131
)
137132
async def get_all_sample_ids_without_analysis_type(
138133
analysis_type: str,
139-
connection: Connection = get_project_read_connection,
134+
connection: Connection = get_project_db_connection(ReadAccessRoles),
140135
):
141136
"""get_all_sample_ids_without_analysis_type"""
142137
atable = AnalysisLayer(connection)
143-
assert connection.project
138+
assert connection.project_id
144139
sequencing_group_ids = (
145140
await atable.get_all_sequencing_group_ids_without_analysis_type(
146-
connection.project, analysis_type
141+
connection.project_id, analysis_type
147142
)
148143
)
149144
return {
@@ -156,12 +151,12 @@ async def get_all_sample_ids_without_analysis_type(
156151
operation_id='getIncompleteAnalyses',
157152
)
158153
async def get_incomplete_analyses(
159-
connection: Connection = get_project_read_connection,
154+
connection: Connection = get_project_db_connection(ReadAccessRoles),
160155
):
161156
"""Get analyses with status queued or in-progress"""
162157
atable = AnalysisLayer(connection)
163-
assert connection.project
164-
results = await atable.get_incomplete_analyses(project=connection.project)
158+
assert connection.project_id
159+
results = await atable.get_incomplete_analyses(project=connection.project_id)
165160
return [r.to_external() for r in results]
166161

167162

@@ -171,13 +166,13 @@ async def get_incomplete_analyses(
171166
)
172167
async def get_latest_complete_analysis_for_type(
173168
analysis_type: str,
174-
connection: Connection = get_project_read_connection,
169+
connection: Connection = get_project_db_connection(ReadAccessRoles),
175170
):
176171
"""Get (SINGLE) latest complete analysis for some analysis type"""
177172
alayer = AnalysisLayer(connection)
178-
assert connection.project
173+
assert connection.project_id
179174
analysis = await alayer.get_latest_complete_analysis_for_type(
180-
project=connection.project, analysis_type=analysis_type
175+
project=connection.project_id, analysis_type=analysis_type
181176
)
182177
return analysis.to_external()
183178

@@ -189,16 +184,16 @@ async def get_latest_complete_analysis_for_type(
189184
async def get_latest_complete_analysis_for_type_post(
190185
analysis_type: str,
191186
meta: dict[str, Any] = Body(..., embed=True), # type: ignore[assignment]
192-
connection: Connection = get_project_read_connection,
187+
connection: Connection = get_project_db_connection(ReadAccessRoles),
193188
):
194189
"""
195190
Get SINGLE latest complete analysis for some analysis type
196191
(you can specify meta attributes in this route)
197192
"""
198193
alayer = AnalysisLayer(connection)
199-
assert connection.project
194+
assert connection.project_id
200195
analysis = await alayer.get_latest_complete_analysis_for_type(
201-
project=connection.project,
196+
project=connection.project_id,
202197
analysis_type=analysis_type,
203198
meta=meta,
204199
)
@@ -229,11 +224,8 @@ async def query_analyses(
229224
if not query.projects:
230225
raise ValueError('Must specify "projects"')
231226

232-
pt = ProjectPermissionsTable(connection)
233-
projects = await pt.get_and_check_access_to_projects_for_names(
234-
user=connection.author,
235-
project_names=query.projects,
236-
allowed_roles=ReadAccessRoles,
227+
projects = connection.get_and_check_access_to_projects_for_names(
228+
query.projects, ReadAccessRoles
237229
)
238230
project_name_map = {p.name: p.id for p in projects}
239231
atable = AnalysisLayer(connection)
@@ -243,27 +235,24 @@ async def query_analyses(
243235

244236
@router.get('/analysis-runner', operation_id='getAnalysisRunnerLog')
245237
async def get_analysis_runner_log(
246-
project_names: list[str] = Query(None), # type: ignore
247-
# author: str = None, # not implemented yet, uncomment when we do
248-
output_dir: str = None,
249-
ar_guid: str = None,
238+
project_names: list[str],
239+
output_dir: str,
240+
ar_guid: str | None = None,
250241
connection: Connection = get_projectless_db_connection,
251-
) -> list[AnalysisInternal]:
242+
) -> list[Analysis]:
252243
"""
253244
Get log for the analysis-runner, useful for checking this history of analysis
254245
"""
255246
atable = AnalysisLayer(connection)
256247
project_ids = None
257-
if project_names:
258-
pt = ProjectPermissionsTable(connection)
259-
projects = await pt.get_and_check_access_to_projects_for_names(
260-
connection.author, project_names, allowed_roles=ReadAccessRoles
261-
)
262-
project_ids = [p.id for p in projects]
248+
249+
projects = connection.get_and_check_access_to_projects_for_names(
250+
project_names, allowed_roles=ReadAccessRoles
251+
)
252+
project_ids = [p.id for p in projects]
263253

264254
results = await atable.get_analysis_runner_log(
265255
project_ids=project_ids,
266-
# author=author,
267256
output_dir=output_dir,
268257
ar_guid=ar_guid,
269258
)
@@ -278,7 +267,7 @@ async def get_analysis_runner_log(
278267
async def get_sample_reads_map(
279268
export_type: ExportType = ExportType.JSON,
280269
sequencing_types: list[str] = Query(None), # type: ignore
281-
connection: Connection = get_project_read_connection,
270+
connection: Connection = get_project_db_connection(ReadAccessRoles),
282271
):
283272
"""
284273
Get map of ExternalSampleId pathToCram InternalSeqGroupID for seqr
@@ -293,9 +282,9 @@ async def get_sample_reads_map(
293282
"""
294283

295284
at = AnalysisLayer(connection)
296-
assert connection.project
285+
assert connection.project_id
297286
objs = await at.get_sample_cram_path_map_for_seqr(
298-
project=connection.project, sequencing_types=sequencing_types
287+
project=connection.project_id, sequencing_types=sequencing_types
299288
)
300289

301290
for r in objs:
@@ -311,7 +300,7 @@ async def get_sample_reads_map(
311300
writer = csv.writer(output, delimiter=export_type.get_delimiter())
312301
writer.writerows(rows)
313302

314-
basefn = f'{connection.project}-seqr-igv-paths-{date.today().isoformat()}'
303+
basefn = f'{connection.project_id}-seqr-igv-paths-{date.today().isoformat()}'
315304

316305
return StreamingResponse(
317306
iter([output.getvalue()]),
@@ -345,9 +334,8 @@ async def get_proportionate_map(
345334
}
346335
}
347336
"""
348-
pt = ProjectPermissionsTable(connection)
349-
project_list = await pt.get_and_check_access_to_projects_for_names(
350-
connection.author, projects, allowed_roles=ReadAccessRoles
337+
project_list = connection.get_and_check_access_to_projects_for_names(
338+
projects, allowed_roles=ReadAccessRoles
351339
)
352340
project_ids = [p.id for p in project_list]
353341

api/routes/analysis_runner.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33
from fastapi import APIRouter
44

5-
from api.utils.db import (
6-
Connection,
7-
get_project_read_connection,
8-
get_project_write_connection,
9-
)
5+
from api.utils.db import Connection, get_project_db_connection
106
from db.python.layers.analysis_runner import AnalysisRunnerLayer
117
from db.python.tables.analysis_runner import AnalysisRunnerFilter
128
from db.python.utils import GenericFilter
139
from models.models.analysis_runner import AnalysisRunner, AnalysisRunnerInternal
10+
from models.models.project import FullWriteAccessRoles, ReadAccessRoles
1411

1512
router = APIRouter(prefix='/analysis-runner', tags=['analysis-runner'])
1613

@@ -32,13 +29,13 @@ async def create_analysis_runner_log( # pylint: disable=too-many-arguments
3229
output_path: str,
3330
hail_version: str | None = None,
3431
cwd: str | None = None,
35-
connection: Connection = get_project_write_connection,
32+
connection: Connection = get_project_db_connection(FullWriteAccessRoles),
3633
) -> str:
3734
"""Create a new analysis runner log"""
3835

3936
alayer = AnalysisRunnerLayer(connection)
4037

41-
if not connection.project:
38+
if not connection.project_id:
4239
raise ValueError('Project not set')
4340

4441
analysis_id = await alayer.insert_analysis_runner_entry(
@@ -58,7 +55,7 @@ async def create_analysis_runner_log( # pylint: disable=too-many-arguments
5855
batch_url=batch_url,
5956
submitting_user=submitting_user,
6057
meta=meta,
61-
project=connection.project,
58+
project=connection.project_id,
6259
audit_log_id=None,
6360
output_path=output_path,
6461
)
@@ -75,13 +72,13 @@ async def get_analysis_runner_logs(
7572
repository: str | None = None,
7673
access_level: str | None = None,
7774
environment: str | None = None,
78-
connection: Connection = get_project_read_connection,
75+
connection: Connection = get_project_db_connection(ReadAccessRoles),
7976
) -> list[AnalysisRunner]:
8077
"""Get analysis runner logs"""
8178

8279
atable = AnalysisRunnerLayer(connection)
8380

84-
if not connection.project:
81+
if not connection.project_id:
8582
raise ValueError('Project not set')
8683

8784
filter_ = AnalysisRunnerFilter(
@@ -90,9 +87,9 @@ async def get_analysis_runner_logs(
9087
repository=GenericFilter(eq=repository),
9188
access_level=GenericFilter(eq=access_level),
9289
environment=GenericFilter(eq=environment),
93-
project=GenericFilter(eq=connection.project),
90+
project=GenericFilter(eq=connection.project_id),
9491
)
9592

9693
logs = await atable.query(filter_)
9794

98-
return [log.to_external({connection.project: project}) for log in logs]
95+
return [log.to_external({connection.project_id: project}) for log in logs]

api/routes/assay.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44

55
from api.utils.db import (
66
Connection,
7-
get_project_read_connection,
7+
get_project_db_connection,
88
get_projectless_db_connection,
99
)
1010
from db.python.layers.assay import AssayLayer
1111
from db.python.tables.assay import AssayFilter
12-
from db.python.tables.project import ProjectPermissionsTable
1312
from db.python.utils import GenericFilter
1413
from models.base import SMBase
1514
from models.models.assay import AssayUpsert
16-
from models.models.group import ReadAccessRoles
15+
from models.models.project import ReadAccessRoles
1716
from models.utils.sample_id_format import sample_id_transform_to_raw_list
1817

1918
router = APIRouter(prefix='/assay', tags=['assay'])
@@ -57,7 +56,8 @@ async def get_assay_by_id(
5756
'/{project}/external_id/{external_id}/details', operation_id='getAssayByExternalId'
5857
)
5958
async def get_assay_by_external_id(
60-
external_id: str, connection=get_project_read_connection
59+
external_id: str,
60+
connection: Connection = get_project_db_connection(ReadAccessRoles),
6161
):
6262
"""Get an assay by ONE of its external identifiers"""
6363
assay_layer = AssayLayer(connection)
@@ -84,12 +84,11 @@ async def get_assays_by_criteria(
8484
):
8585
"""Get assays by criteria"""
8686
assay_layer = AssayLayer(connection)
87-
pt = ProjectPermissionsTable(connection)
8887

8988
pids: list[int] | None = None
9089
if criteria.projects:
91-
project_list = await pt.get_and_check_access_to_projects_for_names(
92-
connection.author, criteria.projects, allowed_roles=ReadAccessRoles
90+
project_list = connection.get_and_check_access_to_projects_for_names(
91+
criteria.projects, allowed_roles=ReadAccessRoles
9392
)
9493
pids = [p.id for p in project_list]
9594

0 commit comments

Comments
 (0)