from __future__ import annotations
from datetime import datetime
from typing import List
from pydantic import BaseModel, ConfigDict, Field
# Import existing models needed for type hints and potentially reuse
from .forms import Form
from .intervals import Interval
from .variables import Variable
# Define a structure for Forms within the context of an Interval, including variables
# Define a structure for Intervals, containing FormStructures
[docs]class IntervalStructure(BaseModel):
# Key identifying fields
interval_id: int = Field(..., alias="intervalId")
interval_name: str = Field(..., alias="intervalName")
# Additional relevant fields from Interval
interval_sequence: int = Field(..., alias="intervalSequence")
interval_description: str = Field(..., alias="intervalDescription")
interval_group_name: str = Field(..., alias="intervalGroupName")
disabled: bool = Field(..., alias="disabled")
date_created: datetime = Field(..., alias="dateCreated")
date_modified: datetime = Field(..., alias="dateModified")
# Nested forms
forms: List[FormStructure] = Field(default_factory=list)
# Use ConfigDict for model configuration
model_config = ConfigDict(populate_by_name=True, extra="ignore")
[docs] @classmethod
def from_interval(cls, interval: Interval, forms: List[FormStructure]) -> IntervalStructure:
"""Creates IntervalStructure from an Interval model and its associated FormStructures."""
interval_data = interval.model_dump(by_alias=True)
# Remove the 'forms' key to avoid multiple values for keyword argument 'forms'
interval_data.pop("forms", None)
return cls(**interval_data, forms=forms)
# Define the root StudyStructure model
[docs]class StudyStructure(BaseModel):
study_key: str = Field(..., alias="studyKey")
intervals: List[IntervalStructure] = Field(default_factory=list)
# Use ConfigDict for model configuration
model_config = ConfigDict(populate_by_name=True)