Add Image models and clean up test code structure

Introduced `ImageCreate` and `Image` models to handle image-related data in the backend. Improved the organization and readability of the testing notebook by consolidating and formatting code into distinct sections with markdown cells.
This commit is contained in:
GotthardG
2025-02-26 15:11:20 +01:00
parent 1606e80f81
commit b04c7b8c95
8 changed files with 191 additions and 11 deletions

View File

@ -11,12 +11,14 @@ from app.schemas import (
Sample,
Image,
ImageCreate,
SampleResult,
)
from app.models import (
Puck as PuckModel,
Sample as SampleModel,
SampleEvent as SampleEventModel,
Image as ImageModel,
Dewar as DewarModel,
)
from app.dependencies import get_db
import logging
@ -165,3 +167,40 @@ async def upload_sample_image(
# Returning the mapped SQLAlchemy object, which will be converted to the
# Pydantic response model.
return new_image
@router.get("/results", response_model=List[SampleResult])
async def get_sample_results(active_pgroup: str, db: Session = Depends(get_db)):
# Query samples for the active pgroup using joins
samples = (
db.query(SampleModel)
.join(SampleModel.puck)
.join(PuckModel.dewar)
.filter(DewarModel.pgroups == active_pgroup)
.all()
)
if not samples:
raise HTTPException(
status_code=404, detail="No samples found for the active pgroup"
)
results = []
for sample in samples:
# Query images associated with each sample
images = db.query(ImageModel).filter(ImageModel.sample_id == sample.id).all()
results.append(
{
"sample_id": sample.id,
"sample_name": sample.sample_name,
"puck_name": sample.puck.puck_name if sample.puck else None,
"dewar_name": sample.puck.dewar.dewar_name
if (sample.puck and sample.puck.dewar)
else None,
"images": [
{"id": img.id, "filepath": img.filepath, "comment": img.comment}
for img in images
],
}
)
return results

View File

@ -804,3 +804,17 @@ class Image(ImageCreate):
class Config:
from_attributes = True
class ImageInfo(BaseModel):
id: int
filepath: str
comment: Optional[str] = None
class SampleResult(BaseModel):
sample_id: int
sample_name: str
puck_name: Optional[str]
dewar_name: Optional[str]
images: List[ImageInfo]