Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update(pydantic) #104

Closed
wants to merge 14 commits into from
10 changes: 5 additions & 5 deletions genotype_api/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pathlib import Path

from pydantic import BaseSettings
from pydantic_settings import BaseSettings

GENOTYPE_PACKAGE = Path(__file__).parent
PACKAGE_ROOT: Path = GENOTYPE_PACKAGE.parent
Expand All @@ -22,10 +22,10 @@ class Config:
class SecuritySettings(BaseSettings):
"""Settings for serving the genotype-api app"""

client_id = ""
algorithm = ""
jwks_uri = "https://www.googleapis.com/oauth2/v3/certs"
api_root_path = "/"
client_id: str = ""
algorithm: str = ""
jwks_uri: str = "https://www.googleapis.com/oauth2/v3/certs"
api_root_path: str = "/"

class Config:
env_file = str(ENV_FILE)
Expand Down
9 changes: 4 additions & 5 deletions genotype_api/constants.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
"""Constants used over the package"""

from enum import Enum
from pydantic import BaseModel
from enum import StrEnum


class TYPES(str, Enum):
class TYPES(StrEnum):
GENOTYPE = "genotype"
SEQUENCE = "sequence"


class SEXES(str, Enum):
class SEXES(StrEnum):
MALE = "male"
FEMALE = "female"
UNKNOWN = "unknown"


class STATUS(str, Enum):
class STATUS(StrEnum):
PASS = "pass"
FAIL = "fail"
CANCEL = "cancel"
Expand Down
33 changes: 16 additions & 17 deletions genotype_api/database/models.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
from collections import Counter
from datetime import datetime

from pydantic import EmailStr, constr, validator
from pydantic import validator
from sqlalchemy import Index
from sqlmodel import Field, Relationship, SQLModel

from genotype_api.constants import CUTOFS, SEXES, STATUS, TYPES
from genotype_api.models import PlateStatusCounts, SampleDetail


class GenotypeBase(SQLModel):
rsnumber: constr(max_length=10) | None
rsnumber: str | None = Field(max_length=10)
analysis_id: int | None = Field(default=None, foreign_key="analysis.id")
allele_1: constr(max_length=1) | None
allele_2: constr(max_length=1) | None
allele_1: str | None = Field(max_length=1)
allele_2: str | None = Field(max_length=1)


class Genotype(GenotypeBase, table=True):
Expand Down Expand Up @@ -48,7 +47,7 @@ class AnalysisBase(SQLModel):
source: str | None
sex: SEXES | None
created_at: datetime | None = datetime.now()
sample_id: constr(max_length=32) | None = Field(default=None, foreign_key="sample.id")
sample_id: str | None = Field(default=None, foreign_key="sample.id", max_length=32)
plate_id: str | None = Field(default=None, foreign_key="plate.id")


Expand Down Expand Up @@ -87,7 +86,7 @@ class SampleBase(SampleSlim):

class Sample(SampleBase, table=True):
__tablename__ = "sample"
id: constr(max_length=32) | None = Field(default=None, primary_key=True)
id: str | None = Field(default=None, primary_key=True, max_length=32)

analyses: list["Analysis"] = Relationship(back_populates="sample")

Expand All @@ -113,32 +112,32 @@ def sequence_analysis(self) -> Analysis | None:


class SampleRead(SampleBase):
id: constr(max_length=32)
id: str = Field(max_length=32)


class SampleCreate(SampleBase):
pass


class SNPBase(SQLModel):
ref: constr(max_length=1) | None
chrom: constr(max_length=5) | None
ref: str | None = Field(max_length=1)
chrom: str | None = Field(max_length=5)
islean marked this conversation as resolved.
Show resolved Hide resolved
pos: int | None


class SNP(SNPBase, table=True):
__tablename__ = "snp"
"""Represent a SNP position under investigation."""

id: constr(max_length=32) | None = Field(default=None, primary_key=True)
id: str | None = Field(default=None, primary_key=True, max_length=32)


class SNPRead(SNPBase):
id: constr(max_length=32)
id: str = Field(max_length=32)


class UserBase(SQLModel):
email: EmailStr = Field(index=True, unique=True)
ChrOertlin marked this conversation as resolved.
Show resolved Hide resolved
email: str = Field(index=True, unique=True)
name: str | None = ""


Expand All @@ -158,7 +157,7 @@ class UserCreate(UserBase):

class PlateBase(SQLModel):
created_at: datetime | None = datetime.now()
plate_id: constr(max_length=16) = Field(index=True, unique=True)
plate_id: str = Field(index=True, unique=True, max_length=16)
signed_by: int | None = Field(default=None, foreign_key="user.id")
signed_at: datetime | None
method_document: str | None
Expand Down Expand Up @@ -216,7 +215,7 @@ def get_detail(cls, value, values) -> SampleDetail:
return SampleDetail(**status, sex=sex)

class Config:
validate_all = True
validate_default = True


class AnalysisReadWithSample(AnalysisRead):
Expand Down Expand Up @@ -255,7 +254,7 @@ def check_detail(cls, value, values):
return PlateStatusCounts(**status_counts, total=len(analyses), commented=commented)

class Config:
validate_all = True
validate_default = True


class PlateReadWithAnalysisDetailSingle(PlateRead):
Expand All @@ -271,7 +270,7 @@ def check_detail(cls, value, values):
return PlateStatusCounts(**status_counts, total=len(analyses), commented=commented)

class Config:
validate_all = True
validate_default = True


def check_snps(genotype_analysis, sequence_analysis):
Expand Down
4 changes: 2 additions & 2 deletions genotype_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class PlateStatusCounts(BaseModel):
commented: int = Field(0, nullable=True)

class Config:
allow_population_by_field_name = True
populate_by_name = True


class SampleDetailStats(BaseModel):
Expand Down Expand Up @@ -53,7 +53,7 @@ def validate_status(cls, value, values) -> SampleDetailStatus:
return SampleDetailStatus(sex=sex, snps=snps, nocalls=nocalls)

class Config:
validate_all = True
validate_default = True


class MatchCounts(BaseModel):
Expand Down
7 changes: 4 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
SQLAlchemy==1.4.30
SQLAlchemy
aiofiles
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⭐ Nice

bcrypt
bump2version
click
coloredlogs
email-validator
fastapi==0.75.0
fastapi
google-auth
gunicorn
httptools
numpy
openpyxl
passlib
pydantic==1.10.14
pydantic
pydantic-settings
pymysql
python-dotenv
python-jose[cryptography]
Expand Down
Loading