temdb 3.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- temdb/alembic.ini +29 -0
- temdb/migrations/env.py +66 -0
- temdb/migrations/script.py.mako +28 -0
- temdb/migrations/versions/.gitkeep +0 -0
- temdb/migrations/versions/20260612_0001_initial_schema.py +542 -0
- temdb/migrations/versions/20260717_ec4c96d0bf77_add_description_to_block_table.py +32 -0
- temdb/migrations/versions/20260721_44f75ed44d66_updated_at_nullable_no_server_default.py +165 -0
- temdb/migrations/versions/20260721_bec991e0d0eb_dataset_status_server_default.py +38 -0
- temdb/migrations/versions/20260801_a1d2f3e4b5c6_add_sections_run_parameters.py +67 -0
- temdb/server/__init__.py +3 -0
- temdb/server/api/__init__.py +0 -0
- temdb/server/api/v2/__init__.py +0 -0
- temdb/server/api/v2/acquisition.py +1094 -0
- temdb/server/api/v2/block.py +228 -0
- temdb/server/api/v2/cutting_session.py +229 -0
- temdb/server/api/v2/dataset.py +139 -0
- temdb/server/api/v2/lens_correction.py +121 -0
- temdb/server/api/v2/microscope.py +93 -0
- temdb/server/api/v2/quality_control.py +185 -0
- temdb/server/api/v2/roi.py +372 -0
- temdb/server/api/v2/section.py +457 -0
- temdb/server/api/v2/specimen.py +246 -0
- temdb/server/api/v2/substrate.py +180 -0
- temdb/server/api/v2/tasks.py +508 -0
- temdb/server/cli.py +48 -0
- temdb/server/config.py +46 -0
- temdb/server/database.py +75 -0
- temdb/server/dependencies.py +24 -0
- temdb/server/exception_handlers.py +109 -0
- temdb/server/ids.py +12 -0
- temdb/server/main.py +172 -0
- temdb/server/sqlmodels/__init__.py +29 -0
- temdb/server/sqlmodels/acquisition.py +101 -0
- temdb/server/sqlmodels/base.py +41 -0
- temdb/server/sqlmodels/block.py +18 -0
- temdb/server/sqlmodels/cutting_session.py +27 -0
- temdb/server/sqlmodels/dataset.py +34 -0
- temdb/server/sqlmodels/lens_correction.py +36 -0
- temdb/server/sqlmodels/microscope.py +22 -0
- temdb/server/sqlmodels/roi.py +55 -0
- temdb/server/sqlmodels/section.py +55 -0
- temdb/server/sqlmodels/specimen.py +17 -0
- temdb/server/sqlmodels/substrate.py +23 -0
- temdb/server/sqlmodels/task.py +60 -0
- temdb/server/sqlmodels/tile.py +53 -0
- temdb/server/sqlmodels/tile_partition.py +147 -0
- temdb-3.0.0.dist-info/METADATA +16 -0
- temdb-3.0.0.dist-info/RECORD +51 -0
- temdb-3.0.0.dist-info/WHEEL +4 -0
- temdb-3.0.0.dist-info/entry_points.txt +3 -0
- temdb-3.0.0.dist-info/licenses/LICENSE +11 -0
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from datetime import datetime, timezone
|
|
3
|
+
|
|
4
|
+
from fastapi import APIRouter, Body, Depends, HTTPException, Query, status
|
|
5
|
+
from fastapi.encoders import jsonable_encoder
|
|
6
|
+
from sqlalchemy import and_, func, select
|
|
7
|
+
from sqlalchemy.exc import IntegrityError
|
|
8
|
+
from sqlalchemy.ext.asyncio import AsyncSession
|
|
9
|
+
|
|
10
|
+
from temdb.models import APIErrorResponse, SectionCreate, SectionQuality, SectionResponse, SectionUpdate
|
|
11
|
+
from temdb.server.dependencies import get_async_session
|
|
12
|
+
from temdb.server.sqlmodels import (
|
|
13
|
+
CuttingSessionSQLModel,
|
|
14
|
+
ROISQLModel,
|
|
15
|
+
SectionSQLModel,
|
|
16
|
+
SubstrateSQLModel,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
section_api = APIRouter(
|
|
20
|
+
tags=["Sections"],
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
logger = logging.getLogger(__name__)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _dump_images(data):
|
|
27
|
+
return {key: val.model_dump(mode="json") for key, val in data.items()}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@section_api.get("/sections", response_model=list[SectionResponse])
|
|
31
|
+
async def list_sections(
|
|
32
|
+
specimen_id: str | None = Query(None, description="Filter by human-readable Specimen ID"),
|
|
33
|
+
block_id: str | None = Query(None, description="Filter by human-readable Block ID"),
|
|
34
|
+
cutting_session_id: str | None = Query(None, description="Filter by human-readable Cutting Session ID"),
|
|
35
|
+
media_id: str | None = Query(None, description="Filter by media ID"),
|
|
36
|
+
quality: SectionQuality | None = Query(None, description="Filter by section quality"),
|
|
37
|
+
skip: int = Query(0, ge=0),
|
|
38
|
+
limit: int = Query(10, ge=1, le=100),
|
|
39
|
+
session: AsyncSession = Depends(get_async_session),
|
|
40
|
+
):
|
|
41
|
+
"""Retrieve a list of sections with optional filters and pagination."""
|
|
42
|
+
statement = select(SectionSQLModel)
|
|
43
|
+
if specimen_id:
|
|
44
|
+
statement = statement.where(SectionSQLModel.specimen_id == specimen_id)
|
|
45
|
+
if block_id:
|
|
46
|
+
statement = statement.where(SectionSQLModel.block_id == block_id)
|
|
47
|
+
if cutting_session_id:
|
|
48
|
+
statement = statement.where(SectionSQLModel.cutting_session_id == cutting_session_id)
|
|
49
|
+
if media_id:
|
|
50
|
+
statement = statement.where(SectionSQLModel.media_id == media_id)
|
|
51
|
+
sections = (await session.scalars(statement.offset(skip).limit(limit))).all()
|
|
52
|
+
if quality:
|
|
53
|
+
sections = [
|
|
54
|
+
section
|
|
55
|
+
for section in sections
|
|
56
|
+
if isinstance(section.section_metrics, dict) and section.section_metrics.get("quality") == quality.value
|
|
57
|
+
]
|
|
58
|
+
return sections
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@section_api.get("/sections/count", response_model=int)
|
|
62
|
+
async def count_sections(
|
|
63
|
+
specimen_id: str | None = Query(None, description="Filter by human-readable Specimen ID"),
|
|
64
|
+
block_id: str | None = Query(None, description="Filter by human-readable Block ID"),
|
|
65
|
+
cutting_session_id: str | None = Query(None, description="Filter by human-readable Cutting Session ID"),
|
|
66
|
+
media_id: str | None = Query(None, description="Filter by media ID"),
|
|
67
|
+
quality: SectionQuality | None = Query(None, description="Filter by section quality"),
|
|
68
|
+
session: AsyncSession = Depends(get_async_session),
|
|
69
|
+
):
|
|
70
|
+
statement = select(func.count()).select_from(SectionSQLModel)
|
|
71
|
+
conditions = []
|
|
72
|
+
if specimen_id:
|
|
73
|
+
conditions.append(SectionSQLModel.specimen_id == specimen_id)
|
|
74
|
+
if block_id:
|
|
75
|
+
conditions.append(SectionSQLModel.block_id == block_id)
|
|
76
|
+
if cutting_session_id:
|
|
77
|
+
conditions.append(SectionSQLModel.cutting_session_id == cutting_session_id)
|
|
78
|
+
if media_id:
|
|
79
|
+
conditions.append(SectionSQLModel.media_id == media_id)
|
|
80
|
+
if quality:
|
|
81
|
+
conditions.append(SectionSQLModel.section_metrics["quality"].as_string() == quality.value)
|
|
82
|
+
if conditions:
|
|
83
|
+
statement = statement.where(and_(*conditions))
|
|
84
|
+
return (await session.scalars(statement)).one()
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@section_api.get("/sections/sessions/{cutting_session_id}", response_model=list[SectionResponse])
|
|
88
|
+
async def list_cutting_session_sections(
|
|
89
|
+
cutting_session_id: str,
|
|
90
|
+
skip: int = Query(0, ge=0),
|
|
91
|
+
limit: int = Query(10, ge=1, le=100),
|
|
92
|
+
session: AsyncSession = Depends(get_async_session),
|
|
93
|
+
):
|
|
94
|
+
"""Retrieve sections associated with a specific cutting session using its human-readable ID."""
|
|
95
|
+
rows = (
|
|
96
|
+
await session.execute(
|
|
97
|
+
select(CuttingSessionSQLModel.id, SectionSQLModel)
|
|
98
|
+
.select_from(CuttingSessionSQLModel)
|
|
99
|
+
.outerjoin(
|
|
100
|
+
SectionSQLModel,
|
|
101
|
+
SectionSQLModel.cutting_session_id == CuttingSessionSQLModel.cutting_session_id,
|
|
102
|
+
)
|
|
103
|
+
.where(CuttingSessionSQLModel.cutting_session_id == cutting_session_id)
|
|
104
|
+
.order_by(SectionSQLModel.section_number)
|
|
105
|
+
)
|
|
106
|
+
).all()
|
|
107
|
+
if not rows:
|
|
108
|
+
raise HTTPException(status_code=404, detail=f"Cutting Session '{cutting_session_id}' not found")
|
|
109
|
+
sections = [row[1] for row in rows if row[1] is not None]
|
|
110
|
+
return sections[skip : skip + limit]
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
@section_api.get("/sections/blocks/{block_id}", response_model=list[SectionResponse])
|
|
114
|
+
async def list_block_sections(
|
|
115
|
+
block_id: str,
|
|
116
|
+
skip: int = Query(0, ge=0),
|
|
117
|
+
limit: int = Query(10, ge=1, le=100),
|
|
118
|
+
session: AsyncSession = Depends(get_async_session),
|
|
119
|
+
):
|
|
120
|
+
"""Retrieve sections associated with a specific block using its human-readable ID."""
|
|
121
|
+
sections = await session.scalars(
|
|
122
|
+
select(SectionSQLModel)
|
|
123
|
+
.where(SectionSQLModel.block_id == block_id)
|
|
124
|
+
.order_by(SectionSQLModel.cutting_session_id, SectionSQLModel.section_number)
|
|
125
|
+
.offset(skip)
|
|
126
|
+
.limit(limit)
|
|
127
|
+
)
|
|
128
|
+
return sections.all()
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
@section_api.get("/sections/specimens/{specimen_id}", response_model=list[SectionResponse])
|
|
132
|
+
async def list_specimen_sections(
|
|
133
|
+
specimen_id: str,
|
|
134
|
+
skip: int = Query(0, ge=0),
|
|
135
|
+
limit: int = Query(10, ge=1, le=100),
|
|
136
|
+
session: AsyncSession = Depends(get_async_session),
|
|
137
|
+
):
|
|
138
|
+
"""Retrieve sections associated with a specific specimen using its human-readable ID."""
|
|
139
|
+
sections = await session.scalars(
|
|
140
|
+
select(SectionSQLModel)
|
|
141
|
+
.where(SectionSQLModel.specimen_id == specimen_id)
|
|
142
|
+
.order_by(SectionSQLModel.block_id, SectionSQLModel.cutting_session_id, SectionSQLModel.section_number)
|
|
143
|
+
.offset(skip)
|
|
144
|
+
.limit(limit)
|
|
145
|
+
)
|
|
146
|
+
return sections.all()
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
@section_api.get("/sections/sessions/{cutting_session_id}/sections/{section_id}", response_model=SectionResponse)
|
|
150
|
+
async def get_section(
|
|
151
|
+
cutting_session_id: str,
|
|
152
|
+
section_id: str,
|
|
153
|
+
session: AsyncSession = Depends(get_async_session),
|
|
154
|
+
):
|
|
155
|
+
"""Retrieve a specific section by its human-readable ID and its session's human-readable ID."""
|
|
156
|
+
row = (
|
|
157
|
+
await session.execute(
|
|
158
|
+
select(
|
|
159
|
+
SectionSQLModel,
|
|
160
|
+
CuttingSessionSQLModel.id,
|
|
161
|
+
SubstrateSQLModel.id,
|
|
162
|
+
)
|
|
163
|
+
.select_from(SectionSQLModel)
|
|
164
|
+
.outerjoin(
|
|
165
|
+
CuttingSessionSQLModel,
|
|
166
|
+
CuttingSessionSQLModel.cutting_session_id == SectionSQLModel.cutting_session_id,
|
|
167
|
+
)
|
|
168
|
+
.outerjoin(
|
|
169
|
+
SubstrateSQLModel,
|
|
170
|
+
SubstrateSQLModel.media_id == SectionSQLModel.media_id,
|
|
171
|
+
)
|
|
172
|
+
.where(
|
|
173
|
+
SectionSQLModel.section_id == section_id,
|
|
174
|
+
SectionSQLModel.cutting_session_id == cutting_session_id,
|
|
175
|
+
)
|
|
176
|
+
)
|
|
177
|
+
).first()
|
|
178
|
+
if row is None:
|
|
179
|
+
raise HTTPException(
|
|
180
|
+
status_code=status.HTTP_404_NOT_FOUND,
|
|
181
|
+
detail=f"Section '{section_id}' not found in session '{cutting_session_id}'",
|
|
182
|
+
)
|
|
183
|
+
section_obj, _cutting_session_ref, _substrate_ref = row
|
|
184
|
+
return section_obj
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
@section_api.post("/sections", status_code=status.HTTP_201_CREATED, response_model=SectionResponse)
|
|
188
|
+
async def create_section(
|
|
189
|
+
section_data: SectionCreate,
|
|
190
|
+
session: AsyncSession = Depends(get_async_session),
|
|
191
|
+
):
|
|
192
|
+
"""Create a new section."""
|
|
193
|
+
new_section_id = f"{section_data.media_id}_S{section_data.section_number}"
|
|
194
|
+
validation_row = (
|
|
195
|
+
await session.execute(
|
|
196
|
+
select(
|
|
197
|
+
CuttingSessionSQLModel,
|
|
198
|
+
SubstrateSQLModel.id,
|
|
199
|
+
SectionSQLModel.id,
|
|
200
|
+
)
|
|
201
|
+
.select_from(CuttingSessionSQLModel)
|
|
202
|
+
.outerjoin(SubstrateSQLModel, SubstrateSQLModel.media_id == section_data.media_id)
|
|
203
|
+
.outerjoin(
|
|
204
|
+
SectionSQLModel,
|
|
205
|
+
and_(
|
|
206
|
+
SectionSQLModel.section_id == new_section_id,
|
|
207
|
+
SectionSQLModel.cutting_session_id == CuttingSessionSQLModel.cutting_session_id,
|
|
208
|
+
),
|
|
209
|
+
)
|
|
210
|
+
.where(CuttingSessionSQLModel.cutting_session_id == section_data.cutting_session_id)
|
|
211
|
+
)
|
|
212
|
+
).first()
|
|
213
|
+
cut_obj = validation_row[0] if validation_row else None
|
|
214
|
+
if cut_obj is None:
|
|
215
|
+
raise HTTPException(
|
|
216
|
+
status_code=status.HTTP_404_NOT_FOUND,
|
|
217
|
+
detail=f"Cutting Session with ID '{section_data.cutting_session_id}' not found",
|
|
218
|
+
)
|
|
219
|
+
existing_ref = validation_row[2]
|
|
220
|
+
if existing_ref is not None:
|
|
221
|
+
raise HTTPException(
|
|
222
|
+
status_code=status.HTTP_400_BAD_REQUEST,
|
|
223
|
+
detail=f"Section with ID '{new_section_id}' already exists in session '{section_data.cutting_session_id}'",
|
|
224
|
+
)
|
|
225
|
+
new_section = SectionSQLModel(
|
|
226
|
+
section_id=new_section_id,
|
|
227
|
+
section_number=section_data.section_number,
|
|
228
|
+
cutting_session_id=cut_obj.cutting_session_id,
|
|
229
|
+
timestamp=section_data.timestamp or datetime.now(timezone.utc),
|
|
230
|
+
block_id=cut_obj.block_id,
|
|
231
|
+
specimen_id=cut_obj.specimen_id,
|
|
232
|
+
optical_image=(_dump_images(section_data.optical_image) if section_data.optical_image is not None else None),
|
|
233
|
+
section_metrics=(
|
|
234
|
+
section_data.section_metrics.model_dump(mode="json") if section_data.section_metrics is not None else None
|
|
235
|
+
),
|
|
236
|
+
run_parameters=(
|
|
237
|
+
section_data.run_parameters.model_dump(mode="json") if section_data.run_parameters is not None else None
|
|
238
|
+
),
|
|
239
|
+
media_id=section_data.media_id,
|
|
240
|
+
aperture_uid=section_data.aperture_uid,
|
|
241
|
+
aperture_index=section_data.aperture_index,
|
|
242
|
+
barcode=section_data.barcode,
|
|
243
|
+
created_at=section_data.created_at or datetime.now(timezone.utc),
|
|
244
|
+
)
|
|
245
|
+
session.add(new_section)
|
|
246
|
+
await session.commit()
|
|
247
|
+
await session.refresh(new_section)
|
|
248
|
+
return new_section
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
@section_api.post(
|
|
252
|
+
"/sections/batch",
|
|
253
|
+
status_code=status.HTTP_201_CREATED,
|
|
254
|
+
summary="Create multiple Sections in bulk",
|
|
255
|
+
response_model=list[SectionResponse],
|
|
256
|
+
responses={
|
|
257
|
+
status.HTTP_400_BAD_REQUEST: {"model": APIErrorResponse, "description": "Invalid input data"},
|
|
258
|
+
status.HTTP_404_NOT_FOUND: {"model": APIErrorResponse, "description": "Parent resource not found"},
|
|
259
|
+
status.HTTP_409_CONFLICT: {"model": APIErrorResponse, "description": "Duplicate section ID"},
|
|
260
|
+
status.HTTP_500_INTERNAL_SERVER_ERROR: {"model": APIErrorResponse, "description": "Internal server error"},
|
|
261
|
+
},
|
|
262
|
+
)
|
|
263
|
+
async def create_sections_batch(
|
|
264
|
+
sections_data: list[SectionCreate],
|
|
265
|
+
session: AsyncSession = Depends(get_async_session),
|
|
266
|
+
):
|
|
267
|
+
"""Creates multiple Section documents from a list."""
|
|
268
|
+
if not sections_data:
|
|
269
|
+
raise HTTPException(
|
|
270
|
+
status_code=status.HTTP_400_BAD_REQUEST,
|
|
271
|
+
detail="Section data list cannot be empty.",
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
sections_to_insert: list[SectionSQLModel] = []
|
|
275
|
+
session_cache: dict[str, CuttingSessionSQLModel] = {}
|
|
276
|
+
substrate_cache: dict[str, SubstrateSQLModel] = {}
|
|
277
|
+
seen_ids: set[str] = set()
|
|
278
|
+
|
|
279
|
+
for idx, section_create in enumerate(sections_data):
|
|
280
|
+
session_id = section_create.cutting_session_id
|
|
281
|
+
media_id = section_create.media_id
|
|
282
|
+
if session_id not in session_cache:
|
|
283
|
+
session_result = await session.scalars(
|
|
284
|
+
select(CuttingSessionSQLModel).where(CuttingSessionSQLModel.cutting_session_id == session_id)
|
|
285
|
+
)
|
|
286
|
+
cut_session = session_result.one_or_none()
|
|
287
|
+
if cut_session is None:
|
|
288
|
+
raise HTTPException(
|
|
289
|
+
status_code=status.HTTP_404_NOT_FOUND,
|
|
290
|
+
detail=f"CuttingSession '{session_id}' not found for item {idx}.",
|
|
291
|
+
)
|
|
292
|
+
session_cache[session_id] = cut_session
|
|
293
|
+
cut_session = session_cache[session_id]
|
|
294
|
+
|
|
295
|
+
if media_id not in substrate_cache:
|
|
296
|
+
substrate_result = await session.scalars(
|
|
297
|
+
select(SubstrateSQLModel).where(SubstrateSQLModel.media_id == media_id)
|
|
298
|
+
)
|
|
299
|
+
substrate = substrate_result.one_or_none()
|
|
300
|
+
if substrate is None:
|
|
301
|
+
raise HTTPException(
|
|
302
|
+
status_code=status.HTTP_404_NOT_FOUND,
|
|
303
|
+
detail=f"Substrate '{media_id}' not found for item {idx}.",
|
|
304
|
+
)
|
|
305
|
+
substrate_cache[media_id] = substrate
|
|
306
|
+
|
|
307
|
+
section_id = f"{media_id}_S{section_create.section_number:05d}"
|
|
308
|
+
if section_id in seen_ids:
|
|
309
|
+
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="duplicate section IDs in batch")
|
|
310
|
+
seen_ids.add(section_id)
|
|
311
|
+
|
|
312
|
+
section_doc = SectionSQLModel(
|
|
313
|
+
section_id=section_id,
|
|
314
|
+
section_number=section_create.section_number,
|
|
315
|
+
timestamp=section_create.timestamp or datetime.now(timezone.utc),
|
|
316
|
+
cutting_session_id=cut_session.cutting_session_id,
|
|
317
|
+
block_id=cut_session.block_id,
|
|
318
|
+
specimen_id=cut_session.specimen_id,
|
|
319
|
+
media_id=media_id,
|
|
320
|
+
optical_image=(
|
|
321
|
+
_dump_images(section_create.optical_image) if section_create.optical_image is not None else None
|
|
322
|
+
),
|
|
323
|
+
aperture_uid=section_create.aperture_uid,
|
|
324
|
+
aperture_index=section_create.aperture_index,
|
|
325
|
+
barcode=section_create.barcode,
|
|
326
|
+
section_metrics=(
|
|
327
|
+
section_create.section_metrics.model_dump(mode="json")
|
|
328
|
+
if section_create.section_metrics is not None
|
|
329
|
+
else None
|
|
330
|
+
),
|
|
331
|
+
run_parameters=(
|
|
332
|
+
section_create.run_parameters.model_dump(mode="json")
|
|
333
|
+
if section_create.run_parameters is not None
|
|
334
|
+
else None
|
|
335
|
+
),
|
|
336
|
+
created_at=section_create.created_at or datetime.now(timezone.utc),
|
|
337
|
+
)
|
|
338
|
+
sections_to_insert.append(section_doc)
|
|
339
|
+
|
|
340
|
+
session.add_all(sections_to_insert)
|
|
341
|
+
try:
|
|
342
|
+
await session.commit()
|
|
343
|
+
except IntegrityError as error:
|
|
344
|
+
await session.rollback()
|
|
345
|
+
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=f"duplicate sections: {error.orig}") from error
|
|
346
|
+
for item in sections_to_insert:
|
|
347
|
+
await session.refresh(item)
|
|
348
|
+
return sections_to_insert
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
@section_api.patch("/sections/sessions/{cutting_session_id}/sections/{section_id}", response_model=SectionResponse)
|
|
352
|
+
async def update_section(
|
|
353
|
+
cutting_session_id: str,
|
|
354
|
+
section_id: str,
|
|
355
|
+
updated_fields: SectionUpdate = Body(...),
|
|
356
|
+
session: AsyncSession = Depends(get_async_session),
|
|
357
|
+
):
|
|
358
|
+
"""Update details of a specific section."""
|
|
359
|
+
section = await session.scalars(
|
|
360
|
+
select(SectionSQLModel).where(
|
|
361
|
+
SectionSQLModel.section_id == section_id,
|
|
362
|
+
SectionSQLModel.cutting_session_id == cutting_session_id,
|
|
363
|
+
)
|
|
364
|
+
)
|
|
365
|
+
section_obj = section.one_or_none()
|
|
366
|
+
if section_obj is None:
|
|
367
|
+
raise HTTPException(
|
|
368
|
+
status_code=status.HTTP_404_NOT_FOUND,
|
|
369
|
+
detail=f"Section '{section_id}' not found in session '{cutting_session_id}'",
|
|
370
|
+
)
|
|
371
|
+
update_data = updated_fields.model_dump(exclude_unset=True)
|
|
372
|
+
if not update_data:
|
|
373
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="No update data provided")
|
|
374
|
+
metrics = update_data.pop("section_metrics", None)
|
|
375
|
+
if metrics is not None:
|
|
376
|
+
section_obj.section_metrics = jsonable_encoder(metrics)
|
|
377
|
+
run_parameters = update_data.pop("run_parameters", None)
|
|
378
|
+
if run_parameters is not None:
|
|
379
|
+
section_obj.run_parameters = jsonable_encoder(run_parameters)
|
|
380
|
+
optical_image = update_data.pop("optical_image", None)
|
|
381
|
+
if optical_image is not None:
|
|
382
|
+
section_obj.optical_image = jsonable_encoder(optical_image)
|
|
383
|
+
for field, value in update_data.items():
|
|
384
|
+
setattr(section_obj, field, value)
|
|
385
|
+
section_obj.updated_at = datetime.now(timezone.utc)
|
|
386
|
+
session.add(section_obj)
|
|
387
|
+
await session.commit()
|
|
388
|
+
await session.refresh(section_obj)
|
|
389
|
+
return section_obj
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
@section_api.delete(
|
|
393
|
+
"/sections/sessions/{cutting_session_id}/sections/{section_id}",
|
|
394
|
+
status_code=status.HTTP_204_NO_CONTENT,
|
|
395
|
+
)
|
|
396
|
+
async def delete_section(
|
|
397
|
+
cutting_session_id: str,
|
|
398
|
+
section_id: str,
|
|
399
|
+
session: AsyncSession = Depends(get_async_session),
|
|
400
|
+
):
|
|
401
|
+
"""Delete a specific section."""
|
|
402
|
+
row = (
|
|
403
|
+
await session.execute(
|
|
404
|
+
select(SectionSQLModel, func.count(ROISQLModel.id))
|
|
405
|
+
.select_from(SectionSQLModel)
|
|
406
|
+
.outerjoin(ROISQLModel, ROISQLModel.section_id == SectionSQLModel.section_id)
|
|
407
|
+
.where(
|
|
408
|
+
SectionSQLModel.section_id == section_id,
|
|
409
|
+
SectionSQLModel.cutting_session_id == cutting_session_id,
|
|
410
|
+
)
|
|
411
|
+
.group_by(SectionSQLModel.id)
|
|
412
|
+
)
|
|
413
|
+
).first()
|
|
414
|
+
if row is None:
|
|
415
|
+
raise HTTPException(
|
|
416
|
+
status_code=status.HTTP_404_NOT_FOUND,
|
|
417
|
+
detail=f"Section '{section_id}' not found in session '{cutting_session_id}'",
|
|
418
|
+
)
|
|
419
|
+
section_obj, roi_count = row
|
|
420
|
+
if roi_count > 0:
|
|
421
|
+
raise HTTPException(
|
|
422
|
+
status_code=status.HTTP_400_BAD_REQUEST,
|
|
423
|
+
detail=f"Cannot delete section '{section_id}' as it has {roi_count} associated ROIs.",
|
|
424
|
+
)
|
|
425
|
+
await session.delete(section_obj)
|
|
426
|
+
await session.commit()
|
|
427
|
+
return None
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
@section_api.get("/sections/media/{media_id}", response_model=list[SectionResponse])
|
|
431
|
+
async def list_sections_by_media(
|
|
432
|
+
media_id: str,
|
|
433
|
+
skip: int = Query(0, ge=0),
|
|
434
|
+
limit: int = Query(10, ge=1, le=100),
|
|
435
|
+
relative_position: int | None = None,
|
|
436
|
+
session: AsyncSession = Depends(get_async_session),
|
|
437
|
+
):
|
|
438
|
+
"""Retrieve sections by media type and ID."""
|
|
439
|
+
statement = select(SectionSQLModel).where(SectionSQLModel.media_id == media_id)
|
|
440
|
+
if relative_position is not None:
|
|
441
|
+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="relative_position is not supported")
|
|
442
|
+
sections = await session.scalars(statement.offset(skip).limit(limit))
|
|
443
|
+
return sections.all()
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
@section_api.get("/sections/barcode/{barcode}", response_model=list[SectionResponse])
|
|
447
|
+
async def get_sections_by_barcode(
|
|
448
|
+
barcode: str,
|
|
449
|
+
skip: int = Query(0, ge=0),
|
|
450
|
+
limit: int = Query(10, ge=1, le=100),
|
|
451
|
+
session: AsyncSession = Depends(get_async_session),
|
|
452
|
+
):
|
|
453
|
+
"""Retrieve sections by barcode."""
|
|
454
|
+
sections = await session.scalars(
|
|
455
|
+
select(SectionSQLModel).where(SectionSQLModel.barcode == barcode).offset(skip).limit(limit)
|
|
456
|
+
)
|
|
457
|
+
return sections.all()
|