Skip to content

Commit

Permalink
feat: update biological material schema and routers
Browse files Browse the repository at this point in the history
  • Loading branch information
harryle95 committed May 30, 2024
1 parent b2704be commit 2af1ad3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
44 changes: 42 additions & 2 deletions src/model/biological_material.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Optional
from uuid import UUID

from litestar.dto import dto_field
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship

from src.model.base import BaseDataclass
from src.model.variable import Variable

__all__ = ("BiologicalMaterial",)


if TYPE_CHECKING:
from src.model.method import Method
from src.model.vocabulary import Vocabulary


class BiologicalMaterial(Variable):
__tablename__: str = "biological_material_table" # type: ignore[assignment]
__mapper_args__ = {"polymorphic_identity": "biological_material"}

id: Mapped[UUID] = mapped_column(ForeignKey("variable_table.id"), primary_key=True, info=dto_field("read-only"))
biological_location: Mapped[str | None]
title: Mapped[str] = mapped_column(nullable=False)
organism_id: Mapped[UUID] = mapped_column(ForeignKey("vocabulary_table.id", ondelete="SET NULL"))
organism: Mapped["Vocabulary"] = relationship(
"Vocabulary", lazy=None, info=dto_field("read-only"), back_populates="organism"
)
genus: Mapped[str | None]
species: Mapped[str | None]
infraspecific_name: Mapped[str | None]

biological_material_latitude: Mapped[str | None]
biological_material_longitude: Mapped[str | None]
biological_material_altitude: Mapped[str | None]
biological_material_coordinates_uncertainty: Mapped[str | None]

material_source_id: Mapped[str | None]
material_source_doi: Mapped[str | None]
material_source_location: Mapped[str | None]
material_source_latitude: Mapped[str | None]
material_source_longitude: Mapped[str | None]
material_source_altitude: Mapped[str | None]
material_source_coordinates_uncertainty: Mapped[str | None]
material_source_description: Mapped[str | None]

# Relationship
Expand All @@ -34,3 +53,24 @@ class BiologicalMaterial(Variable):
lazy=None,
info=dto_field("read-only"),
)


@dataclass(kw_only=True)
class BiologicalMaterialDataclass(BaseDataclass):
title: str
organism_id: UUID | None = field(default=None)
genus: str | None = field(default=None)
species: str | None = field(default=None)
infraspecific_name: str | None = field(default=None)
biological_material_latitude: str | None = field(default=None)
biological_material_longitude: str | None = field(default=None)
biological_material_altitude: str | None = field(default=None)
biological_material_coordinates_uncertainty: str | None = field(default=None)
material_source_id: str | None = field(default=None)
material_source_doi: str | None = field(default=None)
material_source_latitude: str | None = field(default=None)
material_source_longitude: str | None = field(default=None)
material_source_altitude: str | None = field(default=None)
material_source_coordinates_uncertainty: str | None = field(default=None)
material_source_description: str | None = field(default=None)
preprocessing_method_id: UUID | None = field(default=None)
23 changes: 16 additions & 7 deletions src/router/biological_material.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
from src.model import BiologicalMaterial
from src.router.base import BaseController
from litestar.dto import DataclassDTO, DTOConfig

from src.model.biological_material import BiologicalMaterial, BiologicalMaterialDataclass
from src.model.study import Study
from src.router.base import DataclassController
from src.router.utils.dto import DTOGenerator

__all__ = ("BiologicalMaterialController",)


BiologicalMaterialDTO = DTOGenerator[BiologicalMaterial](read_kwargs={"max_nested_depth": 1})
class BiologicalMaterialDTO(DataclassDTO[BiologicalMaterialDataclass]):
config = DTOConfig(rename_strategy="camel")


BiologicalMaterialReturnDTO = DTOGenerator[BiologicalMaterial]()


class BiologicalMaterialController(BaseController[BiologicalMaterial]):
path = "/biological_material"
dto = BiologicalMaterialDTO.write_dto
return_dto = BiologicalMaterialDTO.read_dto
class BiologicalMaterialController(DataclassController[BiologicalMaterial, BiologicalMaterialDataclass]):
path = "/biologicalMaterial"
dto = BiologicalMaterialDTO
return_dto = BiologicalMaterialDTO
attr_map = {"studies": ("study_id", Study)}
selectinload_attrs = [BiologicalMaterial.studies]

0 comments on commit 2af1ad3

Please sign in to comment.