barker/barker/barker/schemas/master.py

74 lines
1.7 KiB
Python
Raw Normal View History

import uuid
from datetime import date, datetime
from decimal import Decimal
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
from . import to_camel
class AccountBase(BaseModel):
name: str = Field(..., min_length=1)
is_starred: bool
is_active: bool
model_config = ConfigDict(str_strip_whitespace=True, alias_generator=to_camel)
class AccountIn(AccountBase):
type: int
is_reconcilable: bool
class Account(AccountIn):
id_: uuid.UUID
code: int
is_fixture: bool
class EmployeeIn(AccountBase):
designation: str
salary: int = Field(ge=0)
points: Decimal = Field(ge=0, lt=1000, multiple_of=0.01)
joining_date: date
leaving_date: date | None = None
@field_validator("joining_date", mode="before")
@classmethod
def parse_joining_date(cls, value):
return datetime.strptime(value, "%d-%b-%Y").date()
@field_validator("leaving_date", mode="before")
@classmethod
def parse_leaving_date(cls, value):
if value is None or value == "":
return None
else:
return datetime.strptime(value, "%d-%b-%Y").date()
@model_validator(mode="after")
def leaving_date_more_than_joining_date(self) -> "EmployeeIn":
if self.is_active:
self.leaving_date = None
elif self.leaving_date < self.joining_date:
raise ValueError("Leaving Date cannot be less than Joining Date")
return self
class Employee(EmployeeIn):
id_: uuid.UUID
code: int
is_fixture: bool
class DbSetting(BaseModel):
id_: uuid.UUID
name: str
data: bytes
class AccountType(BaseModel):
id_: int
name: str
model_config = ConfigDict(alias_generator=to_camel)