Skip to content

Commit

Permalink
try computed field
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrOertlin committed Apr 5, 2024
1 parent 7bc51b3 commit f8f01c7
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions genotype_api/dto/sample.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Module for the sample DTOs."""

from datetime import datetime
from pydantic import BaseModel, field_validator
from pydantic import BaseModel, computed_field
from genotype_api.constants import Sexes, Status, Types
from genotype_api.dto.genotype import GenotypeResponse

Expand All @@ -25,29 +25,27 @@ class SampleResponse(BaseModel):
sex: Sexes | None = None
created_at: datetime | None = datetime.now()
analyses: list[AnalysisOnSample] | None = None
detail: SampleDetail = None

@field_validator("detail")
def get_detail(cls, value, values) -> SampleDetail | None:
analyses = values.get("analyses")
@computed_field(alias="detail")
def get_detail(self) -> SampleDetail | None:
analyses = self.analyses
if analyses:
if len(analyses) != 2:
return SampleDetail()
genotype_analysis: list[AnalysisOnSample] = [
genotype_analysis: AnalysisOnSample = [
analysis for analysis in analyses if analysis.type == "genotype"
][0]
sequence_analysis: list[AnalysisOnSample] = [
sequence_analysis: AnalysisOnSample = [
analysis for analysis in analyses if analysis.type == "sequence"
][0]
status: dict = check_snps(
genotype_analysis=genotype_analysis, sequence_analysis=sequence_analysis
)
sex: str = check_sex(
sample_sex=values.get("sex"),
sample_sex=self.sex,
genotype_analysis=genotype_analysis,
sequence_analysis=sequence_analysis,
)

return SampleDetail(**status, sex=sex)
return None

Expand Down

0 comments on commit f8f01c7

Please sign in to comment.