58 lines
1.5 KiB
Python
58 lines
1.5 KiB
Python
import uuid
|
|
|
|
from datetime import date, datetime
|
|
from decimal import Decimal
|
|
from typing import List, Optional, Union
|
|
|
|
from pydantic import validator
|
|
from pydantic.main import BaseModel
|
|
|
|
from . import to_camel
|
|
|
|
|
|
class DaybookItem(BaseModel):
|
|
id_: Optional[uuid.UUID]
|
|
date_: date
|
|
url: List[str]
|
|
type_: str
|
|
narration: str
|
|
debit_text: Optional[str]
|
|
debit_amount: Optional[Decimal]
|
|
credit_text: Optional[str]
|
|
credit_amount: Optional[Decimal]
|
|
posted: bool
|
|
|
|
@validator("date_", pre=True)
|
|
def parse_date(cls, value: Union[date, str]) -> date:
|
|
if isinstance(value, date):
|
|
return value
|
|
return datetime.strptime(value, "%d-%b-%Y").date()
|
|
|
|
class Config:
|
|
anystr_strip_whitespace = True
|
|
alias_generator = to_camel
|
|
json_encoders = {date: lambda v: v.strftime("%d-%b-%Y")}
|
|
|
|
|
|
class Daybook(BaseModel):
|
|
start_date: date
|
|
finish_date: date
|
|
body: List[DaybookItem]
|
|
|
|
class Config:
|
|
anystr_strip_whitespace = True
|
|
alias_generator = to_camel
|
|
json_encoders = {date: lambda v: v.strftime("%d-%b-%Y")}
|
|
|
|
@validator("start_date", pre=True)
|
|
def parse_start_date(cls, value: Union[date, str]) -> date:
|
|
if isinstance(value, date):
|
|
return value
|
|
return datetime.strptime(value, "%d-%b-%Y").date()
|
|
|
|
@validator("finish_date", pre=True)
|
|
def parse_finish_date(cls, value: Union[date, str]) -> date:
|
|
if isinstance(value, date):
|
|
return value
|
|
return datetime.strptime(value, "%d-%b-%Y").date()
|