Refine event types and update related models and logic

Standardized event types with stricter validation using `Literal`. Adjusted related data and logic to align with new types, including changes to PGROUP assignments, event timeline increments, and schema updates. Cleaned up unused code and clarified database initialization behavior.
This commit is contained in:
GotthardG
2025-03-14 13:11:05 +01:00
parent f41262575e
commit fbc32474ff
9 changed files with 267 additions and 165 deletions

View File

@ -1,4 +1,4 @@
from typing import List, Optional, Union
from typing import List, Optional, Union, Literal
from datetime import datetime
from pydantic import BaseModel, EmailStr, constr, Field, field_validator
from datetime import date
@ -349,13 +349,24 @@ class DataCollectionParameters(BaseModel):
class SampleEventCreate(BaseModel):
event_type: str
event_type: Literal[
"Mounting", "Centering", "Failed", "Lost", "Collecting", "Unmounting"
]
# event_type: str
# Validate event_type against accepted event types
# @field_validator("event_type", mode="before")
# def validate_event_type(cls, value):
# allowed = {"Mounting", "Centering", "Failed",
# "Lost", "Collecting", "Unmounting"}
# if value not in allowed:
# raise ValueError(f"Invalid event_type: {value}.
# Accepted values are: {allowed}")
# return value
class SampleEventResponse(BaseModel):
class SampleEventResponse(SampleEventCreate):
id: int
sample_id: int
event_type: str
timestamp: datetime
class Config:
@ -791,6 +802,7 @@ class Beamtime(BaseModel):
class ImageCreate(BaseModel):
pgroup: str
sample_id: int
sample_event_id: int
filepath: str
status: str = "active"
comment: Optional[str] = None
@ -906,3 +918,19 @@ class SampleResult(BaseModel):
dewar_name: Optional[str]
images: List[ImageInfo]
experiment_runs: Optional[List[ExperimentParametersRead]] = []
class ResultCreate(BaseModel):
sample_id: int
result_id: int
result: Optional[dict]
class ResultResponse(BaseModel):
id: int
sample_id: int
result_id: int
result: Optional[dict]
class Config:
from_attributes = True