81 lines
2.5 KiB
Python
81 lines
2.5 KiB
Python
import uuid
|
|
|
|
from datetime import date, datetime
|
|
from decimal import Decimal
|
|
|
|
from pydantic import (
|
|
BaseModel,
|
|
ConfigDict,
|
|
Field,
|
|
FieldSerializationInfo,
|
|
field_serializer,
|
|
field_validator,
|
|
model_validator,
|
|
)
|
|
|
|
from . import to_camel
|
|
from .account import AccountBase
|
|
from .cost_centre import CostCentreLink
|
|
|
|
|
|
class EmployeeIn(AccountBase):
|
|
designation: str
|
|
salary: int = Field(ge=0)
|
|
points: Decimal = Field(ge=0, lt=1000)
|
|
joining_date: date
|
|
leaving_date: date | None = None
|
|
model_config = ConfigDict(str_strip_whitespace=True, alias_generator=to_camel, populate_by_name=True)
|
|
|
|
@field_validator("joining_date", mode="before")
|
|
@classmethod
|
|
def parse_joining_date(cls, value: date | str) -> date:
|
|
if isinstance(value, date):
|
|
return value
|
|
return datetime.strptime(value, "%d-%b-%Y").date()
|
|
|
|
@field_serializer("joining_date")
|
|
def serialize_joining_date(self, value: date, info: FieldSerializationInfo) -> str:
|
|
return value.strftime("%d-%b-%Y")
|
|
|
|
@field_validator("leaving_date", mode="before")
|
|
@classmethod
|
|
def parse_leaving_date(cls, value: None | date | str) -> date | None:
|
|
if isinstance(value, date):
|
|
return value
|
|
if value is None:
|
|
return value
|
|
return datetime.strptime(value, "%d-%b-%Y").date()
|
|
|
|
@field_serializer("leaving_date")
|
|
def serialize_leaving_date(self, value: date | None, info: FieldSerializationInfo) -> str | None:
|
|
return None if value is None else value.strftime("%d-%b-%Y")
|
|
|
|
@model_validator(mode="after")
|
|
def leaving_date_more_than_joining_date(self) -> "EmployeeIn":
|
|
if self.is_active:
|
|
self.leaving_date = None
|
|
if (not self.is_active) and (self.leaving_date is None):
|
|
raise ValueError("Need leaving date for employee")
|
|
if self.leaving_date is not None and 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 EmployeeBlank(EmployeeIn):
|
|
name: str
|
|
model_config = ConfigDict(str_strip_whitespace=True, alias_generator=to_camel, populate_by_name=True)
|
|
|
|
|
|
class EmployeeLink(BaseModel):
|
|
id_: uuid.UUID = Field(...)
|
|
name: str | None = None
|
|
designation: str | None = None
|
|
cost_centre: CostCentreLink
|
|
model_config = ConfigDict(alias_generator=to_camel, populate_by_name=True)
|