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:
@ -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
|
||||
|
@ -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]
|
||||
|
Reference in New Issue
Block a user